From e0adf2d980a1509c6fca06eb513460f02cba1efb Mon Sep 17 00:00:00 2001 From: BeanCheeseBurrito Date: Sat, 9 Nov 2024 01:32:00 -0800 Subject: [PATCH] Bundle compiler rt when compiling flecs as static library --- .github/workflows/ci.yml | 25 +++++------ native/windows.c | 5 +++ src/Flecs.NET.Native/Flecs.NET.Native.csproj | 4 +- src/Flecs.NET.Native/build.zig | 44 ++++++++++---------- 4 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 native/windows.c diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1ead3c..837caf5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -92,28 +92,25 @@ jobs: working-directory: src/Flecs.NET.Native run: | dotnet build -c Debug -r linux-x64 - dotnet build -c Release -r linux-x64 dotnet build -c Debug -r linux-arm64 - dotnet build -c Release -r linux-arm64 - dotnet build -c Debug -r osx-x64 - dotnet build -c Release -r osx-x64 dotnet build -c Debug -r osx-arm64 - dotnet build -c Release -r osx-arm64 - dotnet build -c Debug -r win-x64 - dotnet build -c Release -r win-x64 dotnet build -c Debug -r win-arm64 - dotnet build -c Release -r win-arm64 - dotnet build -c Debug -r browser-wasm - dotnet build -c Release -r browser-wasm - dotnet build -c Debug -r iossimulator-x64 - dotnet build -c Release -r iossimulator-x64 dotnet build -c Debug -r iossimulator-arm64 - dotnet build -c Release -r iossimulator-arm64 dotnet build -c Debug -r ios-arm64 + + dotnet build -c Release -r linux-x64 + dotnet build -c Release -r linux-arm64 + dotnet build -c Release -r osx-x64 + dotnet build -c Release -r osx-arm64 + dotnet build -c Release -r win-x64 + dotnet build -c Release -r win-arm64 + dotnet build -c Release -r browser-wasm + dotnet build -c Release -r iossimulator-x64 + dotnet build -c Release -r iossimulator-arm64 dotnet build -c Release -r ios-arm64 - name: Run Tests @@ -133,7 +130,7 @@ jobs: - name: Upload Artifacts if: matrix.os == 'macos-13' - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: Nuget Packages path: | diff --git a/native/windows.c b/native/windows.c new file mode 100644 index 0000000..31e4452 --- /dev/null +++ b/native/windows.c @@ -0,0 +1,5 @@ +// Temporary fix for undefined symbol errors when statically linking on windows. +void __mingw_vsnprintf() { } +void __mingw_vfprintf() { } +void __isnanf() { } +void __fpclassifyf() { } diff --git a/src/Flecs.NET.Native/Flecs.NET.Native.csproj b/src/Flecs.NET.Native/Flecs.NET.Native.csproj index e36e9d3..a9ac30c 100644 --- a/src/Flecs.NET.Native/Flecs.NET.Native.csproj +++ b/src/Flecs.NET.Native/Flecs.NET.Native.csproj @@ -136,9 +136,9 @@ - + - $(ZigArgs) -Dsoft-assert=true + $(ZigArgs) -Dcompiler-rt-path="$(ZigLibPath)/compiler_rt.zig" diff --git a/src/Flecs.NET.Native/build.zig b/src/Flecs.NET.Native/build.zig index da8cde7..3dc32f1 100644 --- a/src/Flecs.NET.Native/build.zig +++ b/src/Flecs.NET.Native/build.zig @@ -1,11 +1,11 @@ const std = @import("std"); const Build = std.Build; -pub const LibType = enum { Shared, Static }; +pub const LibraryType = enum { Shared, Static }; const src_flags = [_][]const u8{ + "-std=c99", "-fno-sanitize=undefined", - "-fno-stack-protector", }; const src_files = [_][]const u8{ @@ -123,43 +123,42 @@ const src_files = [_][]const u8{ "../../native/flecs/src/storage/table_graph.c", }; -pub fn compileFlecs(options: anytype, b: *Build, lib_type: LibType) void { - const lib = switch (lib_type) { +pub fn compileFlecs(b: *Build, options: anytype) void { + const lib = switch (options.library_type) { .Shared => b.addSharedLibrary(.{ .name = "flecs", .target = options.target, .optimize = options.optimize, .strip = options.optimize != .Debug, + .link_libc = true, }), .Static => b.addStaticLibrary(.{ .name = "flecs", .target = options.target, .optimize = options.optimize, .strip = options.optimize != .Debug, + .link_libc = true, + .root_source_file = if (options.compiler_rt_path) |path| .{ .cwd_relative = path } else null, }), }; - lib.linkLibC(); - lib.defineCMacro(if (options.optimize == .Debug) "FLECS_DEBUG" else "FLECS_NDEBUG", null); - lib.defineCMacro(if (lib_type == LibType.Shared) "flecs_EXPORTS" else "flecs_STATIC", null); + lib.defineCMacro(if (options.library_type == LibraryType.Shared) "flecs_EXPORTS" else "flecs_STATIC", null); - if (options.soft_assert) { - lib.defineCMacro("FLECS_SOFT_ASSERT", null); - } + lib.addIncludePath(b.path("../../native/flecs/include")); for (src_files) |file| { - lib.addCSourceFile(.{ - .file = b.path(file), - .flags = &src_flags, - }); + lib.addCSourceFile(.{ .file = b.path(file), .flags = &src_flags }); } - lib.addIncludePath(b.path("../../native/flecs/include")); - switch (options.target.result.os.tag) { .windows => { lib.linkSystemLibrary("ws2_32"); + + // Temporary fix to get rid of undefined symbol errors when statically linking in Native AOT. + if (options.library_type == LibraryType.Static) { + lib.addCSourceFile(.{ .file = b.path("../../native/windows.c"), .flags = &src_flags }); + } }, .ios => { if (b.sysroot == null) { @@ -187,12 +186,13 @@ pub fn compileFlecs(options: anytype, b: *Build, lib_type: LibType) void { } pub fn build(b: *Build) void { - const options = .{ + compileFlecs(b, .{ .optimize = b.standardOptimizeOption(.{}), .target = b.standardTargetOptions(.{}), - .soft_assert = b.option(bool, "soft-assert", "Compile with the FLECS_SOFT_ASSERT define.") orelse false, - .library_type = b.option(LibType, "library-type", "Compile as a static or shared library.") orelse LibType.Shared, - }; - - compileFlecs(options, b, options.library_type); + .library_type = b.option(LibraryType, "library-type", "Compile as a static or shared library.") orelse LibraryType.Shared, + // When building static libraries for Windows, zig's compiler-rt needs to be bundled. + // For some reason, setting "bundle_compiler_rt" to true doesn't produce a static library that works with NativeAOT. + // As a work-around, we manually build the compiler_rt.zig file alongside flecs. + .compiler_rt_path = b.option([]const u8, "compiler-rt-path", "Path to the compiler_rt file.") orelse null, + }); }