From 71f2e2b1e4f86b769f3ba62fcc0217d06a5dee11 Mon Sep 17 00:00:00 2001 From: Anders Holmberg Date: Sat, 27 Nov 2021 10:46:18 +0100 Subject: [PATCH] Add macos support to zig build system (#2175) --- examples/build.zig | 17 ++++++++++++++++- src/build.zig | 14 +++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/examples/build.zig b/examples/build.zig index c05808bf..5c3a69d2 100644 --- a/examples/build.zig +++ b/examples/build.zig @@ -21,13 +21,20 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi // zig's mingw headers do not include pthread.h if (std.mem.eql(u8, "core_loading_thread", name) and target.getOsTag() == .windows) continue; - const exe = b.addExecutable(name, path); + const exe = b.addExecutable(name, null); + exe.addCSourceFile(path, switch (target.getOsTag()) { + .windows => &[_][]const u8{}, + .linux => &[_][]const u8{}, + .macos => &[_][]const u8{"-DPLATFORM_DESKTOP"}, + else => @panic("Unsupported OS"), + }); exe.setTarget(target); exe.setBuildMode(mode); exe.linkLibC(); exe.addObjectFile(switch (target.getOsTag()) { .windows => "../src/raylib.lib", .linux => "../src/libraylib.a", + .macos => "../src/libraylib.a", else => @panic("Unsupported OS"), }); @@ -49,6 +56,14 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi exe.linkSystemLibrary("m"); exe.linkSystemLibrary("X11"); }, + .macos => { + exe.linkFramework("Foundation"); + exe.linkFramework("Cocoa"); + exe.linkFramework("OpenGL"); + exe.linkFramework("CoreAudio"); + exe.linkFramework("CoreVideo"); + exe.linkFramework("IOKit"); + }, else => { @panic("Unsupported OS"); }, diff --git a/src/build.zig b/src/build.zig index 6bdae0f9..76d4a661 100644 --- a/src/build.zig +++ b/src/build.zig @@ -28,7 +28,6 @@ pub fn build(b: *std.build.Builder) void { raylib.addCSourceFiles(&.{ "raudio.c", "rcore.c", - "rglfw.c", "rmodels.c", "rshapes.c", "rtext.c", @@ -38,18 +37,31 @@ pub fn build(b: *std.build.Builder) void { switch (raylib.target.toTarget().os.tag) { .windows => { + raylib.addCSourceFiles(&.{"rglfw.c"}, raylib_flags); raylib.linkSystemLibrary("winmm"); raylib.linkSystemLibrary("gdi32"); raylib.linkSystemLibrary("opengl32"); raylib.addIncludeDir("external/glfw/deps/mingw"); }, .linux => { + raylib.addCSourceFiles(&.{"rglfw.c"}, raylib_flags); raylib.linkSystemLibrary("GL"); raylib.linkSystemLibrary("rt"); raylib.linkSystemLibrary("dl"); raylib.linkSystemLibrary("m"); raylib.linkSystemLibrary("X11"); }, + .macos => { + // On macos rglfw.c include Objective-C files. + const raylib_flags_extra_macos = &[_][]const u8{ + "-ObjC", + }; + raylib.addCSourceFiles( + &.{"rglfw.c"}, + raylib_flags ++ raylib_flags_extra_macos, + ); + raylib.linkFramework("Foundation"); + }, else => { @panic("Unsupported OS"); },