Skip to content

Disambiguate PIF target names #3716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 10, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "Bar",
products: [
.library(name: "BarLibProduct", type: .dynamic, targets: ["BarLib"]),
.library(name: "BarLib", type: .dynamic, targets: ["BarLib"]),
],
targets: [
.target(name: "BarLib"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import PackageDescription
let package = Package(
name: "Foo",
products: [
.library(name: "FooLibProduct", type: .static, targets: ["FooLib"]),
.library(name: "FooLib", type: .static, targets: ["FooLib"]),
],
dependencies: [
.package(path: "../Bar")
],
targets: [
.target(name: "FooLib", dependencies: ["CFooLib"]),
.target(name: "CFooLib", dependencies: [
.product(name: "BarLibProduct", package: "Bar"),
.product(name: "BarLib", package: "Bar"),
]),
],
swiftLanguageVersions: [.v4_2, .v5]
Expand Down
16 changes: 8 additions & 8 deletions IntegrationTests/Tests/IntegrationTests/XCBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ final class XCBuildTests: XCTestCase {
let fooPath = path.appending(component: "Foo")
let binaryPath = fooPath.appending(components: ".build", "apple", "Products")

try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "foo")
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "foo")
let debugPath = binaryPath.appending(component: "Debug")
XCTAssertFileExists(debugPath.appending(component: "foo"))
XCTAssertFileExists(debugPath.appending(component: "cfoo"))
XCTAssertFileExists(debugPath.appending(component: "bar"))
XCTAssertNoSuchPath(debugPath.appending(component: "cbar"))

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

try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "cfoo")
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "cfoo")
let debugPath = binaryPath.appending(component: "Debug")
XCTAssertNoSuchPath(debugPath.appending(component: "foo"))
XCTAssertFileExists(debugPath.appending(component: "cfoo"))
XCTAssertNoSuchPath(debugPath.appending(component: "bar"))
XCTAssertNoSuchPath(debugPath.appending(component: "cbar"))

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

try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "bar")
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "bar")
let debugPath = binaryPath.appending(component: "Debug")
XCTAssertNoSuchPath(debugPath.appending(component: "foo"))
XCTAssertNoSuchPath(debugPath.appending(component: "cfoo"))
XCTAssertFileExists(debugPath.appending(component: "bar"))
XCTAssertNoSuchPath(debugPath.appending(component: "cbar"))

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

try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "cbar")
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "cbar")
let debugPath = binaryPath.appending(component: "Debug")
XCTAssertNoSuchPath(debugPath.appending(component: "foo"))
XCTAssertNoSuchPath(debugPath.appending(component: "cfoo"))
XCTAssertNoSuchPath(debugPath.appending(component: "bar"))
XCTAssertFileExists(debugPath.appending(component: "cbar"))

try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--target", "cbar", "-c", "release")
try sh(swiftBuild, "--package-path", fooPath, "--build-system", "xcode", "--product", "cbar", "-c", "release")
let releasePath = binaryPath.appending(component: "Release")
XCTAssertNoSuchPath(releasePath.appending(component: "foo"))
XCTAssertNoSuchPath(releasePath.appending(component: "cfoo"))
Expand Down
12 changes: 10 additions & 2 deletions Sources/XCBuildSupport/PIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,19 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
}
}

private func targetName(for product: ResolvedProduct) -> String {
return Self.targetName(for: product.name)
}

static func targetName(for productName: String) -> String {
return "\(productName)_\(String(productName.hash, radix: 16, uppercase: true))_PackageProduct"
}

