| ... | ... | @@ -15,8 +15,12 @@ pub fn build(b: *std.Build) void { |
| 15 | 15 | // set a preferred release mode, allowing the user to decide how to optimize. |
| 16 | 16 | const optimize = b.standardOptimizeOption(.{}); |
| 17 | 17 | |
| 18 | | const lib = b.addStaticLibrary(.{ |
| 19 | | .name = "$", |
| 18 | // This creates a "module", which represents a collection of source files alongside |
| 19 | // some compilation options, such as optimization mode and linked system libraries. |
| 20 | // Every executable or library we compile will be based on one or more modules. |
| 21 | const lib_mod = b.createModule(.{ |
| 22 | // `root_source_file` is the Zig "entry point" of the module. If a module |
| 23 | // only contains e.g. external object files, you can make this `null`. |
| 20 | 24 | // In this case the main source file is merely a path, however, in more |
| 21 | 25 | // complicated build scripts, this could be a generated file. |
| 22 | 26 | .root_source_file = b.path("src/root.zig"), |
| ... | ... | @@ -24,16 +28,40 @@ pub fn build(b: *std.Build) void { |
| 24 | 28 | .optimize = optimize, |
| 25 | 29 | }); |
| 26 | 30 | |
| 31 | // We will also create a module for our other entry point, 'main.zig'. |
| 32 | const exe_mod = b.createModule(.{ |
| 33 | // `root_source_file` is the Zig "entry point" of the module. If a module |
| 34 | // only contains e.g. external object files, you can make this `null`. |
| 35 | // In this case the main source file is merely a path, however, in more |
| 36 | // complicated build scripts, this could be a generated file. |
| 37 | .root_source_file = b.path("src/main.zig"), |
| 38 | .target = target, |
| 39 | .optimize = optimize, |
| 40 | }); |
| 41 | |
| 42 | // Modules can depend on one another using the `std.Build.Module.addImport` function. |
| 43 | // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a |
| 44 | // file path. In this case, we set up `exe_mod` to import `lib_mod`. |
| 45 | exe_mod.addImport("$_lib", lib_mod); |
| 46 | |
| 47 | // Now, we will create a static library based on the module we created above. |
| 48 | // This creates a `std.Build.Step.Compile`, which is the build step responsible |
| 49 | // for actually invoking the compiler. |
| 50 | const lib = b.addStaticLibrary(.{ |
| 51 | .name = "$", |
| 52 | .root_module = lib_mod, |
| 53 | }); |
| 54 | |
| 27 | 55 | // This declares intent for the library to be installed into the standard |
| 28 | 56 | // location when the user invokes the "install" step (the default step when |
| 29 | 57 | // running `zig build`). |
| 30 | 58 | b.installArtifact(lib); |
| 31 | 59 | |
| 60 | // This creates another `std.Build.Step.Compile`, but this one builds an executable |
| 61 | // rather than a static library. |
| 32 | 62 | const exe = b.addExecutable(.{ |
| 33 | 63 | .name = "$", |
| 34 | | .root_source_file = b.path("src/main.zig"), |
| 35 | | .target = target, |
| 36 | | .optimize = optimize, |
| 64 | .root_module = exe_mod, |
| 37 | 65 | }); |
| 38 | 66 | |
| 39 | 67 | // This declares intent for the executable to be installed into the |
| ... | ... | @@ -67,17 +95,13 @@ pub fn build(b: *std.Build) void { |
| 67 | 95 | // Creates a step for unit testing. This only builds the test executable |
| 68 | 96 | // but does not run it. |
| 69 | 97 | const lib_unit_tests = b.addTest(.{ |
| 70 | | .root_source_file = b.path("src/root.zig"), |
| 71 | | .target = target, |
| 72 | | .optimize = optimize, |
| 98 | .root_module = lib_mod, |
| 73 | 99 | }); |
| 74 | 100 | |
| 75 | 101 | const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); |
| 76 | 102 | |
| 77 | 103 | const exe_unit_tests = b.addTest(.{ |
| 78 | | .root_source_file = b.path("src/main.zig"), |
| 79 | | .target = target, |
| 80 | | .optimize = optimize, |
| 104 | .root_module = exe_mod, |
| 81 | 105 | }); |
| 82 | 106 | |
| 83 | 107 | const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); |