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 f783f13
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 58 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>
24 changes: 24 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,30 @@

<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">
<PropertyGroup>
<ConstantsFileContent>
public static class BuildConstants { public const string ZigLibPath = "$(ZigLibPath)"%3B }
</ConstantsFileContent>
</PropertyGroup>

<Message Importance="High" Text="Test: $(ConstantsFileContent)"/>

<WriteLinesToFile
File="$(IntermediateOutputPath)BuildConstants.cs"
Lines="$(ConstantsFileContent)"
Overwrite="true"/>
</Target>

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

SuppressedWarnings = { "CS8981" },

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

InputFile = GetFlecsHeaderPath(),
OutputFile = GetBindingsOutputPath(),
Expand All @@ -33,11 +37,10 @@
GenerateStructEqualityFunctions = true
};

if (OperatingSystem.IsMacOS())
bindingOptions.SystemIncludeDirectories.Add(GetMacOsHeaders());

BindingGenerator.Generate(bindingOptions);

return;

string GetFlecsHeaderPath([CallerFilePath] string filePath = "")
{
return Path.GetFullPath(Path.Combine(filePath, "..", "..", "..", "submodules", "flecs", "distr", "flecs.h"));
Expand All @@ -47,28 +50,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 f783f13

Please sign in to comment.