-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
151 lines (120 loc) · 5.62 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
const std = @import("std");
const Builder = std.build.Builder;
const glfw = @import("libs/mach-glfw/build.zig");
pub fn build(b: *std.build.Builder) 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 target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const options = Options{};
const exe = b.addExecutable("zigray", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
link(b, exe, options);
exe.install();
exe.addPackagePath("glfw", "libs/mach-glfw/src/main.zig");
glfw.link(b, exe, .{});
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}
pub const LinuxWindowManager = enum {
X11,
Wayland,
};
pub const Options = struct {
/// Defaults to X11 on Linux.
linux_window_manager: ?LinuxWindowManager = null,
/// Defaults to true on Windows
d3d12: ?bool = null,
/// Defaults to true on Darwin
metal: ?bool = null,
/// Defaults to true on Linux, Fuchsia
// TODO(build-system): enable on Windows if we can cross compile Vulkan
vulkan: ?bool = null,
/// Defaults to true on Linux
desktop_gl: ?bool = null,
/// Defaults to true on Android, Linux, Windows, Emscripten
// TODO(build-system): not respected at all currently
opengl_es: ?bool = null,
/// Whether or not minimal debug symbols should be emitted. This is -g1 in most cases, enough to
/// produce stack traces but omitting debug symbols for locals. For spirv-tools and tint in
/// specific, -g0 will be used (no debug symbols at all) to save an additional ~39M.
///
/// When enabled, a debug build of the static library goes from ~947M to just ~53M.
minimal_debug_symbols: bool = true,
/// Whether or not to produce separate static libraries for each component of Dawn (reduces
/// iteration times when building from source / testing changes to Dawn source code.)
separate_libs: bool = false,
/// The binary release version to use from https://github.com/hexops/mach-gpu-dawn/releases
binary_version: []const u8 = "release-e95698d",
/// Detects the default options to use for the given target.
pub fn detectDefaults(self: Options, target: std.Target) Options {
const tag = target.os.tag;
const linux_desktop_like = isLinuxDesktopLike(target);
var options = self;
if (options.linux_window_manager == null and linux_desktop_like) options.linux_window_manager = .X11;
if (options.d3d12 == null) options.d3d12 = tag == .windows;
if (options.metal == null) options.metal = tag.isDarwin();
if (options.vulkan == null) options.vulkan = tag == .fuchsia or linux_desktop_like;
// TODO(build-system): technically Dawn itself defaults desktop_gl to true on Windows.
if (options.desktop_gl == null) options.desktop_gl = linux_desktop_like;
options.opengl_es = false; // TODO(build-system): OpenGL ES
// if (options.opengl_es == null) options.opengl_es = tag == .windows or tag == .emscripten or target.isAndroid() or linux_desktop_like;
return options;
}
pub fn appendFlags(self: Options, flags: *std.ArrayList([]const u8), zero_debug_symbols: bool, is_cpp: bool) !void {
if (self.minimal_debug_symbols) {
if (zero_debug_symbols) try flags.append("-g0") else try flags.append("-g1");
}
if (is_cpp) try flags.append("-std=c++17");
if (self.linux_window_manager != null and self.linux_window_manager.? == .X11) try flags.append("-DDAWN_USE_X11");
}
};
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
const target = (std.zig.system.NativeTargetInfo.detect(b.allocator, step.target) catch unreachable).target;
const opt = options.detectDefaults(target);
linkFromBinary(b, step, opt);
}
pub fn linkFromBinary(_: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
step.addLibraryPath("libs");
step.linkSystemLibrary("dawn");
step.linkLibCpp();
step.addIncludeDir(thisDir() ++ "/libs/include");
step.addIncludeDir(thisDir() ++ "/libs"); // "extern/futureproof.h"
if (options.linux_window_manager != null and options.linux_window_manager.? == .X11) {
step.linkSystemLibrary("X11");
}
if (options.metal.?) {
step.linkFramework("Metal");
step.linkFramework("CoreGraphics");
step.linkFramework("Foundation");
step.linkFramework("IOKit");
step.linkFramework("IOSurface");
step.linkFramework("QuartzCore");
}
if (options.d3d12.?) {
step.linkSystemLibrary("ole32");
step.linkSystemLibrary("dxguid");
}
}
fn isLinuxDesktopLike(target: std.Target) bool {
const tag = target.os.tag;
return !tag.isDarwin() and tag != .windows and tag != .fuchsia and tag != .emscripten and !target.isAndroid();
}
fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}