Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle zig's compiler rt when compiling flecs native libraries #71

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: |
Expand Down
5 changes: 5 additions & 0 deletions native/windows.c
Original file line number Diff line number Diff line change
@@ -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() { }
4 changes: 2 additions & 2 deletions src/Flecs.NET.Native/Flecs.NET.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@
</Otherwise>
</Choose>

<!-- Enable soft asserts if configured -->
<!-- Path to compiler_rt.zig -->
<PropertyGroup>
<ZigArgs Condition="'$(FlecsSoftAssert)' == 'True'">$(ZigArgs) -Dsoft-assert=true</ZigArgs>
<ZigArgs>$(ZigArgs) -Dcompiler-rt-path="$(ZigLibPath)/compiler_rt.zig"</ZigArgs>
</PropertyGroup>

<!-- Here we need host not target toolset to compile/cross-compile -->
Expand Down
44 changes: 22 additions & 22 deletions src/Flecs.NET.Native/build.zig
Original file line number Diff line number Diff line change
@@ -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{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
});
}