-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.zig
280 lines (254 loc) · 11.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const std = @import("std");
const targets: []const std.Target.Query = &.{
// NB: some of these targets don't build yet in Cubyz, but are
// included for completion's sake
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .linux },
.{ .cpu_arch = .aarch64, .os_tag = .windows },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .linux },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
};
fn addPackageCSourceFiles(exe: *std.Build.Step.Compile, dep: *std.Build.Dependency, files: []const []const u8, flags: []const []const u8) void {
exe.addCSourceFiles(.{
.root = dep.path(""),
.files = files,
.flags = flags,
});
}
const freetypeSources = [_][]const u8{
"src/autofit/autofit.c",
"src/base/ftbase.c",
"src/base/ftsystem.c",
"src/base/ftdebug.c",
"src/base/ftbbox.c",
"src/base/ftbdf.c",
"src/base/ftbitmap.c",
"src/base/ftcid.c",
"src/base/ftfstype.c",
"src/base/ftgasp.c",
"src/base/ftglyph.c",
"src/base/ftgxval.c",
"src/base/ftinit.c",
"src/base/ftmm.c",
"src/base/ftotval.c",
"src/base/ftpatent.c",
"src/base/ftpfr.c",
"src/base/ftstroke.c",
"src/base/ftsynth.c",
"src/base/fttype1.c",
"src/base/ftwinfnt.c",
"src/bdf/bdf.c",
"src/bzip2/ftbzip2.c",
"src/cache/ftcache.c",
"src/cff/cff.c",
"src/cid/type1cid.c",
"src/gzip/ftgzip.c",
"src/lzw/ftlzw.c",
"src/pcf/pcf.c",
"src/pfr/pfr.c",
"src/psaux/psaux.c",
"src/pshinter/pshinter.c",
"src/psnames/psnames.c",
"src/raster/raster.c",
"src/sdf/sdf.c",
"src/sfnt/sfnt.c",
"src/smooth/smooth.c",
"src/svg/svg.c",
"src/truetype/truetype.c",
"src/type1/type1.c",
"src/type42/type42.c",
"src/winfonts/winfnt.c",
};
// Inlines are necessaryb to preserve comptime status of flags.
pub inline fn addPortAudio(b: *std.Build, c_lib: *std.Build.Step.Compile, target:std.Build.ResolvedTarget, flags: []const []const u8) void {
// compile portaudio from source:
const portaudio = b.dependency("portaudio", .{});
c_lib.addIncludePath(portaudio.path("include"));
c_lib.installHeadersDirectory(portaudio.path("include"), "", .{});
c_lib.addIncludePath(portaudio.path("src/common"));
addPackageCSourceFiles(c_lib, portaudio, &[_][]const u8 {
"src/common/pa_allocation.c",
"src/common/pa_converters.c",
"src/common/pa_cpuload.c",
"src/common/pa_debugprint.c",
"src/common/pa_dither.c",
"src/common/pa_front.c",
"src/common/pa_process.c",
"src/common/pa_ringbuffer.c",
"src/common/pa_stream.c",
"src/common/pa_trace.c",
}, flags);
if(target.result.os.tag == .windows) {
// windows:
addPackageCSourceFiles(c_lib, portaudio, &[_][]const u8 {"src/os/win/pa_win_coinitialize.c", "src/os/win/pa_win_hostapis.c", "src/os/win/pa_win_util.c", "src/os/win/pa_win_waveformat.c", "src/os/win/pa_win_wdmks_utils.c", "src/os/win/pa_x86_plain_converters.c", }, flags ++ &[_][]const u8{"-DPA_USE_WASAPI"});
c_lib.addIncludePath(portaudio.path("src/os/win"));
// WASAPI:
addPackageCSourceFiles(c_lib, portaudio, &[_][]const u8 {"src/hostapi/wasapi/pa_win_wasapi.c"}, flags);
} else if(target.result.os.tag == .linux) {
// unix:
addPackageCSourceFiles(c_lib, portaudio, &[_][]const u8 {"src/os/unix/pa_unix_hostapis.c", "src/os/unix/pa_unix_util.c"}, flags ++ &[_][]const u8{"-DPA_USE_ALSA"});
c_lib.addIncludePath(portaudio.path("src/os/unix"));
// ALSA:
addPackageCSourceFiles(c_lib, portaudio, &[_][]const u8 {"src/hostapi/alsa/pa_linux_alsa.c"}, flags);
} else if(target.result.os.tag == .macos) {
addPackageCSourceFiles(c_lib, portaudio, &[_][]const u8 {"src/os/unix/pa_unix_hostapis.c", "src/os/unix/pa_unix_util.c"}, flags ++ &[_][]const u8{"-DPA_USE_COREAUDIO"});
// coreaudio:
addPackageCSourceFiles(c_lib, portaudio, &[_][]const u8 {"src/hostapi/coreaudio/pa_mac_core_utilities.c", "src/hostapi/coreaudio/pa_mac_core.c", "src/hostapi/coreaudio/pa_mac_core_blocking.c", }, flags ++ &[_][]const u8{"-DPA_USE_COREAUDIO"});
} else {
std.log.err("Unsupported target: {}\n", .{ target.result.os.tag });
}
}
pub fn addFreetypeAndHarfbuzz(b: *std.Build, c_lib: *std.Build.Step.Compile, target: std.Build.ResolvedTarget, flags: []const []const u8) void {
const freetype = b.dependency("freetype", .{});
const harfbuzz = b.dependency("harfbuzz", .{});
c_lib.defineCMacro("FT2_BUILD_LIBRARY", "1");
c_lib.defineCMacro("HAVE_UNISTD_H", "1");
c_lib.addIncludePath(freetype.path("include"));
c_lib.installHeadersDirectory(freetype.path("include"), "", .{});
addPackageCSourceFiles(c_lib, freetype, &freetypeSources, flags);
if (target.result.os.tag == .macos) c_lib.addCSourceFile(.{
.file = freetype.path("src/base/ftmac.c"),
.flags = &.{},
});
c_lib.addIncludePath(harfbuzz.path("src"));
c_lib.installHeadersDirectory(harfbuzz.path("src"), "", .{});
c_lib.defineCMacro("HAVE_FREETYPE", "1");
c_lib.addCSourceFile(.{.file = harfbuzz.path("src/harfbuzz.cc"), .flags = flags});
c_lib.linkLibCpp();
}
pub inline fn addGLFWSources(b: *std.Build, c_lib: *std.Build.Step.Compile, target: std.Build.ResolvedTarget, flags: []const []const u8) void {
const glfw = b.dependency("glfw", .{});
const root = glfw.path("src");
const os = target.result.os.tag;
const WinSys = enum {win32, x11, cocoa};
// TODO: Wayland
// TODO: Cocoa
const ws: WinSys = switch(os) {
.windows => .win32,
.linux => .x11,
.macos => .x11,
// There are a surprising number of platforms zig supports.
// File a bug report if Cubyz doesn't work on yours.
else => blk: {
std.log.warn("Operating system ({}) is untested.", .{os});
break :blk .x11;
}
};
const ws_flag = switch(ws) {
.win32 => "-D_GLFW_WIN32",
.x11 => "-D_GLFW_X11",
.cocoa => "-D_GLFW_COCOA",
};
var all_flags = std.ArrayList([]const u8).init(b.allocator);
all_flags.appendSlice(flags) catch unreachable;
all_flags.append(ws_flag) catch unreachable;
if(os == .linux) {
all_flags.append("-D_GNU_SOURCE") catch unreachable;
}
c_lib.addIncludePath(glfw.path("include"));
c_lib.installHeader(glfw.path("include/GLFW/glfw3.h"), "GLFW/glfw3.h");
const fileses : [3][]const[]const u8 = .{
&.{"context.c", "init.c", "input.c", "monitor.c", "platform.c", "vulkan.c", "window.c", "egl_context.c", "osmesa_context.c", "null_init.c", "null_monitor.c", "null_window.c", "null_joystick.c"},
switch(os) {
.windows => &.{"win32_module.c", "win32_time.c", "win32_thread.c" },
.linux => &.{"posix_module.c", "posix_time.c", "posix_thread.c", "linux_joystick.c"},
.macos => &.{"cocoa_time.c", "posix_module.c", "posix_thread.c"},
else => &.{"posix_module.c", "posix_time.c", "posix_thread.c", "linux_joystick.c"},
},
switch(ws) {
.win32 => &.{"win32_init.c", "win32_joystick.c", "win32_monitor.c", "win32_window.c", "wgl_context.c"},
.x11 => &.{"x11_init.c", "x11_monitor.c", "x11_window.c", "xkb_unicode.c", "glx_context.c", "posix_poll.c"},
.cocoa => &.{"cocoa_platform.h", "cocoa_joystick.h", "cocoa_init.m", "cocoa_joystick.m", "cocoa_monitor.m", "cocoa_window.m", "nsgl_context.m"},
}
};
for(fileses) |files| {
c_lib.addCSourceFiles(.{
.root = root,
.files = files,
.flags = all_flags.items,
});
}
}
pub inline fn makeCubyzLibs(b: *std.Build, name: []const u8, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, flags: []const []const u8) *std.Build.Step.Compile {
const c_lib = b.addStaticLibrary(.{
.name = name,
.target = target,
.optimize = optimize,
});
c_lib.addAfterIncludePath(b.path("include"));
c_lib.installHeader(b.path("include/glad/glad.h"), "glad/glad.h");
c_lib.installHeader(b.path("include/KHR/khrplatform.h"), "KHR/khrplatform.h");
c_lib.installHeader(b.path("include/stb/stb_image_write.h"), "stb/stb_image_write.h");
c_lib.installHeader(b.path("include/stb/stb_image.h"), "stb/stb_image.h");
c_lib.installHeader(b.path("include/stb/stb_vorbis.h"), "stb/stb_vorbis.h");
addPortAudio(b, c_lib, target, flags);
addFreetypeAndHarfbuzz(b, c_lib, target, flags);
addGLFWSources(b, c_lib, target, flags);
c_lib.addCSourceFile(.{.file = b.path("lib/glad.c"), .flags = flags ++ &[_][]const u8 {"-D_MAC_X11"}});
c_lib.addCSourceFiles(.{.files = &[_][]const u8{"lib/stb_image.c", "lib/stb_image_write.c", "lib/stb_vorbis.c"}, .flags = flags});
return c_lib;
}
fn runChild(step: *std.Build.Step, argv: []const []const u8) !void {
const allocator = step.owner.allocator;
const result = try std.process.Child.run(.{.allocator = allocator, .argv = argv});
try std.io.getStdOut().writeAll(result.stdout);
try std.io.getStdErr().writeAll(result.stderr);
allocator.free(result.stdout);
allocator.free(result.stderr);
}
fn packageFunction(step: *std.Build.Step, _: std.Progress.Node) anyerror!void {
const base: []const []const u8 = &.{"tar", "-czf"};
try runChild(step, base ++ .{"zig-out/cubyz_deps_x86_64-windows-gnu.tar.gz", "zig-out/lib/cubyz_deps_x86_64-windows-gnu.lib"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_aarch64-windows-gnu.tar.gz", "zig-out/lib/cubyz_deps_aarch64-windows-gnu.lib"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_x86_64-linux-musl.tar.gz", "zig-out/lib/libcubyz_deps_x86_64-linux-musl.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_aarch64-linux-musl.tar.gz", "zig-out/lib/libcubyz_deps_aarch64-linux-musl.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_x86_64-macos-none.tar.gz", "zig-out/lib/libcubyz_deps_x86_64-macos-none.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_aarch64-macos-none.tar.gz", "zig-out/lib/libcubyz_deps_aarch64-macos-none.a"});
try runChild(step, base ++ .{"zig-out/cubyz_deps_headers.tar.gz", "zig-out/include"});
}
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const preferredTarget = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const preferredOptimize = b.standardOptimizeOption(.{});
const c_flags = &[_][]const u8{"-g"};
const releaseStep = b.step("release", "Build and package all targets for distribution");
const nativeStep = b.step("native", "Build only native target for debugging or local builds");
const buildStep = b.step("build_all", "Build all targets for distribution");
releaseStep.dependOn(buildStep);
for (targets) |target| {
const t = b.resolveTargetQuery(target);
const name = t.result.linuxTriple(b.allocator) catch unreachable;
const subStep = b.step(name, b.fmt("Build only {s}", .{name}));
const deps = b.fmt("cubyz_deps_{s}", .{name});
const c_lib = makeCubyzLibs(b, deps, t, .ReleaseSmall, c_flags);
const install = b.addInstallArtifact(c_lib, .{});
subStep.dependOn(&install.step);
buildStep.dependOn(subStep);
}
{
const name = preferredTarget.result.linuxTriple(b.allocator) catch unreachable;
const c_lib = makeCubyzLibs(b, b.fmt("cubyz_deps_{s}", .{name}), preferredTarget, preferredOptimize, c_flags);
const install = b.addInstallArtifact(c_lib, .{});
nativeStep.dependOn(&install.step);
}
{
const step = try b.allocator.create(std.Build.Step);
step.* = std.Build.Step.init(.{
.name = "package",
.makeFn = &packageFunction,
.owner = b,
.id = .custom,
});
step.dependOn(buildStep);
releaseStep.dependOn(step);
}
// Alias the default `zig build` to only build native target.
// Run `zig build release` to build all targets.
b.getInstallStep().dependOn(nativeStep);
}