Skip to content

Commit f1b8083

Browse files
committed
Disambiguate PIF target names
A product can have the same name as a target in SwiftPM, but since we create a PIF target for each, we need to come up with a unique name for products. rdar://82744792
1 parent 1fbd6e6 commit f1b8083

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

IntegrationTests/Tests/IntegrationTests/XCBuildTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ final class XCBuildTests: XCTestCase {
4242
let fooPath = path.appending(component: "Foo")
4343
let binaryPath = fooPath.appending(components: ".build", "apple", "Products")
4444

45-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "foo")
45+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "foo")
4646
let debugPath = binaryPath.appending(component: "Debug")
4747
XCTAssertFileExists(debugPath.appending(component: "foo"))
4848
XCTAssertFileExists(debugPath.appending(component: "cfoo"))
4949
XCTAssertFileExists(debugPath.appending(component: "bar"))
5050
XCTAssertNoSuchPath(debugPath.appending(component: "cbar"))
5151

52-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "foo", "-c", "release")
52+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "foo", "-c", "release")
5353
let releasePath = binaryPath.appending(component: "Release")
5454
XCTAssertFileExists(releasePath.appending(component: "foo"))
5555
XCTAssertFileExists(releasePath.appending(component: "cfoo"))
@@ -61,14 +61,14 @@ final class XCBuildTests: XCTestCase {
6161
let fooPath = path.appending(component: "Foo")
6262
let binaryPath = fooPath.appending(components: ".build", "apple", "Products")
6363

64-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "cfoo")
64+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "cfoo")
6565
let debugPath = binaryPath.appending(component: "Debug")
6666
XCTAssertNoSuchPath(debugPath.appending(component: "foo"))
6767
XCTAssertFileExists(debugPath.appending(component: "cfoo"))
6868
XCTAssertNoSuchPath(debugPath.appending(component: "bar"))
6969
XCTAssertNoSuchPath(debugPath.appending(component: "cbar"))
7070

71-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "cfoo", "-c", "release")
71+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "cfoo", "-c", "release")
7272
let releasePath = binaryPath.appending(component: "Release")
7373
XCTAssertNoSuchPath(releasePath.appending(component: "foo"))
7474
XCTAssertFileExists(releasePath.appending(component: "cfoo"))
@@ -80,14 +80,14 @@ final class XCBuildTests: XCTestCase {
8080
let fooPath = path.appending(component: "Foo")
8181
let binaryPath = fooPath.appending(components: ".build", "apple", "Products")
8282

83-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "bar")
83+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "bar")
8484
let debugPath = binaryPath.appending(component: "Debug")
8585
XCTAssertNoSuchPath(debugPath.appending(component: "foo"))
8686
XCTAssertNoSuchPath(debugPath.appending(component: "cfoo"))
8787
XCTAssertFileExists(debugPath.appending(component: "bar"))
8888
XCTAssertNoSuchPath(debugPath.appending(component: "cbar"))
8989

90-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "bar", "-c", "release")
90+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "bar", "-c", "release")
9191
let releasePath = binaryPath.appending(component: "Release")
9292
XCTAssertNoSuchPath(releasePath.appending(component: "foo"))
9393
XCTAssertNoSuchPath(releasePath.appending(component: "cfoo"))
@@ -99,14 +99,14 @@ final class XCBuildTests: XCTestCase {
9999
let fooPath = path.appending(component: "Foo")
100100
let binaryPath = fooPath.appending(components: ".build", "apple", "Products")
101101

102-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "cbar")
102+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "cbar")
103103
let debugPath = binaryPath.appending(component: "Debug")
104104
XCTAssertNoSuchPath(debugPath.appending(component: "foo"))
105105
XCTAssertNoSuchPath(debugPath.appending(component: "cfoo"))
106106
XCTAssertNoSuchPath(debugPath.appending(component: "bar"))
107107
XCTAssertFileExists(debugPath.appending(component: "cbar"))
108108

109-
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "cbar", "-c", "release")
109+
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "cbar", "-c", "release")
110110
let releasePath = binaryPath.appending(component: "Release")
111111
XCTAssertNoSuchPath(releasePath.appending(component: "foo"))
112112
XCTAssertNoSuchPath(releasePath.appending(component: "cfoo"))

