Skip to content

Commit

Permalink
Merge pull request apple#1390 from swiftwasm/maxd/5.3-arm64-fix
Browse files Browse the repository at this point in the history
Fix cross-compilation issues in 5.3 after arm64 upstream merge
  • Loading branch information
MaxDesiatov committed Jul 7, 2020
2 parents c353f21 + 7016cb5 commit 6166408
Show file tree
Hide file tree
Showing 99 changed files with 1,028 additions and 279 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Expand Up @@ -462,6 +462,12 @@ if(SWIFT_PATH_TO_CMARK_BUILD)
endif()
message(STATUS "")

if("${SWIFT_NATIVE_LLVM_TOOLS_PATH}" STREQUAL "")
set(SWIFT_CROSS_COMPILING FALSE)
else()
set(SWIFT_CROSS_COMPILING TRUE)
endif()

include(SwiftSharedCMakeConfig)

# NOTE: We include this before SwiftComponents as it relies on some LLVM CMake
Expand Down
8 changes: 2 additions & 6 deletions benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake
Expand Up @@ -105,7 +105,7 @@ macro(configure_build)
endmacro()

macro(configure_sdks_darwin)
set(macosx_arch "x86_64")
set(macosx_arch "x86_64" "arm64")
set(iphoneos_arch "arm64" "arm64e" "armv7")
set(appletvos_arch "arm64")
set(watchos_arch "armv7k")
Expand Down Expand Up @@ -609,11 +609,7 @@ function (swift_benchmark_compile_archopts)

