Skip to content

Commit

Permalink
Make generated ABI modules depend on WindowsRuntime_ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Jul 24, 2024
1 parent 4b1c553 commit 83f75fb
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 86 deletions.
52 changes: 0 additions & 52 deletions Generator/Sources/ProjectionModel/CAbi/CAbi+writeCoreHeader.swift

This file was deleted.

1 change: 1 addition & 0 deletions Generator/Sources/ProjectionModel/CAbi/CAbi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum CAbi {
}
}

public static var moduleSuffix: String { "_ABI" }
public static var namespacingPrefix: String { "SWRT_" }
public static var hresultName: String { namespacingPrefix + "HResult" }
public static var guidName: String { namespacingPrefix + "Guid" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension SwiftProjection {
internal init(projection: SwiftProjection, name: String, flattenNamespaces: Bool = false) {
self.projection = projection
self.name = name
self.abiModuleName = name + "_ABI"
self.abiModuleName = name + CAbi.moduleSuffix
self.flattenNamespaces = flattenNamespaces
}

Expand Down
4 changes: 2 additions & 2 deletions Generator/Sources/SwiftWinRT/Writing/ABIModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import WindowsMetadata

internal func writeABIModule(_ module: SwiftProjection.Module, toPath directoryPath: String) throws {
let includeDirectoryPath = "\(directoryPath)\\include"
CAbi.writeCoreHeader(to: FileTextOutputStream(path: "\(includeDirectoryPath)\\Core.h", directoryCreation: .ancestors))
try writeABIFile(module: module, toPath: "\(includeDirectoryPath)\\ABI.h")
// FIXME: Support transitive references?
for referencedModule in module.references {
Expand All @@ -19,7 +18,8 @@ fileprivate func writeABIFile(module: SwiftProjection.Module, toPath path: Strin
let cHeaderWriter = CSourceFileWriter(output: FileTextOutputStream(path: path, directoryCreation: .ancestors))

// Write includes
cHeaderWriter.writeInclude(pathSpec: "Core.h", kind: .doubleQuotes)
cHeaderWriter.writeInclude(pathSpec: "WindowsRuntime.h", kind: .doubleQuotes)

for referencedModule in module.references {
guard !referencedModule.isEmpty else { continue }
cHeaderWriter.writeInclude(pathSpec: "\(referencedModule.name).h", kind: .doubleQuotes)
Expand Down
39 changes: 21 additions & 18 deletions Generator/Sources/SwiftWinRT/Writing/SwiftPackageFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,17 @@ import struct Foundation.URL

func writeSwiftPackageFile(_ projection: SwiftProjection, supportPackageLocation: String, toPath path: String) {
var package = SwiftPackage(name: "Projection")

if supportPackageLocation.starts(with: "https://") {
if let separatorIndex = supportPackageLocation.lastIndex(of: ":"),
let lastSlashIndex = supportPackageLocation.lastIndex(of: "/"),
separatorIndex > lastSlashIndex {
let url = String(supportPackageLocation[..<separatorIndex])
let branch = String(supportPackageLocation[supportPackageLocation.index(after: separatorIndex)...])
package.dependencies.append(.package(url: url, branch: branch))
}
else {
fatalError("Unexpected support package location format: \(supportPackageLocation)")
}
} else {
package.dependencies.append(.package(path: supportPackageLocation))
}
package.dependencies.append(getSupportPackageDependency(location: supportPackageLocation))

var productTargets = [String]()

for module in projection.modulesByName.values {
guard !module.isEmpty else { continue }

// ABI module
package.targets.append(.target(
name: module.abiModuleName,
path: "\(module.name)/ABI"))
var abiModuleTarget = SwiftPackage.Target(name: module.abiModuleName, path: "\(module.name)/ABI")
abiModuleTarget.dependencies.append(.product(name: "WindowsRuntime_ABI", package: "swift-winrt"))
package.targets.append(abiModuleTarget)

// Assembly module
var assemblyModuleTarget = SwiftPackage.Target(name: module.name)
Expand Down Expand Up @@ -72,4 +58,21 @@ func writeSwiftPackageFile(_ projection: SwiftProjection, supportPackageLocation
package.products.append(.library(name: "Projection", targets: productTargets))

package.write(version: "5.10", to: FileTextOutputStream(path: path, directoryCreation: .ancestors))
}

fileprivate func getSupportPackageDependency(location: String) -> SwiftPackage.Dependency {
if location.starts(with: "https://") {
if let separatorIndex = location.lastIndex(of: ":"),
let lastSlashIndex = location.lastIndex(of: "/"),
separatorIndex > lastSlashIndex {
let url = String(location[..<separatorIndex])
let branch = String(location[location.index(after: separatorIndex)...])
return .package(url: url, branch: branch)
}
else {
fatalError("Unexpected support package location format: \(location)")
}
} else {
return .package(path: location)
}
}
10 changes: 9 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PackageDescription
let package = Package(
name: "SwiftWinRT",
products: [
.library(
name: "WindowsRuntime_ABI",
targets: ["COM_ABI", "WindowsRuntime_ABI"]),
.library(
name: "WindowsRuntime",
targets: ["COM", "WindowsRuntime"]),
Expand All @@ -23,9 +26,14 @@ let package = Package(
dependencies: ["COM_ABI"],
path: "Support/Sources/WindowsRuntime_ABI",
exclude: ["CMakeLists.txt"]),
.target(
name: "WindowsFoundation_ABI",
dependencies: ["COM_ABI", "WindowsRuntime_ABI"],
path: "Support/Sources/WindowsFoundation_ABI",
exclude: ["CMakeLists.txt"]),
.target(
name: "WindowsRuntime",
dependencies: ["WindowsRuntime_ABI", "COM"],
dependencies: ["COM", "WindowsRuntime_ABI", "WindowsFoundation_ABI"],
path: "Support/Sources/WindowsRuntime",
exclude: ["CMakeLists.txt"]),
.testTarget(
Expand Down
3 changes: 3 additions & 0 deletions Support/Sources/WindowsFoundation_ABI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(WindowsFoundation_ABI INTERFACE)
target_include_directories(WindowsFoundation_ABI PUBLIC include)
target_link_libraries(WindowsFoundation_ABI PUBLIC COM_ABI WindowsRuntime_ABI)
Loading

0 comments on commit 83f75fb

Please sign in to comment.