Sources/XCBuildSupport/PIFBuilder.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,19 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
366366
}
367367
}
368368

369+
private func targetName(for product: ResolvedProduct) -> String {
370+
return Self.targetName(for: product.name)
371+
}
372+
373+
static func targetName(for productName: String) -> String {
374+
return "\(productName)_\(String(productName.hash, radix: 16, uppercase: true))_PackageProduct"
375+
}
376+
369377
private func addMainModuleTarget(for product: ResolvedProduct) {
370378
let productType: PIF.Target.ProductType = product.type == .executable ? .executable : .unitTest
371379
let pifTarget = addTarget(
372380
guid: product.pifTargetGUID,
373-
name: product.name,
381+
name: targetName(for: product),
374382
productType: productType,
375383
productName: product.name
376384
)
@@ -486,7 +494,7 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
486494
// depends.
487495
let pifTarget = addTarget(
488496
guid: product.pifTargetGUID,
489-
name: product.name,
497+
name: targetName(for: product),
490498
productType: productType,
491499
productName: pifTargetProductName
492500
)

Sources/XCBuildSupport/XcodeBuildSystem.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ extension PIFBuilderParameters {
270270
extension BuildSubset {
271271
var pifTargetName: String {
272272
switch self {
273-
case .target(let name), .product(let name):
273+
case .product(let name):
274+
return PackagePIFProjectBuilder.targetName(for: name)
275+
case .target(let name):
274276
return name
275277
case .allExcludingTests:
276278
return PIFBuilder.allExcludingTestsTargetName

Tests/XCBuildSupportTests/PIFBuilderTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ class PIFBuilderTests: XCTestCase {
8080
let projectNames = pif.workspace.projects.map({ $0.name })
8181
XCTAssertEqual(projectNames, ["A", "B", "Aggregate"])
8282
let projectATargetNames = pif.workspace.projects[0].targets.map({ $0.name })
83-
XCTAssertEqual(projectATargetNames, ["aexe", "alib", "A2", "A3"])
83+
XCTAssertEqual(projectATargetNames, ["aexe_79CC9E117_PackageProduct", "alib_79D40CF5C_PackageProduct", "A2", "A3"])
8484
let targetAExeDependencies = pif.workspace.projects[0].targets[0].dependencies
8585
XCTAssertEqual(targetAExeDependencies.map{ $0.targetGUID }, ["PACKAGE-PRODUCT:blib", "PACKAGE-TARGET:A2", "PACKAGE-TARGET:A3"])
8686
let projectBTargetNames = pif.workspace.projects[1].targets.map({ $0.name })
8787
#if ENABLE_TARGET_BASED_DEPENDENCY_RESOLUTION
8888
XCTAssertEqual(projectBTargetNames, ["blib", "B2"])
8989
#else
90-
XCTAssertEqual(projectBTargetNames, ["bexe", "blib", "B2"])
90+
XCTAssertEqual(projectBTargetNames, ["bexe_7ADFD1428_PackageProduct", "blib_7AE74026D_PackageProduct", "B2"])
9191
#endif
9292
}
9393
}
@@ -435,7 +435,7 @@ class PIFBuilderTests: XCTestCase {
435435
// Root Swift executable target
436436

437437
project.checkTarget("PACKAGE-PRODUCT:foo") { target in
438-
XCTAssertEqual(target.name, "foo")
438+
XCTAssertEqual(target.name, "foo_1EF26F7F_PackageProduct")
439439
XCTAssertEqual(target.productType, .executable)
440440
XCTAssertEqual(target.productName, "foo")
441441
XCTAssertEqual(target.dependencies, [
@@ -508,7 +508,7 @@ class PIFBuilderTests: XCTestCase {
508508
// Root Clang executable target
509509

510510
project.checkTarget("PACKAGE-PRODUCT:cfoo") { target in
511-
XCTAssertEqual(target.name, "cfoo")
511+
XCTAssertEqual(target.name, "cfoo_7BF40D05B_PackageProduct")
512512
XCTAssertEqual(target.productType, .executable)
513513
XCTAssertEqual(target.productName, "cfoo")
514514
XCTAssertEqual(target.dependencies, [])
@@ -572,7 +572,7 @@ class PIFBuilderTests: XCTestCase {
572572
// Non-root Swift executable target
573573

574574
project.checkTarget("PACKAGE-PRODUCT:bar") { target in
575-
XCTAssertEqual(target.name, "bar")
575+
XCTAssertEqual(target.name, "bar_1ECDA8F8_PackageProduct")
576576
XCTAssertEqual(target.productType, .executable)
577577
XCTAssertEqual(target.productName, "bar")
578578
XCTAssertEqual(target.dependencies, ["PACKAGE-TARGET:BarLib"])
@@ -627,7 +627,7 @@ class PIFBuilderTests: XCTestCase {
627627
// Non-root Clang executable target
628628

629629
project.checkTarget("PACKAGE-PRODUCT:cbar") { target in
630-
XCTAssertEqual(target.name, "cbar")
630+
XCTAssertEqual(target.name, "cbar_7BEFB595C_PackageProduct")
631631
XCTAssertEqual(target.productType, .executable)
632632
XCTAssertEqual(target.productName, "cbar")
633633
XCTAssertEqual(target.dependencies, [])
@@ -984,7 +984,7 @@ class PIFBuilderTests: XCTestCase {
984984
PIFTester(pif) { workspace in
985985
workspace.checkProject("PACKAGE:/Foo") { project in
986986
project.checkTarget("PACKAGE-PRODUCT:FooLib1") { target in
987-
XCTAssertEqual(target.name, "FooLib1")
987+
XCTAssertEqual(target.name, "FooLib1_32B0F01AD0DD0FF3_PackageProduct")
988988
XCTAssertEqual(target.productType, .packageProduct)
989989
XCTAssertEqual(target.productName, "libFooLib1.a")
990990
XCTAssertEqual(target.dependencies, [
@@ -1021,7 +1021,7 @@ class PIFBuilderTests: XCTestCase {
10211021
}
10221022

10231023
project.checkTarget("PACKAGE-PRODUCT:FooLib2") { target in
1024-
XCTAssertEqual(target.name, "FooLib2")
1024+
XCTAssertEqual(target.name, "FooLib2_32B0F01AD0DD1074_PackageProduct")
10251025
XCTAssertEqual(target.productType, .packageProduct)
10261026
XCTAssertEqual(target.productName, "libFooLib2.a")
10271027
XCTAssertEqual(target.dependencies, [
@@ -1058,7 +1058,7 @@ class PIFBuilderTests: XCTestCase {
10581058

10591059
workspace.checkProject("PACKAGE:/Bar") { project in
10601060
project.checkTarget("PACKAGE-PRODUCT:BarLib") { target in
1061-
XCTAssertEqual(target.name, "BarLib")
1061+
XCTAssertEqual(target.name, "BarLib_175D063FAE17B2_PackageProduct")
10621062
XCTAssertEqual(target.productType, .framework)
10631063
XCTAssertEqual(target.productName, "BarLib.framework")
10641064
XCTAssertEqual(target.dependencies, ["PACKAGE-TARGET:BarLib"])
@@ -1447,7 +1447,7 @@ class PIFBuilderTests: XCTestCase {
14471447
PIFTester(pif) { workspace in
14481448
workspace.checkProject("PACKAGE:/Bar") { project in
14491449
project.checkTarget("PACKAGE-PRODUCT:BarLib") { target in
1450-
XCTAssertEqual(target.name, "BarLib")
1450+
XCTAssertEqual(target.name, "BarLib_175D063FAE17B2_PackageProduct")
14511451
XCTAssertEqual(target.productType, .dynamicLibrary)
14521452
XCTAssertEqual(target.productName, "libBarLib.dylib")
14531453
}
@@ -1493,7 +1493,7 @@ class PIFBuilderTests: XCTestCase {
14931493
PIFTester(pif) { workspace in
14941494
workspace.checkProject("PACKAGE:/Bar") { project in
14951495
project.checkTarget("PACKAGE-PRODUCT:BarLib") { target in
1496-
XCTAssertEqual(target.name, "BarLib")
1496+
XCTAssertEqual(target.name, "BarLib_175D063FAE17B2_PackageProduct")
14971497

14981498
target.checkBuildConfiguration("Debug") { configuration in
14991499
configuration.checkBuildSettings { settings in

0 commit comments

Comments
 (0)