Skip to content

Commit

Permalink
[cxx-interop] pass the C++ language standard to Clang importer in Swi…
Browse files Browse the repository at this point in the history
…ft when C++ interoperability is enabled (swiftlang#6649)

Fixes swiftlang#6565
  • Loading branch information
hyp authored Jun 14, 2023
1 parent fa042e3 commit cefff2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Sources/PackageLoading/PackageBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ public final class PackageBuilder {
}

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

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

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

if lang == .Cxx {
values = ["-cxx-interoperability-mode=default"]
values = ["-cxx-interoperability-mode=default"] + (cxxLanguageStandard.flatMap { ["-Xcc", "-std=\($0)"] } ?? [])
} else {
values = []
}
Expand Down
14 changes: 12 additions & 2 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,9 @@ final class BuildPlanTests: XCTestCase {
Pkg.appending(components: "Sources", "exe", "main.cpp").pathString,
Pkg.appending(components: "Sources", "lib", "lib.c").pathString,
Pkg.appending(components: "Sources", "lib", "libx.cpp").pathString,
Pkg.appending(components: "Sources", "lib", "include", "lib.h").pathString
Pkg.appending(components: "Sources", "lib", "include", "lib.h").pathString,
Pkg.appending(components: "Sources", "swiftInteropLib", "lib.swift").pathString,
Pkg.appending(components: "Sources", "swiftLib", "lib.swift").pathString
)

let observability = ObservabilitySystem.makeForTesting()
Expand All @@ -1382,6 +1384,9 @@ final class BuildPlanTests: XCTestCase {
targets: [
TargetDescription(name: "exe", dependencies: ["lib"]),
TargetDescription(name: "lib", dependencies: []),
TargetDescription(name: "swiftInteropLib", dependencies: [],
settings: [.init(tool: .swift, kind: .interoperabilityMode(.Cxx))]),
TargetDescription(name: "swiftLib", dependencies: [])
]),
],
observabilityScope: observability.topScope
Expand All @@ -1397,7 +1402,7 @@ final class BuildPlanTests: XCTestCase {
let result = try BuildPlanResult(plan: plan)

result.checkProductsCount(1)
result.checkTargetsCount(2)
result.checkTargetsCount(4)

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

Expand Down Expand Up @@ -1447,6 +1452,11 @@ final class BuildPlanTests: XCTestCase {
let contents: String = try fs.readFileContents(yaml)
XCTAssertMatch(contents, .contains(#"-std=gnu99","-c","\#(Pkg.appending(components: "Sources", "lib", "lib.c").escapedPathString())"#))
XCTAssertMatch(contents, .contains(#"-std=c++1z","-c","\#(Pkg.appending(components: "Sources", "lib", "libx.cpp").escapedPathString())"#))

let swiftInteropLib = try result.target(for: "swiftInteropLib").swiftTarget().compileArguments()
XCTAssertMatch(swiftInteropLib, [.anySequence, "-cxx-interoperability-mode=default", "-Xcc", "-std=c++1z", .end])
let swiftLib = try result.target(for: "swiftLib").swiftTarget().compileArguments()
XCTAssertNoMatch(swiftLib, [.anySequence, "-Xcc", "-std=c++1z", .anySequence])
}

func testSwiftCMixed() throws {
Expand Down

0 comments on commit cefff2b

Please sign in to comment.