@@ -2,7 +2,7 @@ const std = @import("std");
22const builtin = @import ("builtin" );
33
44/// Minimum supported version of Zig
5- const min_ver = "0.12 .0" ;
5+ const min_ver = "0.13 .0" ;
66
77comptime {
88 const order = std .SemanticVersion .order ;
@@ -11,36 +11,6 @@ comptime {
1111 @compileError ("Raylib requires zig version " ++ min_ver );
1212}
1313
14- // NOTE(freakmangd): I don't like using a global here, but it prevents having to
15- // get the flags a second time when adding raygui
16- var raylib_flags_arr : std .ArrayListUnmanaged ([]const u8 ) = .{};
17-
18- // This has been tested with zig version 0.12.0
19- pub fn addRaylib (b : * std.Build , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode , options : Options ) ! * std.Build.Step.Compile {
20- const raylib_dep = b .dependencyFromBuildZig (@This (), .{
21- .target = target ,
22- .optimize = optimize ,
23- .raudio = options .raudio ,
24- .rmodels = options .rmodels ,
25- .rshapes = options .rshapes ,
26- .rtext = options .rtext ,
27- .rtextures = options .rtextures ,
28- .platform = options .platform ,
29- .shared = options .shared ,
30- .linux_display_backend = options .linux_display_backend ,
31- .opengl_version = options .opengl_version ,
32- .config = options .config ,
33- });
34- const raylib = raylib_dep .artifact ("raylib" );
35-
36- if (options .raygui ) {
37- const raygui_dep = b .dependency (options .raygui_dependency_name , .{});
38- addRaygui (b , raylib , raygui_dep );
39- }
40-
41- return raylib ;
42- }
43-
4414fn setDesktopPlatform (raylib : * std.Build.Step.Compile , platform : PlatformBackend ) void {
4515 raylib .defineCMacro ("PLATFORM_DESKTOP" , null );
4616
@@ -52,6 +22,30 @@ fn setDesktopPlatform(raylib: *std.Build.Step.Compile, platform: PlatformBackend
5222 }
5323}
5424
25+ fn createEmsdkStep (b : * std.Build , emsdk : * std.Build.Dependency ) * std.Build.Step.Run {
26+ if (builtin .os .tag == .windows ) {
27+ return b .addSystemCommand (&.{emsdk .path ("emsdk.bat" ).getPath (b )});
28+ } else {
29+ return b .addSystemCommand (&.{emsdk .path ("emsdk" ).getPath (b )});
30+ }
31+ }
32+
33+ fn emSdkSetupStep (b : * std.Build , emsdk : * std.Build.Dependency ) ! ? * std.Build.Step.Run {
34+ const dot_emsc_path = emsdk .path (".emscripten" ).getPath (b );
35+ const dot_emsc_exists = ! std .meta .isError (std .fs .accessAbsolute (dot_emsc_path , .{}));
36+
37+ if (! dot_emsc_exists ) {
38+ const emsdk_install = createEmsdkStep (b , emsdk );
39+ emsdk_install .addArgs (&.{ "install" , "latest" });
40+ const emsdk_activate = createEmsdkStep (b , emsdk );
41+ emsdk_activate .addArgs (&.{ "activate" , "latest" });
42+ emsdk_activate .step .dependOn (& emsdk_install .step );
43+ return emsdk_activate ;
44+ } else {
45+ return null ;
46+ }
47+ }
48+
5549/// A list of all flags from `src/config.h` that one may override
5650const config_h_flags = outer : {
5751 // Set this value higher if compile errors happen as `src/config.h` gets larger
@@ -83,21 +77,26 @@ const config_h_flags = outer: {
8377};
8478
8579fn compileRaylib (b : * std.Build , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode , options : Options ) ! * std.Build.Step.Compile {
86- raylib_flags_arr .clearRetainingCapacity ();
80+ var raylib_flags_arr = std .ArrayList ([]const u8 ).init (b .allocator );
81+ defer raylib_flags_arr .deinit ();
8782
88- const shared_flags = &[_ ][]const u8 {
89- "-fPIC" ,
90- "-DBUILD_LIBTYPE_SHARED" ,
91- };
92- try raylib_flags_arr .appendSlice (b .allocator , &[_ ][]const u8 {
83+ try raylib_flags_arr .appendSlice (&[_ ][]const u8 {
9384 "-std=gnu99" ,
9485 "-D_GNU_SOURCE" ,
9586 "-DGL_SILENCE_DEPRECATION=199309L" ,
9687 "-fno-sanitize=undefined" , // https://github.com/raysan5/raylib/issues/3674
9788 });
89+
90+ if (options .shared ) {
91+ try raylib_flags_arr .appendSlice (&[_ ][]const u8 {
92+ "-fPIC" ,
93+ "-DBUILD_LIBTYPE_SHARED" ,
94+ });
95+ }
96+
9897 if (options .config .len > 0 ) {
9998 // Sets a flag indiciating the use of a custom `config.h`
100- try raylib_flags_arr .append (b . allocator , "-DEXTERNAL_CONFIG_FLAGS" );
99+ try raylib_flags_arr .append ("-DEXTERNAL_CONFIG_FLAGS" );
101100
102101 // Splits a space-separated list of config flags into multiple flags
103102 //
@@ -107,7 +106,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
107106
108107 // Apply config flags supplied by the user
109108 while (config_iter .next ()) | config_flag |
110- try raylib_flags_arr .append (b . allocator , config_flag );
109+ try raylib_flags_arr .append (config_flag );
111110
112111 // Apply all relevant configs from `src/config.h` *except* the user-specified ones
113112 //
@@ -125,14 +124,10 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
125124 }
126125
127126 // Otherwise, append default value from config.h to compile flags
128- try raylib_flags_arr .append (b . allocator , flag );
127+ try raylib_flags_arr .append (flag );
129128 }
130129 }
131130
132- if (options .shared ) {
133- try raylib_flags_arr .appendSlice (b .allocator , shared_flags );
134- }
135-
136131 const raylib = if (options .shared )
137132 b .addSharedLibrary (.{
138133 .name = "raylib" ,
@@ -223,6 +218,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
223218 waylandGenerate (b , raylib , "xdg-activation-v1.xml" , "xdg-activation-v1-client-protocol" );
224219 waylandGenerate (b , raylib , "idle-inhibit-unstable-v1.xml" , "idle-inhibit-unstable-v1-client-protocol" );
225220 }
221+
226222 setDesktopPlatform (raylib , options .platform );
227223 } else {
228224 if (options .opengl_version == .auto ) {
@@ -255,8 +251,15 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
255251 setDesktopPlatform (raylib , options .platform );
256252 },
257253 .macos = > {
254+ // Include xcode_frameworks for cross compilation
255+ if (b .lazyDependency ("xcode_frameworks" , .{})) | dep | {
256+ raylib .addSystemFrameworkPath (dep .path ("Frameworks" ));
257+ raylib .addSystemIncludePath (dep .path ("include" ));
258+ raylib .addLibraryPath (dep .path ("lib" ));
259+ }
260+
258261 // On macos rglfw.c include Objective-C files.
259- try raylib_flags_arr .append (b . allocator , "-ObjC" );
262+ try raylib_flags_arr .append ("-ObjC" );
260263 raylib .root_module .addCSourceFile (.{
261264 .file = b .path ("src/rglfw.c" ),
262265 .flags = raylib_flags_arr .items ,
@@ -271,20 +274,19 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
271274 setDesktopPlatform (raylib , options .platform );
272275 },
273276 .emscripten = > {
277+ // Include emscripten for cross compilation
278+ if (b .lazyDependency ("emsdk" , .{})) | dep | {
279+ if (try emSdkSetupStep (b , dep )) | emSdkStep | {
280+ raylib .step .dependOn (& emSdkStep .step );
281+ }
282+
283+ raylib .addIncludePath (dep .path ("upstream/emscripten/cache/sysroot/include" ));
284+ }
285+
274286 raylib .defineCMacro ("PLATFORM_WEB" , null );
275287 if (options .opengl_version == .auto ) {
276288 raylib .defineCMacro ("GRAPHICS_API_OPENGL_ES2" , null );
277289 }
278-
279- if (b .sysroot == null ) {
280- @panic ("Pass '--sysroot \" $EMSDK/upstream/emscripten\" '" );
281- }
282-
283- const cache_include = b .pathJoin (&.{ b .sysroot .? , "cache" , "sysroot" , "include" });
284-
285- var dir = std .fs .openDirAbsolute (cache_include , std.fs.Dir.OpenDirOptions { .access_sub_paths = true , .no_follow = true }) catch @panic ("No emscripten cache. Generate it!" );
286- dir .close ();
287- raylib .addIncludePath (.{ .cwd_relative = cache_include });
288290 },
289291 else = > {
290292 @panic ("Unsupported OS" );
@@ -296,25 +298,19 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
296298 .flags = raylib_flags_arr .items ,
297299 });
298300
299- return raylib ;
300- }
301-
302- /// This function does not need to be called if you passed .raygui = true to addRaylib
303- pub fn addRaygui (b : * std.Build , raylib : * std.Build.Step.Compile , raygui_dep : * std.Build.Dependency ) void {
304- if (raylib_flags_arr .items .len == 0 ) {
305- @panic (
306- \\argument 2 `raylib` in `addRaygui` must come from b.dependency("raylib", ...).artifact("raylib")
307- );
308- }
301+ if (options .raygui ) {
302+ const raygui_dep = b .dependency (options .raygui_dependency_name , .{});
309303
310- var gen_step = b .addWriteFiles ();
311- raylib .step .dependOn (& gen_step .step );
304+ var gen_step = b .addWriteFiles ();
305+ raylib .step .dependOn (& gen_step .step );
312306
313- const raygui_c_path = gen_step .add ("raygui.c" , "#define RAYGUI_IMPLEMENTATION\n #include \" raygui.h\" \n " );
314- raylib .addCSourceFile (.{ .file = raygui_c_path , .flags = raylib_flags_arr .items });
315- raylib .addIncludePath (raygui_dep .path ("src" ));
307+ const raygui_c_path = gen_step .add ("raygui.c" , "#define RAYGUI_IMPLEMENTATION\n #include \" raygui.h\" \n " );
308+ raylib .addCSourceFile (.{ .file = raygui_c_path , .flags = raylib_flags_arr .items });
309+ raylib .addIncludePath (raygui_dep .path ("src" ));
310+ raylib .installHeader (raygui_dep .path ("src/raygui.h" ), "raygui.h" );
311+ }
316312
317- raylib . installHeader ( raygui_dep . path ( "src/raygui.h" ), "raygui.h" ) ;
313+ return raylib ;
318314}
319315
320316pub const Options = struct {
0 commit comments