authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-16 16:19:35+00:00
committergravatar for bratishkaerik@landless-city.netEric Joldasov <bratishkaerik@landless-city.net> 2024-12-18 01:49:14+05:00
logafc77f0603395d9c1829a49081e198b0fa255abc
treeca604dbab15963d63216650d77d76f14c6156e91
parent6bd590ad37dcb7e46c8eb78c54e69ad07e5b565e
signaturelock-open Commit is signed but in an unrecognized format.

init template: expand slightly, migrate from deprecated std.Build APIs


3 files changed, 45 insertions(+), 13 deletions(-)

lib/init/build.zig+35-11
......@@ -15,8 +15,12 @@ pub fn build(b: *std.Build) void {
1515 // set a preferred release mode, allowing the user to decide how to optimize.
1616 const optimize = b.standardOptimizeOption(.{});
1717
18 const lib = b.addStaticLibrary(.{
19 .name = "$",
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`.
2024 // In this case the main source file is merely a path, however, in more
2125 // complicated build scripts, this could be a generated file.
2226 .root_source_file = b.path("src/root.zig"),
......@@ -24,16 +28,40 @@ pub fn build(b: *std.Build) void {
2428 .optimize = optimize,
2529 });
2630
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("$_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.addStaticLibrary(.{
51 .name = "$",
52 .root_module = lib_mod,
53 });
54
2755 // This declares intent for the library to be installed into the standard
2856 // location when the user invokes the "install" step (the default step when
2957 // running `zig build`).
3058 b.installArtifact(lib);
3159
60 // This creates another `std.Build.Step.Compile`, but this one builds an executable
61 // rather than a static library.
3262 const exe = b.addExecutable(.{
3363 .name = "$",
34 .root_source_file = b.path("src/main.zig"),
35 .target = target,
36 .optimize = optimize,
64 .root_module = exe_mod,
3765 });
3866
3967 // This declares intent for the executable to be installed into the
......@@ -67,17 +95,13 @@ pub fn build(b: *std.Build) void {
6795 // Creates a step for unit testing. This only builds the test executable
6896 // but does not run it.
6997 const lib_unit_tests = b.addTest(.{
70 .root_source_file = b.path("src/root.zig"),
71 .target = target,
72 .optimize = optimize,
98 .root_module = lib_mod,
7399 });
74100
75101 const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
76102
77103 const exe_unit_tests = b.addTest(.{
78 .root_source_file = b.path("src/main.zig"),
79 .target = target,
80 .optimize = optimize,
104 .root_module = exe_mod,
81105 });
82106
83107 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
lib/init/src/main.zig+9-1
......@@ -1,7 +1,6 @@
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.
4const std = @import("std");
54
65pub fn main() !void {
76 // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
......@@ -26,6 +25,10 @@ test "simple test" {
2625 try std.testing.expectEqual(@as(i32, 42), list.pop());
2726}
2827
28test "use other module" {
29 try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
30}
31
2932test "fuzz example" {
3033 const global = struct {
3134 fn testOne(input: []const u8) anyerror!void {
......@@ -35,3 +38,8 @@ test "fuzz example" {
3538 };
3639 try std.testing.fuzz(global.testOne, .{});
3740}
41
42const std = @import("std");
43
44/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
45const lib = @import("$_lib");
lib/init/src/root.zig+1-1
......@@ -4,7 +4,7 @@
44const std = @import("std");
55const testing = std.testing;
66
7export fn add(a: i32, b: i32) i32 {
7pub export fn add(a: i32, b: i32) i32 {
88 return a + b;
99}
1010