diff --git a/build.zig b/build.zig index ab98bbe98..a40ac99e2 100644 --- a/build.zig +++ b/build.zig @@ -4,6 +4,148 @@ const builtin = @import("builtin"); /// Minimum supported version of Zig const min_ver = "0.15.1"; +const is_zig_0_16 = builtin.zig_version.minor >= 16; + +fn getAndroidNdk(b: *std.Build) []const u8 { + if (comptime is_zig_0_16) { + return b.graph.environ_map.get("ANDROID_NDK_HOME") orelse ""; + } else { + if (@hasDecl(std.process, "getEnvVarOwned")) { + return std.process.getEnvVarOwned(b.allocator, "ANDROID_NDK_HOME") catch ""; + } + return ""; + } +} + +fn processExample( + b: *std.Build, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + raylib: *std.Build.Step.Compile, + module_subpath: []const u8, + name: []const u8, + all_step: *std.Build.Step, +) !void { + const filename = try std.fmt.allocPrint(b.allocator, "{s}.c", .{name}); + const path = b.pathJoin(&.{ module_subpath, filename }); + + // zig's mingw headers do not include pthread.h + if (std.mem.eql(u8, "core_loading_thread", name) and target.result.os.tag == .windows) return; + + const exe_mod = b.createModule(.{ + .target = target, + .optimize = optimize, + }); + exe_mod.addCSourceFile(.{ .file = b.path(path), .flags = &.{} }); + exe_mod.linkLibrary(raylib); + + const run_step = b.step(name, name); + + if (target.result.os.tag == .emscripten) { + const wasm = b.addLibrary(.{ + .name = name, + .linkage = .static, + .root_module = exe_mod, + }); + + if (std.mem.eql(u8, name, "rlgl_standalone")) { + exe_mod.addIncludePath(b.path("src")); + exe_mod.addIncludePath(b.path("src/external/glfw/include")); + } + if (std.mem.eql(u8, name, "raylib_opengl_interop")) { + exe_mod.addIncludePath(b.path("src/external")); + } + + const emcc_flags = emsdk.emccDefaultFlags(b.allocator, .{ .optimize = optimize }); + const emcc_settings = emsdk.emccDefaultSettings(b.allocator, .{ .optimize = optimize }); + + const install_dir: std.Build.InstallDir = .{ .custom = "htmlout" }; + const emcc_step = emsdk.emccStep(b, raylib, wasm, .{ + .optimize = optimize, + .flags = emcc_flags, + .settings = emcc_settings, + .shell_file_path = b.path("src/shell.html"), + .embed_paths = &.{ + .{ + .src_path = b.pathJoin(&.{ module_subpath, "resources" }), + .virtual_path = "resources", + }, + }, + .install_dir = install_dir, + }); + + const html_filename = try std.fmt.allocPrint(b.allocator, "{s}.html", .{wasm.name}); + const emrun_step = emsdk.emrunStep( + b, + b.getInstallPath(install_dir, html_filename), + &.{"--no_browser"}, + ); + emrun_step.dependOn(emcc_step); + + run_step.dependOn(emrun_step); + all_step.dependOn(emcc_step); + } else { + // special examples that test using these external dependencies directly + // alongside raylib + if (std.mem.eql(u8, name, "rlgl_standalone")) { + exe_mod.addIncludePath(b.path("src")); + exe_mod.addIncludePath(b.path("src/external/glfw/include")); + if (!hasCSource(raylib.root_module, "rglfw.c")) { + exe_mod.addCSourceFile(.{ .file = b.path("src/rglfw.c"), .flags = &.{} }); + } + } + if (std.mem.eql(u8, name, "raylib_opengl_interop")) { + exe_mod.addIncludePath(b.path("src/external")); + } + + switch (target.result.os.tag) { + .windows => { + exe_mod.linkSystemLibrary("winmm", .{}); + exe_mod.linkSystemLibrary("gdi32", .{}); + exe_mod.linkSystemLibrary("opengl32", .{}); + + exe_mod.addCMacro("PLATFORM_DESKTOP", ""); + }, + .linux => { + exe_mod.linkSystemLibrary("GL", .{}); + exe_mod.linkSystemLibrary("rt", .{}); + exe_mod.linkSystemLibrary("dl", .{}); + exe_mod.linkSystemLibrary("m", .{}); + exe_mod.linkSystemLibrary("X11", .{}); + + exe_mod.addCMacro("PLATFORM_DESKTOP", ""); + }, + .macos => { + exe_mod.linkFramework("Foundation", .{}); + exe_mod.linkFramework("Cocoa", .{}); + exe_mod.linkFramework("OpenGL", .{}); + exe_mod.linkFramework("CoreAudio", .{}); + exe_mod.linkFramework("CoreVideo", .{}); + exe_mod.linkFramework("IOKit", .{}); + + exe_mod.addCMacro("PLATFORM_DESKTOP", ""); + }, + else => { + @panic("Unsupported OS"); + }, + } + + const exe = b.addExecutable(.{ + .name = name, + .root_module = exe_mod, + }); + + const install_cmd = b.addInstallArtifact(exe, .{}); + + const run_cmd = b.addRunArtifact(exe); + run_cmd.cwd = b.path(module_subpath); + run_cmd.step.dependOn(&install_cmd.step); + + run_step.dependOn(&run_cmd.step); + all_step.dependOn(&install_cmd.step); + } +} + const emccOutputDir = "zig-out" ++ std.fs.path.sep_str ++ "htmlout" ++ std.fs.path.sep_str; const emccOutputFile = "index.html"; @@ -197,7 +339,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. } var c_source_files: std.ArrayList([]const u8) = try .initCapacity(b.allocator, 2); - c_source_files.appendSliceAssumeCapacity(&.{ "src/rcore.c" }); + c_source_files.appendSliceAssumeCapacity(&.{"src/rcore.c"}); if (options.rshapes) { try c_source_files.append(b.allocator, "src/rshapes.c"); @@ -448,7 +590,7 @@ pub const Options = struct { .linux_display_backend = b.option(LinuxDisplayBackend, "linux_display_backend", "Linux display backend to use") orelse defaults.linux_display_backend, .opengl_version = b.option(OpenglVersion, "opengl_version", "OpenGL version to use") orelse defaults.opengl_version, .config = b.option([]const u8, "config", "Compile with custom define macros overriding config.h") orelse &.{}, - .android_ndk = b.option([]const u8, "android_ndk", "specify path to android ndk") orelse std.process.getEnvVarOwned(b.allocator, "ANDROID_NDK_HOME") catch "", + .android_ndk = b.option([]const u8, "android_ndk", "specify path to android ndk") orelse getAndroidNdk(b), .android_api_version = b.option([]const u8, "android_api_version", "specify target android API level") orelse defaults.android_api_version, }; } @@ -523,133 +665,31 @@ fn addExamples( ) !*std.Build.Step { const all = b.step(module, "All " ++ module ++ " examples"); const module_subpath = b.pathJoin(&.{ "examples", module }); - var dir = try std.fs.cwd().openDir(b.pathFromRoot(module_subpath), .{ .iterate = true }); - defer dir.close(); - - var iter = dir.iterate(); - while (try iter.next()) |entry| { - if (entry.kind != .file) continue; - const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue; - const name = entry.name[0..extension_idx]; - const path = b.pathJoin(&.{ module_subpath, entry.name }); - - // zig's mingw headers do not include pthread.h - if (std.mem.eql(u8, "core_loading_thread", name) and target.result.os.tag == .windows) continue; - - const exe_mod = b.createModule(.{ - .target = target, - .optimize = optimize, - }); - exe_mod.addCSourceFile(.{ .file = b.path(path), .flags = &.{} }); - exe_mod.linkLibrary(raylib); - - const run_step = b.step(name, name); - - if (target.result.os.tag == .emscripten) { - const wasm = b.addLibrary(.{ - .name = name, - .linkage = .static, - .root_module = exe_mod, - }); - - if (std.mem.eql(u8, name, "rlgl_standalone")) { - //TODO: Make rlgl_standalone example work - continue; - } - if (std.mem.eql(u8, name, "raylib_opengl_interop")) { - //TODO: Make raylib_opengl_interop example work - continue; - } - - const emcc_flags = emsdk.emccDefaultFlags(b.allocator, .{ .optimize = optimize }); - const emcc_settings = emsdk.emccDefaultSettings(b.allocator, .{ .optimize = optimize }); - - const install_dir: std.Build.InstallDir = .{ .custom = "htmlout" }; - const emcc_step = emsdk.emccStep(b, raylib, wasm, .{ - .optimize = optimize, - .flags = emcc_flags, - .settings = emcc_settings, - .shell_file_path = b.path("src/shell.html"), - .embed_paths = &.{ - .{ - .src_path = b.pathJoin(&.{ module_subpath, "resources" }), - .virtual_path = "resources", - }, - }, - .install_dir = install_dir, - }); - - const html_filename = try std.fmt.allocPrint(b.allocator, "{s}.html", .{wasm.name}); - const emrun_step = emsdk.emrunStep( - b, - b.getInstallPath(install_dir, html_filename), - &.{"--no_browser"}, - ); - emrun_step.dependOn(emcc_step); - - run_step.dependOn(emrun_step); - all.dependOn(emcc_step); - } else { - // special examples that test using these external dependencies directly - // alongside raylib - if (std.mem.eql(u8, name, "rlgl_standalone")) { - exe_mod.addIncludePath(b.path("src")); - exe_mod.addIncludePath(b.path("src/external/glfw/include")); - if (!hasCSource(raylib.root_module, "rglfw.c")) { - exe_mod.addCSourceFile(.{ .file = b.path("src/rglfw.c"), .flags = &.{} }); - } - } - if (std.mem.eql(u8, name, "raylib_opengl_interop")) { - exe_mod.addIncludePath(b.path("src/external")); - } - switch (target.result.os.tag) { - .windows => { - exe_mod.linkSystemLibrary("winmm", .{}); - exe_mod.linkSystemLibrary("gdi32", .{}); - exe_mod.linkSystemLibrary("opengl32", .{}); + if (comptime is_zig_0_16) { + var dir = try std.Io.Dir.cwd().openDir(b.graph.io, b.pathFromRoot(module_subpath), .{ .iterate = true }); + defer dir.close(b.graph.io); - exe_mod.addCMacro("PLATFORM_DESKTOP", ""); - }, - .linux => { - exe_mod.linkSystemLibrary("GL", .{}); - exe_mod.linkSystemLibrary("rt", .{}); - exe_mod.linkSystemLibrary("dl", .{}); - exe_mod.linkSystemLibrary("m", .{}); - exe_mod.linkSystemLibrary("X11", .{}); - - exe_mod.addCMacro("PLATFORM_DESKTOP", ""); - }, - .macos => { - exe_mod.linkFramework("Foundation", .{}); - exe_mod.linkFramework("Cocoa", .{}); - exe_mod.linkFramework("OpenGL", .{}); - exe_mod.linkFramework("CoreAudio", .{}); - exe_mod.linkFramework("CoreVideo", .{}); - exe_mod.linkFramework("IOKit", .{}); - - exe_mod.addCMacro("PLATFORM_DESKTOP", ""); - }, - else => { - @panic("Unsupported OS"); - }, - } - - const exe = b.addExecutable(.{ - .name = name, - .root_module = exe_mod, - }); - - const install_cmd = b.addInstallArtifact(exe, .{}); - - const run_cmd = b.addRunArtifact(exe); - run_cmd.cwd = b.path(module_subpath); - run_cmd.step.dependOn(&install_cmd.step); - - run_step.dependOn(&run_cmd.step); - all.dependOn(&install_cmd.step); + var iter = dir.iterate(); + while (try iter.next(b.graph.io)) |entry| { + if (entry.kind != .file) continue; + const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue; + const name = entry.name[0..extension_idx]; + try processExample(b, target, optimize, raylib, module_subpath, name, all); + } + } else { + var dir = try std.fs.cwd().openDir(b.pathFromRoot(module_subpath), .{ .iterate = true }); + defer dir.close(); + + var iter = dir.iterate(); + while (try iter.next()) |entry| { + if (entry.kind != .file) continue; + const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue; + const name = entry.name[0..extension_idx]; + try processExample(b, target, optimize, raylib, module_subpath, name, all); } } + return all; }