Skip to content

Commit 38c583e

Browse files
authored
Merge pull request #25233 from apple/revert-25225-revert-25148-runtime-compatibility-autolink
Reinstate "Use autolinking to pull in compatibility libraries."
2 parents c1d007a + 14f9dc6 commit 38c583e

File tree

16 files changed

+198
-62
lines changed

16 files changed

+198
-62
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,8 @@ function(_add_swift_library_single target name)
730730
OBJECT_LIBRARY
731731
SHARED
732732
STATIC
733-
TARGET_LIBRARY)
733+
TARGET_LIBRARY
734+
INSTALL_WITH_SHARED)
734735
set(SWIFTLIB_SINGLE_single_parameter_options
735736
ARCHITECTURE
736737
DEPLOYMENT_VERSION_IOS
@@ -1085,18 +1086,24 @@ function(_add_swift_library_single target name)
10851086
BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR}
10861087
LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR})
10871088

1089+
if(SWIFTLIB_INSTALL_WITH_SHARED)
1090+
set(swift_lib_dir ${SWIFTLIB_DIR})
1091+
else()
1092+
set(swift_lib_dir ${SWIFTSTATICLIB_DIR})
1093+
endif()
1094+
10881095
foreach(config ${CMAKE_CONFIGURATION_TYPES})
10891096
string(TOUPPER ${config} config_upper)
10901097
escape_path_for_xcode(
1091-
"${config}" "${SWIFTSTATICLIB_DIR}" config_lib_dir)
1098+
"${config}" "${swift_lib_dir}" config_lib_dir)
10921099
set_target_properties(${target_static} PROPERTIES
10931100
LIBRARY_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}
10941101
ARCHIVE_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR})
10951102
endforeach()
10961103

10971104
set_target_properties(${target_static} PROPERTIES
1098-
LIBRARY_OUTPUT_DIRECTORY ${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}
1099-
ARCHIVE_OUTPUT_DIRECTORY ${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR})
1105+
LIBRARY_OUTPUT_DIRECTORY ${swift_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}
1106+
ARCHIVE_OUTPUT_DIRECTORY ${swift_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR})
11001107
endif()
11011108

