Skip to content

Commit d79bcd5

Browse files
Merge pull request #343 from cltnschlosser/cs_profileLinkerArgss
Fix for simulator and linux -profile-generate linker args
2 parents 582b778 + e217457 commit d79bcd5

File tree

3 files changed

+140
-7
lines changed

3 files changed

+140
-7
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,11 @@ extension DarwinToolchain {
116116
let clangPath = try clangLibraryPath(for: targetTriple,
117117
parsedOptions: &parsedOptions)
118118

119-
let runtime = targetTriple.darwinPlatform!.libraryNameSuffix
120-
121-
let clangRTPath = clangPath
122-
.appending(component: "libclang_rt.profile_\(runtime).a")
123-
124-
commandLine.appendPath(clangRTPath)
119+
for runtime in targetTriple.darwinPlatform!.profileLibraryNameSuffixes {
120+
let clangRTPath = clangPath
121+
.appending(component: "libclang_rt.profile_\(runtime).a")
122+
commandLine.appendPath(clangRTPath)
123+
}
125124
}
126125

127126
private func addPlatformVersionArg(to commandLine: inout [Job.ArgTemplate],
@@ -372,3 +371,24 @@ extension DarwinToolchain {
372371
return try getToolPath(linkerTool)
373372
}
374373
}
374+
375+
private extension DarwinPlatform {
376+
var profileLibraryNameSuffixes: [String] {
377+
switch self {
378+
case .macOS, .iOS(.catalyst):
379+
return ["osx"]
380+
case .iOS(.device):
381+
return ["ios"]
382+
case .iOS(.simulator):
383+
return ["iossim", "ios"]
384+
case .tvOS(.device):
385+
return ["tvos"]
386+
case .tvOS(.simulator):
387+
return ["tvossim", "tvos"]
388+
case .watchOS(.device):
389+
return ["watchos"]
390+
case .watchOS(.simulator):
391+
return ["watchossim", "watchos"]
392+
}
393+
}
394+
}

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ extension GenericUnixToolchain {
244244
let libProfile = sharedResourceDirPath
245245
.parentDirectory // remove platform name
246246
.appending(components: "clang", "lib", targetTriple.osName,
247-
"libclangrt_profile-\(targetTriple.archName).a")
247+
"libclang_rt.profile-\(targetTriple.archName).a")
248248
commandLine.appendPath(libProfile)
249249

250250
// HACK: Hard-coded from llvm::getInstrProfRuntimeHookVarName()

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,106 @@ final class SwiftDriverTests: XCTestCase {
20022002
}
20032003
}
20042004

2005+
func testProfileLinkerArgs() throws {
2006+
do {
2007+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "x86_64-apple-macosx10.9", "test.swift"])
2008+
let plannedJobs = try driver.planBuild()
2009+
2010+
XCTAssertEqual(plannedJobs.count, 2)
2011+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2012+
2013+
XCTAssertEqual(plannedJobs[1].kind, .link)
2014+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_osx.a"))
2015+
}
2016+
2017+
do {
2018+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "x86_64-apple-ios7.1-simulator", "test.swift"])
2019+
let plannedJobs = try driver.planBuild()
2020+
2021+
XCTAssertEqual(plannedJobs.count, 2)
2022+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2023+
2024+
XCTAssertEqual(plannedJobs[1].kind, .link)
2025+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_ios.a"))
2026+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_iossim.a"))
2027+
}
2028+
2029+
do {
2030+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "arm64-apple-ios7.1", "test.swift"])
2031+
let plannedJobs = try driver.planBuild()
2032+
2033+
XCTAssertEqual(plannedJobs.count, 2)
2034+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2035+
2036+
XCTAssertEqual(plannedJobs[1].kind, .link)
2037+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_ios.a"))
2038+
}
2039+
2040+
do {
2041+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "x86_64-apple-tvos9.0-simulator", "test.swift"])
2042+
let plannedJobs = try driver.planBuild()
2043+
2044+
XCTAssertEqual(plannedJobs.count, 2)
2045+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2046+
2047+
XCTAssertEqual(plannedJobs[1].kind, .link)
2048+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_tvos.a"))
2049+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_tvossim.a"))
2050+
}
2051+
2052+
do {
2053+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "arm64-apple-tvos9.0", "test.swift"])
2054+
let plannedJobs = try driver.planBuild()
2055+
2056+
XCTAssertEqual(plannedJobs.count, 2)
2057+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2058+
2059+
XCTAssertEqual(plannedJobs[1].kind, .link)
2060+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_tvos.a"))
2061+
}
2062+
2063+
do {
2064+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "i386-apple-watchos2.0-simulator", "test.swift"])
2065+
let plannedJobs = try driver.planBuild()
2066+
2067+
XCTAssertEqual(plannedJobs.count, 2)
2068+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2069+
2070+
XCTAssertEqual(plannedJobs[1].kind, .link)
2071+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_watchos.a"))
2072+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_watchossim.a"))
2073+
}
2074+
2075+
do {
2076+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "armv7k-apple-watchos2.0", "test.swift"])
2077+
let plannedJobs = try driver.planBuild()
2078+
2079+
XCTAssertEqual(plannedJobs.count, 2)
2080+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2081+
2082+
XCTAssertEqual(plannedJobs[1].kind, .link)
2083+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile_watchos.a"))
2084+
}
2085+
2086+
// FIXME: This will fail when run on macOS, because
2087+
// swift-autolink-extract is not present
2088+
#if os(Linux)
2089+
do {
2090+
var driver = try Driver(args: ["swiftc", "-profile-generate", "-target", "x86_64-unknown-linux-gnu", "test.swift"])
2091+
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
2092+
2093+
XCTAssertEqual(plannedJobs.count, 2)
2094+
XCTAssertEqual(plannedJobs[0].kind, .compile)
2095+
2096+
XCTAssertEqual(plannedJobs[1].kind, .link)
2097+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile-x86_64.a"))
2098+
XCTAssert(plannedJobs[1].commandLine.contains { $0 == .flag("-u__llvm_profile_runtime") })
2099+
}
2100+
#endif
2101+
2102+
// TODO: Windows
2103+
}
2104+
20052105
func testConditionalCompilationArgValidation() throws {
20062106
try assertDriverDiagnostics(args: ["swiftc", "foo.swift", "-DFOO=BAR"]) {
20072107
$1.expect(.warning("conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'FOO=BAR')"))
@@ -4032,3 +4132,16 @@ where Element == Job
40324132
return filtered
40334133
}
40344134
}
4135+
4136+
private extension Array where Element == Job.ArgTemplate {
4137+
func containsPathWithBasename(_ basename: String) -> Bool {
4138+
contains {
4139+
switch $0 {
4140+
case let .path(path):
4141+
return path.basename == basename
4142+
case .flag, .responseFilePath, .joinedOptionAndPath:
4143+
return false
4144+
}
4145+
}
4146+
}
4147+
}

0 commit comments

Comments
 (0)