forked from akarpovskii/tuile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
127 lines (104 loc) · 4.35 KB
/
build.zig
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const std = @import("std");
const Backend = enum {
ncurses,
crossterm,
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const backend = requestedBackend(b);
const options = b.addOptions();
options.addOption(Backend, "backend", backend);
const module = b.addModule("tuile", .{
.root_source_file = .{ .path = "src/tuile.zig" },
.target = target,
.optimize = optimize,
});
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
module.addOptions("build_options", options);
lib_unit_tests.root_module.addOptions("build_options", options);
switch (backend) {
.ncurses => {
module.link_libc = true;
module.linkSystemLibrary("ncurses", .{});
lib_unit_tests.linkLibC();
lib_unit_tests.linkSystemLibrary("ncurses");
},
.crossterm => {
const build_crab = b.dependency("build.crab", .{
.optimize = .ReleaseSafe,
});
const build_crossterm = b.addRunArtifact(build_crab.artifact("build_crab"));
build_crossterm.addArg("--out");
var crossterm_lib_path = build_crossterm.addOutputFileArg("libcrossterm.a");
build_crossterm.addArg("--deps");
_ = build_crossterm.addDepFileOutputArg("libcrossterm.d");
build_crossterm.addArg("--manifest-path");
_ = build_crossterm.addFileArg(b.path("src/backends/crossterm/Cargo.toml"));
const cargo_target = b.addNamedWriteFiles("cargo-target");
const target_dir = cargo_target.getDirectory();
build_crossterm.addArg("--target-dir");
build_crossterm.addDirectoryArg(target_dir);
build_crossterm.addArgs(&[_][]const u8{
"--",
"--release",
"--quiet",
});
if (@import("builtin").target.os.tag == .windows) {
build_crossterm.addArg("--target");
build_crossterm.addArg("x86_64-pc-windows-gnu");
const strip_symbols = b.addRunArtifact(build_crab.artifact("strip_symbols"));
strip_symbols.addArg("--archive");
strip_symbols.addFileArg(crossterm_lib_path);
strip_symbols.addArg("--temp-dir");
strip_symbols.addDirectoryArg(target_dir);
strip_symbols.addArg("--remove-symbol");
strip_symbols.addArg("___chkstk_ms");
strip_symbols.addArg("--output");
crossterm_lib_path = strip_symbols.addOutputFileArg("libcrossterm.a");
}
module.link_libcpp = true;
module.addLibraryPath(crossterm_lib_path.dirname());
module.linkSystemLibrary("crossterm", .{});
lib_unit_tests.linkLibCpp();
lib_unit_tests.addLibraryPath(crossterm_lib_path.dirname());
lib_unit_tests.linkSystemLibrary("crossterm");
},
}
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
run_lib_unit_tests.has_side_effects = true;
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
}
fn requestedBackend(b: *std.Build) Backend {
const backend_str = b.option([]const u8, "backend", "Backend") orelse
switch (@import("builtin").target.os.tag) {
.windows => @tagName(Backend.crossterm),
else => @tagName(Backend.ncurses),
};
var backend: Backend = undefined;
if (std.ascii.eqlIgnoreCase(backend_str, @tagName(Backend.ncurses))) {
backend = .ncurses;
} else if (std.ascii.eqlIgnoreCase(backend_str, @tagName(Backend.crossterm))) {
backend = .crossterm;
} else {
const names = comptime blk: {
const info = @typeInfo(Backend);
const fields = info.Enum.fields;
var names: [fields.len][]const u8 = undefined;
for (&names, fields) |*name, field| {
name.* = field.name;
}
break :blk names;
};
@panic(b.fmt(
"Option {s} is not a valid backend. Valid options are: {}",
.{ backend_str, std.json.fmt(names, .{}) },
));
}
return backend;
}