Skip to content

Commit 49ec9c6

Browse files
authored
[cxx-interop] pass the C++ language standard to Clang importer in Swift when C++ interoperability is enabled (#6649) (#6652)
Fixes #6565 (cherry picked from commit cefff2b)
1 parent 251b5a1 commit 49ec9c6

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Sources/PackageLoading/PackageBuilder.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ public final class PackageBuilder {
874874
}
875875

876876
// Create the build setting assignment table for this target.
877-
let buildSettings = try self.buildSettings(for: manifestTarget, targetRoot: potentialModule.path)
877+
let buildSettings = try self.buildSettings(for: manifestTarget, targetRoot: potentialModule.path, cxxLanguageStandard: self.manifest.cxxLanguageStandard)
878878

879879
// Compute the path to public headers directory.
880880
let publicHeaderComponent = manifestTarget.publicHeadersPath ?? ClangTarget.defaultPublicHeadersComponent
@@ -1014,7 +1014,7 @@ public final class PackageBuilder {
10141014
}
10151015

10161016
/// Creates build setting assignment table for the given target.
1017-
func buildSettings(for target: TargetDescription?, targetRoot: AbsolutePath) throws -> BuildSettings
1017+
func buildSettings(for target: TargetDescription?, targetRoot: AbsolutePath, cxxLanguageStandard: String? = nil) throws -> BuildSettings
10181018
.AssignmentTable
10191019
{
10201020
var table = BuildSettings.AssignmentTable()
@@ -1086,7 +1086,7 @@ public final class PackageBuilder {
10861086
}
10871087

10881088
if lang == .Cxx {
1089-
values = ["-cxx-interoperability-mode=default"]
1089+
values = ["-cxx-interoperability-mode=default"] + (cxxLanguageStandard.flatMap { ["-Xcc", "-std=\($0)"] } ?? [])
10901090
} else {
10911091
values = []
10921092
}

Tests/BuildTests/BuildPlanTests.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,9 @@ final class BuildPlanTests: XCTestCase {
13661366
Pkg.appending(components: "Sources", "exe", "main.cpp").pathString,
13671367
Pkg.appending(components: "Sources", "lib", "lib.c").pathString,
13681368
Pkg.appending(components: "Sources", "lib", "libx.cpp").pathString,
1369-
Pkg.appending(components: "Sources", "lib", "include", "lib.h").pathString
1369+
Pkg.appending(components: "Sources", "lib", "include", "lib.h").pathString,
1370+
Pkg.appending(components: "Sources", "swiftInteropLib", "lib.swift").pathString,
1371+
Pkg.appending(components: "Sources", "swiftLib", "lib.swift").pathString
13701372
)
13711373

13721374
let observability = ObservabilitySystem.makeForTesting()
@@ -1381,6 +1383,9 @@ final class BuildPlanTests: XCTestCase {
13811383
targets: [
13821384
TargetDescription(name: "exe", dependencies: ["lib"]),
13831385
TargetDescription(name: "lib", dependencies: []),
1386+
TargetDescription(name: "swiftInteropLib", dependencies: [],
1387+
settings: [.init(tool: .swift, kind: .interoperabilityMode(.Cxx))]),
1388+
TargetDescription(name: "swiftLib", dependencies: [])
13841389
]),
13851390
],
13861391
observabilityScope: observability.topScope
@@ -1396,7 +1401,7 @@ final class BuildPlanTests: XCTestCase {
13961401
let result = try BuildPlanResult(plan: plan)
13971402

13981403
result.checkProductsCount(1)
1399-
result.checkTargetsCount(2)
1404+
result.checkTargetsCount(4)
14001405

14011406
let buildPath: AbsolutePath = result.plan.buildParameters.dataPath.appending(components: "debug")
14021407

@@ -1446,6 +1451,11 @@ final class BuildPlanTests: XCTestCase {
14461451
let contents: String = try fs.readFileContents(yaml)
14471452
XCTAssertMatch(contents, .contains(#"-std=gnu99","-c","\#(Pkg.appending(components: "Sources", "lib", "lib.c").escapedPathString())"#))
14481453
XCTAssertMatch(contents, .contains(#"-std=c++1z","-c","\#(Pkg.appending(components: "Sources", "lib", "libx.cpp").escapedPathString())"#))
1454+
1455+
let swiftInteropLib = try result.target(for: "swiftInteropLib").swiftTarget().compileArguments()
1456+
XCTAssertMatch(swiftInteropLib, [.anySequence, "-cxx-interoperability-mode=default", "-Xcc", "-std=c++1z", .end])
1457+
let swiftLib = try result.target(for: "swiftLib").swiftTarget().compileArguments()
1458+
XCTAssertNoMatch(swiftLib, [.anySequence, "-Xcc", "-std=c++1z", .anySequence])
14491459
}
14501460

14511461
func testSwiftCMixed() throws {

0 commit comments

Comments
 (0)