Skip to content

Commit

Permalink
Pass zig include headers to binding generator
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Oct 28, 2024
1 parent 8e2f9bd commit 710c8b8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 56 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ Reference the project and import the native libraries. You should now be able to

### Running the bindings generator
Low-level bindings to the flecs C API are pre-generated and included in the [Flecs.NET.Bindings](https://github.com/BeanCheeseBurrito/Flecs.NET/tree/main/src/Flecs.NET.Bindings) project by default. If needed, you can run the following command to regenerate the bindings file.
> [!NOTE]
> The binding generator needs access to system headers on MacOS. Ensure that XCode is installed.
```console
dotnet run --project src/Flecs.NET.Bindgen
```
Expand Down
25 changes: 25 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,29 @@
<Deterministic Condition="'$(GITHUB_ACTIONS)' == 'true' And '$(Deterministic)' == ''">true</Deterministic>
<AllowedOutputExtensionsInPackageBuildOutputFolder Condition="'$(FlecsPackPdb)' == 'true'">$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<PropertyGroup>
<HostArch>$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)</HostArch>
</PropertyGroup>

<Choose>
<When Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">win-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">win-arm64</HostRuntime>
</PropertyGroup>
</When>
<When Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">linux-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">linux-arm64</HostRuntime>
</PropertyGroup>
</When>
<When Condition="$([MSBuild]::IsOSPlatform('OSX'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">osx-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">osx-arm64</HostRuntime>
</PropertyGroup>
</When>
</Choose>
</Project>
16 changes: 16 additions & 0 deletions src/Flecs.NET.Bindgen/Flecs.NET.Bindgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@

<ItemGroup>
<PackageReference Include="Bindgen.NET" Version="0.1.14"/>
<PackageReference Include="Vezel.Zig.Toolsets.$(HostRuntime)" Version="0.13.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 ZigLibPath = @"$(ZigLibPath)"%3B }'
Overwrite="true"/>
</Target>

</Project>
31 changes: 4 additions & 27 deletions src/Flecs.NET.Bindgen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

SuppressedWarnings = { "CS8981" },

IncludeBuiltInClangHeaders = true,
SystemIncludeDirectories = { Path.Combine(BuildConstants.ZigLibPath, "include") },

InputFile = GetFlecsHeaderPath(),
OutputFile = GetBindingsOutputPath(),
Expand All @@ -34,10 +34,12 @@
};

if (OperatingSystem.IsMacOS())
bindingOptions.SystemIncludeDirectories.Add(GetMacOsHeaders());
bindingOptions.SystemIncludeDirectories.Add(Path.Combine(BuildConstants.ZigLibPath, "libc", "include", "any-macos-any"));

BindingGenerator.Generate(bindingOptions);

return;

string GetFlecsHeaderPath([CallerFilePath] string filePath = "")
{
return Path.GetFullPath(Path.Combine(filePath, "..", "..", "..", "submodules", "flecs", "distr", "flecs.h"));
Expand All @@ -47,28 +49,3 @@ string GetBindingsOutputPath([CallerFilePath] string filePath = "")
{
return Path.GetFullPath(Path.Combine(filePath, "..", "..", "Flecs.NET.Bindings", "Flecs.g.cs"));
}

string GetMacOsHeaders()
{
using Process process = new()
{
StartInfo = new ProcessStartInfo
{
FileName = "xcrun",
Arguments = "--sdk macosx --show-sdk-path",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};

process.Start();
process.WaitForExit();

string path = process.StandardOutput.ReadToEnd().Trim();

if (!Directory.Exists(path))
throw new DirectoryNotFoundException("Couldn't find system headers. Install XCode.");

return path + "/usr/include";
}
28 changes: 1 addition & 27 deletions src/Flecs.NET.Native/Flecs.NET.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<IncludeBuildFiles>false</IncludeBuildFiles>
<IncludeBuildOutput>false</IncludeBuildOutput>
<MSBuildEnableWorkloadResolver>false</MSBuildEnableWorkloadResolver>
<NoWarn>$(NoWarn);CS2008</NoWarn>
<NoWarn>$(NoWarn);CS2008;NU5128</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -20,10 +20,6 @@
<Description>Native libraries for flecs</Description>
</PropertyGroup>

<PropertyGroup>
<HostArch>$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)</HostArch>
</PropertyGroup>

<!-- We want 2 separate natives packages for debug and release modes -->
<Choose>
<When Condition="'$(Configuration)' == 'Debug'">
Expand All @@ -40,28 +36,6 @@
</When>
</Choose>

<!-- As we are cross-compiling, RID != host runtime so we need to determine it for later -->
<Choose>
<When Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">win-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">win-arm64</HostRuntime>
</PropertyGroup>
</When>
<When Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">linux-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">linux-arm64</HostRuntime>
</PropertyGroup>
</When>
<When Condition="$([MSBuild]::IsOSPlatform('OSX'))">
<PropertyGroup>
<HostRuntime Condition="'$(HostArch)' == 'X64'">osx-x64</HostRuntime>
<HostRuntime Condition="'$(HostArch)' == 'Arm64'">osx-arm64</HostRuntime>
</PropertyGroup>
</When>
</Choose>

<!-- Fallback to host runtime when not specified -->
<PropertyGroup>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == ''">$(HostRuntime)</RuntimeIdentifier>
Expand Down

0 comments on commit 710c8b8

Please sign in to comment.