Skip to content

Commit bd7b07c

Browse files
committed
a more wholistic solution for your viewing pleasure
1 parent a8a3dd8 commit bd7b07c

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,58 @@ public final class SwiftTargetBuildDescription {
7474
self.target.sources.paths + self.derivedSources.paths + self.pluginDerivedSources.paths
7575
}
7676

77+
/// The list of all source files in the target, including the derived ones.
78+
private var relativeSources: [RelativePath] {
79+
self.target.sources.relativePaths + self.derivedSources.relativePaths + self
80+
.pluginDerivedSources.relativePaths
81+
}
82+
7783
/// The list of all resource files in the target, including the derived ones.
7884
public var resources: [Resource] {
7985
self.target.underlyingTarget.resources + self.pluginDerivedResources
8086
}
8187

82-
/// The objects in this target.
88+
private var producesBitcodeObjects: Bool {
89+
get throws {
90+
try self.compileArguments().contains {
91+
// Using -lto=x will result in bitcode generation.
92+
$0.hasPrefix("-lto=")
93+
// Using -embed-bitcode will result in bitcode generation.
94+
|| $0 == "-embed-bitcode"
95+
}
96+
}
97+
}
98+
99+
/// The objects in this target, containing either machine code or bitcode
100+
/// depending on the compiler flags used.
83101
public var objects: [AbsolutePath] {
84102
get throws {
85-
let relativePaths = self.target.sources.relativePaths + self.derivedSources.relativePaths + self
86-
.pluginDerivedSources.relativePaths
87-
return try relativePaths.map {
103+
// Prefer bitcode objects over machine code objects if they are
104+
// produced.
105+
try self.producesBitcodeObjects
106+
? self.bitcodeObjects
107+
: self.machineCodeObjects
108+
}
109+
}
110+
111+
/// The machine code objects in this target.
112+
private var machineCodeObjects: [AbsolutePath] {
113+
get throws {
114+
try self.relativeSources.map {
88115
try AbsolutePath(validating: "\($0.pathString).o", relativeTo: tempsPath)
89116
}
90117
}
91118
}
92119

120+
/// The bitcode objects in this target.
121+
private var bitcodeObjects: [AbsolutePath] {
122+
get throws {
123+
try self.relativeSources.map {
124+
try AbsolutePath(validating: "\($0.pathString).bc", relativeTo: tempsPath)
125+
}
126+
}
127+
}
128+
93129
/// The path to the swiftmodule file after compilation.
94130
var moduleOutputPath: AbsolutePath {
95131
// If we're an executable and we're not allowing test targets to link against us, we hide the module.
@@ -729,14 +765,18 @@ public final class SwiftTargetBuildDescription {
729765

730766

731767
// Write out the entries for each source file.
732-
let sources = self.target.sources.paths + self.derivedSources.paths + self.pluginDerivedSources.paths
733-
for (idx, source) in sources.enumerated() {
734-
let object = try objects[idx]
735-
let objectDir = object.parentDirectory
768+
let sources = self.sources
769+
let machineCodeObjects = try self.machineCodeObjects
770+
let bitcodeObjects = try self.bitcodeObjects
736771

737-
let sourceFileName = source.basenameWithoutExt
772+
for idx in 0..<sources.count {
773+
let source = sources[idx]
774+
let machineCodeObject = machineCodeObjects[idx]
775+
let bitcodeObject = bitcodeObjects[idx]
738776

739-
let swiftDepsPath = objectDir.appending(component: sourceFileName + ".swiftdeps")
777+
let sourceFileName = source.basenameWithoutExt
778+
let partialModulePath = self.tempsPath.appending(component: sourceFileName + "~partial.swiftmodule")
779+
let swiftDepsPath = self.tempsPath.appending(component: sourceFileName + ".swiftdeps")
740780

741781
content +=
742782
#"""
@@ -745,7 +785,7 @@ public final class SwiftTargetBuildDescription {
745785
"""#
746786

747787
if !self.buildParameters.useWholeModuleOptimization {
748-
let depsPath = objectDir.appending(component: sourceFileName + ".d")
788+
let depsPath = self.tempsPath.appending(component: sourceFileName + ".d")
749789
content +=
750790
#"""
751791
"dependencies": "\#(depsPath._nativePathString(escaped: true))",
@@ -755,12 +795,10 @@ public final class SwiftTargetBuildDescription {
755795
}
756796

757797

758-
let partialModulePath = objectDir.appending(component: sourceFileName + "~partial.swiftmodule")
759-
760798
content +=
761799
#"""
762-
"llvm-bc": "\#(object._nativePathString(escaped: true))",
763-
"object": "\#(object._nativePathString(escaped: true))",
800+
"object": "\#(machineCodeObject._nativePathString(escaped: true))",
801+
"llvm-bc": "\#(bitcodeObject._nativePathString(escaped: true))",
764802
"swiftmodule": "\#(partialModulePath._nativePathString(escaped: true))",
765803
"swift-dependencies": "\#(swiftDepsPath._nativePathString(escaped: true))"
766804
}\#((idx + 1) < sources.count ? "," : "")

0 commit comments

Comments
 (0)