Skip to content
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
210 changes: 136 additions & 74 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,88 +20,150 @@ on:
nuget-registry:
description: 'NuGet registry'
required: true
default: 'GitLab'
default: 'GitHub'
type: choice
options:
- GitLab
- GitHub
- NuGet

jobs:
build:

runs-on: ${{ matrix.os }}

permissions:
packages: write

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-13, windows-latest]
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
3.1.x
5.0.x
6.0.x
7.0.x
8.0.x

- name: Set Version Suffix
shell: bash
run: |
if [ '${{ github.event.inputs.use-auto-generated-version }}' != 'false' ]; then
echo "NugetVersionSuffix=-dev-$(date +'%Y-%m-%d-%H-%M-%S')" >> $GITHUB_ENV
fi

- name: Restore Dependencies
shell: bash
run: dotnet restore

- name: Generate Bindings
shell: bash
run: dotnet run --project Box2D.NET.Bindgen

- name: Build Projects
shell: bash
run: |
dotnet build -c Debug
dotnet build -c Release

- name: Run Tests
shell: bash
run: dotnet test

- name: Pack Nuget Packages
shell: bash
run: |
if [ '${{ github.event.inputs.nuget-registry }}' == 'NuGet' ]; then
dotnet pack --property:NugetVersionSuffix=$NugetVersionSuffix -c Debug
dotnet pack --property:NugetVersionSuffix=$NugetVersionSuffix -c Release
else
dotnet pack --property:NugetVersionSuffix=$NugetVersionSuffix --property:Box2DPackPdb=true -c Debug
dotnet pack --property:NugetVersionSuffix=$NugetVersionSuffix --property:Box2DPackPdb=true -c Release
fi

- name: Upload Artifacts
if: matrix.os == 'ubuntu-latest'
uses: actions/upload-artifact@v3
with:
name: Nuget Packages
path: |
**/Box2D.NET.*.nupkg
**/Box2D.NET.*.snupkg

- name: Push NuGet Packages
if: matrix.os == 'ubuntu-latest' && github.event_name == 'workflow_dispatch' && github.event.inputs.push-packages == 'true'
shell: bash
run: |
if [ '${{ github.event.inputs.nuget-registry }}' == 'GitLab' ]; then
dotnet nuget push **/Box2D.NET.*.nupkg --api-key '${{ secrets.GITLAB_ACCESS_TOKEN }}' --source 'https://gitlab.com/api/v4/projects/53993416/packages/nuget/index.json'
elif [ '${{ github.event.inputs.nuget-registry }}' == 'NuGet' ]; then
dotnet nuget push **/Box2D.NET.*.nupkg --api-key '${{ secrets.NUGET_ACCESS_TOKEN }}' --source 'https://api.nuget.org/v3/index.json'
fi
- uses: actions/checkout@v4
with:
submodules: 'recursive'
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Setup Android SDK
uses: android-actions/setup-android@v3

- name: Set Version Suffix
shell: bash
run: |
if [ '${{ github.event.inputs.use-auto-generated-version }}' != 'false' ]; then
echo "BuildVersionSuffix=build.$(git rev-list --count HEAD)" >> $GITHUB_ENV
fi

- name: Set IOS SDK Variables
if: matrix.os == 'macos-latest'
shell: bash
run: |
echo "IOS_SDK_SYSROOT=$(xcrun --sdk iphoneos --show-sdk-path)" >> $GITHUB_ENV
echo "IOS_SIMULATOR_SDK_SYSROOT=$(xcrun --sdk iphonesimulator --show-sdk-path)" >> $GITHUB_ENV

- name: Set Android NDK Variables
if: matrix.os == 'macos-latest'
shell: bash
run: |
echo "ANDROID_NDK=$(find "$ANDROID_SDK_ROOT/ndk/" -mindepth 1 -maxdepth 1 -type d | sort | head -n 1)" >> $GITHUB_ENV
echo "ANDROID_NDK_SYSROOT=$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot" >> $GITHUB_ENV

- name: Restore Dependencies
shell: bash
run: dotnet restore

- name: Generate Bindings
shell: bash
run: dotnet run --project Box2D.NET.Bindgen

- name: Build Projects
shell: bash
run: |
dotnet build -c Debug
dotnet build -c Release

- name: Build Natives
shell: bash
if: matrix.os == 'macos-latest'
working-directory: Box2D.NET.Native
run: |
dotnet build -c Debug -r linux-x64
dotnet build -c Debug -r linux-x86
dotnet build -c Debug -r linux-arm64
dotnet build -c Debug -r osx-x64
dotnet build -c Debug -r osx-arm64
dotnet build -c Debug -r win-x64
dotnet build -c Debug -r win-x86
dotnet build -c Debug -r win-arm64
# dotnet build -c Debug -r browser-wasm
dotnet build -c Debug -r iossimulator-x64
dotnet build -c Debug -r iossimulator-arm64
dotnet build -c Debug -r ios-arm64
dotnet build -c Debug -r android-arm64
dotnet build -c Debug -r android-x64

