Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ $RECYCLE.BIN/
*.swp

# Zig
zig-cache/
.zig-cache/
src/Box2D.NET.Native/debug
src/Box2D.NET.Native/release

Expand Down
57 changes: 55 additions & 2 deletions Box2D.NET.Bindgen/Box2D.NET.Bindgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ContinuousIntegrationBuild>false</ContinuousIntegrationBuild>
Expand All @@ -11,7 +11,60 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bindgen.NET" Version="0.1.14"/>
<PackageReference Include="Bindgen.NET" Version="0.1.17" />
</ItemGroup>

<!-- -->
<!-- The binding generator needs access to system headers. The rest of the code below grabs the headers provided by zig. -->
<!-- -->
<PropertyGroup>
<HostArch>$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)</HostArch>
</PropertyGroup>

<!-- Populate "HostRuntime" with the platform that is used to select the correct local zig nuget path. -->
<!-- Also, populate "LibCPlatform" with the platform that is used to select the correct libc path within the zig path. -->
<Choose>
<When Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">win-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">win-arm64</HostRuntime>
<LibCPlatform>any-windows-any</LibCPlatform>
</PropertyGroup>
</When>
<When Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">linux-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">linux-arm64</HostRuntime>
<LibCPlatform>generic-glibc</LibCPlatform>
</PropertyGroup>
</When>
<When Condition="$([MSBuild]::IsOSPlatform('OSX'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">osx-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">osx-arm64</HostRuntime>
<LibCPlatform>any-macos-any</LibCPlatform>
</PropertyGroup>
</When>
</Choose>

<ItemGroup>
<!-- This NuGet package contains system headers that can be provided to the binding generator. -->
<PackageReference Include="Vezel.Zig.Toolsets.$(HostRuntime)" Version="0.14.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<!-- Store the location of the zig lib folder as a global variable that can be accessed in code. -->
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)BuildConstants.cs"/>
</ItemGroup>

<Target Name="GenerateConstants" BeforeTargets="CoreCompile">
<WriteLinesToFile
File="$(IntermediateOutputPath)BuildConstants.cs"
Lines='public static class BuildConstants { public const string ZigLibIncludePath = @"$(ZigLibPath)/include"%3B public const string ZigCLibIncludePath = @"$(ZigLibPath)/libc/include/$(LibCPlatform)"%3B }'
Overwrite="true"
/>
</Target>
</Project>
29 changes: 7 additions & 22 deletions Box2D.NET.Bindgen/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
using System.Runtime.CompilerServices;
using Bindgen.NET;
using Bindgen.NET;
using System.Runtime.CompilerServices;

const string library = "box2d";

BindingOptions options = new()
{
Namespace = "Box2D.NET.Bindings",
Class = "B2",

DllImportPath = library,
DllFilePaths =
{
library,
"lib" + library,
"runtimes/linux-x64/native/lib" + library,
"runtimes/linux-arm64/native/lib" + library,
"runtimes/osx-x64/native/lib" + library,
"runtimes/osx-arm64/native/lib" + library,
"runtimes/win-x64/native/" + library,
"runtimes/win-arm64/native/" + library
},

IncludeBuiltInClangHeaders = true,
IncludeDirectories = { GetNativeDirectory("include") },

IncludeDirectories = [GetNativeDirectory("include")],
SystemIncludeDirectories = [BuildConstants.ZigLibIncludePath, BuildConstants.ZigCLibIncludePath],
TreatInputFileAsRawSourceCode = true,
InputFile = """
#include <box2d/base.h>
Expand All @@ -36,7 +22,7 @@
OutputFile = GetOutputDirectory("B2.g.cs"),

RemappedPrefixes =
{
[
("b2AABB_", "AABB"),
("b2Body_", "Body"),
("b2Chain_", "Chain"),
Expand All @@ -55,11 +41,10 @@
("b2World_", "World"),
("b2_", ""),
("b2", ""),
},
],

GenerateMacros = true,
GenerateExternVariables = true,
SuppressedWarnings = { "CS9084" }
SuppressedWarnings = ["CS9084"]
};

BindingGenerator.Generate(options);
Expand Down
Loading
Loading