Skip to content
Closed
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
42 changes: 12 additions & 30 deletions Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ public final class SwiftTargetBuildDescription {
let fileSystem: FileSystem

/// The modulemap file for this target, if any.
private(set) var moduleMap: AbsolutePath?
var moduleMap: AbsolutePath? {
if self.shouldGenerateModuleMap {
return self.tempsPath.appending(component: moduleMapFilename)
} else {
return nil
}
}

/// The results of applying any build tool plugins to this target.
public let buildToolPluginInvocationResults: [BuildToolPluginInvocationResult]
Expand All @@ -237,6 +243,11 @@ public final class SwiftTargetBuildDescription {
/// Whether or not to generate code for test observation.
private let shouldGenerateTestObservation: Bool

/// Whether or not to generate a module map for this target.
var shouldGenerateModuleMap: Bool {
return self.shouldEmitObjCCompatibilityHeader
}

/// Create a new target description with target and build parameters.
init(
package: ResolvedPackage,
Expand Down Expand Up @@ -286,10 +297,6 @@ public final class SwiftTargetBuildDescription {
observabilityScope: observabilityScope
)

if self.shouldEmitObjCCompatibilityHeader {
self.moduleMap = try self.generateModuleMap()
}

// Do nothing if we're not generating a bundle.
if self.bundlePath != nil {
try self.generateResourceAccessor()
Expand Down Expand Up @@ -723,31 +730,6 @@ public final class SwiftTargetBuildDescription {
return path
}

/// Generates the module map for the Swift target and returns its path.
private func generateModuleMap() throws -> AbsolutePath {
let path = self.tempsPath.appending(component: moduleMapFilename)

let bytes = ByteString(
#"""
module \#(self.target.c99name) {
header "\#(self.objCompatibilityHeaderPath.pathString)"
requires objc
}

"""#.utf8
)

// Return early if the contents are identical.
if self.fileSystem.isFile(path), try self.fileSystem.readFileContents(path) == bytes {
return path
}

try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
try self.fileSystem.writeFileContents(path, bytes: bytes)

return path
}

/// Returns the path to the ObjC compatibility header for this Swift target.
var objCompatibilityHeaderPath: AbsolutePath {
self.tempsPath.appending("\(self.target.name)-Swift.h")
Expand Down
20 changes: 19 additions & 1 deletion Sources/Build/BuildManifest/LLBuildManifestBuilder+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,26 @@ extension LLBuildManifestBuilder {
func createSwiftCompileCommand(
_ target: SwiftTargetBuildDescription
) throws {
var modulesReadyInputs = [Node]()

// If the given target needs a generated module map, set up the dependency and required task to write out the module map.
if target.shouldGenerateModuleMap, let moduleMapPath = target.moduleMap {
modulesReadyInputs.append(.file(moduleMapPath))

self.manifest.addWriteSwiftModuleMapCommand(
moduleName: target.target.c99name,
objCompatibilityHeaderPath: target.objCompatibilityHeaderPath,
outputPath: moduleMapPath
)
}

let modulesReady = self.addPhonyCommand(
targetName: target.target.getLLBuildModulesReadyCmdName(config: self.buildConfig),
inputs: modulesReadyInputs
)

// Inputs.
let inputs = try self.computeSwiftCompileCmdInputs(target)
let inputs = try self.computeSwiftCompileCmdInputs(target) + [modulesReady]

// Outputs.
let objectNodes = try target.objects.map(Node.file)
Expand Down
48 changes: 48 additions & 0 deletions Sources/LLBuildManifest/LLBuildManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum WriteAuxiliary {
LinkFileList.self,
SourcesFileList.self,
SwiftGetVersion.self,
SwiftModuleMap.self,
XCTestInfoPlist.self
]

Expand Down Expand Up @@ -56,6 +57,39 @@ public enum WriteAuxiliary {
}
}

public struct SwiftModuleMap: AuxiliaryFileType {
public static let name = "swift-modulemap"

public static func computeInputs(
moduleName: String,
objCompatibilityHeaderPath: AbsolutePath
) -> [Node] {
return [
.virtual(Self.name),
.virtual(moduleName),
// this can't be a path since we don't create any rule to generate this file, it's a byproduct of the compilation
.virtual(objCompatibilityHeaderPath.pathString)
]
}

public static func getFileContents(inputs: [Node]) throws -> String {
guard inputs.count == 2 else {
throw StringError("invalid module map generation task, inputs: \(inputs)")
}

let moduleName = inputs[0].extractedVirtualNodeName
let objCompatibilityHeaderPath = try AbsolutePath(validating: inputs[1].extractedVirtualNodeName)

return #"""
module \#(moduleName) {
header "\#(objCompatibilityHeaderPath.pathString)"
requires objc
}

"""#
}
}

public struct ClangModuleMap: AuxiliaryFileType {
public static let name = "modulemap"

Expand Down Expand Up @@ -334,6 +368,20 @@ public struct LLBuildManifest {
commands[name] = Command(name: name, tool: tool)
}

public mutating func addWriteSwiftModuleMapCommand(
moduleName: String,
objCompatibilityHeaderPath: AbsolutePath,
outputPath: AbsolutePath
) {
let inputs = WriteAuxiliary.SwiftModuleMap.computeInputs(
moduleName: moduleName,
objCompatibilityHeaderPath: objCompatibilityHeaderPath
)
let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: outputPath)
let name = outputPath.pathString
commands[name] = Command(name: name, tool: tool)
}

public mutating func addWriteClangModuleMapCommand(
targetName: String,
moduleName: String,
Expand Down
6 changes: 3 additions & 3 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ final class BuildPlanTests: XCTestCase {
let contents: String = try fs.readFileContents(yaml)
let swiftGetVersionFilePath = try XCTUnwrap(llbuild.swiftGetVersionFiles.first?.value)
XCTAssertMatch(contents, .contains("""
inputs: ["\(Pkg.appending(components: "Sources", "exe", "main.swift").escapedPathString)","\(swiftGetVersionFilePath.escapedPathString)","\(buildPath.appending(components: "PkgLib.swiftmodule").escapedPathString)","\(buildPath.appending(components: "exe.build", "sources").escapedPathString)"]
inputs: ["\(Pkg.appending(components: "Sources", "exe", "main.swift").escapedPathString)","\(swiftGetVersionFilePath.escapedPathString)","\(buildPath.appending(components: "PkgLib.swiftmodule").escapedPathString)","<exe-release.modules-ready>","\(buildPath.appending(components: "exe.build", "sources").escapedPathString)"]
"""))

}
Expand Down Expand Up @@ -904,7 +904,7 @@ final class BuildPlanTests: XCTestCase {
let buildPath = plan.buildParameters.dataPath.appending(components: "debug")
let swiftGetVersionFilePath = try XCTUnwrap(llbuild.swiftGetVersionFiles.first?.value)
XCTAssertMatch(contents, .contains("""
inputs: ["\(Pkg.appending(components: "Sources", "exe", "main.swift").escapedPathString)","\(swiftGetVersionFilePath.escapedPathString)","\(buildPath.appending(components: "exe.build", "sources").escapedPathString)"]
inputs: ["\(Pkg.appending(components: "Sources", "exe", "main.swift").escapedPathString)","\(swiftGetVersionFilePath.escapedPathString)","<exe-debug.modules-ready>","\(buildPath.appending(components: "exe.build", "sources").escapedPathString)"]
"""))
}
}
Expand Down Expand Up @@ -3972,7 +3972,7 @@ final class BuildPlanTests: XCTestCase {
let suffix = ""
#endif
XCTAssertMatch(contents, .contains("""
inputs: ["\(PkgA.appending(components: "Sources", "swiftlib", "lib.swift").escapedPathString)","\(swiftGetVersionFilePath.escapedPathString)","\(buildPath.appending(components: "exe\(suffix)").escapedPathString)","\(buildPath.appending(components: "swiftlib.build", "sources").escapedPathString)"]
inputs: ["\(PkgA.appending(components: "Sources", "swiftlib", "lib.swift").escapedPathString)","\(swiftGetVersionFilePath.escapedPathString)","\(buildPath.appending(components: "exe\(suffix)").escapedPathString)","<swiftlib-debug.modules-ready>","\(buildPath.appending(components: "swiftlib.build", "sources").escapedPathString)"]
outputs: ["\(buildPath.appending(components: "swiftlib.build", "lib.swift.o").escapedPathString)","\(buildPath.escapedPathString)
"""))
}
Expand Down