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

[Swift bindings] Add bindings experimental NuGet to the dotnet-experimental feed #2878

Merged
Merged
Show file tree
Hide file tree
Changes from 11 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
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,5 @@ launchSettings.json

# Testing artifacts
testing/
src/samples/**/Unsafe*.cs
src/samples/**/Swift.*.cs
src/samples/**/Library/*.cs
src/samples/
GeneratedBindings/
14 changes: 0 additions & 14 deletions SwiftBindings.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Swift.Runtime.Tests", "src\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Swift.Bindings.Integration.Tests", "src\Swift.Bindings\tests\IntegrationTests\Swift.Bindings.Integration.Tests.csproj", "{CF9035B8-57F0-49A2-B985-94B1F6389339}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Swift.Bindings.Framework.Tests", "src\Swift.Bindings\tests\FrameworkTests\Swift.Bindings.Framework.Tests.csproj", "{BCC05AA9-F43C-478F-B417-D7E4EFD29362}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -88,17 +86,5 @@ Global
{CF9035B8-57F0-49A2-B985-94B1F6389339}.Release|x64.Build.0 = Release|Any CPU
{CF9035B8-57F0-49A2-B985-94B1F6389339}.Release|x86.ActiveCfg = Release|Any CPU
{CF9035B8-57F0-49A2-B985-94B1F6389339}.Release|x86.Build.0 = Release|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Debug|x64.ActiveCfg = Debug|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Debug|x64.Build.0 = Debug|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Debug|x86.ActiveCfg = Debug|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Debug|x86.Build.0 = Debug|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Release|Any CPU.Build.0 = Release|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Release|x64.ActiveCfg = Release|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Release|x64.Build.0 = Release|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Release|x86.ActiveCfg = Release|Any CPU
{BCC05AA9-F43C-478F-B417-D7E4EFD29362}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions eng/pipelines/common/templates/build-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ parameters:
pool: {}
isOfficialBuild: false
runTests: true
bindings:
isBindingsBuild: false
scriptArgs: ''

jobs:

Expand Down Expand Up @@ -51,6 +54,10 @@ jobs:
/p:TargetPlatform=${{ parameters.archType }}
displayName: Build and Test

- ${{ if and(eq(parameters.bindings.isBindingsBuild, true), ne(parameters.osGroup, 'Windows_NT')) }}:
- script: ./generate.sh ${{ parameters.bindings.scriptArgs }}
displayName: Generate Bindings

- ${{ if eq(parameters.isOfficialBuild, true) }}:
- template: /eng/pipelines/common/upload-intermediate-artifacts-step.yml
parameters:
Expand Down
3 changes: 3 additions & 0 deletions eng/pipelines/runtimelab-official.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ extends:
name: Azure Pipelines
image: macos-latest
os: macOS
bindings:
isBindingsBuild: true
scriptArgs: '--platform MacOSX --arch arm64e-apple-macos --framework StoreKit --framework SwiftUI --experimental'

- ${{ if eq(variables.isOfficialBuild, true) }}:
- template: /eng/pipelines/common/templates/publish.yml
Expand Down
136 changes: 136 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env bash

usage()
{
echo "Common settings:"
echo " --platform <value> Platform: MacOSX, iPhoneOS, iPhoneSimulator, AppleTVOS, AppleTVSimulator"
echo " --arch <value> Architecture: arm64e-apple-macos, x86_64-apple-macos"
echo " --framework Framework to generate bindings for"
echo " --help Print help and exit (short: -h)"
echo ""

echo "Actions:"
echo " --experimental Generates only Runtime.Swift namespace when bindings for frameworks are not complete"
}

source="${BASH_SOURCE[0]}"

# resolve $SOURCE until the file is no longer a symlink
while [[ -h $source ]]; do
scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
source="$(readlink "$source")"

# if $source was a relative symlink, we need to resolve it relative to the path where the
# symlink file was located
[[ $source != /* ]] && source="$scriptroot/$source"
done

scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"

platform=''
arch=''
frameworks=()
experimental=false

output_dir="./GeneratedBindings"

while [[ $# > 0 ]]; do
opt="$(echo "${1/#--/-}" | tr "[:upper:]" "[:lower:]")"
case "$opt" in
-help|-h)
usage
exit 0
;;
-experimental)
experimental=true
;;
-platform)
platform=$2
shift
;;
-arch)
arch=$2
shift
;;
-framework)
frameworks+=("$2")
shift
;;
esac

shift
done

# Output directory for generated bindings
rm -rf "$output_dir"
mkdir -p "$output_dir"

cd "$output_dir"

# Function to extract ABI file
function ExtractABI {
local framework=$1

echo "Generating ABI for framework '$framework', platform '$platform', architecture '$arch'"

local sdk_path=$(xcrun -sdk $(echo "$platform" | tr '[:upper:]' '[:lower:]') --show-sdk-path)
local swift_interface_path="/Applications/Xcode.app/Contents/Developer/Platforms/${platform}.platform/Developer/SDKs/${platform}.sdk/System/Library/Frameworks/${framework}.framework/Versions/Current/Modules/${framework}.swiftmodule/${arch}.swiftinterface"

if [ ! -f "$swift_interface_path" ]; then
echo "Error: Swift interface file not found for framework '$framework'."
return 1
fi

xcrun swift-frontend -compile-module-from-interface "$swift_interface_path" \
-module-name "$framework" \
-sdk "$sdk_path" \
-emit-abi-descriptor-path "./${framework}.abi.json"
}

# Function to generate bindings
function InvokeProjectionTooling {
local framework=$1

$scriptroot/.dotnet/dotnet $scriptroot/artifacts/bin/Swift.Bindings/Release/net9.0/Swift.Bindings.dll -a "$framework" -o "./"

if $experimental; then
rm -rf "./Swift.$framework.cs"
fi
}

# Function to generate NuGet package
function PackNuGet {
local project_file="./Swift.Bindings.${platform}.Experimental.csproj"

cat <<EOL > "$project_file"
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
</PropertyGroup>
</Project>
EOL

$scriptroot/.dotnet/dotnet pack "$project_file"
}

function Generate {
for framework in "${frameworks[@]}"; do
echo "Processing framework: $framework"

if ExtractABI "$framework"; then
InvokeProjectionTooling "$framework"
else
echo "Skipping framework '$framework' due to errors."
fi
done

PackNuGet

echo "Process completed."
}

Generate
17 changes: 12 additions & 5 deletions src/Swift.Bindings/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,18 @@ public static void GenerateBindings(Queue<string> paths, string outputDirectory,
}

// Copy the Swift.Runtime library to the output directory
var libraryDirectory = Path.Combine(Directory.GetParent(outputDirectory)!.FullName, "Library");
Directory.CreateDirectory(libraryDirectory);
var dirInfo = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Library"));
foreach (var fileInfo in dirInfo.GetFiles())
fileInfo.CopyTo(Path.Combine(libraryDirectory, fileInfo.Name), true);
string[] sourceDirectories = { "Metadata", "ManualBindings" };
foreach (var sourceDir in sourceDirectories)
{
var destinationDirectory = Path.Combine(Directory.GetParent(outputDirectory)!.FullName, sourceDir);
Directory.CreateDirectory(destinationDirectory);

var sourceDirInfo = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, sourceDir));
foreach (var fileInfo in sourceDirInfo.GetFiles())
{
fileInfo.CopyTo(Path.Combine(destinationDirectory, fileInfo.Name), true);
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
</ImageAsset>
<ImageAsset Include="Assets.xcassets\TrailImage.imageset\Contents.json">
<Visible>false</Visible>
</ImageAsset>
</ImageAsset>
</ItemGroup>

<!-- Reference Swift.Bindings.MacOSX.Experimental from dotnet-experimental feed -->
<!-- <ItemGroup>
<PackageReference Include="Swift.Bindings.MacOSX.Experimental" />
</ItemGroup> -->

<!-- Notes about the values set below:
1. For macOS it's possible to sign both the app bundle (EnableCodesigning=true) and the package (*.pkg) (EnablePackageSigning=true),
and these are signed separately and with different certificates.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<!-- Compile generated bindings with tests -->
<ItemGroup>
<Compile Include="$(OutputPath)/**/*.cs" Exclude="$(OutputPath)/Library/*.cs;$(OutputPath)/Metadata/*.cs" />
<Compile Include="$(OutputPath)/**/*.cs" Exclude="$(OutputPath)/Metadata/*.cs;$(OutputPath)/ManualBindings/*.cs" />
</ItemGroup>
</Target>
</Project>
Expand Down
4 changes: 2 additions & 2 deletions src/Swift.Runtime/src/Swift.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<Content Include="TypeDatabase.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Library/*.cs">
<Content Include="Metadata/*.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Metadata/*.cs">
<Content Include="ManualBindings/*.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Expand Down