-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
190 lines (169 loc) · 6.24 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const platforms: []const std.Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
// .{ .cpu_arch = .x86_64, .os_tag = .windows },
// .{ .cpu_arch = .aarch64, .os_tag = .windows },
};
// zig build system guide
// https://ziglang.org/learn/build-system/
pub fn build(b: *std.Build) !void {
// All top-level steps you can invoke on the command line.
const build_steps = .{
.zig_ini_test = b.step("zig-ini:test", "Run zig-ini unit test"),
.bindings_wasm = b.step("bindings:wasm", "Build wasm bindings"),
.bindings_node = b.step("bindings:node", "Build node bindings"),
};
const optimize = b.standardOptimizeOption(.{});
const zig_ini_module = build_zig_ini_module(b);
build_wasm_bindings(b, build_steps.bindings_wasm, .{
.zig_ini_module = zig_ini_module,
.target = try resolve_target(b, .{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
}),
.optimize = optimize,
});
build_node_bindings(b, build_steps.bindings_node, .{
.zig_ini_module = zig_ini_module,
});
build_zig_ini_test(b, build_steps.zig_ini_test, .{
.zig_ini_module = zig_ini_module,
.target = try resolve_target(b, .{}),
.optimize = optimize,
});
}
fn build_zig_ini_module(b: *std.Build) *std.Build.Module {
const zig_ini_module = b.addModule("zig-ini", .{
.root_source_file = b.path("src/ini.zig"),
});
return zig_ini_module;
}
fn build_wasm_bindings(
b: *std.Build,
step_wasm_bindings: *std.Build.Step,
options: struct {
zig_ini_module: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
},
) void {
const wasm_bindings_generate = b.addExecutable(.{
.name = "zig-ini",
.root_source_file = b.path("bindings/wasm/src/wasm_bindings.zig"),
.target = options.target,
.optimize = .ReleaseSmall,
});
wasm_bindings_generate.root_module.addImport("zig-ini", options.zig_ini_module);
wasm_bindings_generate.rdynamic = true;
wasm_bindings_generate.entry = .disabled;
step_wasm_bindings.dependOn(&b.addInstallFile(wasm_bindings_generate.getEmittedBin(), b.pathJoin(&.{
"../bindings/wasm",
"zig-ini.wasm",
})).step);
var write_build_script = b.addSystemCommand(&.{
"./node_modules/.bin/jiek",
"build",
"-f",
"bindings-wasm",
"-c",
"bindings/wasm/.jiek.config.js",
});
step_wasm_bindings.dependOn(&write_build_script.step);
const copy_dist = b.addSystemCommand(&.{
"cp",
"-r",
"./bindings/wasm/dist",
"./npm/zig-ini-wasm",
});
step_wasm_bindings.dependOn(©_dist.step);
}
fn build_node_bindings(
b: *std.Build,
step_node_bindings: *std.Build.Step,
options: struct {
zig_ini_module: *std.Build.Module,
},
) void {
inline for (platforms) |platform| {
const node_bindings_generate = b.addSharedLibrary(.{
.name = "zig-ini",
.root_source_file = b.path("bindings/node/src/node_bindings.zig"),
.optimize = .ReleaseSmall,
.target = b.resolveTargetQuery(platform),
});
node_bindings_generate.root_module.addImport("zig-ini", options.zig_ini_module);
node_bindings_generate.addSystemIncludePath(b.path("bindings/node/node_modules/node-api-headers/include"));
node_bindings_generate.linker_allow_shlib_undefined = true;
node_bindings_generate.linkLibC();
step_node_bindings.dependOn(&b.addInstallFile(node_bindings_generate.getEmittedBin(), b.pathJoin(&.{
"../npm/@zig-ini",
arch_os_to_str(platform),
"zig-ini.node",
})).step);
}
// do build and copy file
const node_build_script = b.addSystemCommand(&.{
"./node_modules/.bin/jiek",
"build",
"-f",
"bindings-node",
"--noMin",
});
step_node_bindings.dependOn(&node_build_script.step);
const copy_dist = b.addSystemCommand(&.{ "cp", "-r", "./bindings/node/dist", "./npm/zig-ini" });
copy_dist.step.dependOn(&node_build_script.step);
// const copy_post_install = b.addSystemCommand(&.{ "cp", "-r", "./bindings/node/install.js", "./npm/zig-ini" });
step_node_bindings.dependOn(©_dist.step);
// step_node_bindings.dependOn(©_post_install.step);
}
fn build_zig_ini_test(
b: *std.Build,
step_zig_ini_test: *std.Build.Step,
options: struct {
zig_ini_module: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
},
) void {
const zig_init_test = b.addTest(.{
.root_source_file = b.path("src/test.zig"),
.target = options.target,
.optimize = options.optimize,
});
const run_ini_unit_test = b.addRunArtifact(zig_init_test);
step_zig_ini_test.dependOn(&run_ini_unit_test.step);
}
fn resolve_target(b: *std.Build, target_requested: std.Target.Query) !std.Build.ResolvedTarget {
var target: std.Target.Query = .{
.cpu_arch = builtin.target.cpu.arch,
.os_tag = builtin.target.os.tag,
};
if (target_requested.cpu_arch) |cpu_arch| {
target.cpu_arch = cpu_arch;
}
if (target_requested.os_tag) |os_tag| {
target.os_tag = os_tag;
}
const targets: []const std.Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .linux },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
.{ .cpu_arch = .aarch64, .os_tag = .windows },
.{ .cpu_arch = .wasm32, .os_tag = .freestanding },
};
for (targets) |query| {
if (query.cpu_arch == target.cpu_arch and query.os_tag == target.os_tag) {
return b.resolveTargetQuery(query);
}
}
return error.UnsupportedTarget;
}
inline fn arch_os_to_str(query: std.Target.Query) []const u8 {
const arch = query.cpu_arch orelse return "unknown";
const os = query.os_tag orelse return "unknown";
return @tagName(arch) ++ "-" ++ @tagName(os);
}