-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
260 lines (222 loc) · 7.4 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const std = @import("std");
/// Path to current directory containing this 'build.zig'
pub const root_path = getRootPath();
fn getRootPath() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
for (examples) |example| {
const exe = b.addExecutable(example.name, example.path);
exe.setTarget(target);
exe.setBuildMode(mode);
// zig fmt: off
linkPkg(b, exe, .{.build_pixie = .{
.check_existence = true,
.option = .{
.gc = .arc,
.extra_options = &[_][]const u8{
"--cc:clang",
"--clang.exe:zig-cc",
"--clang.linkerexe:zig-cc",
},
},
}});
// zig fmt: on
exe.install();
}
}
const Example = struct {
name: []const u8,
path: []const u8,
};
const examples = [_]Example{
Example{
.name = "square",
.path = root_path ++ "/example/square.zig",
},
Example{
.name = "gradient_heart",
.path = root_path ++ "/example/gradient_heart.zig",
},
};
// ========================================
// Package SDK
// ========================================
pub const pkg = std.build.Pkg{
.name = "pixie-zig",
.source = .{ .path = root_path ++ "/src/main.zig" },
};
pub const LinkPkgOption = union(enum) {
/// Link pre-installed 'pixie'
link_pixie: struct {
path: []const u8,
name: []const u8 = "pixie",
},
/// Build 'pixie' from source
build_pixie: struct {
/// Install via `nimble`
install_pixie: bool = false,
/// Check if library 'pixie_ffi' exists
check_existence: bool = false,
/// Nim build options
option: NimBuild = .{
.mode = .release,
.gc = .orc,
.linkage = .static,
.optimization = .speed,
},
},
};
pub fn linkPkg(b: *std.build.Builder, exe: *std.build.LibExeObjStep, option: LinkPkgOption) void {
const pixie_path = root_path ++ "/vendor/pixie";
// Link '-lc' if needed
if (!exe.is_linking_libc) {
exe.linkLibC();
}
switch (option) {
.link_pixie => |opt| {
exe.addLibraryPath(opt.path);
exe.linkSystemLibrary(opt.name);
exe.addPackage(pkg);
},
.build_pixie => |opt| {
if (opt.check_existence) {
const library_paths = switch (exe.target.getOsTag()) {
.windows => &[_][]const u8{
pixie_path ++ "/libpixie_ffi.a",
pixie_path ++ "/libpixie_ffi.lib",
pixie_path ++ "/libpixie_ffi.dll",
pixie_path ++ "/pixie_ffi.a",
pixie_path ++ "/pixie_ffi.lib",
pixie_path ++ "/pixie_ffi.dll",
},
.macos => &[_][]const u8{
pixie_path ++ "/libpixie_ffi.a",
pixie_path ++ "/libpixie_ffi.so",
pixie_path ++ "/libpixie_ffi.dylib",
pixie_path ++ "/pixie_ffi.a",
pixie_path ++ "/pixie_ffi.so",
pixie_path ++ "/pixie_ffi.dylib",
},
else => &[_][]const u8{
pixie_path ++ "/libpixie_ffi.a",
pixie_path ++ "/libpixie_ffi.so",
pixie_path ++ "/pixie_ffi.a",
pixie_path ++ "/pixie_ffi.so",
},
};
for (library_paths) |library_path| if (checkFileExist(library_path)) {
exe.addLibraryPath(pixie_path);
exe.linkSystemLibrary("pixie_ffi");
exe.addPackage(pkg);
return;
};
}
_ = b.findProgram(
&[_][]const u8{"nim"},
&[_][]const u8{},
) catch @panic("'nim' not found");
// Install 'pixie' via `nimble` for building FFI source
if (opt.install_pixie) {
_ = b.findProgram(
&[_][]const u8{"nimble"},
&[_][]const u8{},
) catch @panic("'nimble' not found");
_ = b.exec(&[_][]const u8{
"nimble", "install", "pixie",
}) catch @panic("failed to install 'pixie'");
}
// Make a build command
const build_command = NimBuild.makeCommand(b.allocator, pixie_path ++ "/pixie_ffi.nim", opt.option) catch unreachable;
defer b.allocator.free(build_command);
_ = b.exec(build_command) catch @panic("failed to build 'pixie'");
exe.addLibraryPath(pixie_path);
exe.linkSystemLibrary("pixie_ffi");
exe.addPackage(pkg);
},
}
}
fn checkFileExist(path: []const u8) bool {
var file = std.fs.cwd().openFile(path, .{}) catch |err| switch (err) {
error.FileNotFound => return false,
else => unreachable,
};
defer file.close();
return true;
}
pub const NimBuild = struct {
/// Build mode
mode: BuildMode = .release,
/// Garbage collector
gc: Gc = .orc,
/// Library linkage
linkage: Linkage = .shared,
/// Optimization
optimization: Optimization = .speed,
/// Use LTO (Link Time Optimization)
use_lto: bool = false,
/// Extra options
extra_options: ?[]const []const u8 = null,
pub fn makeCommand(allocator: std.mem.Allocator, path: []const u8, option: NimBuild) ![][]const u8 {
var result = std.ArrayList([]const u8).init(allocator);
errdefer result.deinit();
// User must verify `nim` exists on their machine
try result.append("nim");
try result.append("c");
// Prevent duplicating 'main' symbol
try result.append("--noMain:on");
// Add build mode
try result.append(switch (option.mode) {
.debug => "-d:debug",
.release => "-d:release",
.danger => "-d:danger",
});
// Choose garbage collector
try result.append(switch (option.gc) {
.arc => "--gc:arc",
.orc => "--gc:orc",
});
// Choose library linkage
try result.append(switch (option.linkage) {
.static => "--app:staticlib",
.shared => "--app:lib",
});
// Add optimization
try result.append(switch (option.optimization) {
.none => "--opt:none",
.speed => "--opt:speed",
.size => "--opt:size",
});
// Use LTO if enabled
if (option.use_lto) {
try result.append("-d:lto");
}
// Add extra options
if (option.extra_options) |extra_options| {
try result.appendSlice(extra_options);
}
// Add Nim source file
try result.append(path);
return result.toOwnedSlice();
}
pub const BuildMode = enum {
debug,
release,
danger,
};
pub const Gc = enum {
arc,
orc,
};
pub const Linkage = enum {
static,
shared,
};
pub const Optimization = enum {
none,
speed,
size,
};
};