Skip to content

Commit 74bc85d

Browse files
authored
rdar://143411533 (Object files and module wrap output end up in the same location, stomping on each other) (#31)
On non macho-o platforms, swift-driver will create an additional module-wrap task if debugging is enabled which has an object file as output that will be named like the module and placed next to it. Since Swift Build will emit the Swift module next to the object files of a target, there can be a naming conflict, leading to us only ending up with the module-wrap content and the actual object file being stomped on. This introduces a new `PER_ARCH_MODULE_FILE_DIR` build setting that's the same as `PER_ARCH_OBJECT_FILE_DIR` by default that allows us to customize the output directory.
1 parent 80a2ae2 commit 74bc85d

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

Sources/SWBApplePlatform/Plugin.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
public import SWBUtil
1414
import SWBCore
15+
import SWBMacro
16+
import SWBProtocol
1517
import Foundation
1618
import SWBTaskConstruction
1719

@@ -23,6 +25,7 @@ import SWBTaskConstruction
2325
manager.register(XCStringsInputFileGroupingStrategyExtension(), type: InputFileGroupingStrategyExtensionPoint.self)
2426
manager.register(TaskProducersExtension(), type: TaskProducerExtensionPoint.self)
2527
manager.register(MacCatalystInfoExtension(), type: SDKVariantInfoExtensionPoint.self)
28+
manager.register(AppleSettingsBuilderExtension(), type: SettingsBuilderExtensionPoint.self)
2629
}
2730

2831
struct TaskProducersExtension: TaskProducerExtension {
@@ -158,3 +161,26 @@ struct XCStringsInputFileGroupingStrategyExtension: InputFileGroupingStrategyExt
158161
return ["xcstrings": Factory()]
159162
}
160163
}
164+
165+
struct AppleSettingsBuilderExtension: SettingsBuilderExtension {
166+
func addSDKSettings(_ sdk: SDK, _ variant: SDKVariant?, _ sparseSDKs: [SDK]) throws -> [String : String] {
167+
guard variant?.llvmTargetTripleVendor == "apple" else {
168+
return [:]
169+
}
170+
171+
return [
172+
"PER_ARCH_MODULE_FILE_DIR": "$(PER_ARCH_OBJECT_FILE_DIR)",
173+
]
174+
}
175+
176+
func addBuiltinDefaults(fromEnvironment environment: [String : String], parameters: BuildParameters) throws -> [String : String] { [:] }
177+
func addOverrides(fromEnvironment: [String : String], parameters: BuildParameters) throws -> [String : String] { [:] }
178+
func addProductTypeDefaults(productType: ProductTypeSpec) -> [String : String] { [:] }
179+
func addSDKOverridingSettings(_ sdk: SDK, _ variant: SDKVariant?, _ sparseSDKs: [SDK], specLookupContext: any SWBCore.SpecLookupContext) throws -> [String : String] { [:] }
180+
func addPlatformSDKSettings(_ platform: SWBCore.Platform?, _ sdk: SDK, _ sdkVariant: SDKVariant?) -> [String : String] { [:] }
181+
func xcconfigOverrideData(fromParameters: BuildParameters) -> ByteString { ByteString() }
182+
func getTargetTestingSwiftPluginFlags(_ scope: MacroEvaluationScope, toolchainRegistry: ToolchainRegistry, sdkRegistry: SDKRegistry, activeRunDestination: RunDestinationInfo?, project: SWBCore.Project?) -> [String] { [] }
183+
func shouldSkipPopulatingValidArchs(platform: SWBCore.Platform) -> Bool { false }
184+
func shouldDisableXOJITPreviews(platformName: String, sdk: SDK?) -> Bool { false }
185+
func overridingBuildSettings(_: MacroEvaluationScope, platform: SWBCore.Platform?, productType: ProductTypeSpec) -> [String : String] { [:] }
186+
}

