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 {...@@ -15,8 +15,12 @@ pub fn build(b: *std.Build) void {
15 // set a preferred release mode, allowing the user to decide how to optimize.15 // set a preferred release mode, allowing the user to decide how to optimize.
16 const optimize = b.standardOptimizeOption(.{});16 const optimize = b.standardOptimizeOption(.{});
1717
18 const lib = b.addStaticLibrary(.{18 // This creates a "module", which represents a collection of source files alongside
19 .name = "$",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`.
20 // In this case the main source file is merely a path, however, in more24 // In this case the main source file is merely a path, however, in more
21 // complicated build scripts, this could be a generated file.25 // complicated build scripts, this could be a generated file.
22 .root_source_file = b.path("src/root.zig"),26 .root_source_file = b.path("src/root.zig"),
...@@ -24,16 +28,40 @@ pub fn build(b: *std.Build) void {...@@ -24,16 +28,40 @@ pub fn build(b: *std.Build) void {
24 .optimize = optimize,28 .optimize = optimize,
25 });29 });
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
27 // This declares intent for the library to be installed into the standard55 // This declares intent for the library to be installed into the standard
28 // location when the user invokes the "install" step (the default step when56 // location when the user invokes the "install" step (the default step when
29 // running `zig build`).57 // running `zig build`).
30 b.installArtifact(lib);58 b.installArtifact(lib);
3159
60 // This creates another `std.Build.Step.Compile`, but this one builds an executable
61 // rather than a static library.
32 const exe = b.addExecutable(.{62 const exe = b.addExecutable(.{
33 .name = "$",63 .name = "$",
34 .root_source_file = b.path("src/main.zig"),64 .root_module = exe_mod,
35 .target = target,
36 .optimize = optimize,
37 });65 });
3866
39 // This declares intent for the executable to be installed into the67 // This declares intent for the executable to be installed into the
...@@ -67,17 +95,13 @@ pub fn build(b: *std.Build) void {...@@ -67,17 +95,13 @@ pub fn build(b: *std.Build) void {
67 // Creates a step for unit testing. This only builds the test executable95 // Creates a step for unit testing. This only builds the test executable
68 // but does not run it.96 // but does not run it.
69 const lib_unit_tests = b.addTest(.{97 const lib_unit_tests = b.addTest(.{
70 .root_source_file = b.path("src/root.zig"),98 .root_module = lib_mod,
71 .target = target,
72 .optimize = optimize,
73 });99 });
74100
75 const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);101 const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
76102
77 const exe_unit_tests = b.addTest(.{103 const exe_unit_tests = b.addTest(.{
78 .root_source_file = b.path("src/main.zig"),104 .root_module = exe_mod,
79 .target = target,
80 .optimize = optimize,
81 });105 });
82106
83 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);107 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
lib/init/src/main.zig+9-1
...@@ -1,7 +1,6 @@...@@ -1,7 +1,6 @@
1//! By convention, main.zig is where your main function lives in the case that1//! 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 convention2//! 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.3//! is to delete this file and start with root.zig instead.
4const std = @import("std");
54
6pub fn main() !void {5pub fn main() !void {
7 // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)6 // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
...@@ -26,6 +25,10 @@ test "simple test" {...@@ -26,6 +25,10 @@ test "simple test" {
26 try std.testing.expectEqual(@as(i32, 42), list.pop());25 try std.testing.expectEqual(@as(i32, 42), list.pop());
27}26}
2827
28test "use other module" {
29 try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
30}
31
29test "fuzz example" {32test "fuzz example" {
30 const global = struct {33 const global = struct {
31 fn testOne(input: []const u8) anyerror!void {34 fn testOne(input: []const u8) anyerror!void {
...@@ -35,3 +38,8 @@ test "fuzz example" {...@@ -35,3 +38,8 @@ test "fuzz example" {
35 };38 };
36 try std.testing.fuzz(global.testOne, .{});39 try std.testing.fuzz(global.testOne, .{});
37}40}
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 @@...@@ -4,7 +4,7 @@
4const std = @import("std");4const std = @import("std");
5const testing = std.testing;5const testing = std.testing;
66
7export fn add(a: i32, b: i32) i32 {7pub export fn add(a: i32, b: i32) i32 {
8 return a + b;8 return a + b;
9}9}
1010