dotnet build -c Release -r linux-x64
dotnet build -c Release -r linux-x86
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-x86
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
dotnet build -c Release -r android-arm64
dotnet build -c Release -r android-x64

- name: Run Tests
shell: bash
run: dotnet test -p:SkipNatives=true

- name: Pack Nuget Packages
shell: bash
run: |
dotnet pack --property:VersionSuffix=$BuildVersionSuffix -p:SkipNatives=true -p:BuildSelectorPackage=true

if [ '${{ github.event.inputs.nuget-registry }}' == 'NuGet' ]; then
dotnet pack --property:VersionSuffix=$BuildVersionSuffix -p:SkipNatives=true -c Debug
dotnet pack --property:VersionSuffix=$BuildVersionSuffix -p:SkipNatives=true -c Release
else
dotnet pack --property:VersionSuffix=$BuildVersionSuffix --property:PackPdb=true -p:SkipNatives=true -c Debug
dotnet pack --property:VersionSuffix=$BuildVersionSuffix --property:PackPdb=true -p:SkipNatives=true -c Release
fi

- name: Upload Artifacts
if: matrix.os == 'macos-latest'
uses: actions/upload-artifact@v4
with:
name: Nuget Packages
path: |
**/Box2D.NET.*.nupkg
**/Box2D.NET.*.snupkg

- name: Push NuGet Packages
if: >
matrix.os == 'macos-latest' &&
github.repository_owner == 'BeanCheeseBurrito' &&
(
(github.event_name == 'workflow_dispatch' && github.event.inputs.push-packages == 'true') ||
(github.event_name == 'push' && github.ref == 'refs/heads/main')
)
shell: bash
run: |
if [ '${{ github.event_name }}' == 'workflow_dispatch' ]; then
if [ '${{ github.event.inputs.nuget-registry }}' == 'NuGet' ]; then
dotnet nuget push **/Box2D.NET.*.nupkg --skip-duplicate --api-key '${{ secrets.NUGET_ACCESS_TOKEN }}' --source 'https://api.nuget.org/v3/index.json'
elif [ '${{ github.event.inputs.nuget-registry }}' == 'GitHub' ]; then
dotnet nuget push **/Box2D.NET.*.nupkg --skip-duplicate --api-key '${{ secrets.GITHUB_TOKEN }}' --source 'https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json'
fi
elif [ '${{ github.ref }}' == 'refs/heads/main' ]; then
dotnet nuget push **/Box2D.NET.*.nupkg --skip-duplicate --api-key '${{ secrets.GITHUB_TOKEN }}' --source 'https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json'
fi
21 changes: 19 additions & 2 deletions Box2D.NET.Bindgen/Box2D.NET.Bindgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ContinuousIntegrationBuild>false</ContinuousIntegrationBuild>
<Deterministic>false</Deterministic>
<IsPackable>false</IsPackable>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == ''">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bindgen.NET" Version="0.1.14"/>
<PackageReference Include="Bindgen.NET" Version="0.1.17"/>
<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>
56 changes: 22 additions & 34 deletions Box2D.NET.Bindgen/Program.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
using System.Runtime.CompilerServices;
using Bindgen.NET;

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
},
DllImportPath = "box2d",

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

OutputFile = GetOutputDirectory(),
InputFile = GetInputFile(),
TreatInputFileAsRawSourceCode = true,
InputFile = """
#include <box2d/base.h>
#include <box2d/box2d.h>
#include <box2d/collision.h>
#include <box2d/id.h>
#include <box2d/math_functions.h>
#include <box2d/types.h>
""",
OutputFile = GetOutputDirectory("B2.g.cs"),

GenerateDisableRuntimeMarshallingAttribute = true,

RemappedPrefixes =
{
Expand All @@ -55,11 +37,7 @@
("b2World_", "World"),
("b2_", ""),
("b2", ""),
},

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

BindingGenerator.Generate(options);
Expand All @@ -69,12 +47,22 @@ string GetCurrentFilePath([CallerFilePath] string filePath = "")
return filePath;
}

string GetNativeDirectory(string path)
string GetIncludeDirectory()
{
return GetCurrentFilePath() + "/../../native/box2d/" + path;
return Path.GetFullPath(GetCurrentFilePath() + "/../../native/box2d/include");
}

string GetOutputDirectory(string fileName)
string GetOutputDirectory()
{
return GetCurrentFilePath() + "/../../Box2D.NET.Bindings/" + fileName;
return GetCurrentFilePath() + "/../../Box2D.NET.Bindings/B2.g.cs";
}

// Generates an input file that includes all Box2D headers.
string GetInputFile()
{
IEnumerable<string> headerFiles = Directory
.EnumerateFiles(GetIncludeDirectory(), "*.h", SearchOption.AllDirectories)
.Select((string filePath) => $"#include <{Path.GetRelativePath(GetIncludeDirectory(), filePath)}>");

return string.Join('\n', headerFiles);
}
Loading