Sources/SWBCore/Settings/BuiltinMacros.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ public final class BuiltinMacros {
901901
public static let PER_ARCH_CFLAGS = BuiltinMacros.declareStringListMacro("PER_ARCH_CFLAGS")
902902
public static let PER_ARCH_LD = BuiltinMacros.declareStringMacro("PER_ARCH_LD")
903903
public static let PER_ARCH_LDPLUSPLUS = BuiltinMacros.declareStringMacro("PER_ARCH_LDPLUSPLUS")
904+
public static let PER_ARCH_MODULE_FILE_DIR = BuiltinMacros.declarePathMacro("PER_ARCH_MODULE_FILE_DIR")
904905
public static let PER_ARCH_OBJECT_FILE_DIR = BuiltinMacros.declarePathMacro("PER_ARCH_OBJECT_FILE_DIR")
905906
public static let PER_VARIANT_CFLAGS = BuiltinMacros.declareStringListMacro("PER_VARIANT_CFLAGS")
906907
public static let PER_VARIANT_OBJECT_FILE_DIR = BuiltinMacros.declareStringMacro("PER_VARIANT_OBJECT_FILE_DIR")
@@ -1978,6 +1979,7 @@ public final class BuiltinMacros {
19781979
PER_ARCH_CFLAGS,
19791980
PER_ARCH_LD,
19801981
PER_ARCH_LDPLUSPLUS,
1982+
PER_ARCH_MODULE_FILE_DIR,
19811983
PER_ARCH_OBJECT_FILE_DIR,
19821984
PER_VARIANT_CFLAGS,
19831985
PER_VARIANT_OBJECT_FILE_DIR,

Sources/SWBCore/Specs/CoreBuildSystem.xcspec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,11 @@ For more information on mergable libraries, see [Configuring your project to use
29992999
Type = Path;
30003000
DefaultValue = "$(TEMP_ROOT)/UninstalledProducts";
30013001
},
3002+
{
3003+
Name = "PER_ARCH_MODULE_FILE_DIR";
3004+
Type = Path;
3005+
DefaultValue = "$(PER_ARCH_OBJECT_FILE_DIR)/Modules";
3006+
},
30023007
{
30033008
Name = "ENABLE_HEADER_DEPENDENCIES";
30043009
Type = Boolean;

Sources/SWBCore/Specs/Tools/SwiftCompiler.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,9 +1338,9 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
13381338
}
13391339

13401340
static func getSwiftModuleFilePathInternal(_ scope: MacroEvaluationScope, _ mode: SwiftCompilationMode) -> Path {
1341-
let objectFileDir = scope.evaluate(BuiltinMacros.PER_ARCH_OBJECT_FILE_DIR)
1341+
let moduleFileDir = scope.evaluate(BuiltinMacros.PER_ARCH_MODULE_FILE_DIR)
13421342
let moduleName = scope.evaluate(BuiltinMacros.SWIFT_MODULE_NAME)
1343-
return objectFileDir.join(moduleName + ".swiftmodule").appendingFileNameSuffix(mode.moduleBaseNameSuffix)
1343+
return moduleFileDir.join(moduleName + ".swiftmodule").appendingFileNameSuffix(mode.moduleBaseNameSuffix)
13441344
}
13451345

13461346
static public func getSwiftModuleFilePath(_ scope: MacroEvaluationScope) -> Path {
@@ -2959,8 +2959,8 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
29592959
let containsSources = (producer.configuredTarget?.target as? StandardTarget)?.sourcesBuildPhase?.buildFiles.filter { currentPlatformFilter.matches($0.platformFilters) }.isEmpty == false
29602960
if containsSources && inputFileTypes.contains(where: { $0.conformsTo(identifier: "sourcecode.swift") }) && scope.evaluate(BuiltinMacros.GCC_GENERATE_DEBUGGING_SYMBOLS) {
29612961
let moduleName = scope.evaluate(BuiltinMacros.SWIFT_MODULE_NAME)
2962-
let objectFileDir = scope.evaluate(BuiltinMacros.PER_ARCH_OBJECT_FILE_DIR)
2963-
let moduleFilePath = objectFileDir.join(moduleName + ".swiftmodule")
2962+
let moduleFileDir = scope.evaluate(BuiltinMacros.PER_ARCH_MODULE_FILE_DIR)
2963+
let moduleFilePath = moduleFileDir.join(moduleName + ".swiftmodule")
29642964
args += [["-Xlinker", "-add_ast_path", "-Xlinker", moduleFilePath.str]]
29652965
if scope.evaluate(BuiltinMacros.SWIFT_GENERATE_ADDITIONAL_LINKER_ARGS) {
29662966
args += [["@\(Path(moduleFilePath.appendingFileNameSuffix("-linker-args").withoutSuffix + ".resp").str)"]]

Sources/SWBTaskConstruction/TaskProducers/BuildPhaseTaskProducers/SourcesTaskProducer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,10 @@ final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, FilesBase
399399
for arch in scope.evaluate(BuiltinMacros.ARCHS) {
400400
let scope = settings.globalScope.subscope(binding: BuiltinMacros.archCondition, to: arch)
401401
let moduleName = scope.evaluate(BuiltinMacros.SWIFT_MODULE_NAME)
402-
let objectFileDir = scope.evaluate(BuiltinMacros.PER_ARCH_OBJECT_FILE_DIR)
403-
swiftModulePaths[arch] = objectFileDir.join(moduleName + ".swiftmodule")
402+
let moduleFileDir = scope.evaluate(BuiltinMacros.PER_ARCH_MODULE_FILE_DIR)
403+
swiftModulePaths[arch] = moduleFileDir.join(moduleName + ".swiftmodule")
404404
if scope.evaluate(BuiltinMacros.SWIFT_GENERATE_ADDITIONAL_LINKER_ARGS) {
405-
swiftModuleAdditionalLinkerArgResponseFilePaths[arch] = objectFileDir.join("\(moduleName)-linker-args.resp")
405+
swiftModuleAdditionalLinkerArgResponseFilePaths[arch] = moduleFileDir.join("\(moduleName)-linker-args.resp")
406406
}
407407
}
408408
}

0 commit comments

Comments
 (0)