@@ -74,22 +74,58 @@ public final class SwiftTargetBuildDescription {
74
74
self . target. sources. paths + self . derivedSources. paths + self . pluginDerivedSources. paths
75
75
}
76
76
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
+
77
83
/// The list of all resource files in the target, including the derived ones.
78
84
public var resources : [ Resource ] {
79
85
self . target. underlyingTarget. resources + self . pluginDerivedResources
80
86
}
81
87
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.
83
101
public var objects : [ AbsolutePath ] {
84
102
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 {
88
115
try AbsolutePath ( validating: " \( $0. pathString) .o " , relativeTo: tempsPath)
89
116
}
90
117
}
91
118
}
92
119
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
+
93
129
/// The path to the swiftmodule file after compilation.
94
130
var moduleOutputPath : AbsolutePath {
95
131
// 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 {
729
765
730
766
731
767
// 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
736
771
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]
738
776
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 " )
740
780
741
781
content +=
742
782
#"""
@@ -745,7 +785,7 @@ public final class SwiftTargetBuildDescription {
745
785
"""#
746
786
747
787
if !self . buildParameters. useWholeModuleOptimization {
748
- let depsPath = objectDir . appending ( component: sourceFileName + " .d " )
788
+ let depsPath = self . tempsPath . appending ( component: sourceFileName + " .d " )
749
789
content +=
750
790
#"""
751
791
"dependencies": " \#( depsPath. _nativePathString ( escaped: true ) ) ",
@@ -755,12 +795,10 @@ public final class SwiftTargetBuildDescription {
755
795
}
756
796
757
797
758
- let partialModulePath = objectDir. appending ( component: sourceFileName + " ~partial.swiftmodule " )
759
-
760
798
content +=
761
799
#"""
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 ) ) ",
764
802
"swiftmodule": " \#( partialModulePath. _nativePathString ( escaped: true ) ) ",
765
803
"swift-dependencies": " \#( swiftDepsPath. _nativePathString ( escaped: true ) ) "
766
804
} \#( ( idx + 1 ) < sources. count ? " , " : " " )
0 commit comments