Skip to content

Commit 42514f4

Browse files
committed
Start a Compatibility51 library for backporting fixes to Swift 5.1 runtimes
1 parent f125d12 commit 42514f4

File tree

10 files changed

+126
-7
lines changed

10 files changed

+126
-7
lines changed

lib/Basic/Platform.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,22 +406,34 @@ swift::getSwiftRuntimeCompatibilityVersionForTarget(
406406
if (Minor <= 14) {
407407
return llvm::VersionTuple(5, 0);
408408
} else if (Minor <= 15) {
409-
return llvm::VersionTuple(5, 1);
409+
if (Micro <= 3) {
410+
return llvm::VersionTuple(5, 1);
411+
} else {
412+
return llvm::VersionTuple(5, 2);
413+
}
410414
}
411415
}
412416
} else if (Triple.isiOS()) { // includes tvOS
413417
Triple.getiOSVersion(Major, Minor, Micro);
414418
if (Major <= 12) {
415419
return llvm::VersionTuple(5, 0);
416420
} else if (Major <= 13) {
417-
return llvm::VersionTuple(5, 1);
421+
if (Minor <= 3) {
422+
return llvm::VersionTuple(5, 1);
423+
} else {
424+
return llvm::VersionTuple(5, 2);
425+
}
418426
}
419427
} else if (Triple.isWatchOS()) {
420428
Triple.getWatchOSVersion(Major, Minor, Micro);
421429
if (Major <= 5) {
422430
return llvm::VersionTuple(5, 0);
423431
} else if (Major <= 6) {
424-
return llvm::VersionTuple(5, 1);
432+
if (Minor <= 1) {
433+
return llvm::VersionTuple(5, 1);
434+
} else {
435+
return llvm::VersionTuple(5, 2);
436+
}
425437
}
426438
}
427439

lib/Driver/DarwinToolChains.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
345345
options::OPT_runtime_compatibility_version);
346346
if (value.equals("5.0")) {
347347
runtimeCompatibilityVersion = llvm::VersionTuple(5, 0);
348+
} else if (value.equals("5.1")) {
349+
runtimeCompatibilityVersion = llvm::VersionTuple(5, 1);
348350
} else if (value.equals("none")) {
349351
runtimeCompatibilityVersion = None;
350352
} else {
@@ -367,6 +369,18 @@ toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
367369
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
368370
}
369371
}
372+
373+
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 1)) {
374+
// Swift 5.1 compatibility library
375+
SmallString<128> BackDeployLib;
376+
BackDeployLib.append(SharedResourceDirPath);
377+
llvm::sys::path::append(BackDeployLib, "libswiftCompatibility51.a");
378+
379+
if (llvm::sys::fs::exists(BackDeployLib)) {
380+
Arguments.push_back("-force_load");
381+
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
382+
}
383+
}
370384
}
371385

372386
if (job.getKind() == LinkKind::Executable) {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,8 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
14131413
runtimeCompatibilityVersion = None;
14141414
} else if (version.equals("5.0")) {
14151415
runtimeCompatibilityVersion = llvm::VersionTuple(5, 0);
1416+
} else if (version.equals("5.1")) {
1417+
runtimeCompatibilityVersion = llvm::VersionTuple(5, 1);
14161418
} else {
14171419
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
14181420
versionArg->getAsString(Args), version);

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,11 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
476476
LibraryKind::Library,
477477
/*forceLoad*/ true));
478478
}
479+
if (*compatibilityVersion <= llvm::VersionTuple(5, 1)) {
480+
this->addLinkLibrary(LinkLibrary("swiftCompatibility51",
481+
LibraryKind::Library,
482+
/*forceLoad*/ true));
483+
}
479484
}
480485

