diff --git a/lib/init/build.zig b/lib/init/build.zig index d53cbbb982c3..ccf550bf9bf7 100644 --- a/lib/init/build.zig +++ b/lib/init/build.zig @@ -15,25 +15,59 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const lib = b.addStaticLibrary(.{ - .name = "$", + // Here we are creating a module. Module is a set of files + // that you can use to build executable or library, or import into other module. + // + // Here we are creating a module, but not exposing it to package manager. + // To expose it to package manager, use function `addModule` and choose + // a name for it. Packages that want to import your module will need to use + // exactly this name in their build.zig: + // + // ```zig + // // In your build.zig + // const main_mod = b.addModule("main", .{ ... }); + // + // // In their build.zig + // const some_dep = b.dependency("...", .{ ... }); + // const some_mod = some_dep.module("main"); + // ``` + const main_mod = b.createModule(.{ // In this case the main source file is merely a path, however, in more // complicated build scripts, this could be a generated file. - .root_source_file = b.path("src/root.zig"), + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); - // This declares intent for the library to be installed into the standard - // location when the user invokes the "install" step (the default step when - // running `zig build`). - b.installArtifact(lib); + // You can import to this module other Zig modules, add C/C++ source files, link libraries, + // and etc. To import Zig modules, use `addImport` function: + // + // ```zig + // main_mod.addImport("some_mod", some_mod); + // ``` + // + // If they are added unconditionally, you can also import them during module creation instead: + // ```zig + // const main_mod = b.createModule(.{ + // // ... + // .imports = &.{ + // .{ .name = "some_mod", .module = some_mod }, + // .{ .name = "another_mod", .module = another_mod }, + // }, + // }); + // ``` + // + // Now you can leverage it in your source code by using `@import("some_mod")` + // syntax (get all definitions from root source file) or by using `@embedFile("some_mod")` + // (get literal content of the root source file at comptime). + // + // Note that Zig's "std" module (a.k.a. standard library) + // is always available, you don't need to import it manually. - const exe = b.addExecutable(.{ + // After finishing module, we are building executable out of it. + const exe = b.addExecutable2(.{ .name = "$", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, + .root_module = main_mod, }); // This declares intent for the executable to be installed into the @@ -66,26 +100,38 @@ pub fn build(b: *std.Build) void { // Creates a step for unit testing. This only builds the test executable // but does not run it. - const lib_unit_tests = b.addTest(.{ + const exe_unit_tests = b.addTest2(.{ + .root_module = main_mod, + }); + + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // the `zig build --help` menu, providing a way for the user to request + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_exe_unit_tests.step); + + // Below we are doing same things, but for static library. + const another_mod = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); - const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + const lib = b.addLibrary(.{ + .name = "$", + .root_module = another_mod, + .linkage = .static, + }); - const exe_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, + b.installArtifact(lib); + + const lib_unit_tests = b.addTest2(.{ + .root_module = another_mod, }); - const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); - // Similar to creating the run step earlier, this exposes a `test` step to - // the `zig build --help` menu, providing a way for the user to request - // running the unit tests. - const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_lib_unit_tests.step); - test_step.dependOn(&run_exe_unit_tests.step); }