Skip to content

Commit 4ffc64a

Browse files
committed
[BuildPlan] Add c++ link args if needed
1 parent b7bbfc4 commit 4ffc64a

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,28 @@ public class BuildPlan {
417417
for buildProduct in buildProducts {
418418
// FIXME: Ask this from package graph instead.
419419
let allModules = try! topologicalSort(buildProduct.product.modules, successors: { $0.dependencies })
420-
// Add pkgConfig libs arguments.
421-
for case let module as CModule in allModules {
422-
buildProduct.additionalFlags += pkgConfig(for: module).libs
420+
var linkCpp = false
421+
422+
for module in allModules {
423+
switch module {
424+
case let module as CModule:
425+
// Add pkgConfig libs arguments.
426+
buildProduct.additionalFlags += pkgConfig(for: module).libs
427+
case let module as ClangModule:
428+
if module.containsCppFiles {
429+
linkCpp = true
430+
}
431+
default: break
432+
}
433+
}
434+
// Link C++ if needed.
435+
// Note: This will come from build settings in future.
436+
if linkCpp {
437+
#if os(macOS)
438+
buildProduct.additionalFlags += ["-lc++"]
439+
#else
440+
buildProduct.additionalFlags += ["-lstdc++"]
441+
#endif
423442
}
424443
}
425444
}

Sources/Build/misc.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import Basic
1212
import Utility
1313
import PackageModel
1414

15-
import func POSIX.getenv
16-
import func POSIX.popen
17-
1815
public protocol Toolchain {
1916
/// Path of the `swiftc` compiler.
2017
var swiftCompiler: AbsolutePath { get }
@@ -42,30 +39,6 @@ extension AbsolutePath {
4239
}
4340

4441
extension ClangModule {
45-
// Returns language specific arguments for a ClangModule.
46-
var languageLinkArgs: [String] {
47-
var args = [String]()
48-
// Check if this module contains any cpp file.
49-
var linkCpp = self.containsCppFiles
50-
51-
// Otherwise check if any of its dependencies contains a cpp file.
52-
// FIXME: It is expensive to iterate over all of the dependencies.
53-
// Figure out a way to cache this kind of lookups.
54-
if !linkCpp {
55-
for case let dep as ClangModule in recursiveDependencies {
56-
if dep.containsCppFiles {
57-
linkCpp = true
58-
break
59-
}
60-
}
61-
}
62-
// Link C++ if found any cpp source.
63-
if linkCpp {
64-
args += ["-lstdc++"]
65-
}
66-
return args
67-
}
68-
6942
var containsCppFiles: Bool {
7043
return sources.paths.contains { $0.isCpp }
7144
}

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,39 @@ final class BuildPlanTests: XCTestCase {
196196
XCTAssertEqual(try result.target(for: "exe").swiftTarget().compileArguments(), ["-Onone", "-g", "-enable-testing", "-j8", "-DSWIFT_PACKAGE", "-Xcc", "-fmodule-map-file=/Clibgit/module.modulemap", "-module-cache-path", "/path/to/build/debug/ModuleCache"])
197197
}
198198

199+
func testCppModule() throws {
200+
let fs = InMemoryFileSystem(emptyFiles:
201+
"/Pkg/Sources/exe/main.swift",
202+
"/Pkg/Sources/lib/lib.cpp",
203+
"/Pkg/Sources/lib/include/lib.h"
204+
)
205+
let pkg = Package(
206+
name: "Pkg",
207+
targets: [
208+
Target(name: "exe", dependencies: ["lib"]),
209+
]
210+
)
211+
let graph = try loadMockPackageGraph(["/Pkg": pkg], root: "/Pkg", in: fs)
212+
let result = BuildPlanResult(plan: try BuildPlan(buildParameters: mockBuildParameters(), graph: graph, fileSystem: fs))
213+
result.checkProductsCount(1)
214+
result.checkTargetsCount(2)
215+
let linkArgs = try result.buildProduct(for: "exe").linkArguments()
216+
217+
#if os(macOS)
218+
XCTAssertTrue(linkArgs.contains("-lc++"))
219+
#else
220+
XCTAssertTrue(linkArgs.contains("-lstdc++"))
221+
#endif
222+
}
223+
199224
static var allTests = [
200225
("testBasicClangPackage", testBasicClangPackage),
201226
("testBasicReleasePackage", testBasicReleasePackage),
202227
("testBasicSwiftPackage", testBasicSwiftPackage),
203228
("testCModule", testCModule),
204229
("testSwiftCMixed", testSwiftCMixed),
205230
("testTestModule", testTestModule),
231+
("testCppModule", testCppModule),
206232
]
207233
}
208234

0 commit comments

Comments
 (0)