|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub fn build(b: *std.Build) !void { |
| 4 | + const target = b.standardTargetOptions(.{}); |
| 5 | + const optimize = b.standardOptimizeOption(.{}); |
| 6 | + |
| 7 | + const build_examples_option = b.option(bool, "examples", "Build example files") orelse false; |
| 8 | + |
| 9 | + const run_step = b.step("run", "Run the app"); |
| 10 | + |
| 11 | + const wasmer_module = b.addModule("wasmer", .{ |
| 12 | + .root_source_file = b.path("src/wasmer.zig"), |
| 13 | + .target = target, |
| 14 | + .optimize = optimize, |
| 15 | + }); |
| 16 | + |
| 17 | + if (build_examples_option) { |
| 18 | + var examples_dir = try std.fs.cwd().openDir("examples", .{ .iterate = true }); |
| 19 | + defer examples_dir.close(); |
| 20 | + |
| 21 | + var examples_dir_iter = examples_dir.iterate(); |
| 22 | + |
| 23 | + while (try examples_dir_iter.next()) |entry| { |
| 24 | + if (entry.kind == .file and std.mem.endsWith(u8, entry.name, ".zig")) { |
| 25 | + var buffer_exe_path = std.ArrayList(u8).init(b.allocator); |
| 26 | + const exe_name = entry.name[0 .. entry.name.len - 4]; |
| 27 | + |
| 28 | + try buffer_exe_path.appendSlice("examples/"); |
| 29 | + try buffer_exe_path.appendSlice(entry.name); |
| 30 | + |
| 31 | + const exe_path = try buffer_exe_path.toOwnedSlice(); |
| 32 | + |
| 33 | + const example_exe = b.addExecutable(.{ |
| 34 | + .name = exe_name, |
| 35 | + .root_source_file = b.path(exe_path), |
| 36 | + .target = target, |
| 37 | + .optimize = optimize, |
| 38 | + .link_libc = true, |
| 39 | + }); |
| 40 | + |
| 41 | + example_exe.root_module.addImport("wasmer", wasmer_module); |
| 42 | + example_exe.root_module.addLibraryPath(.{ .cwd_relative = "/home/afirium/.wasmer/lib" }); |
| 43 | + example_exe.root_module.linkSystemLibrary("wasmer", .{}); |
| 44 | + |
| 45 | + b.installArtifact(example_exe); |
| 46 | + const run_example_cmd = b.addRunArtifact(example_exe); |
| 47 | + run_example_cmd.step.dependOn(b.getInstallStep()); |
| 48 | + |
| 49 | + run_step.dependOn(&run_example_cmd.step); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Creates a step for unit testing. This only builds the test executable |
| 55 | + // but does not run it. |
| 56 | + const wasmer_unit_tests = b.addTest(.{ |
| 57 | + .root_source_file = b.path("src/wasmer.zig"), |
| 58 | + .target = target, |
| 59 | + .optimize = optimize, |
| 60 | + }); |
| 61 | + |
| 62 | + wasmer_unit_tests.linkLibC(); |
| 63 | + wasmer_unit_tests.addLibraryPath(.{ .cwd_relative = "/home/afirium/.wasmer/lib" }); |
| 64 | + wasmer_unit_tests.linkSystemLibrary("wasmer"); |
| 65 | + |
| 66 | + const run_wasmer_unit_tests = b.addRunArtifact(wasmer_unit_tests); |
| 67 | + |
| 68 | + // Similar to creating the run step earlier, this exposes a `test` step to |
| 69 | + // the `zig build --help` menu, providing a way for the user to request |
| 70 | + // running the unit tests. |
| 71 | + const test_step = b.step("test", "Run unit tests"); |
| 72 | + test_step.dependOn(&run_wasmer_unit_tests.step); |
| 73 | +} |
0 commit comments