Skip to content

Commit 25dc764

Browse files
committed
Fix build for Zig 0.16.0-dev.1657+985a3565c, drop 0.14.x support
1 parent 4bd594b commit 25dc764

3 files changed

Lines changed: 81 additions & 79 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
zig-version: [0.14.0, latest]
1817
os: [ubuntu-latest, macos-latest, windows-latest]
1918
runs-on: ${{ matrix.os }}
2019
steps:
@@ -29,8 +28,6 @@ jobs:
2928
3029
- name: Setup Zig
3130
uses: mlugg/setup-zig@v2
32-
with:
33-
version: ${{ matrix.zig-version }}
3431

3532
- name: Check Formatting
3633
run: zig fmt --ast-check --check .

build.zig

Lines changed: 80 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,86 @@ pub fn build(b: *std.Build) void {
55
const optimize = b.standardOptimizeOption(.{});
66
const t = target.result;
77

8+
const mod = b.createModule(.{
9+
.target = target,
10+
.optimize = optimize,
11+
.link_libc = true,
12+
});
813
const lib = b.addLibrary(.{
914
.name = "SDL2",
1015
.version = .{ .major = 2, .minor = 32, .patch = 10 },
1116
.linkage = if (t.abi.isAndroid()) .dynamic else .static,
12-
.root_module = b.createModule(.{
13-
.target = target,
14-
.optimize = optimize,
15-
.link_libc = true,
16-
}),
17+
.root_module = mod,
1718
});
1819

1920
const sdl_include_path = b.path("include");
20-
lib.addCSourceFiles(.{ .files = &generic_src_files });
21-
lib.root_module.addCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1");
22-
lib.root_module.addCMacro("HAVE_GCC_ATOMICS", "1");
23-
lib.root_module.addCMacro("HAVE_GCC_SYNC_LOCK_TEST_AND_SET", "1");
21+
mod.addCSourceFiles(.{ .files = &generic_src_files });
22+
mod.addCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1");
23+
mod.addCMacro("HAVE_GCC_ATOMICS", "1");
24+
mod.addCMacro("HAVE_GCC_SYNC_LOCK_TEST_AND_SET", "1");
2425

2526
switch (t.os.tag) {
2627
.windows => {
27-
lib.root_module.addCMacro("SDL_STATIC_LIB", "");
28-
lib.addCSourceFiles(.{ .files = &windows_src_files });
29-
lib.linkSystemLibrary("user32");
30-
lib.linkSystemLibrary("shell32");
31-
lib.linkSystemLibrary("advapi32");
32-
lib.linkSystemLibrary("setupapi");
33-
lib.linkSystemLibrary("winmm");
34-
lib.linkSystemLibrary("gdi32");
35-
lib.linkSystemLibrary("imm32");
36-
lib.linkSystemLibrary("version");
37-
lib.linkSystemLibrary("oleaut32");
38-
lib.linkSystemLibrary("ole32");
28+
mod.addCMacro("SDL_STATIC_LIB", "");
29+
mod.addCSourceFiles(.{ .files = &windows_src_files });
30+
mod.linkSystemLibrary("user32", .{});
31+
mod.linkSystemLibrary("shell32", .{});
32+
mod.linkSystemLibrary("advapi32", .{});
33+
mod.linkSystemLibrary("setupapi", .{});
34+
mod.linkSystemLibrary("winmm", .{});
35+
mod.linkSystemLibrary("gdi32", .{});
36+
mod.linkSystemLibrary("imm32", .{});
37+
mod.linkSystemLibrary("version", .{});
38+
mod.linkSystemLibrary("oleaut32", .{});
39+
mod.linkSystemLibrary("ole32", .{});
3940
},
4041
.macos => {
41-
lib.addCSourceFiles(.{ .files = &darwin_src_files });
42-
lib.addCSourceFiles(.{
42+
mod.addCSourceFiles(.{ .files = &darwin_src_files });
43+
mod.addCSourceFiles(.{
4344
.files = &objective_c_src_files,
4445
.flags = &.{"-fobjc-arc"},
4546
});
46-
lib.linkFramework("OpenGL");
47-
lib.linkFramework("Metal");
48-
lib.linkFramework("CoreVideo");
49-
lib.linkFramework("Cocoa");
50-
lib.linkFramework("IOKit");
51-
lib.linkFramework("ForceFeedback");
52-
lib.linkFramework("Carbon");
53-
lib.linkFramework("CoreAudio");
54-
lib.linkFramework("AudioToolbox");
55-
lib.linkFramework("AVFoundation");
56-
lib.linkFramework("Foundation");
47+
mod.linkFramework("OpenGL", .{});
48+
mod.linkFramework("Metal", .{});
49+
mod.linkFramework("CoreVideo", .{});
50+
mod.linkFramework("Cocoa", .{});
51+
mod.linkFramework("IOKit", .{});
52+
mod.linkFramework("ForceFeedback", .{});
53+
mod.linkFramework("Carbon", .{});
54+
mod.linkFramework("CoreAudio", .{});
55+
mod.linkFramework("AudioToolbox", .{});
56+
mod.linkFramework("AVFoundation", .{});
57+
mod.linkFramework("Foundation", .{});
5758
},
5859
.emscripten => {
59-
lib.root_module.addCMacro("__EMSCRIPTEN_PTHREADS__ ", "1");
60-
lib.root_module.addCMacro("USE_SDL", "2");
61-
lib.addCSourceFiles(.{ .files = &emscripten_src_files });
60+
mod.addCMacro("__EMSCRIPTEN_PTHREADS__ ", "1");
61+
mod.addCMacro("USE_SDL", "2");
62+
mod.addCSourceFiles(.{ .files = &emscripten_src_files });
6263
if (b.sysroot == null) {
6364
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'");
6465
}
6566

6667
const cache_include = std.fs.path.join(b.allocator, &.{ b.sysroot.?, "cache", "sysroot", "include" }) catch @panic("Out of memory");
6768
defer b.allocator.free(cache_include);
6869

69-
var dir = std.fs.openDirAbsolute(cache_include, .{ .access_sub_paths = true, .no_follow = true }) catch @panic("No emscripten cache. Generate it!");
70+
// TODO: Remove compatibility shim when Zig 0.16.0 is the minimum required version.
71+
const open_dir_opts: std.fs.Dir.OpenOptions = if (@hasField(std.fs.Dir.OpenOptions, "follow_symlinks"))
72+
.{ .access_sub_paths = true, .follow_symlinks = false }
73+
else
74+
.{ .access_sub_paths = true, .no_follow = true };
75+
var dir = std.fs.openDirAbsolute(cache_include, open_dir_opts) catch @panic("No emscripten cache. Generate it!");
7076
dir.close();
7177

72-
lib.addIncludePath(.{ .cwd_relative = cache_include });
78+
mod.addIncludePath(.{ .cwd_relative = cache_include });
7379
},
7480
else => {
7581
if (t.abi.isAndroid()) {
76-
lib.root_module.addCSourceFiles(.{
82+
mod.addCSourceFiles(.{
7783
.files = &android_src_files,
7884
});
7985

8086
// This is needed for "src/render/opengles/SDL_render_gles.c" to compile
81-
lib.root_module.addCMacro("GL_GLEXT_PROTOTYPES", "1");
87+
mod.addCMacro("GL_GLEXT_PROTOTYPES", "1");
8288

8389
// Add Java files to dependency so that they can be copied downstream
8490
const java_dir = b.path("android-project/app/src/main/java/org/libsdl/app");
@@ -99,17 +105,17 @@ pub fn build(b: *std.Build) void {
99105
}
100106

101107
// https://github.com/libsdl-org/SDL/blob/release-2.30.6/Android.mk#L82C62-L82C69
102-
lib.linkSystemLibrary("dl");
103-
lib.linkSystemLibrary("GLESv1_CM");
104-
lib.linkSystemLibrary("GLESv2");
105-
lib.linkSystemLibrary("OpenSLES");
106-
lib.linkSystemLibrary("log");
107-
lib.linkSystemLibrary("android");
108+
mod.linkSystemLibrary("dl", .{});
109+
mod.linkSystemLibrary("GLESv1_CM", .{});
110+
mod.linkSystemLibrary("GLESv2", .{});
111+
mod.linkSystemLibrary("OpenSLES", .{});
112+
mod.linkSystemLibrary("log", .{});
113+
mod.linkSystemLibrary("android", .{});
108114
}
109115
},
110116
}
111117

112-
lib.addIncludePath(sdl_include_path);
118+
mod.addIncludePath(sdl_include_path);
113119

114120
const use_pregenerated_config = switch (t.os.tag) {
115121
.windows, .macos, .emscripten => true,
@@ -118,17 +124,17 @@ pub fn build(b: *std.Build) void {
118124
};
119125

120126
if (use_pregenerated_config) {
121-
lib.addIncludePath(b.path("include-pregen"));
127+
mod.addIncludePath(b.path("include-pregen"));
122128
lib.installHeadersDirectory(b.path("include-pregen"), "SDL2", .{});
123-
lib.addCSourceFiles(.{ .files = render_driver_sw.src_files });
129+
mod.addCSourceFiles(.{ .files = render_driver_sw.src_files });
124130
} else {
125131
// causes pregenerated SDL_config.h to assert an error
126-
lib.root_module.addCMacro("USING_GENERATED_CONFIG_H", "");
132+
mod.addCMacro("USING_GENERATED_CONFIG_H", "");
127133

128134
const config_header = configHeader(b, t);
129135
switch (t.os.tag) {
130136
.linux => {
131-
lib.addCSourceFiles(.{ .files = &linux_src_files });
137+
mod.addCSourceFiles(.{ .files = &linux_src_files });
132138
config_header.addValues(.{
133139
.SDL_VIDEO_OPENGL = 1,
134140
.SDL_VIDEO_OPENGL_ES = 1,
@@ -141,50 +147,49 @@ pub fn build(b: *std.Build) void {
141147
.SDL_VIDEO_OPENGL_OSMESA = 1,
142148
.SDL_VIDEO_OPENGL_OSMESA_DYNAMIC = 1,
143149
});
144-
applyOptions(b, lib, config_header, &linux_options);
150+
applyOptions(b, mod, config_header, &linux_options);
145151
},
146152
else => {},
147153
}
148-
lib.addConfigHeader(config_header);
149-
lib.installHeader(config_header.getOutput(), "SDL2/SDL_config.h");
154+
mod.addConfigHeader(config_header);
155+
lib.installHeader(config_header.getOutputFile(), "SDL2/SDL_config.h");
150156

151-
// TODO: Remove compatibility shim when Zig 0.15.0 is the minimum required version.
152-
const fmt_shim = if (@hasDecl(std, "Io")) "{f}" else "{}";
153157
const revision_header = b.addConfigHeader(.{
154158
.style = .{ .cmake = b.path("include/SDL_revision.h.cmake") },
155159
.include_path = "SDL_revision.h",
156160
}, .{
157-
.SDL_REVISION = b.fmt("SDL-" ++ fmt_shim, .{lib.version.?}),
161+
.SDL_REVISION = b.fmt("SDL-{f}", .{lib.version.?}),
158162
.SDL_VENDOR_INFO = "allyourcodebase.com",
159163
});
160-
lib.addConfigHeader(revision_header);
161-
lib.installHeader(revision_header.getOutput(), "SDL2/SDL_revision.h");
164+
mod.addConfigHeader(revision_header);
165+
lib.installHeader(revision_header.getOutputFile(), "SDL2/SDL_revision.h");
162166
}
163167

164168
const use_hidapi = b.option(bool, "use_hidapi", "Use hidapi shared library") orelse t.abi.isAndroid();
165169

166170
if (use_hidapi) {
171+
const hidapi_mod = b.createModule(.{
172+
.target = target,
173+
.optimize = optimize,
174+
.link_libc = true,
175+
.link_libcpp = true,
176+
});
167177
const hidapi_lib = b.addLibrary(.{
168178
.name = "hidapi",
169179
.linkage = .dynamic,
170-
.root_module = b.createModule(.{
171-
.target = target,
172-
.optimize = optimize,
173-
.link_libc = true,
174-
.link_libcpp = true,
175-
}),
180+
.root_module = hidapi_mod,
176181
});
177-
hidapi_lib.addIncludePath(sdl_include_path);
178-
hidapi_lib.addIncludePath(b.path("include-pregen"));
179-
hidapi_lib.root_module.addCSourceFiles(.{
182+
hidapi_mod.addIncludePath(sdl_include_path);
183+
hidapi_mod.addIncludePath(b.path("include-pregen"));
184+
hidapi_mod.addCSourceFiles(.{
180185
.root = b.path(""),
181186
.files = &[_][]const u8{
182187
"src/hidapi/android/hid.cpp",
183188
},
184189
.flags = &.{"-std=c++11"},
185190
});
186-
hidapi_lib.linkSystemLibrary("log");
187-
lib.linkLibrary(hidapi_lib);
191+
hidapi_mod.linkSystemLibrary("log", .{});
192+
mod.linkLibrary(hidapi_lib);
188193
b.installArtifact(hidapi_lib);
189194
}
190195

@@ -992,22 +997,22 @@ const linux_options = [_]SdlOption{
992997

993998
fn applyOptions(
994999
b: *std.Build,
995-
lib: *std.Build.Step.Compile,
1000+
mod: *std.Build.Module,
9961001
config_header: *std.Build.Step.ConfigHeader,
9971002
comptime options: []const SdlOption,
9981003
) void {
9991004
inline for (options) |option| {
10001005
const enabled = if (b.option(bool, option.name, option.desc)) |o| o else option.default;
10011006
for (option.c_macros) |name| {
1002-
lib.root_module.addCMacro(name, if (enabled) "1" else "0");
1007+
mod.addCMacro(name, if (enabled) "1" else "0");
10031008
}
10041009
for (option.sdl_configs) |config| {
10051010
config_header.values.put(config, .{ .int = if (enabled) 1 else 0 }) catch @panic("OOM");
10061011
}
10071012
if (enabled) {
1008-
lib.addCSourceFiles(.{ .files = option.src_files });
1013+
mod.addCSourceFiles(.{ .files = option.src_files });
10091014
for (option.system_libs) |lib_name| {
1010-
lib.linkSystemLibrary(lib_name);
1015+
mod.linkSystemLibrary(lib_name, .{});
10111016
}
10121017
}
10131018
}

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = .SDL,
33
.version = "2.32.10",
44
.fingerprint = 0x7ac4ce41df223a25,
5-
.minimum_zig_version = "0.14.0",
5+
.minimum_zig_version = "0.15.0",
66
.dependencies = .{},
77
.paths = .{
88
"LICENSE.txt",

0 commit comments

Comments
 (0)