-
Notifications
You must be signed in to change notification settings - Fork 28
/
build.zig
224 lines (192 loc) · 6.94 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const shared = b.option(bool, "shared", "Build as a shared library") orelse false;
const use_x11 = b.option(bool, "x11", "Build with X11. Only useful on Linux") orelse true;
const use_wl = b.option(bool, "wayland", "Build with Wayland. Only useful on Linux") orelse true;
const use_opengl = b.option(bool, "opengl", "Build with OpenGL; deprecated on MacOS") orelse false;
const use_gles = b.option(bool, "gles", "Build with GLES; not supported on MacOS") orelse false;
const use_metal = b.option(bool, "metal", "Build with Metal; only supported on MacOS") orelse true;
const lib = std.Build.Step.Compile.create(b, .{
.name = "glfw",
.kind = .lib,
.linkage = if (shared) .dynamic else .static,
.root_module = .{
.target = target,
.optimize = optimize,
},
});
lib.addIncludePath(b.path("include"));
lib.linkLibC();
if (shared) lib.defineCMacro("_GLFW_BUILD_DLL", "1");
lib.installHeadersDirectory(b.path("include/GLFW"), "GLFW", .{});
// GLFW headers depend on these headers, so they must be distributed too.
if (b.lazyDependency("vulkan_headers", .{
.target = target,
.optimize = optimize,
})) |dep| {
lib.installLibraryHeaders(dep.artifact("vulkan-headers"));
}
if (target.result.os.tag == .linux) {
if (b.lazyDependency("x11_headers", .{
.target = target,
.optimize = optimize,
})) |dep| {
lib.linkLibrary(dep.artifact("x11-headers"));
lib.installLibraryHeaders(dep.artifact("x11-headers"));
}
if (b.lazyDependency("wayland_headers", .{})) |dep| {
lib.addIncludePath(dep.path("wayland"));
lib.addIncludePath(dep.path("wayland-protocols"));
lib.installHeadersDirectory(dep.path("wayland"), ".", .{});
lib.installHeadersDirectory(dep.path("wayland-protocols"), ".", .{});
}
}
if (target.result.isDarwin()) {
// MacOS: this must be defined for macOS 13.3 and older.
lib.defineCMacro("__kernel_ptr_semantics", "");
if (b.lazyDependency("xcode_frameworks", .{
.target = target,
.optimize = optimize,
})) |dep| {
lib.root_module.addSystemFrameworkPath(dep.path("Frameworks"));
lib.root_module.addSystemIncludePath(dep.path("include"));
lib.root_module.addLibraryPath(dep.path("lib"));
}
}
const include_src_flag = "-Isrc";
switch (target.result.os.tag) {
.windows => {
lib.linkSystemLibrary("gdi32");
lib.linkSystemLibrary("user32");
lib.linkSystemLibrary("shell32");
if (use_opengl) {
lib.linkSystemLibrary("opengl32");
}
if (use_gles) {
lib.linkSystemLibrary("GLESv3");
}
const flags = [_][]const u8{ "-D_GLFW_WIN32", include_src_flag };
lib.addCSourceFiles(.{
.files = &base_sources,
.flags = &flags,
});
lib.addCSourceFiles(.{
.files = &windows_sources,
.flags = &flags,
});
},
.macos => {
// Transitive dependencies, explicit linkage of these works around
// ziglang/zig#17130
lib.linkFramework("CFNetwork");
lib.linkFramework("ApplicationServices");
lib.linkFramework("ColorSync");
lib.linkFramework("CoreText");
lib.linkFramework("ImageIO");
// Direct dependencies
lib.linkSystemLibrary("objc");
lib.linkFramework("IOKit");
lib.linkFramework("CoreFoundation");
lib.linkFramework("AppKit");
lib.linkFramework("CoreServices");
lib.linkFramework("CoreGraphics");
lib.linkFramework("Foundation");
if (use_metal) {
lib.linkFramework("Metal");
}
if (use_opengl) {
lib.linkFramework("OpenGL");
}
const flags = [_][]const u8{ "-D_GLFW_COCOA", include_src_flag };
lib.addCSourceFiles(.{
.files = &base_sources,
.flags = &flags,
});
lib.addCSourceFiles(.{
.files = &macos_sources,
.flags = &flags,
});
},
// everything that isn't windows or mac is linux :P
else => {
var sources = std.BoundedArray([]const u8, 64).init(0) catch unreachable;
var flags = std.BoundedArray([]const u8, 16).init(0) catch unreachable;
sources.appendSlice(&base_sources) catch unreachable;
sources.appendSlice(&linux_sources) catch unreachable;
if (use_x11) {
sources.appendSlice(&linux_x11_sources) catch unreachable;
flags.append("-D_GLFW_X11") catch unreachable;
}
if (use_wl) {
lib.defineCMacro("WL_MARSHAL_FLAG_DESTROY", "1");
sources.appendSlice(&linux_wl_sources) catch unreachable;
flags.append("-D_GLFW_WAYLAND") catch unreachable;
flags.append("-Wno-implicit-function-declaration") catch unreachable;
}
flags.append(include_src_flag) catch unreachable;
lib.addCSourceFiles(.{
.files = sources.slice(),
.flags = flags.slice(),
});
},
}
b.installArtifact(lib);
}
const base_sources = [_][]const u8{
"src/context.c",
"src/egl_context.c",
"src/init.c",
"src/input.c",
"src/monitor.c",
"src/null_init.c",
"src/null_joystick.c",
"src/null_monitor.c",
"src/null_window.c",
"src/osmesa_context.c",
"src/platform.c",
"src/vulkan.c",
"src/window.c",
};
const linux_sources = [_][]const u8{
"src/linux_joystick.c",
"src/posix_module.c",
"src/posix_poll.c",
"src/posix_thread.c",
"src/posix_time.c",
"src/xkb_unicode.c",
};
const linux_wl_sources = [_][]const u8{
"src/wl_init.c",
"src/wl_monitor.c",
"src/wl_window.c",
};
const linux_x11_sources = [_][]const u8{
"src/glx_context.c",
"src/x11_init.c",
"src/x11_monitor.c",
"src/x11_window.c",
};
const windows_sources = [_][]const u8{
"src/wgl_context.c",
"src/win32_init.c",
"src/win32_joystick.c",
"src/win32_module.c",
"src/win32_monitor.c",
"src/win32_thread.c",
"src/win32_time.c",
"src/win32_window.c",
};
const macos_sources = [_][]const u8{
// C sources
"src/cocoa_time.c",
"src/posix_module.c",
"src/posix_thread.c",
// ObjC sources
"src/cocoa_init.m",
"src/cocoa_joystick.m",
"src/cocoa_monitor.m",
"src/cocoa_window.m",
"src/nsgl_context.m",
};