if(is_darwin)
# If host == target.
if("${BENCH_COMPILE_ARCHOPTS_PLATFORM}" STREQUAL "macosx")
set(OUTPUT_EXEC "${benchmark-bin-dir}/Benchmark_${BENCH_COMPILE_ARCHOPTS_OPT}")
else()
set(OUTPUT_EXEC "${benchmark-bin-dir}/Benchmark_${BENCH_COMPILE_ARCHOPTS_OPT}-${target}")
endif()
set(OUTPUT_EXEC "${benchmark-bin-dir}/Benchmark_${BENCH_COMPILE_ARCHOPTS_OPT}-${target}")
else()
# If we are on Linux, we do not support cross compiling.
set(OUTPUT_EXEC "${benchmark-bin-dir}/Benchmark_${BENCH_COMPILE_ARCHOPTS_OPT}")
Expand Down
27 changes: 22 additions & 5 deletions cmake/modules/DarwinSDKs.cmake
Expand Up @@ -4,17 +4,34 @@ option(SWIFT_ENABLE_IOS32

if(SWIFT_ENABLE_IOS32)
set(SUPPORTED_IOS_ARCHS "armv7;armv7s;arm64;arm64e")
set(SUPPORTED_IOS_SIMULATOR_ARCHS "i386;x86_64")
set(SUPPORTED_IOS_SIMULATOR_ARCHS "i386;x86_64;arm64")
else()
set(SUPPORTED_IOS_ARCHS "arm64;arm64e")
set(SUPPORTED_IOS_SIMULATOR_ARCHS "x86_64")
set(SUPPORTED_IOS_SIMULATOR_ARCHS "x86_64;arm64")
endif()

set(SUPPORTED_TVOS_ARCHS "arm64")
set(SUPPORTED_TVOS_SIMULATOR_ARCHS "x86_64")
set(SUPPORTED_TVOS_SIMULATOR_ARCHS "x86_64;arm64")
set(SUPPORTED_WATCHOS_ARCHS "armv7k")
set(SUPPORTED_WATCHOS_SIMULATOR_ARCHS "i386")
set(SUPPORTED_OSX_ARCHS "x86_64")
set(SUPPORTED_WATCHOS_SIMULATOR_ARCHS "i386;arm64")
set(SUPPORTED_OSX_ARCHS "x86_64;arm64;arm64e")

# Get the SDK version from SDKSettings.
execute_process(
COMMAND "defaults" "read" "${CMAKE_OSX_SYSROOT}/SDKSettings.plist" "Version"
OUTPUT_VARIABLE SWIFT_OSX_SDK_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

# Remove the last component, if any. e.g. 10.15.26 -> 10.15
string(REGEX REPLACE "\([0-9]*[.][0-9]*\)[.][0-9]*" "\\1"
SWIFT_OSX_SDK_VERSION "${SWIFT_OSX_SDK_VERSION}")

if (${SWIFT_OSX_SDK_VERSION} STREQUAL "10.14" OR
${SWIFT_OSX_SDK_VERSION} STREQUAL "10.15")
set(SUPPORTED_OSX_ARCHS "x86_64")
else()
set(SUPPORTED_OSX_ARCHS "x86_64;arm64e")
endif()

is_sdk_requested(OSX swift_build_osx)
if(swift_build_osx)
Expand Down
35 changes: 35 additions & 0 deletions cmake/modules/SwiftConfigureSDK.cmake
Expand Up @@ -85,6 +85,38 @@ function(_report_sdk prefix)
message(STATUS "")
endfunction()

# Remove architectures not supported by the SDK from the given list.
function(remove_sdk_unsupported_archs name os sdk_path architectures_var)
execute_process(COMMAND
/usr/libexec/PlistBuddy -c "Print :SupportedTargets:${os}:Archs" ${sdk_path}/SDKSettings.plist
OUTPUT_VARIABLE sdk_supported_archs
RESULT_VARIABLE plist_error)

if (NOT plist_error EQUAL 0)
message(STATUS "${os} SDK at ${sdk_path} does not publish its supported architectures")
return()
endif()

set(architectures)
foreach(arch ${${architectures_var}})
if(sdk_supported_archs MATCHES "${arch}\n")
list(APPEND architectures ${arch})
elseif(arch MATCHES "^armv7(s)?$" AND os STREQUAL "iphoneos")
# 32-bit iOS is not listed explicitly in SDK settings.
message(STATUS "Assuming ${name} SDK at ${sdk_path} supports architecture ${arch}")
list(APPEND architectures ${arch})
elseif(arch STREQUAL "i386" AND os STREQUAL "iphonesimulator")
# 32-bit iOS simulatoris not listed explicitly in SDK settings.
message(STATUS "Assuming ${name} SDK at ${sdk_path} supports architecture ${arch}")
list(APPEND architectures ${arch})
else()
message(STATUS "${name} SDK at ${sdk_path} does not support architecture ${arch}")
endif()
endforeach()

set("${architectures_var}" ${architectures} PARENT_SCOPE)
endfunction()

# Configure an SDK
#
# Usage:
Expand Down Expand Up @@ -164,6 +196,9 @@ macro(configure_sdk_darwin
SWIFT_SDK_${prefix}_ARCHITECTURES) # result
endif()

# Remove any architectures not supported by the SDK.
remove_sdk_unsupported_archs(${name} ${xcrun_name} ${SWIFT_SDK_${prefix}_PATH} SWIFT_SDK_${prefix}_ARCHITECTURES)

list_intersect(
"${SWIFT_DARWIN_MODULE_ARCHS}" # lhs
"${architectures}" # rhs
Expand Down
2 changes: 1 addition & 1 deletion cmake/modules/SwiftSharedCMakeConfig.cmake
Expand Up @@ -58,7 +58,7 @@ macro(swift_common_standalone_build_config_llvm product)
fix_imported_targets_for_xcode("${LLVM_EXPORTED_TARGETS}")
endif()

if(NOT CMAKE_CROSSCOMPILING)
if(NOT CMAKE_CROSSCOMPILING AND NOT SWIFT_CROSS_COMPILING)
set(${product}_NATIVE_LLVM_TOOLS_PATH "${LLVM_TOOLS_BINARY_DIR}")
endif()

Expand Down
22 changes: 22 additions & 0 deletions include/swift/AST/AvailabilitySpec.h
Expand Up @@ -68,16 +68,33 @@ class PlatformVersionConstraintAvailabilitySpec : public AvailabilitySpec {
SourceLoc PlatformLoc;

llvm::VersionTuple Version;

// For macOS Big Sur, we we canonicalize 10.16 to 11.0 for compile-time
// checking since clang canonicalizes availability markup. However, to
// support Beta versions of macOS Big Sur where the OS
// reports 10.16 at run time, we need to compare against 10.16,
//
// This means for:
//
// if #available(macOS 10.16, *) { ... }
//
// we need to keep around both a canonical version for use in compile-time
// checks and an uncanonicalized version for the version to actually codegen
// with.
llvm::VersionTuple RuntimeVersion;

SourceRange VersionSrcRange;

public:
PlatformVersionConstraintAvailabilitySpec(PlatformKind Platform,
SourceLoc PlatformLoc,
llvm::VersionTuple Version,
llvm::VersionTuple RuntimeVersion,
SourceRange VersionSrcRange)
: AvailabilitySpec(AvailabilitySpecKind::PlatformVersionConstraint),
Platform(Platform),
PlatformLoc(PlatformLoc), Version(Version),
RuntimeVersion(RuntimeVersion),
VersionSrcRange(VersionSrcRange) {}

/// The required platform.
Expand All @@ -93,6 +110,11 @@ class PlatformVersionConstraintAvailabilitySpec : public AvailabilitySpec {
llvm::VersionTuple getVersion() const { return Version; }
SourceRange getVersionSrcRange() const { return VersionSrcRange; }

// The version to be used in codegen for version comparisons at run time.
// This is required to support beta versions of macOS Big Sur that
// report 10.16 at run time.
llvm::VersionTuple getRuntimeVersion() const { return RuntimeVersion; }

SourceRange getSourceRange() const;

void print(raw_ostream &OS, unsigned Indent) const;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/PlatformKind.h
Expand Up @@ -20,6 +20,7 @@
#include "swift/Basic/LLVM.h"
#include "swift/Config.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/VersionTuple.h"

namespace swift {

Expand Down Expand Up @@ -65,6 +66,9 @@ PlatformKind targetPlatform(LangOptions &LangOpts);
/// an explicit attribute for the child.
bool inheritsAvailabilityFromPlatform(PlatformKind Child, PlatformKind Parent);

llvm::VersionTuple canonicalizePlatformVersion(
PlatformKind platform, const llvm::VersionTuple &version);

} // end namespace swift

#endif
4 changes: 2 additions & 2 deletions include/swift/Remote/InProcessMemoryReader.h
Expand Up @@ -38,7 +38,7 @@ class InProcessMemoryReader final : public MemoryReader {
#else
auto applePlatform = false;
#endif
#if defined(__APPLE__) && __APPLE__ && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_IOS) && TARGET_OS_WATCH) || (defined(TARGET_OS_TV) && TARGET_OS_TV))
#if defined(__APPLE__) && __APPLE__ && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_IOS) && TARGET_OS_WATCH) || (defined(TARGET_OS_TV) && TARGET_OS_TV) || defined(__arm64__))
auto iosDerivedPlatform = true;
#else
auto iosDerivedPlatform = false;
Expand Down Expand Up @@ -67,7 +67,7 @@ class InProcessMemoryReader final : public MemoryReader {
case DLQ_GetObjCReservedLowBits: {
auto result = static_cast<uint8_t *>(outBuffer);
if (applePlatform && !iosDerivedPlatform && (sizeof(void *) == 8)) {
// Obj-C reserves low bit on 64-bit macOS only.
// Obj-C reserves low bit on 64-bit Intel macOS only.
// Other Apple platforms don't reserve this bit (even when
// running on x86_64-based simulators).
*result = 1;
Expand Down
Expand Up @@ -585,7 +585,7 @@ swift_reflection_interop_minimalDataLayoutQueryFunction8(
#else
int applePlatform = 0;
#endif
#if defined(__APPLE__) && __APPLE__ && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_IOS) && TARGET_OS_WATCH) || (defined(TARGET_OS_TV) && TARGET_OS_TV))
#if defined(__APPLE__) && __APPLE__ && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_IOS) && TARGET_OS_WATCH) || (defined(TARGET_OS_TV) && TARGET_OS_TV) || defined(__arm64__))
int iosDerivedPlatform = 1;
#else
int iosDerivedPlatform = 0;
Expand Down
55 changes: 45 additions & 10 deletions lib/AST/Availability.cpp
Expand Up @@ -246,9 +246,16 @@ AvailabilityContext ASTContext::getSwift50Availability() {
return AvailabilityContext::alwaysAvailable();

if (target.isMacOSX()) {
if (target.isAArch64())
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(10,14,4)));
} else if (target.isiOS()) {
if (target.isAArch64() &&
(target.isSimulatorEnvironment() || target.isMacCatalystEnvironment()))
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(12,2)));
} else if (target.isWatchOS()) {
Expand All @@ -274,9 +281,16 @@ AvailabilityContext ASTContext::getSwift51Availability() {
return AvailabilityContext::alwaysAvailable();

if (target.isMacOSX()) {
if (target.isAArch64())
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(10,15,0)));
} else if (target.isiOS()) {
if (target.isAArch64() &&
(target.isSimulatorEnvironment() || target.isMacCatalystEnvironment()))
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(13,0,0)));
} else if (target.isWatchOS()) {
Expand All @@ -301,18 +315,27 @@ AvailabilityContext ASTContext::getSwift52Availability() {
if (target.getArchName() == "arm64e")
return AvailabilityContext::alwaysAvailable();

if (target.isMacOSX() ) {
if (target.isMacOSX()) {
if (target.isAArch64())
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(10, 99, 0)));
VersionRange::allGTE(llvm::VersionTuple(10, 15, 4)));
} else if (target.isiOS()) {
if (target.isAArch64() &&
(target.isSimulatorEnvironment() || target.isMacCatalystEnvironment()))
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(99, 0, 0)));
VersionRange::allGTE(llvm::VersionTuple(13, 4, 0)));
} else if (target.isWatchOS()) {
if (target.isArch64Bit())
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(9, 99, 0)));
} else {
return AvailabilityContext::alwaysAvailable();
VersionRange::allGTE(llvm::VersionTuple(6, 2, 0)));
}
return AvailabilityContext::alwaysAvailable();
}