private func addMainModuleTarget(for product: ResolvedProduct) {
let productType: PIF.Target.ProductType = product.type == .executable ? .executable : .unitTest
let pifTarget = addTarget(
guid: product.pifTargetGUID,
name: product.name,
name: targetName(for: product),
productType: productType,
productName: product.name
)
Expand Down Expand Up @@ -486,7 +494,7 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
// depends.
let pifTarget = addTarget(
guid: product.pifTargetGUID,
name: product.name,
name: targetName(for: product),
productType: productType,
productName: pifTargetProductName
)
Expand Down
4 changes: 3 additions & 1 deletion Sources/XCBuildSupport/XcodeBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ extension PIFBuilderParameters {
extension BuildSubset {
var pifTargetName: String {
switch self {
case .target(let name), .product(let name):
case .product(let name):
return PackagePIFProjectBuilder.targetName(for: name)
case .target(let name):
return name
case .allExcludingTests:
return PIFBuilder.allExcludingTestsTargetName
Expand Down
22 changes: 11 additions & 11 deletions Tests/XCBuildSupportTests/PIFBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ class PIFBuilderTests: XCTestCase {
let projectNames = pif.workspace.projects.map({ $0.name })
XCTAssertEqual(projectNames, ["A", "B", "Aggregate"])
let projectATargetNames = pif.workspace.projects[0].targets.map({ $0.name })
XCTAssertEqual(projectATargetNames, ["aexe", "alib", "A2", "A3"])
XCTAssertEqual(projectATargetNames, ["aexe_79CC9E117_PackageProduct", "alib_79D40CF5C_PackageProduct", "A2", "A3"])
let targetAExeDependencies = pif.workspace.projects[0].targets[0].dependencies
XCTAssertEqual(targetAExeDependencies.map{ $0.targetGUID }, ["PACKAGE-PRODUCT:blib", "PACKAGE-TARGET:A2", "PACKAGE-TARGET:A3"])
let projectBTargetNames = pif.workspace.projects[1].targets.map({ $0.name })
#if ENABLE_TARGET_BASED_DEPENDENCY_RESOLUTION
XCTAssertEqual(projectBTargetNames, ["blib", "B2"])
#else
XCTAssertEqual(projectBTargetNames, ["bexe", "blib", "B2"])
XCTAssertEqual(projectBTargetNames, ["bexe_7ADFD1428_PackageProduct", "blib_7AE74026D_PackageProduct", "B2"])
#endif
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ class PIFBuilderTests: XCTestCase {
// Root Swift executable target

project.checkTarget("PACKAGE-PRODUCT:foo") { target in
XCTAssertEqual(target.name, "foo")
XCTAssertEqual(target.name, "foo_1EF26F7F_PackageProduct")
XCTAssertEqual(target.productType, .executable)
XCTAssertEqual(target.productName, "foo")
XCTAssertEqual(target.dependencies, [
Expand Down Expand Up @@ -508,7 +508,7 @@ class PIFBuilderTests: XCTestCase {
// Root Clang executable target

project.checkTarget("PACKAGE-PRODUCT:cfoo") { target in
XCTAssertEqual(target.name, "cfoo")
XCTAssertEqual(target.name, "cfoo_7BF40D05B_PackageProduct")
XCTAssertEqual(target.productType, .executable)
XCTAssertEqual(target.productName, "cfoo")
XCTAssertEqual(target.dependencies, [])
Expand Down Expand Up @@ -572,7 +572,7 @@ class PIFBuilderTests: XCTestCase {
// Non-root Swift executable target

project.checkTarget("PACKAGE-PRODUCT:bar") { target in
XCTAssertEqual(target.name, "bar")
XCTAssertEqual(target.name, "bar_1ECDA8F8_PackageProduct")
XCTAssertEqual(target.productType, .executable)
XCTAssertEqual(target.productName, "bar")
XCTAssertEqual(target.dependencies, ["PACKAGE-TARGET:BarLib"])
Expand Down Expand Up @@ -627,7 +627,7 @@ class PIFBuilderTests: XCTestCase {
// Non-root Clang executable target

project.checkTarget("PACKAGE-PRODUCT:cbar") { target in
XCTAssertEqual(target.name, "cbar")
XCTAssertEqual(target.name, "cbar_7BEFB595C_PackageProduct")
XCTAssertEqual(target.productType, .executable)
XCTAssertEqual(target.productName, "cbar")
XCTAssertEqual(target.dependencies, [])
Expand Down Expand Up @@ -984,7 +984,7 @@ class PIFBuilderTests: XCTestCase {
PIFTester(pif) { workspace in
workspace.checkProject("PACKAGE:/Foo") { project in
project.checkTarget("PACKAGE-PRODUCT:FooLib1") { target in
XCTAssertEqual(target.name, "FooLib1")
XCTAssertEqual(target.name, "FooLib1_32B0F01AD0DD0FF3_PackageProduct")
XCTAssertEqual(target.productType, .packageProduct)
XCTAssertEqual(target.productName, "libFooLib1.a")
XCTAssertEqual(target.dependencies, [
Expand Down Expand Up @@ -1021,7 +1021,7 @@ class PIFBuilderTests: XCTestCase {
}

project.checkTarget("PACKAGE-PRODUCT:FooLib2") { target in
XCTAssertEqual(target.name, "FooLib2")
XCTAssertEqual(target.name, "FooLib2_32B0F01AD0DD1074_PackageProduct")
XCTAssertEqual(target.productType, .packageProduct)
XCTAssertEqual(target.productName, "libFooLib2.a")
XCTAssertEqual(target.dependencies, [
Expand Down Expand Up @@ -1058,7 +1058,7 @@ class PIFBuilderTests: XCTestCase {

workspace.checkProject("PACKAGE:/Bar") { project in
project.checkTarget("PACKAGE-PRODUCT:BarLib") { target in
XCTAssertEqual(target.name, "BarLib")
XCTAssertEqual(target.name, "BarLib_175D063FAE17B2_PackageProduct")
XCTAssertEqual(target.productType, .framework)
XCTAssertEqual(target.productName, "BarLib.framework")
XCTAssertEqual(target.dependencies, ["PACKAGE-TARGET:BarLib"])
Expand Down Expand Up @@ -1447,7 +1447,7 @@ class PIFBuilderTests: XCTestCase {
PIFTester(pif) { workspace in
workspace.checkProject("PACKAGE:/Bar") { project in
project.checkTarget("PACKAGE-PRODUCT:BarLib") { target in
XCTAssertEqual(target.name, "BarLib")
XCTAssertEqual(target.name, "BarLib_175D063FAE17B2_PackageProduct")
XCTAssertEqual(target.productType, .dynamicLibrary)
XCTAssertEqual(target.productName, "libBarLib.dylib")
}
Expand Down Expand Up @@ -1493,7 +1493,7 @@ class PIFBuilderTests: XCTestCase {
PIFTester(pif) { workspace in
workspace.checkProject("PACKAGE:/Bar") { project in
project.checkTarget("PACKAGE-PRODUCT:BarLib") { target in
XCTAssertEqual(target.name, "BarLib")
XCTAssertEqual(target.name, "BarLib_175D063FAE17B2_PackageProduct")

target.checkBuildConfiguration("Debug") { configuration in
configuration.checkBuildSettings { settings in
Expand Down