authorgravatar for ryan@barth.techRyan Barth <ryan@barth.tech> 2023-10-13 23:06:38-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-10-14 19:57:47+03:00
log8a15c9249c6b6d47c22651d65a7b68db487cd479
treee63a5fbe27377b1bcc08482331c4e3cbb9ed6360
parent895c81ce72430be818befefa56046d966c01f8ea

update build.zig examples from current stdlib


1 files changed, 53 insertions(+), 4 deletions(-)

doc/langref.html.in+53-4
...@@ -10759,6 +10759,9 @@ const separator = if (builtin.os.tag == .windows) '\\' else '/';...@@ -10759,6 +10759,9 @@ const separator = if (builtin.os.tag == .windows) '\\' else '/';
10759 {#code_begin|syntax|build_executable#}10759 {#code_begin|syntax|build_executable#}
10760const std = @import("std");10760const std = @import("std");
1076110761
10762// Although this function looks imperative, note that its job is to
10763// declaratively construct a build graph that will be executed by an external
10764// runner.
10762pub fn build(b: *std.Build) void {10765pub fn build(b: *std.Build) void {
10763 // Standard target options allows the person running `zig build` to choose10766 // Standard target options allows the person running `zig build` to choose
10764 // what target to build for. Here we do not override the defaults, which10767 // what target to build for. Here we do not override the defaults, which
...@@ -10773,20 +10776,56 @@ pub fn build(b: *std.Build) void {...@@ -10773,20 +10776,56 @@ pub fn build(b: *std.Build) void {
1077310776
10774 const exe = b.addExecutable(.{10777 const exe = b.addExecutable(.{
10775 .name = "example",10778 .name = "example",
10779 // In this case the main source file is merely a path, however, in more
10780 // complicated build scripts, this could be a generated file.
10776 .root_source_file = .{ .path = "src/main.zig" },10781 .root_source_file = .{ .path = "src/main.zig" },
10777 .target = target,10782 .target = target,
10778 .optimize = optimize,10783 .optimize = optimize,
10779 });10784 });
10780 exe.install();
1078110785
10782 const run_cmd = exe.run();10786 // This declares intent for the executable to be installed into the
10787 // standard location when the user invokes the "install" step (the default
10788 // step when running `zig build`).
10789 b.installArtifact(exe);
10790
10791 // This *creates* a Run step in the build graph, to be executed when another
10792 // step is evaluated that depends on it. The next line below will establish
10793 // such a dependency.
10794 const run_cmd = b.addRunArtifact(exe);
10795
10796 // By making the run step depend on the install step, it will be run from the
10797 // installation directory rather than directly from within the cache directory.
10798 // This is not necessary, however, if the application depends on other installed
10799 // files, this ensures they will be present and in the expected location.
10783 run_cmd.step.dependOn(b.getInstallStep());10800 run_cmd.step.dependOn(b.getInstallStep());
10801
10802 // This allows the user to pass arguments to the application in the build
10803 // command itself, like this: `zig build run -- arg1 arg2 etc`
10784 if (b.args) |args| {10804 if (b.args) |args| {
10785 run_cmd.addArgs(args);10805 run_cmd.addArgs(args);
10786 }10806 }
1078710807
10808 // This creates a build step. It will be visible in the `zig build --help` menu,
10809 // and can be selected like this: `zig build run`
10810 // This will evaluate the `run` step rather than the default, which is "install".
10788 const run_step = b.step("run", "Run the app");10811 const run_step = b.step("run", "Run the app");
10789 run_step.dependOn(&run_cmd.step);10812 run_step.dependOn(&run_cmd.step);
10813
10814 // Creates a step for unit testing. This only builds the test executable
10815 // but does not run it.
10816 const unit_tests = b.addTest(.{
10817 .root_source_file = .{ .path = "src/main.zig" },
10818 .target = target,
10819 .optimize = optimize,
10820 });
10821
10822 const run_unit_tests = b.addRunArtifact(unit_tests);
10823
10824 // Similar to creating the run step earlier, this exposes a `test` step to
10825 // the `zig build --help` menu, providing a way for the user to request
10826 // running the unit tests.
10827 const test_step = b.step("test", "Run unit tests");
10828 test_step.dependOn(&run_unit_tests.step);
10790}10829}
10791 {#code_end#}10830 {#code_end#}
10792 {#header_close#}10831 {#header_close#}
...@@ -10798,21 +10837,31 @@ pub fn build(b: *std.Build) void {...@@ -10798,21 +10837,31 @@ pub fn build(b: *std.Build) void {
10798const std = @import("std");10837const std = @import("std");
1079910838
10800pub fn build(b: *std.Build) void {10839pub fn build(b: *std.Build) void {
10840 const target = b.standardTargetOptions(.{});
10801 const optimize = b.standardOptimizeOption(.{});10841 const optimize = b.standardOptimizeOption(.{});
10842
10802 const lib = b.addStaticLibrary(.{10843 const lib = b.addStaticLibrary(.{
10803 .name = "example",10844 .name = "example",
10804 .root_source_file = .{ .path = "src/main.zig" },10845 .root_source_file = .{ .path = "src/main.zig" },
10846 .target = target,
10805 .optimize = optimize,10847 .optimize = optimize,
10806 });10848 });
10807 lib.install();10849
10850 // This declares intent for the library to be installed into the standard
10851 // location when the user invokes the "install" step (the default step when
10852 // running `zig build`).
10853 b.installArtifact(lib);
1080810854
10809 const main_tests = b.addTest(.{10855 const main_tests = b.addTest(.{
10810 .root_source_file = .{ .path = "src/main.zig" },10856 .root_source_file = .{ .path = "src/main.zig" },
10857 .target = target,
10811 .optimize = optimize,10858 .optimize = optimize,
10812 });10859 });
1081310860
10861 const run_main_tests = b.addRunArtifact(main_tests);
10862
10814 const test_step = b.step("test", "Run library tests");10863 const test_step = b.step("test", "Run library tests");
10815 test_step.dependOn(&main_tests.step);10864 test_step.dependOn(&run_main_tests.step);
10816}10865}
10817 {#code_end#}10866 {#code_end#}
10818 {#header_close#}10867 {#header_close#}