This repository was archived by the owner on Nov 26, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbuild.zig
More file actions
59 lines (48 loc) · 2.27 KB
/
build.zig
File metadata and controls
59 lines (48 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const std = @import("std");
/// Import the `Translator` helper from the `translate_c` dependency.
const Translator = @import("translate_c").Translator;
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Prepare the `translate-c` dependency.
const translate_c = b.dependency("translate_c", .{});
// Create a step to translate `main.c`. This also creates a Zig module from the output.
const trans: Translator = .init(translate_c, .{
.c_source_file = b.path("main.c"),
.target = target,
.optimize = optimize,
});
trans.defineCMacro("__TRANSLATE_C__", null);
// Build an executable from `trans.mod` (the Zig module containing the translated code).
const translated_exe = b.addExecutable(.{
.name = "translated-exe",
.root_module = trans.mod,
});
// Like above, but compile the C code directly instead of using `translate-c`.
const c_module = b.createModule(.{
.link_libc = true,
.target = target,
.optimize = optimize,
});
c_module.addCSourceFile(.{ .file = b.path("main.c") });
const c_exe = b.addExecutable(.{
.name = "c-exe",
.root_module = c_module,
});
// We have two executables. Run them both, and test that they do what we expect.
const test_step = b.step("test", "Build and test both executables");
b.default_step = test_step;
// Windows libc changes stdout newlines to CRLF by default.
const newline: []const u8 = if (target.result.os.tag == .windows) "\r\n" else "\n";
const test_c = b.addRunArtifact(c_exe);
test_c.expectStdOutEqual(b.fmt("Hello from my C program!{s}", .{newline}));
test_step.dependOn(&test_c.step);
const test_translated = b.addRunArtifact(translated_exe);
test_translated.expectStdOutEqual(b.fmt("Hello from my Zig program!{s}", .{newline}));
test_step.dependOn(&test_translated.step);
// These are just steps you can use to actually see the output of each executable.
b.step("run-translated", "Run the translated Zig executable")
.dependOn(&b.addRunArtifact(translated_exe).step);
b.step("run-c", "Run the directly compiled C executable")
.dependOn(&b.addRunArtifact(c_exe).step);
}