481486
if (auto compatibilityVersion =

stdlib/toolchain/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ endif()
5959

6060
add_subdirectory(legacy_layouts)
6161
add_subdirectory(Compatibility50)
62+
add_subdirectory(Compatibility51)
6263
add_subdirectory(CompatibilityDynamicReplacements)

stdlib/toolchain/Compatibility50/Overrides.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -30,7 +30,7 @@ struct OverrideSection {
3030
#include "../../public/runtime/CompatibilityOverride.def"
3131
};
3232

33-
OverrideSection Overrides
33+
OverrideSection Swift50Overrides
3434
__attribute__((used, section("__DATA,__swift_hooks"))) = {
3535
.version = 0,
3636
.conformsToProtocol = swift50override_conformsToProtocol,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
set(library_name "swiftCompatibility51")
3+
4+
add_swift_target_library("${library_name}" STATIC
5+
Overrides.cpp
6+
7+
TARGET_SDKS ${SWIFT_APPLE_PLATFORMS}
8+
9+
C_COMPILE_FLAGS ${CXX_COMPILE_FLAGS}
10+
LINK_FLAGS ${CXX_LINK_FLAGS}
11+
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
12+
13+
INSTALL_IN_COMPONENT compiler
14+
INSTALL_WITH_SHARED)
15+
16+
17+
# FIXME: We need a more flexible mechanism to add lipo targets generated by
18+
# add_swift_target_library to the ALL target. Until then this hack is necessary
19+
# to ensure these libraries build.
20+
foreach(sdk ${SWIFT_SDKS})
21+
set(target_name "${library_name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
22+
if(NOT TARGET "${target_name}")
23+
continue()
24+
endif()
25+
26+
set_target_properties("${target_name}"
27+
PROPERTIES
28+
EXCLUDE_FROM_ALL FALSE)
29+
endforeach()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- Overrides.cpp - Compat override table for Swift 5.1 runtime ------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file provides compatibility override hooks for Swift 5.1 runtimes.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "../../public/runtime/CompatibilityOverride.h"
18+
19+
#include <dlfcn.h>
20+
#include <mach-o/dyld.h>
21+
#include <mach-o/getsect.h>
22+
23+
using namespace swift;
24+
25+
struct OverrideSection {
26+
uintptr_t version;
27+
#define OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \
28+
Override_ ## name name;
29+
#include "../../public/runtime/CompatibilityOverride.def"
30+
};
31+
32+
OverrideSection Swift51Overrides
33+
__attribute__((used, section("__DATA,__swift51_hooks"))) = {
34+
.version = 0,
35+
};
36+
37+
// Allow this library to get force-loaded by autolinking
38+
__attribute__((weak, visibility("hidden")))
39+
extern "C"
40+
char _swift_FORCE_LOAD_$_swiftCompatibility51 = 0;

test/IRGen/autolink-runtime-compatibility.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,35 @@
1010
// Doesn't autolink compatibility library because target OS doesn't need it
1111
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.24 -emit-ir -parse-stdlib %s | %FileCheck -check-prefix=NO-FORCE-LOAD %s
1212

13+
// Only autolinks 5.1 compatibility library because target OS has 5.1
14+
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.15 -emit-ir -parse-stdlib %s | %FileCheck -check-prefix=FORCE-LOAD-51 %s
15+
1316
// Autolinks because compatibility library was explicitly asked for
1417
// RUN: %target-swift-frontend -runtime-compatibility-version 5.0 -emit-ir -parse-stdlib %s | %FileCheck -check-prefix=FORCE-LOAD %s
18+
// RUN: %target-swift-frontend -runtime-compatibility-version 5.1 -emit-ir -parse-stdlib %s | %FileCheck -check-prefix=FORCE-LOAD-51 %s
1519
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.24 -runtime-compatibility-version 5.0 -emit-ir -parse-stdlib %s | %FileCheck -check-prefix=FORCE-LOAD %s
20+
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.24 -runtime-compatibility-version 5.1 -emit-ir -parse-stdlib %s | %FileCheck -check-prefix=FORCE-LOAD-51 %s
1621

1722
public func foo() {}
1823

1924
// NO-FORCE-LOAD-NOT: FORCE_LOAD
2025
// NO-FORCE-LOAD-NOT: !{!"-lswiftCompatibility50"}
26+
// NO-FORCE-LOAD-NOT: !{!"-lswiftCompatibility51"}
2127
// NO-FORCE-LOAD-NOT: !{!"-lswiftCompatibilityDynamicReplacements"}
2228

2329
// FORCE-LOAD: declare {{.*}} @"_swift_FORCE_LOAD_$_swiftCompatibility50"
2430
// FORCE-LOAD: declare {{.*}} @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"
25-
2631
// FORCE-LOAD-DAG: [[AUTOLINK_SWIFT_COMPAT:![0-9]+]] = !{!"-lswiftCompatibility50"}
32+
// FORCE-LOAD-DAG: !{!"-lswiftCompatibility51"}
2733
// FORCE-LOAD-DAG: !{!"-lswiftCompatibilityDynamicReplacements"}
2834
// FORCE-LOAD-DAG: !llvm.linker.options = !{{{.*}}[[AUTOLINK_SWIFT_COMPAT]]{{[,}]}}
35+
36+
// FORCE-LOAD-51-NOT: @"_swift_FORCE_LOAD_$_swiftCompatibility50"
37+
// FORCE-LOAD-51-NOT: @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"
38+
// FORCE-LOAD-51: declare {{.*}} @"_swift_FORCE_LOAD_$_swiftCompatibility51"
39+
// FORCE-LOAD-51-NOT: @"_swift_FORCE_LOAD_$_swiftCompatibility50"
40+
// FORCE-LOAD-51-NOT: @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"
41+
// FORCE-LOAD-51-DAG: [[AUTOLINK_SWIFT_COMPAT:![0-9]+]] = !{!"-lswiftCompatibility51"}
42+
// FORCE-LOAD-51-DAG: !llvm.linker.options = !{{{.*}}[[AUTOLINK_SWIFT_COMPAT]]{{[,}]}}
43+
// FORCE-LOAD-51-NOT: @"_swift_FORCE_LOAD_$_swiftCompatibility50"
44+
// FORCE-LOAD-51-NOT: @"_swift_FORCE_LOAD_$_swiftCompatibilityDynamicReplacements"

test/stdlib/Compatibility50Linking.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-clang %s -all_load %test-resource-dir/%target-sdk-name/libswiftCompatibility50.a -lobjc -o %t/main
2+
// RUN: %target-clang %s -all_load %test-resource-dir/%target-sdk-name/libswiftCompatibility50.a %test-resource-dir/%target-sdk-name/libswiftCompatibility51.a -lobjc -o %t/main
33
// RUN: %target-run %t/main
44
// REQUIRES: objc_interop
55
// REQUIRES: executable_test

0 commit comments

Comments
 (0)