Skip to content

Commit 64f9904

Browse files
committed
init
0 parents  commit 64f9904

4 files changed

Lines changed: 261 additions & 0 deletions

File tree

build.zig

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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("zig_plscommig_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 = "zig_plscommig",
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 = "zig_plscommig",
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+
}

build.zig.zon

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = .zig_plscommig,
10+
11+
// This is a [Semantic Version](https://semver.org/).
12+
// In a future version of Zig it will be used for package deduplication.
13+
.version = "0.0.0",
14+
15+
// Together with name, this represents a globally unique package
16+
// identifier. This field is generated by the Zig toolchain when the
17+
// package is first created, and then *never changes*. This allows
18+
// unambiguous detection of one package being an updated version of
19+
// another.
20+
//
21+
// When forking a Zig project, this id should be regenerated (delete the
22+
// field and run `zig build`) if the upstream project is still maintained.
23+
// Otherwise, the fork is *hostile*, attempting to take control over the
24+
// original project's identity. Thus it is recommended to leave the comment
25+
// on the following line intact, so that it shows up in code reviews that
26+
// modify the field.
27+
.fingerprint = 0xda1b26808fe012b0, // Changing this has security and trust implications.
28+
29+
// Tracks the earliest Zig version that the package considers to be a
30+
// supported use case.
31+
.minimum_zig_version = "0.14.0",
32+
33+
// This field is optional.
34+
// Each dependency must either provide a `url` and `hash`, or a `path`.
35+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
36+
// Once all dependencies are fetched, `zig build` no longer requires
37+
// internet connectivity.
38+
.dependencies = .{
39+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
40+
//.example = .{
41+
// // When updating this field to a new URL, be sure to delete the corresponding
42+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
43+
// // the new URL. If the contents of a URL change this will result in a hash mismatch
44+
// // which will prevent zig from using it.
45+
// .url = "https://example.com/foo.tar.gz",
46+
//
47+
// // This is computed from the file contents of the directory of files that is
48+
// // obtained after fetching `url` and applying the inclusion rules given by
49+
// // `paths`.
50+
// //
51+
// // This field is the source of truth; packages do not come from a `url`; they
52+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
53+
// // obtain a package matching this `hash`.
54+
// //
55+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
56+
// .hash = "...",
57+
//
58+
// // When this is provided, the package is found in a directory relative to the
59+
// // build root. In this case the package's hash is irrelevant and therefore not
60+
// // computed. This field and `url` are mutually exclusive.
61+
// .path = "foo",
62+
//
63+
// // When this is set to `true`, a package is declared to be lazily
64+
// // fetched. This makes the dependency only get fetched if it is
65+
// // actually used.
66+
// .lazy = false,
67+
//},
68+
},
69+
70+
// Specifies the set of files and directories that are included in this package.
71+
// Only files and directories listed here are included in the `hash` that
72+
// is computed for this package. Only files listed here will remain on disk
73+
// when using the zig package manager. As a rule of thumb, one should list
74+
// files required for compilation plus any license(s).
75+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
76+
// the build root itself.
77+
// A directory listed here means that all files within, recursively, are included.
78+
.paths = .{
79+
"build.zig",
80+
"build.zig.zon",
81+
"src",
82+
// For example...
83+
//"LICENSE",
84+
//"README.md",
85+
},
86+
}

src/main.zig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! By convention, main.zig is where your main function lives in the case that
2+
//! you are building an executable. If you are making a library, the convention
3+
//! is to delete this file and start with root.zig instead.
4+
5+
pub fn main() !void {
6+
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
7+
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
8+
9+
// stdout is for the actual output of your application, for example if you
10+
// are implementing gzip, then only the compressed bytes should be sent to
11+
// stdout, not any debugging messages.
12+
const stdout_file = std.io.getStdOut().writer();
13+
var bw = std.io.bufferedWriter(stdout_file);
14+
const stdout = bw.writer();
15+
16+
try stdout.print("Run `zig build test` to run the tests.\n", .{});
17+
18+
try bw.flush(); // Don't forget to flush!
19+
}
20+
21+
test "simple test" {
22+
var list = std.ArrayList(i32).init(std.testing.allocator);
23+
defer list.deinit(); // Try commenting this out and see if zig detects the memory leak!
24+
try list.append(42);
25+
try std.testing.expectEqual(@as(i32, 42), list.pop());
26+
}
27+
28+
test "use other module" {
29+
try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
30+
}
31+
32+
test "fuzz example" {
33+
const Context = struct {
34+
fn testOne(context: @This(), input: []const u8) anyerror!void {
35+
_ = context;
36+
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
37+
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
38+
}
39+
};
40+
try std.testing.fuzz(Context{}, Context.testOne, .{});
41+
}
42+
43+
const std = @import("std");
44+
45+
/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
46+
const lib = @import("zig_plscommig_lib");

src/root.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! By convention, root.zig is the root source file when making a library. If
2+
//! you are making an executable, the convention is to delete this file and
3+
//! start with main.zig instead.
4+
const std = @import("std");
5+
const testing = std.testing;
6+
7+
pub export fn add(a: i32, b: i32) i32 {
8+
return a + b;
9+
}
10+
11+
test "basic add functionality" {
12+
try testing.expect(add(3, 7) == 10);
13+
}

0 commit comments

Comments
 (0)