| ... | @@ -10836,12 +10836,25 @@ pub fn build(b: *std.Build) void { | ... | @@ -10836,12 +10836,25 @@ pub fn build(b: *std.Build) void { |
| 10836 | {#code_begin|syntax|build_library#} | 10836 | {#code_begin|syntax|build_library#} |
| 10837 | const std = @import("std"); | 10837 | const std = @import("std"); |
| 10838 | | 10838 | |
| | 10839 | // Although this function looks imperative, note that its job is to |
| | 10840 | // declaratively construct a build graph that will be executed by an external |
| | 10841 | // runner. |
| 10839 | pub fn build(b: *std.Build) void { | 10842 | pub fn build(b: *std.Build) void { |
| | 10843 | // Standard target options allows the person running `zig build` to choose |
| | 10844 | // what target to build for. Here we do not override the defaults, which |
| | 10845 | // means any target is allowed, and the default is native. Other options |
| | 10846 | // for restricting supported target set are available. |
| 10840 | const target = b.standardTargetOptions(.{}); | 10847 | const target = b.standardTargetOptions(.{}); |
| | 10848 | |
| | 10849 | // Standard optimization options allow the person running `zig build` to select |
| | 10850 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not |
| | 10851 | // set a preferred release mode, allowing the user to decide how to optimize. |
| 10841 | const optimize = b.standardOptimizeOption(.{}); | 10852 | const optimize = b.standardOptimizeOption(.{}); |
| 10842 | | 10853 | |
| 10843 | const lib = b.addStaticLibrary(.{ | 10854 | const lib = b.addStaticLibrary(.{ |
| 10844 | .name = "example", | 10855 | .name = "example", |
| | 10856 | // In this case the main source file is merely a path, however, in more |
| | 10857 | // complicated build scripts, this could be a generated file. |
| 10845 | .root_source_file = .{ .path = "src/main.zig" }, | 10858 | .root_source_file = .{ .path = "src/main.zig" }, |
| 10846 | .target = target, | 10859 | .target = target, |
| 10847 | .optimize = optimize, | 10860 | .optimize = optimize, |
| ... | @@ -10852,6 +10865,8 @@ pub fn build(b: *std.Build) void { | ... | @@ -10852,6 +10865,8 @@ pub fn build(b: *std.Build) void { |
| 10852 | // running `zig build`). | 10865 | // running `zig build`). |
| 10853 | b.installArtifact(lib); | 10866 | b.installArtifact(lib); |
| 10854 | | 10867 | |
| | 10868 | // Creates a step for unit testing. This only builds the test executable |
| | 10869 | // but does not run it. |
| 10855 | const main_tests = b.addTest(.{ | 10870 | const main_tests = b.addTest(.{ |
| 10856 | .root_source_file = .{ .path = "src/main.zig" }, | 10871 | .root_source_file = .{ .path = "src/main.zig" }, |
| 10857 | .target = target, | 10872 | .target = target, |
| ... | @@ -10860,6 +10875,9 @@ pub fn build(b: *std.Build) void { | ... | @@ -10860,6 +10875,9 @@ pub fn build(b: *std.Build) void { |
| 10860 | | 10875 | |
| 10861 | const run_main_tests = b.addRunArtifact(main_tests); | 10876 | const run_main_tests = b.addRunArtifact(main_tests); |
| 10862 | | 10877 | |
| | 10878 | // This creates a build step. It will be visible in the `zig build --help` menu, |
| | 10879 | // and can be selected like this: `zig build test` |
| | 10880 | // This will evaluate the `test` step rather than the default, which is "install". |
| 10863 | const test_step = b.step("test", "Run library tests"); | 10881 | const test_step = b.step("test", "Run library tests"); |
| 10864 | test_step.dependOn(&run_main_tests.step); | 10882 | test_step.dependOn(&run_main_tests.step); |
| 10865 | } | 10883 | } |