yaml-z

git clone git://git.electrosoup.com/yaml-z
Log | Files | Refs | Submodules

build.zig (6047B)


      1 const std = @import("std");
      2 
      3 // Although this function looks imperative, note that its job is to
      4 // declaratively construct a build graph that will be executed by an external
      5 // runner.
      6 pub fn build(b: *std.Build) void {
      7     // Standard target options allows the person running `zig build` to choose
      8     // what target to build for. Here we do not override the defaults, which
      9     // means any target is allowed, and the default is native. Other options
     10     // for restricting supported target set are available.
     11     const target = b.standardTargetOptions(.{});
     12 
     13     // Standard optimization options allow the person running `zig build` to select
     14     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
     15     // set a preferred release mode, allowing the user to decide how to optimize.
     16     const optimize = b.standardOptimizeOption(.{});
     17 
     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`.
     24         // In this case the main source file is merely a path, however, in more
     25         // complicated build scripts, this could be a generated file.
     26         .root_source_file = b.path("src/root.zig"),
     27         .target = target,
     28         .optimize = optimize,
     29     });
     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("yaml_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.addLibrary(.{
     51         .linkage = .static,
     52         .name = "yaml",
     53         .root_module = lib_mod,
     54     });
     55 
     56     // This declares intent for the library to be installed into the standard
     57     // location when the user invokes the "install" step (the default step when
     58     // running `zig build`).
     59     b.installArtifact(lib);
     60 
     61     // This creates another `std.Build.Step.Compile`, but this one builds an executable
     62     // rather than a static library.
     63     const exe = b.addExecutable(.{
     64         .name = "yaml",
     65         .root_module = exe_mod,
     66     });
     67 
     68     // This declares intent for the executable to be installed into the
     69     // standard location when the user invokes the "install" step (the default
     70     // step when running `zig build`).
     71     b.installArtifact(exe);
     72 
     73     // This *creates* a Run step in the build graph, to be executed when another
     74     // step is evaluated that depends on it. The next line below will establish
     75     // such a dependency.
     76     const run_cmd = b.addRunArtifact(exe);
     77 
     78     // By making the run step depend on the install step, it will be run from the
     79     // installation directory rather than directly from within the cache directory.
     80     // This is not necessary, however, if the application depends on other installed
     81     // files, this ensures they will be present and in the expected location.
     82     run_cmd.step.dependOn(b.getInstallStep());
     83 
     84     // This allows the user to pass arguments to the application in the build
     85     // command itself, like this: `zig build run -- arg1 arg2 etc`
     86     if (b.args) |args| {
     87         run_cmd.addArgs(args);
     88     }
     89 
     90     // This creates a build step. It will be visible in the `zig build --help` menu,
     91     // and can be selected like this: `zig build run`
     92     // This will evaluate the `run` step rather than the default, which is "install".
     93     const run_step = b.step("run", "Run the app");
     94     run_step.dependOn(&run_cmd.step);
     95 
     96     // Creates a step for unit testing. This only builds the test executable
     97     // but does not run it.
     98     const lib_unit_tests = b.addTest(.{
     99         .root_module = lib_mod,
    100     });
    101 
    102     const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
    103 
    104     const exe_unit_tests = b.addTest(.{
    105         .root_module = exe_mod,
    106     });
    107 
    108     const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
    109 
    110     // Similar to creating the run step earlier, this exposes a `test` step to
    111     // the `zig build --help` menu, providing a way for the user to request
    112     // running the unit tests.
    113     const test_step = b.step("test", "Run unit tests");
    114     test_step.dependOn(&run_lib_unit_tests.step);
    115     test_step.dependOn(&run_exe_unit_tests.step);
    116 
    117     const test_suite_exe = b.addExecutable(.{
    118         .name = "yaml-test-suite",
    119         .root_module = b.createModule(.{
    120             .root_source_file = b.path("test/yaml_test_suite.zig"),
    121             .target = target,
    122             .optimize = optimize,
    123             .imports = &.{
    124                 .{ .name = "yaml", .module = lib_mod },
    125             },
    126         }),
    127     });
    128     b.installArtifact(test_suite_exe);
    129     const run_test_suite = b.addRunArtifact(test_suite_exe);
    130     run_test_suite.step.dependOn(b.getInstallStep());
    131     if (b.args) |args| {
    132         run_test_suite.addArgs(args);
    133     }
    134     const test_suite_step = b.step(
    135         "test-suite",
    136         "Run official YAML test suite",
    137     );
    138     test_suite_step.dependOn(&run_test_suite.step);
    139 }