AvailabilityContext ASTContext::getSwift53Availability() {
Expand All @@ -322,14 +345,26 @@ AvailabilityContext ASTContext::getSwift53Availability() {
return AvailabilityContext::alwaysAvailable();

if (target.isMacOSX() ) {
if (target.isAArch64())
return AvailabilityContext::alwaysAvailable();

llvm::VersionTuple macOVersion53(10, 16, 0);
macOVersion53 = canonicalizePlatformVersion(PlatformKind::OSX, macOVersion53);
return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(10, 99, 0)));
VersionRange::allGTE(macOVersion53));
} else if (target.isiOS()) {
if (target.isAArch64() &&
(target.isSimulatorEnvironment() || target.isMacCatalystEnvironment()))
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(99, 0, 0)));
VersionRange::allGTE(llvm::VersionTuple(14, 0, 0)));
} else if (target.isWatchOS()) {
if (target.isArch64Bit())
return AvailabilityContext::alwaysAvailable();

return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(9, 99, 0)));
VersionRange::allGTE(llvm::VersionTuple(7, 0, 0)));
} else {
return AvailabilityContext::alwaysAvailable();
}
Expand All @@ -340,7 +375,7 @@ AvailabilityContext ASTContext::getSwiftFutureAvailability() {

if (target.isMacOSX() ) {
return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(10, 99, 0)));
VersionRange::allGTE(llvm::VersionTuple(99, 99, 0)));
} else if (target.isiOS()) {
return AvailabilityContext(
VersionRange::allGTE(llvm::VersionTuple(99, 0, 0)));
Expand Down
15 changes: 15 additions & 0 deletions lib/AST/PlatformKind.cpp
Expand Up @@ -20,6 +20,7 @@
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"


using namespace swift;

StringRef swift::platformString(PlatformKind platform) {
Expand Down Expand Up @@ -155,3 +156,17 @@ bool swift::inheritsAvailabilityFromPlatform(PlatformKind Child,

return false;
}

llvm::VersionTuple swift::canonicalizePlatformVersion(
PlatformKind platform, const llvm::VersionTuple &version) {

// Canonicalize macOS version for macOS Big Sur to great
// 10.16 as 11.0.
if (platform == PlatformKind::OSX ||
platform == PlatformKind::OSXApplicationExtension) {
return llvm::Triple::getCanonicalVersionForOS(llvm::Triple::MacOSX,
version);
}

return version;
}
12 changes: 12 additions & 0 deletions lib/Basic/Platform.cpp
Expand Up @@ -403,6 +403,9 @@ swift::getSwiftRuntimeCompatibilityVersionForTarget(
if (Triple.isMacOSX()) {
Triple.getMacOSXVersion(Major, Minor, Micro);
if (Major == 10) {
if (Triple.isAArch64() && Minor <= 16)
return llvm::VersionTuple(5, 3);

if (Minor <= 14) {
return llvm::VersionTuple(5, 0);
} else if (Minor <= 15) {
Expand All @@ -412,9 +415,18 @@ swift::getSwiftRuntimeCompatibilityVersionForTarget(
return llvm::VersionTuple(5, 2);
}
}
} else if (Major == 11) {
return llvm::VersionTuple(5, 3);
}
} else if (Triple.isiOS()) { // includes tvOS
Triple.getiOSVersion(Major, Minor, Micro);

// arm64 simulators and macCatalyst are introduced in iOS 14.0/tvOS 14.0
// with Swift 5.3
if (Triple.isAArch64() && Major <= 14 &&
(Triple.isSimulatorEnvironment() || Triple.isMacCatalystEnvironment()))
return llvm::VersionTuple(5, 3);

if (Major <= 12) {
return llvm::VersionTuple(5, 0);
} else if (Major <= 13) {
Expand Down

0 comments on commit 6166408

Please sign in to comment.