Skip to content

Commit cefff2b

Browse files
authored
[cxx-interop] pass the C++ language standard to Clang importer in Swift when C++ interoperability is enabled (swiftlang#6649)
Fixes swiftlang#6565
1 parent fa042e3 commit cefff2b

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
@@ -877,7 +877,7 @@ public final class PackageBuilder {
877877
}
878878

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

882882
// Compute the path to public headers directory.
883883
let publicHeaderComponent = manifestTarget.publicHeadersPath ?? ClangTarget.defaultPublicHeadersComponent
@@ -1017,7 +1017,7 @@ public final class PackageBuilder {
10171017
}
10181018

10191019
/// Creates build setting assignment table for the given target.
1020-
func buildSettings(for target: TargetDescription?, targetRoot: AbsolutePath) throws -> BuildSettings
1020+
func buildSettings(for target: TargetDescription?, targetRoot: AbsolutePath, cxxLanguageStandard: String? = nil) throws -> BuildSettings
10211021
.AssignmentTable
10221022
{
10231023
var table = BuildSettings.AssignmentTable()
@@ -1089,7 +1089,7 @@ public final class PackageBuilder {
10891089
}
10901090

10911091
if lang == .Cxx {
1092-
values = ["-cxx-interoperability-mode=default"]
1092+
values = ["-cxx-interoperability-mode=default"] + (cxxLanguageStandard.flatMap { ["-Xcc", "-std=\($0)"] } ?? [])
10931093
} else {
10941094
values = []
10951095
}

Tests/BuildTests/BuildPlanTests.swift

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

13731375
let observability = ObservabilitySystem.makeForTesting()
@@ -1382,6 +1384,9 @@ final class BuildPlanTests: XCTestCase {
13821384
targets: [
13831385
TargetDescription(name: "exe", dependencies: ["lib"]),
13841386
TargetDescription(name: "lib", dependencies: []),
1387+
TargetDescription(name: "swiftInteropLib", dependencies: [],
1388+
settings: [.init(tool: .swift, kind: .interoperabilityMode(.Cxx))]),
1389+
TargetDescription(name: "swiftLib", dependencies: [])
13851390
]),
13861391
],
13871392
observabilityScope: observability.topScope
@@ -1397,7 +1402,7 @@ final class BuildPlanTests: XCTestCase {
13971402
let result = try BuildPlanResult(plan: plan)
13981403

13991404
result.checkProductsCount(1)
1400-
result.checkTargetsCount(2)
1405+
result.checkTargetsCount(4)
14011406

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

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

14521462
func testSwiftCMixed() throws {

0 commit comments

Comments
 (0)