Skip to content

Commit 054c5a4

Browse files
authored
Merge pull request #2 from dying-will-bullet/update
feat: new template
2 parents 169c63e + 0d61cc7 commit 054c5a4

File tree

5 files changed

+115
-21
lines changed

5 files changed

+115
-21
lines changed

build.zig

+53-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
1919
.name = "zig-project-template",
2020
// In this case the main source file is merely a path, however, in more
2121
// complicated build scripts, this could be a generated file.
22-
.root_source_file = .{ .path = "src/lib.zig" },
22+
.root_source_file = .{ .path = "src/root.zig" },
2323
.target = target,
2424
.optimize = optimize,
2525
});
@@ -29,19 +29,63 @@ pub fn build(b: *std.Build) void {
2929
// running `zig build`).
3030
b.installArtifact(lib);
3131

32+
const exe = b.addExecutable(.{
33+
.name = "zig-project-template",
34+
.root_source_file = .{ .path = "src/main.zig" },
35+
.target = target,
36+
.optimize = optimize,
37+
});
38+
39+
// This declares intent for the executable to be installed into the
40+
// standard location when the user invokes the "install" step (the default
41+
// step when running `zig build`).
42+
b.installArtifact(exe);
43+
44+
// This *creates* a Run step in the build graph, to be executed when another
45+
// step is evaluated that depends on it. The next line below will establish
46+
// such a dependency.
47+
const run_cmd = b.addRunArtifact(exe);
48+
49+
// By making the run step depend on the install step, it will be run from the
50+
// installation directory rather than directly from within the cache directory.
51+
// This is not necessary, however, if the application depends on other installed
52+
// files, this ensures they will be present and in the expected location.
53+
run_cmd.step.dependOn(b.getInstallStep());
54+
55+
// This allows the user to pass arguments to the application in the build
56+
// command itself, like this: `zig build run -- arg1 arg2 etc`
57+
if (b.args) |args| {
58+
run_cmd.addArgs(args);
59+
}
60+
61+
// This creates a build step. It will be visible in the `zig build --help` menu,
62+
// and can be selected like this: `zig build run`
63+
// This will evaluate the `run` step rather than the default, which is "install".
64+
const run_step = b.step("run", "Run the app");
65+
run_step.dependOn(&run_cmd.step);
66+
3267
// Creates a step for unit testing. This only builds the test executable
3368
// but does not run it.
34-
const main_tests = b.addTest(.{
35-
.root_source_file = .{ .path = "src/lib.zig" },
69+
const lib_unit_tests = b.addTest(.{
70+
.root_source_file = .{ .path = "src/root.zig" },
3671
.target = target,
3772
.optimize = optimize,
3873
});
3974

40-
const run_main_tests = b.addRunArtifact(main_tests);
75+
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
4176

42-
// This creates a build step. It will be visible in the `zig build --help` menu,
43-
// and can be selected like this: `zig build test`
44-
// This will evaluate the `test` step rather than the default, which is "install".
45-
const test_step = b.step("test", "Run library tests");
46-
test_step.dependOn(&run_main_tests.step);
77+
const exe_unit_tests = b.addTest(.{
78+
.root_source_file = .{ .path = "src/main.zig" },
79+
.target = target,
80+
.optimize = optimize,
81+
});
82+
83+
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
84+
85+
// Similar to creating the run step earlier, this exposes a `test` step to
86+
// the `zig build --help` menu, providing a way for the user to request
87+
// running the unit tests.
88+
const test_step = b.step("test", "Run unit tests");
89+
test_step.dependOn(&run_lib_unit_tests.step);
90+
test_step.dependOn(&run_exe_unit_tests.step);
4791
}

build.zig.zon

+59-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
.{
22
.name = "zig-project-template",
3-
.version = "0.1.0",
4-
.dependencies = .{},
3+
// This is a [Semantic Version](https://semver.org/).
4+
// In a future version of Zig it will be used for package deduplication.
5+
.version = "0.0.0",
6+
7+
// This field is optional.
8+
// This is currently advisory only; Zig does not yet do anything
9+
// with this value.
10+
//.minimum_zig_version = "0.11.0",
11+
12+
// This field is optional.
13+
// Each dependency must either provide a `url` and `hash`, or a `path`.
14+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
15+
// Once all dependencies are fetched, `zig build` no longer requires
16+
// Internet connectivity.
17+
.dependencies = .{
18+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
19+
//.example = .{
20+
// // When updating this field to a new URL, be sure to delete the corresponding
21+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
22+
// // the new URL.
23+
// .url = "https://example.com/foo.tar.gz",
24+
//
25+
// // This is computed from the file contents of the directory of files that is
26+
// // obtained after fetching `url` and applying the inclusion rules given by
27+
// // `paths`.
28+
// //
29+
// // This field is the source of truth; packages do not come from an `url`; they
30+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
31+
// // obtain a package matching this `hash`.
32+
// //
33+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
34+
// .hash = "...",
35+
//
36+
// // When this is provided, the package is found in a directory relative to the
37+
// // build root. In this case the package's hash is irrelevant and therefore not
38+
// // computed. This field and `url` are mutually exclusive.
39+
// .path = "foo",
40+
//},
41+
},
42+
43+
// Specifies the set of files and directories that are included in this package.
44+
// Only files and directories listed here are included in the `hash` that
45+
// is computed for this package.
46+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
47+
// the build root itself.
48+
// A directory listed here means that all files within, recursively, are included.
49+
.paths = .{
50+
// This makes *all* files, recursively, included in this package. It is generally
51+
// better to explicitly list the files and directories instead, to insure that
52+
// fetching from tarballs, file system paths, and version control all result
53+
// in the same contents hash.
54+
"",
55+
// For example...
56+
//"build.zig",
57+
//"build.zig.zon",
58+
//"src",
59+
//"LICENSE",
60+
//"README.md",
61+
},
562
}

src/lib.zig

-10
This file was deleted.

src/main.zig

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub fn main() void {}
2+

src/root.zig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)