Skip to content

Commit

Permalink
Use mscorlib.winmd by default (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Jan 8, 2025
1 parent 5e32b48 commit 38a63d0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ jobs:
shell: pwsh
run: swift build --verbose --build-tests

- name: Build mscorlib.winmd
working-directory: Generator
shell: pwsh
run: .\SPMPostBuild.ps1 -Config debug

- name: Test code generator
working-directory: Generator
shell: pwsh
Expand All @@ -49,8 +54,7 @@ jobs:
- name: Build WinRTComponent
working-directory: InteropTests
shell: pwsh
run: |
& .\SPMPrebuild.ps1 -SwiftWinRT "$Env:GITHUB_WORKSPACE\Generator\.build\debug\SwiftWinRT.exe"
run: .\SPMPrebuild.ps1 -SwiftWinRT "$Env:GITHUB_WORKSPACE\Generator\.build\debug\SwiftWinRT.exe"

- name: Build InteropTests
working-directory: InteropTests
Expand Down
2 changes: 1 addition & 1 deletion Generator/Dependencies/swift-dotnetmetadata/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FetchContent_Declare(
swift-dotnetmetadata
GIT_REPOSITORY https://github.com/tristanlabelle/swift-dotnetmetadata.git
GIT_TAG 20790178c573dcc2238fa2af8680c0e6f23f7c5e
GIT_TAG b4870688be31d889ed51da9beac466ede9f366dd
SOURCE_SUBDIR "don't use cmakelists")
FetchContent_MakeAvailable(swift-dotnetmetadata)

Expand Down
2 changes: 1 addition & 1 deletion Generator/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"location" : "https://github.com/tristanlabelle/swift-dotnetmetadata",
"state" : {
"branch" : "main",
"revision" : "20790178c573dcc2238fa2af8680c0e6f23f7c5e"
"revision" : "b4870688be31d889ed51da9beac466ede9f366dd"
}
}
],
Expand Down
26 changes: 26 additions & 0 deletions Generator/SPMPostBuild.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
.SYNOPSIS
Builds mscorlib.winmd and locates it next to SwiftWinRT.exe.
.PARAMETER Config
The build configuration that was used to build SwiftWinRT.exe.
#>
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory = $true)]
[string] $Config
)

$TargetTripleArch = switch ($Env:PROCESSOR_ARCHITECTURE) {
"amd64" { "x86_64" }
"arm64" { "aarch64" }
"x86" { "i686" }
default { throw "Unsupported architecture: $Env:PROCESSOR_ARCHITECTURE" }
}

$BinaryDir = "$PSScriptRoot\.build\$TargetTripleArch-unknown-windows-msvc\$Config"
if (-not (Test-Path $BinaryDir)) {
throw "The binary directory does not exist: $BinaryDir"
}

& "$PSScriptRoot\.build\checkouts\swift-dotnetmetadata\WindowsMetadataCoreLibrary\Assemble.ps1" -OutputPath "$BinaryDir\mscorlib.winmd"
3 changes: 3 additions & 0 deletions Generator/Sources/SwiftWinRT/CommandLineArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ struct CommandLineArguments: ParsableCommand {
abstract: "A WinRT projections generator for Swift.")
}

@Option(name: .customLong("mscorlib"), help: .init("A path to the mscorlib.winmd/dll to use. Defaults to looking for mscorlib.winmd next to the exe.", valueName: "path"))
var mscorlibPath: String? = nil

@Option(name: .customLong("reference"), help: .init("A path to a .winmd file with the APIs to project.", valueName: "file"))
var references: [String] = []

Expand Down
14 changes: 9 additions & 5 deletions Generator/Sources/SwiftWinRT/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ do {
let spmOptions = SPMOptions(commandLineArguments: commandLineArguments, projectionConfig: projectionConfig)
let cmakeOptions = CMakeOptions(commandLineArguments: commandLineArguments, projectionConfig: projectionConfig)

// Resolve the mscorlib dependency from the .NET Framework 4 machine installation
let context = WinMDLoadContext()
guard let mscorlibPath = SystemAssemblies.DotNetFramework4.mscorlibPath else {
throw AssemblyLoadError.notFound(message: "mscorlib was not found on the machine.")
}
_ = try context.load(path: mscorlibPath)
_ = try context.load(path: commandLineArguments.mscorlibPath ?? getDefaultMscorlibPath())

let projection = try createProjection(
commandLineArguments: commandLineArguments,
Expand All @@ -52,4 +48,12 @@ catch let error {
print(error)
fflush(stdout)
throw error
}

func getDefaultMscorlibPath() -> String {
Bundle.main.executableURL!
.deletingLastPathComponent()
.appendingPathComponent("mscorlib.winmd", isDirectory: false)
.path
.replacingOccurrences(of: "/", with: "\\")
}
9 changes: 6 additions & 3 deletions InteropTests/SPMPrebuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@ $ErrorActionPreference = "Stop"

if (-not $SwiftWinRT) {
Write-Host -ForegroundColor Cyan "Building SwiftWinRT.exe with SPM..."
$SwiftConfiguration = "debug"
$BuildConfig = "debug"
$RepoRoot = (& git.exe -C "$PSScriptRoot" rev-parse --path-format=absolute --show-toplevel).Trim()
$GeneratorProjectDir = "$RepoRoot\Generator"

& swift.exe build `
--package-path $GeneratorProjectDir `
--configuration $SwiftConfiguration `
--configuration $BuildConfig `
--build-path "$GeneratorProjectDir\.build"
if ($LASTEXITCODE -ne 0) { throw "Failed to build SwiftWinRT.exe" }

& "$GeneratorProjectDir\SPMPostBuild.ps1" -Config $BuildConfig

$TargetTripleArch = switch ($Env:PROCESSOR_ARCHITECTURE) {
"amd64" { "x86_64" }
"arm64" { "aarch64" }
"x86" { "i686" }
default { throw "Unsupported architecture: $Env:PROCESSOR_ARCHITECTURE" }
}
$SwiftWinRT = "$GeneratorProjectDir\.build\$TargetTripleArch-unknown-windows-msvc\$SwiftConfiguration\SwiftWinRT.exe"

$SwiftWinRT = "$GeneratorProjectDir\.build\$TargetTripleArch-unknown-windows-msvc\$BuildConfig\SwiftWinRT.exe"
}
else {
$SwiftWinRT = [IO.Path]::GetFullPath($SwiftWinRT)
Expand Down

0 comments on commit 38a63d0

Please sign in to comment.