Skip to content

Commit f853267

Browse files
committed
fix: rework build.zig to use raylib build and add back wasm
Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 12f9474 commit f853267

7 files changed

Lines changed: 160 additions & 96 deletions

File tree

.github/workflows/build-natives.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- linux-x64
1818
- linux-x86
1919
- linux-arm64
20-
# - browser-wasm
20+
- browser-wasm
2121
- osx-x64
2222
- osx-arm64
2323
- win-x64

lib/raygui

lib/raylib

Submodule raylib updated 83 files

src/Raylib.NET.Native/build.zig

Lines changed: 110 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,132 @@
11
const std = @import("std");
2-
const builtin = @import("builtin");
2+
const rl = @import("raylib");
33

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");
838

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.
943
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
1244
.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
1351
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/" });
1754
} 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/" });
2157
// https://github.com/ziglang/zig/issues/7935
22-
lib.link_z_notext = true;
58+
raylib.link_z_notext = true;
2359
} 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/" });
2962
}
30-
31-
lib.addLibraryPath(.{ .cwd_relative = "/usr/lib" });
3263
},
33-
else => {}
64+
else => {},
3465
}
3566

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,
4771
});
72+
rl.addRaygui(b, raylib, raygui_dep, options);
4873

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);
5579

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");
6097

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" });
64103

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+
}
67119

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+
});
71126

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);
74131
}
75-
76-
b.installArtifact(try compileRaylib(b, target, optimize, false));
77132
}

src/Raylib.NET/Raylib.cs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,13 @@ public static unsafe partial class Raylib
766766
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
767767
public static partial void OpenURL(string url);
768768

769+
/// <summary>
770+
/// Set the current threshold (minimum) log level
771+
/// </summary>
772+
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
773+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
774+
public static partial void SetTraceLogLevel(int logLevel);
775+
769776
/// <summary>
770777
/// Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR...)
771778
/// </summary>
@@ -774,11 +781,11 @@ public static unsafe partial class Raylib
774781
public static partial void TraceLog(int logLevel, string text, IntPtr args);
775782

776783
/// <summary>
777-
/// Set the current threshold (minimum) log level
784+
/// Set custom trace log
778785
/// </summary>
779786
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
780787
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
781-
public static partial void SetTraceLogLevel(int logLevel);
788+
public static unsafe partial void SetTraceLogCallback(delegate* unmanaged[Cdecl]<int, sbyte*, sbyte*, void> callback);
782789

783790
/// <summary>
784791
/// Internal memory allocator
@@ -802,88 +809,81 @@ public static unsafe partial class Raylib
802809
public static unsafe partial void MemFree(void* ptr);
803810

804811
/// <summary>
805-
/// Set custom trace log
806-
/// </summary>
807-
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
808-
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
809-
public static unsafe partial void SetTraceLogCallback(delegate* unmanaged[Cdecl]<int, sbyte*, sbyte*, void> callback);
810-
811-
/// <summary>
812-
/// Set custom file binary data loader
812+
/// Load file data as byte array (read)
813813
/// </summary>
814814
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
815815
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
816-
public static unsafe partial void SetLoadFileDataCallback(delegate* unmanaged[Cdecl]<sbyte*, int*, byte*> callback);
816+
public static unsafe partial byte* LoadFileData(string fileName, ref int dataSize);
817817

818818
/// <summary>
819-
/// Set custom file binary data saver
819+
/// Unload file data allocated by LoadFileData()
820820
/// </summary>
821821
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
822822
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
823-
public static unsafe partial void SetSaveFileDataCallback(delegate* unmanaged[Cdecl]<sbyte*, void*, int, sbyte> callback);
823+
public static unsafe partial void UnloadFileData(byte* data);
824824

825825
/// <summary>
826-
/// Set custom file text data loader
826+
/// Save data to file from byte array (write), returns true on success
827827
/// </summary>
828828
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
829829
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
830-
public static unsafe partial void SetLoadFileTextCallback(delegate* unmanaged[Cdecl]<sbyte*, sbyte*> callback);
830+
public static unsafe partial NativeBool SaveFileData(string fileName, void* data, int dataSize);
831831

832832
/// <summary>
833-
/// Set custom file text data saver
833+
/// Export data to code (.h), returns true on success
834834
/// </summary>
835835
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
836836
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
837-
public static unsafe partial void SetSaveFileTextCallback(delegate* unmanaged[Cdecl]<sbyte*, sbyte*, sbyte> callback);
837+
public static unsafe partial NativeBool ExportDataAsCode(byte* data, int dataSize, string fileName);
838838

