Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
strategy:
fail-fast: false
matrix:
zig-version: [0.14.0, latest]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -29,8 +28,6 @@ jobs:

- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: ${{ matrix.zig-version }}

- name: Check Formatting
run: zig fmt --ast-check --check .
Expand Down
155 changes: 80 additions & 75 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,86 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const t = target.result;

const mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
const lib = b.addLibrary(.{
.name = "SDL2",
.version = .{ .major = 2, .minor = 32, .patch = 10 },
.linkage = if (t.abi.isAndroid()) .dynamic else .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
.root_module = mod,
});

const sdl_include_path = b.path("include");
lib.addCSourceFiles(.{ .files = &generic_src_files });
lib.root_module.addCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1");
lib.root_module.addCMacro("HAVE_GCC_ATOMICS", "1");
lib.root_module.addCMacro("HAVE_GCC_SYNC_LOCK_TEST_AND_SET", "1");
mod.addCSourceFiles(.{ .files = &generic_src_files });
mod.addCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1");
mod.addCMacro("HAVE_GCC_ATOMICS", "1");
mod.addCMacro("HAVE_GCC_SYNC_LOCK_TEST_AND_SET", "1");

switch (t.os.tag) {
.windows => {
lib.root_module.addCMacro("SDL_STATIC_LIB", "");
lib.addCSourceFiles(.{ .files = &windows_src_files });
lib.linkSystemLibrary("user32");
lib.linkSystemLibrary("shell32");
lib.linkSystemLibrary("advapi32");
lib.linkSystemLibrary("setupapi");
lib.linkSystemLibrary("winmm");
lib.linkSystemLibrary("gdi32");
lib.linkSystemLibrary("imm32");
lib.linkSystemLibrary("version");
lib.linkSystemLibrary("oleaut32");
lib.linkSystemLibrary("ole32");
mod.addCMacro("SDL_STATIC_LIB", "");
mod.addCSourceFiles(.{ .files = &windows_src_files });
mod.linkSystemLibrary("user32", .{});
mod.linkSystemLibrary("shell32", .{});
mod.linkSystemLibrary("advapi32", .{});
mod.linkSystemLibrary("setupapi", .{});
mod.linkSystemLibrary("winmm", .{});
mod.linkSystemLibrary("gdi32", .{});
mod.linkSystemLibrary("imm32", .{});
mod.linkSystemLibrary("version", .{});
mod.linkSystemLibrary("oleaut32", .{});
mod.linkSystemLibrary("ole32", .{});
},
.macos => {
lib.addCSourceFiles(.{ .files = &darwin_src_files });
lib.addCSourceFiles(.{
mod.addCSourceFiles(.{ .files = &darwin_src_files });
mod.addCSourceFiles(.{
.files = &objective_c_src_files,
.flags = &.{"-fobjc-arc"},
});
lib.linkFramework("OpenGL");
lib.linkFramework("Metal");
lib.linkFramework("CoreVideo");
lib.linkFramework("Cocoa");
lib.linkFramework("IOKit");
lib.linkFramework("ForceFeedback");
lib.linkFramework("Carbon");
lib.linkFramework("CoreAudio");
lib.linkFramework("AudioToolbox");
lib.linkFramework("AVFoundation");
lib.linkFramework("Foundation");
mod.linkFramework("OpenGL", .{});
mod.linkFramework("Metal", .{});
mod.linkFramework("CoreVideo", .{});
mod.linkFramework("Cocoa", .{});
mod.linkFramework("IOKit", .{});
mod.linkFramework("ForceFeedback", .{});
mod.linkFramework("Carbon", .{});
mod.linkFramework("CoreAudio", .{});
mod.linkFramework("AudioToolbox", .{});
mod.linkFramework("AVFoundation", .{});
mod.linkFramework("Foundation", .{});
},
.emscripten => {
lib.root_module.addCMacro("__EMSCRIPTEN_PTHREADS__ ", "1");
lib.root_module.addCMacro("USE_SDL", "2");
lib.addCSourceFiles(.{ .files = &emscripten_src_files });
mod.addCMacro("__EMSCRIPTEN_PTHREADS__ ", "1");
mod.addCMacro("USE_SDL", "2");
mod.addCSourceFiles(.{ .files = &emscripten_src_files });
if (b.sysroot == null) {
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'");
}

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

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

lib.addIncludePath(.{ .cwd_relative = cache_include });
mod.addIncludePath(.{ .cwd_relative = cache_include });
},
else => {
if (t.abi.isAndroid()) {
lib.root_module.addCSourceFiles(.{
mod.addCSourceFiles(.{
.files = &android_src_files,
});

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

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

// https://github.com/libsdl-org/SDL/blob/release-2.30.6/Android.mk#L82C62-L82C69
lib.linkSystemLibrary("dl");
lib.linkSystemLibrary("GLESv1_CM");
lib.linkSystemLibrary("GLESv2");
lib.linkSystemLibrary("OpenSLES");
lib.linkSystemLibrary("log");
lib.linkSystemLibrary("android");
mod.linkSystemLibrary("dl", .{});
mod.linkSystemLibrary("GLESv1_CM", .{});
mod.linkSystemLibrary("GLESv2", .{});
mod.linkSystemLibrary("OpenSLES", .{});
mod.linkSystemLibrary("log", .{});
mod.linkSystemLibrary("android", .{});
}
},
}

lib.addIncludePath(sdl_include_path);
mod.addIncludePath(sdl_include_path);

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

if (use_pregenerated_config) {
lib.addIncludePath(b.path("include-pregen"));
mod.addIncludePath(b.path("include-pregen"));
lib.installHeadersDirectory(b.path("include-pregen"), "SDL2", .{});
lib.addCSourceFiles(.{ .files = render_driver_sw.src_files });
mod.addCSourceFiles(.{ .files = render_driver_sw.src_files });
} else {
// causes pregenerated SDL_config.h to assert an error
lib.root_module.addCMacro("USING_GENERATED_CONFIG_H", "");
mod.addCMacro("USING_GENERATED_CONFIG_H", "");

const config_header = configHeader(b, t);
switch (t.os.tag) {
.linux => {
lib.addCSourceFiles(.{ .files = &linux_src_files });
mod.addCSourceFiles(.{ .files = &linux_src_files });
config_header.addValues(.{
.SDL_VIDEO_OPENGL = 1,
.SDL_VIDEO_OPENGL_ES = 1,
Expand All @@ -141,50 +147,49 @@ pub fn build(b: *std.Build) void {
.SDL_VIDEO_OPENGL_OSMESA = 1,
.SDL_VIDEO_OPENGL_OSMESA_DYNAMIC = 1,
});
applyOptions(b, lib, config_header, &linux_options);
applyOptions(b, mod, config_header, &linux_options);
},
else => {},
}
lib.addConfigHeader(config_header);
lib.installHeader(config_header.getOutput(), "SDL2/SDL_config.h");
mod.addConfigHeader(config_header);
lib.installHeader(config_header.getOutputFile(), "SDL2/SDL_config.h");

// TODO: Remove compatibility shim when Zig 0.15.0 is the minimum required version.
const fmt_shim = if (@hasDecl(std, "Io")) "{f}" else "{}";
const revision_header = b.addConfigHeader(.{
.style = .{ .cmake = b.path("include/SDL_revision.h.cmake") },
.include_path = "SDL_revision.h",
}, .{
.SDL_REVISION = b.fmt("SDL-" ++ fmt_shim, .{lib.version.?}),
.SDL_REVISION = b.fmt("SDL-{f}", .{lib.version.?}),
.SDL_VENDOR_INFO = "allyourcodebase.com",
});
lib.addConfigHeader(revision_header);
lib.installHeader(revision_header.getOutput(), "SDL2/SDL_revision.h");
mod.addConfigHeader(revision_header);
lib.installHeader(revision_header.getOutputFile(), "SDL2/SDL_revision.h");
}

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

if (use_hidapi) {
const hidapi_mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
});
const hidapi_lib = b.addLibrary(.{
.name = "hidapi",
.linkage = .dynamic,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
}),
.root_module = hidapi_mod,
});
hidapi_lib.addIncludePath(sdl_include_path);
hidapi_lib.addIncludePath(b.path("include-pregen"));
hidapi_lib.root_module.addCSourceFiles(.{
hidapi_mod.addIncludePath(sdl_include_path);
hidapi_mod.addIncludePath(b.path("include-pregen"));
hidapi_mod.addCSourceFiles(.{
.root = b.path(""),
.files = &[_][]const u8{
"src/hidapi/android/hid.cpp",
},
.flags = &.{"-std=c++11"},
});
hidapi_lib.linkSystemLibrary("log");
lib.linkLibrary(hidapi_lib);
hidapi_mod.linkSystemLibrary("log", .{});
mod.linkLibrary(hidapi_lib);
b.installArtifact(hidapi_lib);
}

Expand Down Expand Up @@ -992,22 +997,22 @@ const linux_options = [_]SdlOption{

fn applyOptions(
b: *std.Build,
lib: *std.Build.Step.Compile,
mod: *std.Build.Module,
config_header: *std.Build.Step.ConfigHeader,
comptime options: []const SdlOption,
) void {
inline for (options) |option| {
const enabled = if (b.option(bool, option.name, option.desc)) |o| o else option.default;
for (option.c_macros) |name| {
lib.root_module.addCMacro(name, if (enabled) "1" else "0");
mod.addCMacro(name, if (enabled) "1" else "0");
}
for (option.sdl_configs) |config| {
config_header.values.put(config, .{ .int = if (enabled) 1 else 0 }) catch @panic("OOM");
}
if (enabled) {
lib.addCSourceFiles(.{ .files = option.src_files });
mod.addCSourceFiles(.{ .files = option.src_files });
for (option.system_libs) |lib_name| {
lib.linkSystemLibrary(lib_name);
mod.linkSystemLibrary(lib_name, .{});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.name = .SDL,
.version = "2.32.10",
.fingerprint = 0x7ac4ce41df223a25,
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.0",
.dependencies = .{},
.paths = .{
"LICENSE.txt",
Expand Down