Skip to content

Commit

Permalink
Make generated ABI modules depend on one another (#299)
Browse files Browse the repository at this point in the history
And rename "Module" to "Projection"
  • Loading branch information
tristanlabelle authored Sep 17, 2024
1 parent acccf55 commit 7473c2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
20 changes: 13 additions & 7 deletions Generator/Sources/SwiftWinRT/Writing/ABIModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import WindowsMetadata

internal func writeABIModule(_ module: Module, directoryPath: String, generateCMakeLists: Bool) throws {
let includeDirectoryPath = "\(directoryPath)\\include"
let includeSWRTDirectoryPath = "\(includeDirectoryPath)\\SWRT"

try writeABIFile(module: module, toPath: "\(includeDirectoryPath)\\\(module.name).h")
// FIXME: Support transitive references?
for referencedModule in module.references {
guard !referencedModule.isEmpty else { continue }
try writeABIFile(module: referencedModule, toPath: "\(includeDirectoryPath)\\\(referencedModule.name).h")
}
try writeABIFile(module: module, toPath: "\(includeSWRTDirectoryPath)\\\(module.name).h")

try writeModulemapFile(module: module, toPath: "\(includeDirectoryPath)\\module.modulemap")

if generateCMakeLists {
let cmakeListsWriter = CMakeListsWriter(output: FileTextOutputStream(
Expand All @@ -32,7 +30,7 @@ fileprivate func writeABIFile(module: Module, toPath path: String) throws {

for referencedModule in module.references {
guard !referencedModule.isEmpty else { continue }
cHeaderWriter.writeInclude(pathSpec: "\(referencedModule.name).h", kind: .doubleQuotes)
cHeaderWriter.writeInclude(pathSpec: "SWRT/\(referencedModule.name).h", kind: .doubleQuotes)
}

// Declare enums
Expand Down Expand Up @@ -130,3 +128,11 @@ fileprivate func getSortedInterfaces(module: Module) throws -> [BoundType] {
interfacesByMangledName.sort { $0.key < $1.key }
return Array(interfacesByMangledName.values)
}

fileprivate func writeModulemapFile(module: Module, toPath path: String) throws {
let output = IndentedTextOutputStream(inner: FileTextOutputStream(path: path, directoryCreation: .ancestors))
output.writeIndentedBlock(header: "module \(module.abiModuleName) {", footer: "}") {
output.writeFullLine("header \"SWRT/\(module.name).h\"")
output.writeFullLine("export *")
}
}
15 changes: 8 additions & 7 deletions Generator/Sources/SwiftWinRT/Writing/SwiftPackageFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ func writeSwiftPackageFile(
package.targets.append(abiModuleTarget)

// Swift module
var swiftModuleTarget: SwiftPackage.Target = .target(name: module.name)
swiftModuleTarget.path = "\(module.name)/Module"
swiftModuleTarget.dependencies.append(.product(name: "WindowsRuntime", package: "swift-winrt"))
var projectionModuleTarget: SwiftPackage.Target = .target(name: module.name)
projectionModuleTarget.path = "\(module.name)/Projection"
projectionModuleTarget.dependencies.append(.product(name: "WindowsRuntime", package: "swift-winrt"))

for referencedModule in module.references {
guard !referencedModule.isEmpty else { continue }
swiftModuleTarget.dependencies.append(.target(name: referencedModule.name))
abiModuleTarget.dependencies.append(.target(name: referencedModule.abiModuleName))
projectionModuleTarget.dependencies.append(.target(name: referencedModule.name))
}

swiftModuleTarget.dependencies.append(.target(name: module.abiModuleName))
projectionModuleTarget.dependencies.append(.target(name: module.abiModuleName))

package.targets.append(swiftModuleTarget)
package.targets.append(projectionModuleTarget)

// Define a product for the module
var moduleProduct: SwiftPackage.Product = .library(name: module.name, type: dynamicLibraries ? .dynamic : nil, targets: [])
moduleProduct.targets.append(swiftModuleTarget.name)
moduleProduct.targets.append(projectionModuleTarget.name)
moduleProduct.targets.append(abiModuleTarget.name)

// Namespace modules
Expand Down
4 changes: 2 additions & 2 deletions Generator/Sources/SwiftWinRT/writeProjectionFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fileprivate func writeModuleFiles(
try writeABIModule(module, directoryPath: "\(directoryPath)\\ABI", generateCMakeLists: generateCMakeLists)

try writeSwiftModuleFiles(
module, directoryPath: "\(directoryPath)\\Module",
module, directoryPath: "\(directoryPath)\\Projection",
generateCMakeLists: generateCMakeLists, dynamicLibrary: dynamicLibrary)

if !module.flattenNamespaces {
Expand All @@ -51,7 +51,7 @@ fileprivate func writeModuleFiles(
path: "\(directoryPath)\\CMakeLists.txt",
directoryCreation: .ancestors))
writer.writeAddSubdirectory("ABI")
writer.writeAddSubdirectory("Module")
writer.writeAddSubdirectory("Projection")
if !module.flattenNamespaces {
writer.writeAddSubdirectory("Namespaces")
}
Expand Down

0 comments on commit 7473c2c

Please sign in to comment.