839839
/// <summary>
840-
/// Load file data as byte array (read)
840+
/// Load text data from file (read), returns a '\0' terminated string
841841
/// </summary>
842842
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
843843
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
844-
public static unsafe partial byte* LoadFileData(string fileName, ref int dataSize);
844+
public static partial string LoadFileText(string fileName);
845845

846846
/// <summary>
847-
/// Unload file data allocated by LoadFileData()
847+
/// Unload file text data allocated by LoadFileText()
848848
/// </summary>
849849
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
850850
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
851-
public static unsafe partial void UnloadFileData(byte* data);
851+
public static partial void UnloadFileText(string text);
852852

853853
/// <summary>
854-
/// Save data to file from byte array (write), returns true on success
854+
/// Save text data to file (write), string must be '\0' terminated, returns true on success
855855
/// </summary>
856856
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
857857
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
858-
public static unsafe partial NativeBool SaveFileData(string fileName, void* data, int dataSize);
858+
public static partial NativeBool SaveFileText(string fileName, string text);
859859

860860
/// <summary>
861-
/// Export data to code (.h), returns true on success
861+
/// Set custom file binary data loader
862862
/// </summary>
863863
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
864864
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
865-
public static unsafe partial NativeBool ExportDataAsCode(byte* data, int dataSize, string fileName);
865+
public static unsafe partial void SetLoadFileDataCallback(delegate* unmanaged[Cdecl]<sbyte*, int*, byte*> callback);
866866

867867
/// <summary>
868-
/// Load text data from file (read), returns a '\0' terminated string
868+
/// Set custom file binary data saver
869869
/// </summary>
870870
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
871871
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
872-
public static partial string LoadFileText(string fileName);
872+
public static unsafe partial void SetSaveFileDataCallback(delegate* unmanaged[Cdecl]<sbyte*, void*, int, sbyte> callback);
873873

874874
/// <summary>
875-
/// Unload file text data allocated by LoadFileText()
875+
/// Set custom file text data loader
876876
/// </summary>
877877
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
878878
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
879-
public static partial void UnloadFileText(string text);
879+
public static unsafe partial void SetLoadFileTextCallback(delegate* unmanaged[Cdecl]<sbyte*, sbyte*> callback);
880880

881881
/// <summary>
882-
/// Save text data to file (write), string must be '\0' terminated, returns true on success
882+
/// Set custom file text data saver
883883
/// </summary>
884884
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
885885
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
886-
public static partial NativeBool SaveFileText(string fileName, string text);
886+
public static unsafe partial void SetSaveFileTextCallback(delegate* unmanaged[Cdecl]<sbyte*, sbyte*, sbyte> callback);
887887

888888
/// <summary>
889889
/// Rename file (if exists)
@@ -1081,6 +1081,20 @@ public static unsafe partial class Raylib
10811081
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
10821082
public static partial void UnloadDroppedFiles(FilePathList files);
10831083

1084+
/// <summary>
1085+
/// Get the file count in a directory
1086+
/// </summary>
1087+
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
1088+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
1089+
public static partial uint GetDirectoryFileCount(string dirPath);
1090+
1091+
/// <summary>
1092+
/// Get the file count in a directory with extension filtering and recursive directory scan. Use 'DIR' in the filter string to include directories in the result
1093+
/// </summary>
1094+
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
1095+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
1096+
public static partial uint GetDirectoryFileCountEx(string basePath, string filter, NativeBool scanSubdirs);
1097+
10841098
/// <summary>
10851099
/// Compress data (DEFLATE algorithm), memory must be MemFree()
10861100
/// </summary>

src/Raylib.NET/Raymath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public static unsafe partial class Raymath
516516

517517
/// <summary>
518518
/// Projects a Vector3 from screen space into object space
519-
/// NOTE: We are avoiding calling other raymath functions despite available
519+
/// NOTE: Self-contained function, no other raymath functions are called
520520
/// </summary>
521521
[LibraryImport(LIBRARY, StringMarshalling = StringMarshalling.Utf8)]
522522
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]

src/Raylib.NET/Types/FilePathList.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ namespace RaylibNET;
88
[StructLayout(LayoutKind.Sequential)]
99
public partial struct FilePathList
1010
{
11-
/// <summary>
12-
/// Filepaths max entries
13-
/// </summary>
14-
public uint Capacity;
1511
/// <summary>
1612
/// Filepaths entries count
1713
/// </summary>
@@ -21,9 +17,8 @@ public partial struct FilePathList
2117
/// </summary>
2218
public unsafe sbyte** Paths;
2319

24-
public unsafe FilePathList(uint capacity, uint count, sbyte** paths)
20+
public unsafe FilePathList(uint count, sbyte** paths)
2521
{
26-
this.Capacity = capacity;
2722
this.Count = count;
2823
this.Paths = paths;
2924
}

0 commit comments

Comments
 (0)