|
1 | 1 | const std = @import("std"); |
2 | | -const rl = @import("raylib"); |
| 2 | +const builtin = @import("builtin"); |
3 | 3 |
|
4 | | -pub fn build(b: *std.Build) !void { |
5 | | - const target = b.standardTargetOptions(.{}); |
6 | | - const optimize = b.standardOptimizeOption(.{}); |
7 | | - |
8 | | - const options = rl.Options.getOptions(b); |
9 | | - |
10 | | - const raylib_dep = b.dependency("raylib", .{ |
11 | | - .target = target, |
12 | | - .optimize = optimize, |
13 | | - .raudio = options.raudio, |
14 | | - .rmodels = options.rmodels, |
15 | | - .rshapes = options.rshapes, |
16 | | - .rtext = options.rtext, |
17 | | - .rtextures = options.rtextures, |
18 | | - .platform = options.platform, |
19 | | - .linkage = options.linkage, |
20 | | - .linux_display_backend = options.linux_display_backend, |
21 | | - .opengl_version = options.opengl_version, |
22 | | - .android_api_version = options.android_api_version, |
23 | | - .android_ndk = options.android_ndk, |
24 | | - }); |
25 | | - const raylib = raylib_dep.artifact("raylib"); |
| 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 }); |
| 6 | + const raygui = b.dependency("raygui", .{ .target = target, .optimize = optimize }); |
| 7 | + const lib = raylib.artifact("raylib"); |
26 | 8 |
|
27 | | - // Add platform-specific include/library paths for cross-compiling |
28 | 9 | 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 |
29 | 12 | .linux => { |
30 | 13 | if (target.result.cpu.arch == .aarch64) { |
31 | | - raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib/aarch64-linux-gnu/" }); |
32 | | - raylib.addIncludePath(.{ .cwd_relative = "/usr/include/aarch64-linux-gnu/" }); |
33 | | - raylib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
| 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" }); |
34 | 17 | } else if (target.result.cpu.arch == .x86) { |
35 | | - raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib/i386-linux-gnu/" }); |
36 | | - raylib.addIncludePath(.{ .cwd_relative = "/usr/include/i386-linux-gnu/" }); |
37 | | - raylib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
38 | | - raylib.link_z_notext = true; |
| 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" }); |
| 21 | + // https://github.com/ziglang/zig/issues/7935 |
| 22 | + lib.link_z_notext = true; |
39 | 23 | } else if (target.result.cpu.arch == .x86_64) { |
40 | | - raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib/x86_64-linux-gnu/" }); |
41 | | - raylib.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu/" }); |
42 | | - raylib.addIncludePath(.{ .cwd_relative = "/usr/include" }); |
| 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" }); |
43 | 27 | } else { |
44 | | - raylib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
| 28 | + lib.addSystemIncludePath(.{ .cwd_relative = "/usr/include" }); |
45 | 29 | } |
46 | | - raylib.addLibraryPath(.{ .cwd_relative = "/usr/lib" }); |
| 30 | + |
| 31 | + lib.addLibraryPath(.{ .cwd_relative = "/usr/lib" }); |
47 | 32 | }, |
48 | | - else => {}, |
| 33 | + else => {} |
| 34 | + } |
| 35 | + |
| 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", |
| 47 | + }); |
| 48 | + |
| 49 | + if (shared) { |
| 50 | + try cflags.appendSlice(b.allocator, &[_][]const u8{ |
| 51 | + "-fPIC", |
| 52 | + "-DBUILD_LIBTYPE_SHARED", |
| 53 | + }); |
49 | 54 | } |
50 | 55 |
|
51 | | - const raygui_dep = b.dependency("raygui", .{ |
52 | | - .target = target, |
53 | | - .optimize = optimize, |
| 56 | + lib.addCSourceFile(.{ |
| 57 | + .file = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n"), |
| 58 | + .flags = cflags.items, |
54 | 59 | }); |
55 | 60 |
|
56 | | - rl.addRaygui(b, raylib, raygui_dep, options); |
| 61 | + lib.addIncludePath(raylib.path("src")); |
| 62 | + lib.addIncludePath(raygui.path("src")); |
| 63 | + lib.installHeader(raygui.path("src/raygui.h"), "raygui.h"); |
| 64 | + |
| 65 | + return lib; |
| 66 | +} |
| 67 | + |
| 68 | +pub fn build(b: *std.Build) !void { |
| 69 | + const target = b.standardTargetOptions(.{}); |
| 70 | + const optimize = b.standardOptimizeOption(.{}); |
| 71 | + |
| 72 | + if (target.result.os.tag != .emscripten) { |
| 73 | + b.installArtifact(try compileRaylib(b, target, optimize, true)); |
| 74 | + } |
57 | 75 |
|
58 | | - b.installArtifact(raylib); |
| 76 | + b.installArtifact(try compileRaylib(b, target, optimize, false)); |
59 | 77 | } |
0 commit comments