|
1 | 1 | const std = @import("std"); |
2 | | -const builtin = @import("builtin"); |
| 2 | +const rl = @import("raylib"); |
3 | 3 |
|
4 | | -pub fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, shared: bool) !*std.Build.Step.Compile { |
5 | | - const raylib = b.dependency("raylib", .{ .target = target, .optimize = optimize, .linkage = if (shared) std.builtin.LinkMode.dynamic else std.builtin.LinkMode.static, .linux_display_backend = .X11, .opengl_version = .gl_4_3 }); |
6 | | - const raygui = b.dependency("raygui", .{ .target = target, .optimize = optimize }); |
7 | | - const lib = raylib.artifact("raylib"); |
| 4 | +pub fn build(b: *std.Build) !void { |
| 5 | + const target = b.standardTargetOptions(.{}); |
| 6 | + const optimize = b.standardOptimizeOption(.{}); |
| 7 | + |
| 8 | + // Determine linkage based on target |
| 9 | + const linkage = if (target.result.os.tag == .emscripten) |
| 10 | + std.builtin.LinkMode.static |
| 11 | + else |
| 12 | + std.builtin.LinkMode.dynamic; |
| 13 | + |
| 14 | + // Get raylib options for configuring the build |
| 15 | + const options = rl.Options{ |
| 16 | + .linkage = linkage, |
| 17 | + .linux_display_backend = .X11, |
| 18 | + .opengl_version = .gl_4_3, |
| 19 | + }; |
| 20 | + |
| 21 | + // Build raylib using raylib's build system |
| 22 | + const raylib_dep = b.dependency("raylib", .{ |
| 23 | + .target = target, |
| 24 | + .optimize = optimize, |
| 25 | + .raudio = options.raudio, |
| 26 | + .rmodels = options.rmodels, |
| 27 | + .rshapes = options.rshapes, |
| 28 | + .rtext = options.rtext, |
| 29 | + .rtextures = options.rtextures, |
| 30 | + .platform = options.platform, |
| 31 | + .linkage = options.linkage, |
| 32 | + .linux_display_backend = options.linux_display_backend, |
| 33 | + .opengl_version = options.opengl_version, |
| 34 | + .android_api_version = options.android_api_version, |
| 35 | + .android_ndk = options.android_ndk, |
| 36 | + }); |
| 37 | + const raylib = raylib_dep.artifact("raylib"); |
8 | 38 |
|
| 39 | + // Add platform-specific include/library paths for cross-compiling |
| 40 | + // Note: On Ubuntu/Debian, cross-compilation libraries are installed in multiarch paths. |
| 41 | + // On other distros (like Arch), these paths don't exist and Zig will warn but continue. |
| 42 | + // The warnings are cosmetic and don't affect the build - standard paths are added first. |
9 | 43 | switch (target.result.os.tag) { |
10 | | - // Due to *terrible* zig default behaviour for include paths when cross-compiling I have to do this |
11 | | - // The includes are resolved properly only when -Dtarget=native (or omitted) is passed |
12 | 44 | .linux => { |
| 45 | + // Add standard paths that work on all distros |
| 46 | + raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib" }); |
| 47 | + raylib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
| 48 | + |
| 49 | + // Add Ubuntu/Debian multiarch paths for cross-compilation |
| 50 | + // These are needed for CI builds on Ubuntu when cross-compiling to different architectures |
13 | 51 | if (target.result.cpu.arch == .aarch64) { |
14 | | - lib.addLibraryPath(.{ .cwd_relative = "/usr/lib/aarch64-linux-gnu/" }); |
15 | | - lib.addIncludePath(.{ .cwd_relative = "/usr/include/aarch64-linux-gnu/" }); |
16 | | - lib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
| 52 | + raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib/aarch64-linux-gnu/" }); |
| 53 | + raylib.addIncludePath(.{ .cwd_relative = "/usr/include/aarch64-linux-gnu/" }); |
17 | 54 | } else if (target.result.cpu.arch == .x86) { |
18 | | - lib.addLibraryPath(.{ .cwd_relative = "/usr/lib/i386-linux-gnu/" }); |
19 | | - lib.addIncludePath(.{ .cwd_relative = "/usr/include/i386-linux-gnu/" }); |
20 | | - lib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
| 55 | + raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib/i386-linux-gnu/" }); |
| 56 | + raylib.addIncludePath(.{ .cwd_relative = "/usr/include/i386-linux-gnu/" }); |
21 | 57 | // https://github.com/ziglang/zig/issues/7935 |
22 | | - lib.link_z_notext = true; |
| 58 | + raylib.link_z_notext = true; |
23 | 59 | } else if (target.result.cpu.arch == .x86_64) { |
24 | | - lib.addLibraryPath(.{ .cwd_relative = "/usr/lib/x86_64-linux-gnu/" }); |
25 | | - lib.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu/" }); |
26 | | - lib.addIncludePath(.{ .cwd_relative = "/usr/include" }); |
27 | | - } else { |
28 | | - lib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
| 60 | + raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib/x86_64-linux-gnu/" }); |
| 61 | + raylib.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu/" }); |
29 | 62 | } |
30 | | - |
31 | | - lib.addLibraryPath(.{ .cwd_relative = "/usr/lib" }); |
32 | 63 | }, |
33 | | - else => {} |
| 64 | + else => {}, |
34 | 65 | } |
35 | 66 |
|
36 | | - var gen_step = b.addWriteFiles(); |
37 | | - lib.step.dependOn(&gen_step.step); |
38 | | - |
39 | | - var cflags = std.ArrayList([]const u8){}; |
40 | | - defer cflags.deinit(b.allocator); |
41 | | - |
42 | | - try cflags.appendSlice(b.allocator, &[_][]const u8{ |
43 | | - "-std=gnu99", |
44 | | - "-D_GNU_SOURCE", |
45 | | - "-DGL_SILENCE_DEPRECATION=199309L", |
46 | | - "-fno-sanitize=undefined", |
| 67 | + // Add raygui using raylib's helper function |
| 68 | + const raygui_dep = b.dependency("raygui", .{ |
| 69 | + .target = target, |
| 70 | + .optimize = optimize, |
47 | 71 | }); |
| 72 | + rl.addRaygui(b, raylib, raygui_dep, options); |
48 | 73 |
|
49 | | - if (shared) { |
50 | | - try cflags.appendSlice(b.allocator, &[_][]const u8{ |
51 | | - "-fPIC", |
52 | | - "-DBUILD_LIBTYPE_SHARED", |
53 | | - }); |
54 | | - } |
| 74 | + // Install the library |
| 75 | + // For non-emscripten targets, also build a static library version |
| 76 | + if (target.result.os.tag != .emscripten) { |
| 77 | + // Install the shared library |
| 78 | + b.installArtifact(raylib); |
55 | 79 |
|
56 | | - lib.addCSourceFile(.{ |
57 | | - .file = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n"), |
58 | | - .flags = cflags.items, |
59 | | - }); |
| 80 | + // Also build and install a static version |
| 81 | + const raylib_static_dep = b.dependency("raylib", .{ |
| 82 | + .target = target, |
| 83 | + .optimize = optimize, |
| 84 | + .raudio = options.raudio, |
| 85 | + .rmodels = options.rmodels, |
| 86 | + .rshapes = options.rshapes, |
| 87 | + .rtext = options.rtext, |
| 88 | + .rtextures = options.rtextures, |
| 89 | + .platform = options.platform, |
| 90 | + .linkage = .static, |
| 91 | + .linux_display_backend = options.linux_display_backend, |
| 92 | + .opengl_version = options.opengl_version, |
| 93 | + .android_api_version = options.android_api_version, |
| 94 | + .android_ndk = options.android_ndk, |
| 95 | + }); |
| 96 | + const raylib_static = raylib_static_dep.artifact("raylib"); |
60 | 97 |
|
61 | | - lib.addIncludePath(raylib.path("src")); |
62 | | - lib.addIncludePath(raygui.path("src")); |
63 | | - lib.installHeader(raygui.path("src/raygui.h"), "raygui.h"); |
| 98 | + // Add same platform-specific paths for static lib |
| 99 | + switch (target.result.os.tag) { |
| 100 | + .linux => { |
| 101 | + raylib_static.addLibraryPath(.{ .cwd_relative = "/usr/lib" }); |
| 102 | + raylib_static.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
64 | 103 |
|
65 | | - return lib; |
66 | | -} |
| 104 | + // Add multiarch paths for cross-compilation |
| 105 | + if (target.result.cpu.arch == .aarch64) { |
| 106 | + raylib_static.addLibraryPath(.{ .cwd_relative = "/usr/lib/aarch64-linux-gnu/" }); |
| 107 | + raylib_static.addIncludePath(.{ .cwd_relative = "/usr/include/aarch64-linux-gnu/" }); |
| 108 | + } else if (target.result.cpu.arch == .x86) { |
| 109 | + raylib_static.addLibraryPath(.{ .cwd_relative = "/usr/lib/i386-linux-gnu/" }); |
| 110 | + raylib_static.addIncludePath(.{ .cwd_relative = "/usr/include/i386-linux-gnu/" }); |
| 111 | + raylib_static.link_z_notext = true; |
| 112 | + } else if (target.result.cpu.arch == .x86_64) { |
| 113 | + raylib_static.addLibraryPath(.{ .cwd_relative = "/usr/lib/x86_64-linux-gnu/" }); |
| 114 | + raylib_static.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu/" }); |
| 115 | + } |
| 116 | + }, |
| 117 | + else => {}, |
| 118 | + } |
67 | 119 |
|
68 | | -pub fn build(b: *std.Build) !void { |
69 | | - const target = b.standardTargetOptions(.{}); |
70 | | - const optimize = b.standardOptimizeOption(.{}); |
| 120 | + // Add raygui to static library too |
| 121 | + rl.addRaygui(b, raylib_static, raygui_dep, .{ |
| 122 | + .linkage = .static, |
| 123 | + .linux_display_backend = options.linux_display_backend, |
| 124 | + .opengl_version = options.opengl_version, |
| 125 | + }); |
71 | 126 |
|
72 | | - if (target.result.os.tag != .emscripten) { |
73 | | - b.installArtifact(try compileRaylib(b, target, optimize, true)); |
| 127 | + b.installArtifact(raylib_static); |
| 128 | + } else { |
| 129 | + // For emscripten, just install the static library |
| 130 | + b.installArtifact(raylib); |
74 | 131 | } |
75 | | - |
76 | | - b.installArtifact(try compileRaylib(b, target, optimize, false)); |
77 | 132 | } |
0 commit comments