Skip to content

Commit 3098649

Browse files
authored
Merge pull request #1297 from apple/revert-1295-NoMoreArcLite
Revert "Remove support for linking arclite"
2 parents 7f4c42c + 3423cff commit 3098649

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,10 +2804,6 @@ extension Driver {
28042804
diagnosticsEngine.emit(.error_hermetic_seal_requires_lto)
28052805
}
28062806
}
2807-
if parsedOptions.hasArgument(.linkObjcRuntime) ||
2808-
parsedOptions.hasArgument(.noLinkObjcRuntime) {
2809-
diagnosticsEngine.emit(.warning_darwin_link_objc_deprecated())
2810-
}
28112807
}
28122808

28132809
private static func validateSanitizerAddressUseOdrIndicatorFlag(

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,38 @@ import struct TSCBasic.AbsolutePath
1616
import struct TSCBasic.RelativePath
1717

1818
extension DarwinToolchain {
19+
internal func findXcodeClangPath() throws -> AbsolutePath? {
20+
let result = try executor.checkNonZeroExit(
21+
args: "xcrun", "-toolchain", "default", "-f", "clang",
22+
environment: env
23+
).trimmingCharacters(in: .whitespacesAndNewlines)
24+
25+
return result.isEmpty ? nil : try AbsolutePath(validating: result)
26+
}
27+
28+
internal func findXcodeClangLibPath(_ additionalPath: String) throws -> AbsolutePath? {
29+
let path = try getToolPath(.swiftCompiler)
30+
.parentDirectory // 'swift'
31+
.parentDirectory // 'bin'
32+
.appending(components: "lib", additionalPath)
33+
34+
if fileSystem.exists(path) { return path }
35+
36+
// If we don't have a 'lib/arc/' directory, find the "arclite" library
37+
// relative to the Clang in the active Xcode.
38+
if let clangPath = try? findXcodeClangPath() {
39+
return clangPath
40+
.parentDirectory // 'clang'
41+
.parentDirectory // 'bin'
42+
.appending(components: "lib", additionalPath)
43+
}
44+
return nil
45+
}
46+
47+
internal func findARCLiteLibPath() throws -> AbsolutePath? {
48+
return try findXcodeClangLibPath("arc")
49+
}
50+
1951
/// Adds the arguments necessary to link the files from the given set of
2052
/// options for a Darwin platform.
2153
public func addPlatformSpecificLinkerArgs(
@@ -187,6 +219,13 @@ extension DarwinToolchain {
187219
commandLine.appendPath(VirtualPath.lookup(sdkPath))
188220
}
189221

222+
// -link-objc-runtime also implies -fobjc-link-runtime
223+
if parsedOptions.hasFlag(positive: .linkObjcRuntime,
224+
negative: .noLinkObjcRuntime,
225+
default: false) {
226+
commandLine.appendFlag("-fobjc-link-runtime")
227+
}
228+
190229
let targetTriple = targetInfo.target.triple
191230
commandLine.appendFlag("--target=\(targetTriple.triple)")
192231
if let variantTriple = targetInfo.targetVariant?.triple {

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ public final class DarwinToolchain: Toolchain {
188188
targetVariantTriple: Triple?,
189189
compilerOutputType: FileType?,
190190
diagnosticsEngine: DiagnosticsEngine) throws {
191+
// On non-darwin hosts, libArcLite won't be found and a warning will be emitted
192+
// Guard for the sake of tests running on all platforms
193+
#if canImport(Darwin)
194+
// Validating arclite library path when link-objc-runtime.
195+
validateLinkObjcRuntimeARCLiteLib(&parsedOptions,
196+
targetTriple: targetTriple,
197+
diagnosticsEngine: diagnosticsEngine)
198+
#endif
191199
// Validating apple platforms deployment targets.
192200
try validateDeploymentTarget(&parsedOptions, targetTriple: targetTriple,
193201
compilerOutputType: compilerOutputType)
@@ -239,6 +247,22 @@ public final class DarwinToolchain: Toolchain {
239247
}
240248
}
241249
}
250+
251+
func validateLinkObjcRuntimeARCLiteLib(_ parsedOptions: inout ParsedOptions,
252+
targetTriple: Triple,
253+
diagnosticsEngine: DiagnosticsEngine) {
254+
guard parsedOptions.hasFlag(positive: .linkObjcRuntime,
255+
negative: .noLinkObjcRuntime,
256+
default: !targetTriple.supports(.nativeARC))
257+
else {
258+
return
259+
}
260+
261+
guard let _ = try? findARCLiteLibPath() else {
262+
diagnosticsEngine.emit(.warn_arclite_not_found_when_link_objc_runtime)
263+
return
264+
}
265+
}
242266

243267
struct DarwinSDKInfo: Decodable {
244268
private enum CodingKeys: String, CodingKey {
@@ -380,6 +404,15 @@ public final class DarwinToolchain: Toolchain {
380404
}
381405
}
382406

407+
extension Diagnostic.Message {
408+
static var warn_arclite_not_found_when_link_objc_runtime: Diagnostic.Message {
409+
.warning(
410+
"unable to find Objective-C runtime support library 'arclite'; " +
411+
"pass '-no-link-objc-runtime' to silence this warning"
412+
)
413+
}
414+
}
415+
383416
private extension Version {
384417
var sdkVersionString: String {
385418
if patch == 0 && prerelease.isEmpty && metadata.isEmpty {

Sources/SwiftDriver/Utilities/Diagnostics.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ extension Diagnostic.Message {
5151
.warning("inferring simulator environment for target '\(originalTriple.triple)'; use '-target \(inferredTriple.triple)' instead")
5252
}
5353

54-
static func warning_darwin_link_objc_deprecated() -> Diagnostic.Message {
55-
.warning("-link-objc-runtime is no longer supported on Apple platforms")
56-
}
57-
5854
static func error_argument_not_allowed_with(arg: String, other: String) -> Diagnostic.Message {
5955
.error("argument '\(arg)' is not allowed with '\(other)'")
6056
}

Sources/SwiftDriver/Utilities/Triple+Platforms.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,25 @@ extension Triple {
398398
}
399399
}
400400
}
401+
402+
extension Triple.FeatureAvailability {
403+
/// Linking `libarclite` is unnecessary for triples supporting this feature.
404+
///
405+
/// This impacts the `-link-objc-runtime` flag in Swift, which is akin to the
406+
/// `-fobjc-link-runtime` build setting in clang. When set, these flags
407+
/// automatically link libobjc, and any compatibility libraries that don't
408+
/// ship with the OS. The versions here are the first OSes that support
409+
/// ARC natively in their respective copies of the Objective-C runtime,
410+
/// and therefore do not require additional support libraries.
411+
static let nativeARC = Self(
412+
macOS: .available(since: Triple.Version(10, 11, 0)),
413+
iOS: .available(since: Triple.Version(9, 0, 0)),
414+
tvOS: .available(since: Triple.Version(9, 0, 0)),
415+
watchOS: .availableInAllVersions
416+
)
417+
// When updating the versions listed here, please record the most recent
418+
// feature being depended on and when it was introduced:
419+
//
420+
// - Make assigning 'nil' to an NSMutableDictionary subscript delete the
421+
// entry, like it does for Swift.Dictionary, rather than trap.
422+
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,6 @@ final class SwiftDriverTests: XCTestCase {
601601
}
602602
}
603603

604-
func testLinkObjCFlagWarning() throws {
605-
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-link-objc-runtime") {
606-
$1.expect(.warning("-link-objc-runtime is no longer supported on Apple platforms"))
607-
}
608-
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-no-link-objc-runtime") {
609-
$1.expect(.warning("-link-objc-runtime is no longer supported on Apple platforms"))
610-
}
611-
}
612-
613604
func testHermeticSealAtLink() throws {
614605
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-experimental-hermetic-seal-at-link", "-lto=llvm-full") { driver in
615606
let jobs = try driver.planBuild()
@@ -1782,6 +1773,39 @@ final class SwiftDriverTests: XCTestCase {
17821773
XCTAssertFalse(cmd.contains(.flag("-shared")))
17831774
}
17841775

1776+
do {
1777+
// -fobjc-link-runtime default
1778+
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-apple-macosx10.15"], env: env)
1779+
let plannedJobs = try driver.planBuild()
1780+
XCTAssertEqual(3, plannedJobs.count)
1781+
let linkJob = plannedJobs[2]
1782+
XCTAssertEqual(linkJob.kind, .link)
1783+
let cmd = linkJob.commandLine
1784+
XCTAssertFalse(cmd.contains(.flag("-fobjc-link-runtime")))
1785+
}
1786+
1787+
do {
1788+
// -fobjc-link-runtime enable
1789+
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-apple-macosx10.15", "-link-objc-runtime"], env: env)
1790+
let plannedJobs = try driver.planBuild()
1791+
XCTAssertEqual(3, plannedJobs.count)
1792+
let linkJob = plannedJobs[2]
1793+
XCTAssertEqual(linkJob.kind, .link)
1794+
let cmd = linkJob.commandLine
1795+
XCTAssertTrue(cmd.contains(.flag("-fobjc-link-runtime")))
1796+
}
1797+
1798+
do {
1799+
// -fobjc-link-runtime disable override
1800+
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-apple-macosx10.15", "-link-objc-runtime", "-no-link-objc-runtime"], env: env)
1801+
let plannedJobs = try driver.planBuild()
1802+
XCTAssertEqual(3, plannedJobs.count)
1803+
let linkJob = plannedJobs[2]
1804+
XCTAssertEqual(linkJob.kind, .link)
1805+
let cmd = linkJob.commandLine
1806+
XCTAssertFalse(cmd.contains(.flag("-fobjc-link-runtime")))
1807+
}
1808+
17851809
do {
17861810
// Xlinker flags
17871811
// Ensure that Xlinker flags are passed as such to the clang linker invocation.
@@ -3812,6 +3836,11 @@ final class SwiftDriverTests: XCTestCase {
38123836

38133837
// Ensure arm64_32 is not restricted to back-deployment like other 32-bit archs (armv7k/i386).
38143838
XCTAssertNoThrow(try Driver(args: ["swiftc", "-emit-module", "-c", "-target", "arm64_32-apple-watchos12.0", "foo.swift"]))
3839+
3840+
// On non-darwin hosts, libArcLite won't be found and a warning will be emitted
3841+
#if os(macOS)
3842+
try assertNoDriverDiagnostics(args: "swiftc", "-c", "-target", "x86_64-apple-macosx10.14", "-link-objc-runtime", "foo.swift")
3843+
#endif
38153844
}
38163845

38173846
func testProfileArgValidation() throws {

0 commit comments

Comments
 (0)