-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
227 lines (198 loc) · 7.25 KB
/
build.zig
File metadata and controls
227 lines (198 loc) · 7.25 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const std = @import("std");
const builtin = @import("builtin");
/// Roc target definitions
const RocTarget = enum {
x64mac,
x64win,
x64musl,
x64glibc,
wasm32,
arm64mac,
arm64win,
arm64musl,
fn toZigTarget(self: RocTarget) std.Target.Query {
return switch (self) {
.x64mac => .{ .cpu_arch = .x86_64, .os_tag = .macos },
.x64win => .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
.x64musl => .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl },
.x64glibc => .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
.wasm32 => .{ .cpu_arch = .wasm32, .os_tag = .wasi },
.arm64mac => .{ .cpu_arch = .aarch64, .os_tag = .macos },
.arm64win => .{ .cpu_arch = .aarch64, .os_tag = .windows, .abi = .gnu },
.arm64musl => .{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .musl },
};
}
fn targetDir(self: RocTarget) []const u8 {
return switch (self) {
.x64mac => "x64mac",
.x64win => "x64win",
.x64musl => "x64musl",
.x64glibc => "x64glibc",
.wasm32 => "wasm32",
.arm64mac => "arm64mac",
.arm64win => "arm64win",
.arm64musl => "arm64musl",
};
}
fn libFilename(self: RocTarget) []const u8 {
return switch (self) {
.x64win, .arm64win => "host.lib",
else => "libhost.a",
};
}
};
/// All cross-compilation targets
const all_targets = [_]RocTarget{
.x64mac,
.x64win,
.x64musl,
.x64glibc,
.arm64mac,
.arm64win,
.arm64musl,
// Note: wasm32 excluded as WebSocket server doesn't make sense for WASM
};
pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
// Get the roc dependency and its builtins module
const roc_dep = b.dependency("roc", .{});
const builtins_module = roc_dep.module("builtins");
// Cleanup step
const cleanup_step = b.step("clean", "Remove all built library files");
for (all_targets) |roc_target| {
cleanup_step.dependOn(&CleanupStep.create(b, b.path(
b.pathJoin(&.{ "platform", "targets", roc_target.targetDir(), roc_target.libFilename() }),
)).step);
}
cleanup_step.dependOn(&CleanupStep.create(b, b.path("platform/libhost.a")).step);
cleanup_step.dependOn(&CleanupStep.create(b, b.path("platform/host.lib")).step);
// Default step: build for all targets
const all_step = b.getInstallStep();
all_step.dependOn(cleanup_step);
const copy_all = b.addUpdateSourceFiles();
all_step.dependOn(©_all.step);
// Build for each Roc target
for (all_targets) |roc_target| {
const target_step_name = @tagName(roc_target);
const target_step = b.step(target_step_name, b.fmt("Build host library for {s} target only", .{target_step_name}));
target_step.dependOn(cleanup_step);
const target = b.resolveTargetQuery(roc_target.toZigTarget());
const host_lib = buildHostLib(b, target, optimize, builtins_module);
const copy_target = b.addUpdateSourceFiles();
copy_target.addCopyFileToSource(
host_lib.getEmittedBin(),
b.pathJoin(&.{ "platform", "targets", roc_target.targetDir(), roc_target.libFilename() }),
);
target_step.dependOn(©_target.step);
copy_all.addCopyFileToSource(
host_lib.getEmittedBin(),
b.pathJoin(&.{ "platform", "targets", roc_target.targetDir(), roc_target.libFilename() }),
);
}
// Native step: build only for the current platform
const native_step = b.step("native", "Build host library for native platform only");
native_step.dependOn(cleanup_step);
const native_target = b.standardTargetOptions(.{});
const native_roc_target = detectNativeRocTarget(native_target.result) orelse {
std.debug.print("Unsupported native platform\n", .{});
return;
};
const native_lib = buildHostLib(b, native_target, optimize, builtins_module);
b.installArtifact(native_lib);
const copy_native = b.addUpdateSourceFiles();
copy_native.addCopyFileToSource(
native_lib.getEmittedBin(),
b.pathJoin(&.{ "platform", "targets", native_roc_target.targetDir(), native_roc_target.libFilename() }),
);
native_step.dependOn(©_native.step);
native_step.dependOn(&native_lib.step);
// Test step
const test_step = b.step("test", "Run all tests");
const host_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("platform/host.zig"),
.target = native_target,
.optimize = optimize,
.imports = &.{
.{ .name = "builtins", .module = builtins_module },
},
}),
});
if (native_target.result.os.tag != .windows and native_target.result.os.tag != .wasi) {
host_tests.linkLibC();
}
const run_host_tests = b.addRunArtifact(host_tests);
test_step.dependOn(&run_host_tests.step);
}
fn detectNativeRocTarget(target: std.Target) ?RocTarget {
return switch (target.os.tag) {
.macos => switch (target.cpu.arch) {
.x86_64 => .x64mac,
.aarch64 => .arm64mac,
else => null,
},
.linux => switch (target.cpu.arch) {
.x86_64 => .x64glibc,
.aarch64 => .arm64musl,
else => null,
},
.windows => switch (target.cpu.arch) {
.x86_64 => .x64win,
.aarch64 => .arm64win,
else => null,
},
else => null,
};
}
const CleanupStep = struct {
step: std.Build.Step,
path: std.Build.LazyPath,
fn create(b: *std.Build, path: std.Build.LazyPath) *CleanupStep {
const self = b.allocator.create(CleanupStep) catch @panic("OOM");
self.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = "cleanup",
.owner = b,
.makeFn = make,
}),
.path = path,
};
return self;
}
fn make(step: *std.Build.Step, options: std.Build.Step.MakeOptions) !void {
_ = options;
const self: *CleanupStep = @fieldParentPtr("step", step);
const path = self.path.getPath2(step.owner, null);
std.fs.cwd().deleteFile(path) catch |err| switch (err) {
error.FileNotFound => {},
else => return err,
};
}
};
fn buildHostLib(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
builtins_module: *std.Build.Module,
) *std.Build.Step.Compile {
const host_lib = b.addLibrary(.{
.name = "host",
.linkage = .static,
.root_module = b.createModule(.{
.root_source_file = b.path("platform/host.zig"),
.target = target,
.optimize = optimize,
.strip = optimize != .Debug,
.pic = true,
.imports = &.{
.{ .name = "builtins", .module = builtins_module },
},
}),
});
host_lib.bundle_compiler_rt = true;
if (target.result.os.tag != .windows and target.result.os.tag != .wasi) {
host_lib.linkLibC();
}
return host_lib;
}