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

Pass zig include headers to binding generator #63

Merged
merged 1 commit into from
Oct 28, 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
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
Loading