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 '/';
1075910759 {#code_begin|syntax|build_executable#}
1076010760const 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.
1076210765pub fn build(b: *std.Build) void {
1076310766 // Standard target options allows the person running `zig build` to choose
1076410767 // what target to build for. Here we do not override the defaults, which
......@@ -10773,20 +10776,56 @@ pub fn build(b: *std.Build) void {
1077310776
1077410777 const exe = b.addExecutable(.{
1077510778 .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.
1077610781 .root_source_file = .{ .path = "src/main.zig" },
1077710782 .target = target,
1077810783 .optimize = optimize,
1077910784 });
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.
1078310800 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`
1078410804 if (b.args) |args| {
1078510805 run_cmd.addArgs(args);
1078610806 }
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".
1078810811 const run_step = b.step("run", "Run the app");
1078910812 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);
1079010829}
1079110830 {#code_end#}
1079210831 {#header_close#}
......@@ -10798,21 +10837,31 @@ pub fn build(b: *std.Build) void {
1079810837const std = @import("std");
1079910838
1080010839pub fn build(b: *std.Build) void {
10840 const target = b.standardTargetOptions(.{});
1080110841 const optimize = b.standardOptimizeOption(.{});
10842
1080210843 const lib = b.addStaticLibrary(.{
1080310844 .name = "example",
1080410845 .root_source_file = .{ .path = "src/main.zig" },
10846 .target = target,
1080510847 .optimize = optimize,
1080610848 });
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
1080910855 const main_tests = b.addTest(.{
1081010856 .root_source_file = .{ .path = "src/main.zig" },
10857 .target = target,
1081110858 .optimize = optimize,
1081210859 });
1081310860
10861 const run_main_tests = b.addRunArtifact(main_tests);
10862
1081410863 const test_step = b.step("test", "Run library tests");
10815 test_step.dependOn(&main_tests.step);
10864 test_step.dependOn(&run_main_tests.step);
1081610865}
1081710866 {#code_end#}
1081810867 {#header_close#}