···11+const std = @import("std");
22+33+// Although this function looks imperative, note that its job is to
44+// declaratively construct a build graph that will be executed by an external
55+// runner.
66+pub fn build(b: *std.Build) void {
77+ // Standard target options allows the person running `zig build` to choose
88+ // what target to build for. Here we do not override the defaults, which
99+ // means any target is allowed, and the default is native. Other options
1010+ // for restricting supported target set are available.
1111+ const target = b.standardTargetOptions(.{});
1212+1313+ // Standard optimization options allow the person running `zig build` to select
1414+ // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
1515+ // set a preferred release mode, allowing the user to decide how to optimize.
1616+ const optimize = b.standardOptimizeOption(.{});
1717+1818+ // This creates a "module", which represents a collection of source files alongside
1919+ // some compilation options, such as optimization mode and linked system libraries.
2020+ // Every executable or library we compile will be based on one or more modules.
2121+ const lib_mod = b.createModule(.{
2222+ // `root_source_file` is the Zig "entry point" of the module. If a module
2323+ // only contains e.g. external object files, you can make this `null`.
2424+ // In this case the main source file is merely a path, however, in more
2525+ // complicated build scripts, this could be a generated file.
2626+ .root_source_file = b.path("src/root.zig"),
2727+ .target = target,
2828+ .optimize = optimize,
2929+ });
3030+3131+ // We will also create a module for our other entry point, 'main.zig'.
3232+ const exe_mod = b.createModule(.{
3333+ // `root_source_file` is the Zig "entry point" of the module. If a module
3434+ // only contains e.g. external object files, you can make this `null`.
3535+ // In this case the main source file is merely a path, however, in more
3636+ // complicated build scripts, this could be a generated file.
3737+ .root_source_file = b.path("src/main.zig"),
3838+ .target = target,
3939+ .optimize = optimize,
4040+ });
4141+4242+ // Modules can depend on one another using the `std.Build.Module.addImport` function.
4343+ // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
4444+ // file path. In this case, we set up `exe_mod` to import `lib_mod`.
4545+ exe_mod.addImport("zaprus_lib", lib_mod);
4646+4747+ // Now, we will create a static library based on the module we created above.
4848+ // This creates a `std.Build.Step.Compile`, which is the build step responsible
4949+ // for actually invoking the compiler.
5050+ const lib = b.addLibrary(.{
5151+ .linkage = .static,
5252+ .name = "zaprus",
5353+ .root_module = lib_mod,
5454+ });
5555+5656+ // This declares intent for the library to be installed into the standard
5757+ // location when the user invokes the "install" step (the default step when
5858+ // running `zig build`).
5959+ b.installArtifact(lib);
6060+6161+ // This creates another `std.Build.Step.Compile`, but this one builds an executable
6262+ // rather than a static library.
6363+ const exe = b.addExecutable(.{
6464+ .name = "zaprus",
6565+ .root_module = exe_mod,
6666+ });
6767+6868+ // This declares intent for the executable to be installed into the
6969+ // standard location when the user invokes the "install" step (the default
7070+ // step when running `zig build`).
7171+ b.installArtifact(exe);
7272+7373+ // This *creates* a Run step in the build graph, to be executed when another
7474+ // step is evaluated that depends on it. The next line below will establish
7575+ // such a dependency.
7676+ const run_cmd = b.addRunArtifact(exe);
7777+7878+ // By making the run step depend on the install step, it will be run from the
7979+ // installation directory rather than directly from within the cache directory.
8080+ // This is not necessary, however, if the application depends on other installed
8181+ // files, this ensures they will be present and in the expected location.
8282+ run_cmd.step.dependOn(b.getInstallStep());
8383+8484+ // This allows the user to pass arguments to the application in the build
8585+ // command itself, like this: `zig build run -- arg1 arg2 etc`
8686+ if (b.args) |args| {
8787+ run_cmd.addArgs(args);
8888+ }
8989+9090+ // This creates a build step. It will be visible in the `zig build --help` menu,
9191+ // and can be selected like this: `zig build run`
9292+ // This will evaluate the `run` step rather than the default, which is "install".
9393+ const run_step = b.step("run", "Run the app");
9494+ run_step.dependOn(&run_cmd.step);
9595+9696+ // Creates a step for unit testing. This only builds the test executable
9797+ // but does not run it.
9898+ const lib_unit_tests = b.addTest(.{
9999+ .root_module = lib_mod,
100100+ });
101101+102102+ const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
103103+104104+ const exe_unit_tests = b.addTest(.{
105105+ .root_module = exe_mod,
106106+ });
107107+108108+ const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
109109+110110+ // Similar to creating the run step earlier, this exposes a `test` step to
111111+ // the `zig build --help` menu, providing a way for the user to request
112112+ // running the unit tests.
113113+ const test_step = b.step("test", "Run unit tests");
114114+ test_step.dependOn(&run_lib_unit_tests.step);
115115+ test_step.dependOn(&run_exe_unit_tests.step);
116116+}
+86
build.zig.zon
···11+.{
22+ // This is the default name used by packages depending on this one. For
33+ // example, when a user runs `zig fetch --save <url>`, this field is used
44+ // as the key in the `dependencies` table. Although the user can choose a
55+ // different name, most users will stick with this provided value.
66+ //
77+ // It is redundant to include "zig" in this name because it is already
88+ // within the Zig package namespace.
99+ .name = .zaprus,
1010+1111+ // This is a [Semantic Version](https://semver.org/).
1212+ // In a future version of Zig it will be used for package deduplication.
1313+ .version = "0.0.0",
1414+1515+ // Together with name, this represents a globally unique package
1616+ // identifier. This field is generated by the Zig toolchain when the
1717+ // package is first created, and then *never changes*. This allows
1818+ // unambiguous detection of one package being an updated version of
1919+ // another.
2020+ //
2121+ // When forking a Zig project, this id should be regenerated (delete the
2222+ // field and run `zig build`) if the upstream project is still maintained.
2323+ // Otherwise, the fork is *hostile*, attempting to take control over the
2424+ // original project's identity. Thus it is recommended to leave the comment
2525+ // on the following line intact, so that it shows up in code reviews that
2626+ // modify the field.
2727+ .fingerprint = 0x1827606eedde2d07, // Changing this has security and trust implications.
2828+2929+ // Tracks the earliest Zig version that the package considers to be a
3030+ // supported use case.
3131+ .minimum_zig_version = "0.14.0",
3232+3333+ // This field is optional.
3434+ // Each dependency must either provide a `url` and `hash`, or a `path`.
3535+ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
3636+ // Once all dependencies are fetched, `zig build` no longer requires
3737+ // internet connectivity.
3838+ .dependencies = .{
3939+ // See `zig fetch --save <url>` for a command-line interface for adding dependencies.
4040+ //.example = .{
4141+ // // When updating this field to a new URL, be sure to delete the corresponding
4242+ // // `hash`, otherwise you are communicating that you expect to find the old hash at
4343+ // // the new URL. If the contents of a URL change this will result in a hash mismatch
4444+ // // which will prevent zig from using it.
4545+ // .url = "https://example.com/foo.tar.gz",
4646+ //
4747+ // // This is computed from the file contents of the directory of files that is
4848+ // // obtained after fetching `url` and applying the inclusion rules given by
4949+ // // `paths`.
5050+ // //
5151+ // // This field is the source of truth; packages do not come from a `url`; they
5252+ // // come from a `hash`. `url` is just one of many possible mirrors for how to
5353+ // // obtain a package matching this `hash`.
5454+ // //
5555+ // // Uses the [multihash](https://multiformats.io/multihash/) format.
5656+ // .hash = "...",
5757+ //
5858+ // // When this is provided, the package is found in a directory relative to the
5959+ // // build root. In this case the package's hash is irrelevant and therefore not
6060+ // // computed. This field and `url` are mutually exclusive.
6161+ // .path = "foo",
6262+ //
6363+ // // When this is set to `true`, a package is declared to be lazily
6464+ // // fetched. This makes the dependency only get fetched if it is
6565+ // // actually used.
6666+ // .lazy = false,
6767+ //},
6868+ },
6969+7070+ // Specifies the set of files and directories that are included in this package.
7171+ // Only files and directories listed here are included in the `hash` that
7272+ // is computed for this package. Only files listed here will remain on disk
7373+ // when using the zig package manager. As a rule of thumb, one should list
7474+ // files required for compilation plus any license(s).
7575+ // Paths are relative to the build root. Use the empty string (`""`) to refer to
7676+ // the build root itself.
7777+ // A directory listed here means that all files within, recursively, are included.
7878+ .paths = .{
7979+ "build.zig",
8080+ "build.zig.zon",
8181+ "src",
8282+ // For example...
8383+ //"LICENSE",
8484+ //"README.md",
8585+ },
8686+}
+46
src/main.zig
···11+//! By convention, main.zig is where your main function lives in the case that
22+//! you are building an executable. If you are making a library, the convention
33+//! is to delete this file and start with root.zig instead.
44+55+pub fn main() !void {
66+ // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
77+ std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
88+99+ // stdout is for the actual output of your application, for example if you
1010+ // are implementing gzip, then only the compressed bytes should be sent to
1111+ // stdout, not any debugging messages.
1212+ const stdout_file = std.io.getStdOut().writer();
1313+ var bw = std.io.bufferedWriter(stdout_file);
1414+ const stdout = bw.writer();
1515+1616+ try stdout.print("Run `zig build test` to run the tests.\n", .{});
1717+1818+ try bw.flush(); // Don't forget to flush!
1919+}
2020+2121+test "simple test" {
2222+ var list = std.ArrayList(i32).init(std.testing.allocator);
2323+ defer list.deinit(); // Try commenting this out and see if zig detects the memory leak!
2424+ try list.append(42);
2525+ try std.testing.expectEqual(@as(i32, 42), list.pop());
2626+}
2727+2828+test "use other module" {
2929+ try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
3030+}
3131+3232+test "fuzz example" {
3333+ const Context = struct {
3434+ fn testOne(context: @This(), input: []const u8) anyerror!void {
3535+ _ = context;
3636+ // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
3737+ try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
3838+ }
3939+ };
4040+ try std.testing.fuzz(Context{}, Context.testOne, .{});
4141+}
4242+4343+const std = @import("std");
4444+4545+/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
4646+const lib = @import("zaprus_lib");
+13
src/root.zig
···11+//! By convention, root.zig is the root source file when making a library. If
22+//! you are making an executable, the convention is to delete this file and
33+//! start with main.zig instead.
44+const std = @import("std");
55+const testing = std.testing;
66+77+pub export fn add(a: i32, b: i32) i32 {
88+ return a + b;
99+}
1010+1111+test "basic add functionality" {
1212+ try testing.expect(add(3, 7) == 10);
1313+}