11021109
set_target_properties(${target}
@@ -1347,8 +1354,14 @@ function(_add_swift_library_single target name)
13471354
set_property(TARGET "${target_static}" APPEND_STRING PROPERTY
13481355
COMPILE_FLAGS " ${c_compile_flags}")
13491356
# FIXME: The fallback paths here are going to be dynamic libraries.
1357+
1358+
if(SWIFTLIB_INSTALL_WITH_SHARED)
1359+
set(search_base_dir ${SWIFTLIB_DIR})
1360+
else()
1361+
set(search_base_dir ${SWIFTSTATICLIB_DIR})
1362+
endif()
13501363
set(library_search_directories
1351-
"${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
1364+
"${search_base_dir}/${SWIFTLIB_SINGLE_SUBDIR}"
13521365
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
13531366
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
13541367
swift_target_link_search_directories("${target_static}" "${library_search_directories}")
@@ -1477,6 +1490,7 @@ endfunction()
14771490
# [IS_STDLIB]
14781491
# [IS_STDLIB_CORE]
14791492
# [TARGET_LIBRARY]
1493+
# [INSTALL_WITH_SHARED]
14801494
# INSTALL_IN_COMPONENT comp
14811495
# DEPLOYMENT_VERSION_OSX version
14821496
# DEPLOYMENT_VERSION_IOS version
@@ -1583,6 +1597,9 @@ endfunction()
15831597
# DEPLOYMENT_VERSION_WATCHOS
15841598
# The minimum deployment version to build for if this is an WATCHOS library.
15851599
#
1600+
# INSTALL_WITH_SHARED
1601+
# Install a static library target alongside shared libraries
1602+
#
15861603
# source1 ...
15871604
# Sources to add into this library.
15881605
function(add_swift_target_library name)
@@ -1597,7 +1614,8 @@ function(add_swift_target_library name)
15971614
OBJECT_LIBRARY
15981615
SHARED
15991616
STATIC
1600-
TARGET_LIBRARY)
1617+
TARGET_LIBRARY
1618+
INSTALL_WITH_SHARED)
16011619
set(SWIFTLIB_single_parameter_options
16021620
DEPLOYMENT_VERSION_IOS
16031621
DEPLOYMENT_VERSION_OSX
@@ -1882,6 +1900,7 @@ function(add_swift_target_library name)
18821900
${SWIFTLIB_SHARED_keyword}
18831901
${SWIFTLIB_STATIC_keyword}
18841902
${SWIFTLIB_OBJECT_LIBRARY_keyword}
1903+
${SWIFTLIB_INSTALL_WITH_SHARED_keyword}
18851904
${SWIFTLIB_SOURCES}
18861905
MODULE_TARGET ${MODULE_VARIANT_NAME}
18871906
SDK ${sdk}
@@ -1996,7 +2015,7 @@ function(add_swift_target_library name)
19962015
set(resource_dir_sdk_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
19972016
precondition(resource_dir_sdk_subdir)
19982017

1999-
if(SWIFTLIB_SHARED)
2018+
if(SWIFTLIB_SHARED OR SWIFTLIB_INSTALL_WITH_SHARED)
20002019
set(resource_dir "swift")
20012020
set(file_permissions
20022021
OWNER_READ OWNER_WRITE OWNER_EXECUTE
@@ -2058,10 +2077,18 @@ function(add_swift_target_library name)
20582077
list(APPEND THIN_INPUT_TARGETS_STATIC "${TARGET}-static")
20592078
endforeach()
20602079

2080+
if(SWIFTLIB_INSTALL_WITH_SHARED)
2081+
set(install_subdir "swift")
2082+
set(universal_subdir ${SWIFTLIB_DIR})
2083+
else()
2084+
set(install_subdir "swift_static")
2085+
set(universal_subdir ${SWIFTSTATICLIB_DIR})
2086+
endif()
2087+
20612088
set(lipo_target_static
20622089
"${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-static")
20632090
set(UNIVERSAL_LIBRARY_NAME
2064-
"${SWIFTSTATICLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
2091+
"${universal_subdir}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
20652092
_add_swift_lipo_target(SDK
20662093
${sdk}
20672094
TARGET
@@ -2070,7 +2097,7 @@ function(add_swift_target_library name)
20702097
"${UNIVERSAL_LIBRARY_NAME}"
20712098
${THIN_INPUT_TARGETS_STATIC})
20722099
swift_install_in_component(FILES "${UNIVERSAL_LIBRARY_NAME}"
2073-
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${resource_dir_sdk_subdir}"
2100+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${install_subdir}/${resource_dir_sdk_subdir}"
20742101
PERMISSIONS
20752102
OWNER_READ OWNER_WRITE
20762103
GROUP_READ

cmake/modules/SwiftSource.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,10 @@ function(_compile_swift_files
253253
list(APPEND swift_flags "-module-name" "${SWIFTFILE_MODULE_NAME}")
254254
endif()
255255

256-
# Force swift 5 mode for Standard Library.
256+
# Force swift 5 mode for Standard Library and overlays.
257257
if (SWIFTFILE_IS_STDLIB)
258258
list(APPEND swift_flags "-swift-version" "5")
259259
endif()
260-
261-
# Force swift 4 compatibility mode for overlays.
262260
if (SWIFTFILE_IS_SDK_OVERLAY)
263261
list(APPEND swift_flags "-swift-version" "5")
264262
endif()
@@ -267,6 +265,12 @@ function(_compile_swift_files
267265
list(APPEND swift_flags "-autolink-force-load")
268266
endif()
269267

268+
# Don't need to link runtime compatibility libraries for older runtimes
269+
# into the new runtime.
270+
if (SWIFTFILE_IS_STDLIB OR SWIFTFILE_IS_SDK_OVERLAY)
271+
list(APPEND swift_flags "-runtime-compatibility-version" "none")
272+
endif()
273+
270274
if (SWIFTFILE_IS_STDLIB_CORE OR SWIFTFILE_IS_SDK_OVERLAY)
271275
list(APPEND swift_flags "-warn-swift3-objc-inference-complete")
272276
endif()

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
// FIXME: This include is just for llvm::SanitizerCoverageOptions. We should
2727
// split the header upstream so we don't include so much.
2828
#include "llvm/Transforms/Instrumentation.h"
29+
#include "llvm/Support/VersionTuple.h"
2930
#include <string>
3031
#include <vector>
3132

@@ -224,6 +225,9 @@ class IRGenOptions {
224225
};
225226

226227
TypeInfoDumpFilter TypeInfoFilter;
228+
229+
/// Pull in runtime compatibility shim libraries by autolinking.
230+
Optional<llvm::VersionTuple> AutolinkRuntimeCompatibilityLibraryVersion;
227231

228232
IRGenOptions()
229233
: DWARFVersion(2), OutputKind(IRGenOutputKind::LLVMAssembly),

include/swift/Basic/Platform.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace llvm {
2121
class Triple;
22+
class VersionTuple;
2223
}
2324

2425
namespace swift {
@@ -85,6 +86,12 @@ namespace swift {
8586
/// The input triple should already be "normalized" in the sense that
8687
/// llvm::Triple::normalize() would not affect it.
8788
llvm::Triple getTargetSpecificModuleTriple(const llvm::Triple &triple);
89+
90+
91+
/// Get the Swift runtime version to deploy back to, given a deployment target expressed as an
92+
/// LLVM target triple.
93+
Optional<llvm::VersionTuple>
94+
getSwiftRuntimeCompatibilityVersionForTarget(const llvm::Triple &Triple);
8895
} // end namespace swift
8996

9097
#endif // SWIFT_BASIC_PLATFORM_H

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,4 +892,9 @@ def vfsoverlay_EQ : Joined<["-"], "vfsoverlay=">,
892892
def runtime_compatibility_version : Separate<["-"], "runtime-compatibility-version">,
893893
Flags<[FrontendOption]>,
894894
HelpText<"Link compatibility library for Swift runtime version, or 'none'">;
895+
896+
def disable_autolinking_runtime_compatibility : Flag<["-"], "disable-autolinking-runtime-compatibility">,
897+
Flags<[FrontendOption]>,
898+
HelpText<"Do not use autolinking for runtime compatibility libraries">;
899+
895900
include "FrontendOptions.td"

lib/Basic/Platform.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/ADT/StringExtras.h"
1515
#include "llvm/ADT/StringSwitch.h"
1616
#include "llvm/ADT/Triple.h"
17+
#include "llvm/Support/VersionTuple.h"
1718

1819
using namespace swift;
1920

@@ -313,3 +314,37 @@ llvm::Triple swift::getTargetSpecificModuleTriple(const llvm::Triple &triple) {
313314
return triple;
314315
}
315316

317+
Optional<llvm::VersionTuple>
318+
swift::getSwiftRuntimeCompatibilityVersionForTarget(const llvm::Triple &Triple){
319+
unsigned Major, Minor, Micro;
320+
321+
if (Triple.isMacOSX()) {
322+
Triple.getMacOSXVersion(Major, Minor, Micro);
323+
if (Major == 10) {
324+
if (Minor <= 14) {
325+
return llvm::VersionTuple(5, 0);
326+
} else {
327+
return None;
328+
}
329+
} else {
330+
return None;
331+
}
332+
} else if (Triple.isiOS()) { // includes tvOS
333+
Triple.getiOSVersion(Major, Minor, Micro);
334+
if (Major <= 12) {
335+
return llvm::VersionTuple(5, 0);
336+
} else {
337+
return None;
338+
}
339+
} else if (Triple.isWatchOS()) {
340+
Triple.getWatchOSVersion(Major, Minor, Micro);
341+
if (Major <= 5) {
342+
return llvm::VersionTuple(5, 0);
343+
} else {
344+
return None;
345+
}
346+
} else {
347+
return None;
348+
}
349+
}
350+

lib/Driver/DarwinToolChains.cpp

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/Support/Path.h"
3434
#include "llvm/Support/Process.h"
3535
#include "llvm/Support/Program.h"
36+
#include "llvm/Support/VersionTuple.h"
3637

3738
using namespace swift;
3839
using namespace swift::driver;
@@ -221,42 +222,6 @@ static bool wantsObjCRuntime(const llvm::Triple &triple) {
221222
llvm_unreachable("unknown Darwin OS");
222223
}
223224

224-
/// Return the earliest backward deployment compatibility version we need to
225-
/// link in for the given target triple, if any.
226-
static Optional<std::pair<unsigned, unsigned>>
227-
getSwiftRuntimeCompatibilityVersionForTarget(const llvm::Triple &Triple) {
228-
unsigned Major, Minor, Micro;
229-
230-
if (Triple.isMacOSX()) {
231-
Triple.getMacOSXVersion(Major, Minor, Micro);
232-
if (Major == 10) {
233-
if (Minor <= 14) {
234-
return std::make_pair(5u, 0u);
235-
} else {
236-
return None;
237-
}
238-
} else {
239-
return None;
240-
}
241-
} else if (Triple.isiOS()) { // includes tvOS
242-
Triple.getiOSVersion(Major, Minor, Micro);
243-
if (Major <= 12) {
244-
return std::make_pair(5u, 0u);
245-
} else {
246-
return None;
247-
}
248-
} else if (Triple.isWatchOS()) {
249-
Triple.getWatchOSVersion(Major, Minor, Micro);
250-
if (Major <= 5) {
251-
return std::make_pair(5u, 0u);
252-
} else {
253-
return None;
254-
}
255-
} else {
256-
return None;
257-
}
258-
}
259-
260225
ToolChain::InvocationInfo
261226
toolchains::Darwin::constructInvocation(const LinkJobAction &job,
262227
const JobContext &context) const
@@ -431,12 +396,13 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
431396

432397
// Link compatibility libraries, if we're deploying back to OSes that
433398
// have an older Swift runtime.
434-
Optional<std::pair<unsigned, unsigned>> runtimeCompatibilityVersion;
399+
Optional<llvm::VersionTuple> runtimeCompatibilityVersion;
435400

436401
if (context.Args.hasArg(options::OPT_runtime_compatibility_version)) {
437-
auto value = context.Args.getLastArgValue(options::OPT_runtime_compatibility_version);
402+
auto value = context.Args.getLastArgValue(
403+
options::OPT_runtime_compatibility_version);
438404
if (value.equals("5.0")) {
439-
runtimeCompatibilityVersion = std::make_pair(5u, 0u);
405+
runtimeCompatibilityVersion = llvm::VersionTuple(5, 0);
440406
} else if (value.equals("none")) {
441407
runtimeCompatibilityVersion = None;
442408
} else {
@@ -448,7 +414,7 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
448414
}
449415

450416
if (runtimeCompatibilityVersion) {
451-
if (*runtimeCompatibilityVersion <= std::make_pair(5u, 0u)) {
417+
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 0)) {
452418
// Swift 5.0 compatibility library
453419
SmallString<128> BackDeployLib;
454420
BackDeployLib.append(RuntimeLibPath);

lib/Driver/ToolChains.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ ToolChain::constructInvocation(const CompileJobAction &job,
433433
Arguments.push_back("-debug-info-store-invocation");
434434
}
435435

436+
if (context.Args.hasArg(
437+
options::OPT_disable_autolinking_runtime_compatibility)) {
438+
Arguments.push_back("-disable-autolinking-runtime-compatibility");
439+
}
440+
441+
if (auto arg = context.Args.getLastArg(
442+
options::OPT_runtime_compatibility_version)) {
443+
Arguments.push_back("-runtime-compatibility-version");
444+
Arguments.push_back(arg->getValue());
445+
}
446+
436447
return II;
437448
}
438449

lib/Frontend/CompilerInvocation.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,30 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
11551155
A->getAsString(Args), A->getValue());
11561156
}
11571157
}
1158+
1159+
// Autolink runtime compatibility libraries, if asked to.
1160+
if (!Args.hasArg(options::OPT_disable_autolinking_runtime_compatibility)) {
1161+
Optional<llvm::VersionTuple> runtimeCompatibilityVersion;
1162+
1163+
if (auto versionArg = Args.getLastArg(
1164+
options::OPT_runtime_compatibility_version)) {
1165+
auto version = StringRef(versionArg->getValue());
1166+
if (version.equals("none")) {
1167+
runtimeCompatibilityVersion = None;
1168+
} else if (version.equals("5.0")) {
1169+
runtimeCompatibilityVersion = llvm::VersionTuple(5, 0);
1170+
} else {
1171+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
1172+
versionArg->getAsString(Args), version);
1173+
}
1174+
} else {
1175+
runtimeCompatibilityVersion =
1176+
getSwiftRuntimeCompatibilityVersionForTarget(Triple);
1177+
}
1178+
1179+
Opts.AutolinkRuntimeCompatibilityLibraryVersion =
1180+
runtimeCompatibilityVersion;
1181+
}
11581182

11591183
return false;
11601184
}

0 commit comments

Comments
 (0)