Skip to content

Commit

Permalink
Support AARCH64 for Fuchsia
Browse files Browse the repository at this point in the history
This change adds support for compiling Fuchsia for ARM64. It also
changes the arguments passed to build-script to remove hardcoded file
paths to sysroots and include directories in a Fuchsia build tree.

Some changes were made to be able to support building more than one
architecture for a non-Darwin platform as the Unix sdk targets only
support one at a time.
  • Loading branch information
zbowling committed Nov 23, 2017
1 parent 6128a51 commit a499c41
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 60 deletions.
50 changes: 41 additions & 9 deletions CMakeLists.txt
Expand Up @@ -181,10 +181,35 @@ set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING
"Path on an Android device where build products will be pushed. These are used when running the test suite against the device")

#
# User-configurable ICU specific options for Android, FreeBSD, Linux and Haiku.
# User-configurable Fuchsia specific options
#

foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;FUCHSIA;HAIKU)
set(SWIFT_FUCHSIA_AARCH64_SYSROOT "" CACHE STRING
"Path to a Fuchsia zircon aarch64 sysroot directory")

set(SWIFT_FUCHSIA_X86_64_SYSROOT "" CACHE STRING
"Path to a Fuchsia zircon x86_64 sysroot directory")

set(SWIFT_FUCHSIA_AARCH64_LIBS "" CACHE STRING
"Path to a Fuchsia aarch64 shared libs directory")

set(SWIFT_FUCHSIA_X86_64_LIBS "" CACHE STRING
"Path to a Fuchsia x86_64 shared libs directory")

set(SWIFT_FUCHSIA_ICU_UC_INCLUDE "" CACHE STRING
"Path to a directory containing Fuchsia's ICU common headers")

set(SWIFT_FUCHSIA_ICU_I18N_INCLUDE "" CACHE STRING
"Path to a directory containing Fuchsia's ICU i18n headers")

set(SWIFT_FUCHSIA_TOOLCHAIN_PATH "" CACHE STRING
"Path to a directory containing a Fuchsia clang toolchain")

#
# User-configurable ICU specific options for non-Darwin SDKs.
#

foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU)
set(SWIFT_${sdk}_ICU_UC "" CACHE STRING
"Path to a directory containing the icuuc library for ${sdk}")
set(SWIFT_${sdk}_ICU_UC_INCLUDE "" CACHE STRING
Expand Down Expand Up @@ -755,15 +780,22 @@ endif()

# Should we cross-compile the standard library for Fuchsia
is_sdk_requested(FUCHSIA swift_build_fuchsia)
if(swift_build_fuchsia AND NOT "${SWIFT_FUCHSIA_BUILD_PATH}" STREQUAL "")
# HACK: support arm64 fuchsia builds
set(SWIFT_FUCHSIA_SYSROOT
"${SWIFT_FUCHSIA_BUILD_PATH}/out/build-zircon/build-zircon-pc-x86-64/sysroot")
if(swift_build_fuchsia)
set(fuchsia_archs)

if (NOT SWIFT_FUCHSIA_AARCH64_SYSROOT STREQUAL "")
set(SWIFT_SDK_FUCHSIA_ARCH_aarch64_PATH "${SWIFT_FUCHSIA_AARCH64_SYSROOT}")
list(APPEND fuchsia_archs "aarch64")
endif()
if (NOT SWIFT_FUCHSIA_X86_64_SYSROOT STREQUAL "")
set(SWIFT_SDK_FUCHSIA_ARCH_x86_64_PATH "${SWIFT_FUCHSIA_X86_64_SYSROOT}")
list(APPEND fuchsia_archs "x86_64")
endif()

set(SWIFT_SDK_FUCHSIA_ARCH_x86_64_LINKER
"${SWIFT_FUCHSIA_BUILD_PATH}/buildtools/linux-x64/clang/bin/ld.lld")
set(SWIFT_FUCHSIA_ICU_UC "-licuuc")
set(SWIFT_FUCHSIA_ICU_I18N "-licui18n")

configure_sdk_unix(FUCHSIA "Fuchsia" "fuchsia" "fuchsia" "x86_64" "x86_64-unknown-fuchsia" "${SWIFT_FUCHSIA_SYSROOT}")
configure_sdk_fuchsia(FUCHSIA "Fuchsia" "fuchsia" "fuchsia" "${fuchsia_archs}")

if (NOT ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux"))
message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Fuchsia")
Expand Down
37 changes: 23 additions & 14 deletions cmake/modules/AddSwift.cmake
Expand Up @@ -107,9 +107,9 @@ function(_add_variant_c_compile_link_flags)

is_darwin_based_sdk("${CFLAGS_SDK}" IS_DARWIN)
if(IS_DARWIN)
list(APPEND result "-isysroot" "${SWIFT_SDK_${CFLAGS_SDK}_PATH}")
elseif(NOT SWIFT_COMPILER_IS_MSVC_LIKE AND NOT "${SWIFT_SDK_${CFLAGS_SDK}_PATH}" STREQUAL "/")
list(APPEND result "--sysroot=${SWIFT_SDK_${CFLAGS_SDK}_PATH}")
list(APPEND result "-isysroot" "${SWIFT_SDK_${CFLAGS_SDK}_ARCH_${CFLAGS_ARCH}_PATH}")
elseif(NOT SWIFT_COMPILER_IS_MSVC_LIKE AND NOT "${SWIFT_SDK_${CFLAGS_SDK}_ARCH_${CFLAGS_ARCH}_PATH}" STREQUAL "/")
list(APPEND result "--sysroot=${SWIFT_SDK_${CFLAGS_SDK}_ARCH_${CFLAGS_ARCH}_PATH}")
endif()

if("${CFLAGS_SDK}" STREQUAL "ANDROID")
Expand All @@ -121,8 +121,7 @@ function(_add_variant_c_compile_link_flags)

if("${CFLAGS_SDK}" STREQUAL "FUCHSIA")
list(APPEND result
"-I${SWIFT_FUCHSIA_BUILD_PATH}/buildtools/linux-x64/clang/lib/x86_64-fuchsia/include/c++/v1/"
"-L${SWIFT_FUCHSIA_BUILD_PATH}/buildtools/linux-x64/clang/lib/x86_64-fuchsia/lib")
"-I${SWIFT_FUCHSIA_TOOLCHAIN_PATH}/lib/${CFLAGS_ARCH}-fuchsia/include/c++/v1/")
endif()

if(IS_DARWIN)
Expand Down Expand Up @@ -277,10 +276,15 @@ function(_add_variant_c_compile_flags)
endif()

if("${CFLAGS_SDK}" STREQUAL "FUCHSIA")
if ("${CFLAGS_ARCH}" STREQUAL "x86_64")
# LLVM may assume that CMPXCHG16B instruction doesn't exist on some x64 cpus
# and will attempt to replace it with a libcall to a libgcc function that
# doesn't exist in compiler-rt
list(APPEND result "-mcx16")
endif()
list(APPEND result
"-mcx16"
"-I${SWIFT_FUCHSIA_BUILD_PATH}/third_party/icu/source/common/"
"-I${SWIFT_FUCHSIA_BUILD_PATH}/third_party/icu/source/i18n/")
"-I${SWIFT_FUCHSIA_ICU_CU_INCLUDE}"
"-I${SWIFT_FUCHSIA_ICU_I18N_INCLUDE}")
endif()

set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
Expand All @@ -290,9 +294,9 @@ function(_add_variant_swift_compile_flags
sdk arch build_type enable_assertions result_var_name)
set(result ${${result_var_name}})

# On Windows, we don't set SWIFT_SDK_WINDOWS_PATH, so don't include it.
# On Windows, we don't set SWIFT_SDK_WINDOWS_ARCH_{ARCH}_PATH, so don't include it.
if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
list(APPEND result "-sdk" "${SWIFT_SDK_${sdk}_PATH}")
list(APPEND result "-sdk" "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}")
endif()

if(BUILD_STANDALONE)
Expand All @@ -307,7 +311,7 @@ function(_add_variant_swift_compile_flags
is_darwin_based_sdk("${sdk}" IS_DARWIN)
if(IS_DARWIN)
list(APPEND result
"-F" "${SWIFT_SDK_${sdk}_PATH}/../../../Developer/Library/Frameworks")
"-F" "${SWIFT_SDK_${sdk}_ARCH_{arch}_PATH}/../../../Developer/Library/Frameworks")
endif()

is_build_type_optimized("${build_type}" optimized)
Expand Down Expand Up @@ -340,7 +344,7 @@ function(_add_variant_link_flags)
${ARGN})

precondition(LFLAGS_SDK MESSAGE "Should specify an SDK")
precondition(LFLAGS_SDK MESSAGE "Should specify an architecture")
precondition(LFLAGS_ARCH MESSAGE "Should specify an architecture")

set(result ${${LFLAGS_RESULT_VAR_NAME}})
set(library_search_directories ${${LFLAGS_LIBRARY_SEARCH_DIRECTORIES_VAR_NAME}})
Expand Down Expand Up @@ -383,7 +387,12 @@ function(_add_variant_link_flags)
"${SWIFT_ANDROID_PREBUILT_PATH}/arm-linux-androideabi/lib/armv7-a"
"${SWIFT_ANDROID_PREBUILT_PATH}/lib/gcc/arm-linux-androideabi/${SWIFT_ANDROID_NDK_GCC_VERSION}.x")
elseif("${LFLAGS_SDK}" STREQUAL "FUCHSIA")
list(APPEND result "-lc" "-ldl")
list(APPEND result "-lc" "-ldl" "-lzircon" "-lfdio")
if (LFLAGS_ARCH STREQUAL "aarch64")
list(APPEND library_search_directories "${SWIFT_FUCHSIA_AARCH64_LIBS}")
elseif (LFLAGS_ARCH STREQUAL "x86_64")
list(APPEND library_search_directories "${SWIFT_FUCHSIA_X86_64_LIBS}")
endif()
else()
# If lto is enabled, we need to add the object path flag so that the LTO code
# generator leaves the intermediate object file in a place where it will not
Expand Down Expand Up @@ -1617,7 +1626,7 @@ function(add_swift_library name)
# Add PrivateFrameworks, rdar://28466433
set(swiftlib_link_flags_all ${SWIFTLIB_LINK_FLAGS})
if(SWIFTLIB_IS_SDK_OVERLAY)
list(APPEND swiftlib_swift_compile_flags_all "-Fsystem" "${SWIFT_SDK_${sdk}_PATH}/System/Library/PrivateFrameworks/")
list(APPEND swiftlib_swift_compile_flags_all "-Fsystem" "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/System/Library/PrivateFrameworks/")
endif()

if("${sdk}" STREQUAL "IOS_SIMULATOR")
Expand Down
42 changes: 40 additions & 2 deletions cmake/modules/SwiftConfigureSDK.cmake
Expand Up @@ -22,7 +22,9 @@ function(_report_sdk prefix)
message(STATUS " ${arch} LIB: ${${arch}_LIB}")
endforeach()
else()
message(STATUS " Path: ${SWIFT_SDK_${prefix}_PATH}")
foreach(arch ${SWIFT_SDK_${prefix}_ARCHITECTURES})
message(STATUS " ${arch} Path: ${SWIFT_SDK_${prefix}_ARCH_${ARCH}_PATH}")
endforeach()
endif()
message(STATUS " Version: ${SWIFT_SDK_${prefix}_VERSION}")
message(STATUS " Build number: ${SWIFT_SDK_${prefix}_BUILD_NUMBER}")
Expand Down Expand Up @@ -122,6 +124,9 @@ macro(configure_sdk_darwin
set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "MACHO")

foreach(arch ${architectures})
# On Darwin, all archs share the same SDK path.
set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH "${SWIFT_SDK_${prefix}_PATH}")

set(SWIFT_SDK_${prefix}_ARCH_${arch}_TRIPLE
"${arch}-apple-${SWIFT_SDK_${prefix}_TRIPLE_NAME}${SWIFT_SDK_${prefix}_DEPLOYMENT_VERSION}")
endforeach()
Expand All @@ -137,8 +142,10 @@ macro(configure_sdk_unix
# Note: this has to be implemented as a macro because it sets global
# variables.

# Todo: this only supports building an SDK for one target arch only.

set(SWIFT_SDK_${prefix}_NAME "${name}")
set(SWIFT_SDK_${prefix}_PATH "${sdkpath}")
set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH "${sdkpath}")
set(SWIFT_SDK_${prefix}_VERSION "don't use")
set(SWIFT_SDK_${prefix}_BUILD_NUMBER "don't use")
set(SWIFT_SDK_${prefix}_DEPLOYMENT_VERSION "don't use")
Expand All @@ -160,6 +167,37 @@ macro(configure_sdk_unix
_report_sdk("${prefix}")
endmacro()

macro(configure_sdk_fuchsia
prefix name lib_subdir triple_name architectures)
# Note: this has to be implemented as a macro because it sets global
# variables.

set(SWIFT_SDK_${prefix}_NAME "${name}")
# SDK PATHs are configured by caller
set(SWIFT_SDK_${prefix}_VERSION "don't use")
set(SWIFT_SDK_${prefix}_BUILD_NUMBER "don't use")
set(SWIFT_SDK_${prefix}_DEPLOYMENT_VERSION "don't use")
set(SWIFT_SDK_${prefix}_LIB_SUBDIR "${lib_subdir}")
set(SWIFT_SDK_${prefix}_VERSION_MIN_NAME "")
set(SWIFT_SDK_${prefix}_TRIPLE_NAME "${triple_name}")
set(SWIFT_SDK_${prefix}_ARCHITECTURES "${architectures}")
if("${prefix}" STREQUAL "CYGWIN")
set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "COFF")
else()
set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "ELF")
endif()

foreach(arch ${architectures})
set(SWIFT_SDK_${prefix}_ARCH_${arch}_TRIPLE "${arch}-unknown-fuchsia")
set(SWIFT_SDK_${prefix}_ARCH_${arch}_LINKER "${SWIFT_FUCHSIA_TOOLCHAIN_PATH}/bin/ld.lld")
endforeach()

# Add this to the list of known SDKs.
list(APPEND SWIFT_CONFIGURED_SDKS "${prefix}")

_report_sdk("${prefix}")
endmacro()

macro(configure_sdk_windows prefix sdk_name environment architectures)
# Note: this has to be implemented as a macro because it sets global
# variables.
Expand Down
18 changes: 9 additions & 9 deletions lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -823,7 +823,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,

if (FrontendOpts.InputKind == InputFileKind::IFK_SIL)
Opts.DisableAvailabilityChecking = true;

if (auto A = Args.getLastArg(OPT_enable_access_control,
OPT_disable_access_control)) {
Opts.EnableAccessControl
Expand Down Expand Up @@ -854,7 +854,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.EnableTargetOSChecking
= A->getOption().matches(OPT_enable_target_os_checking);
}

Opts.EnableASTScopeLookup |= Args.hasArg(OPT_enable_astscope_lookup);
Opts.DebugConstraintSolver |= Args.hasArg(OPT_debug_constraints);
Opts.EnableConstraintPropagation |= Args.hasArg(OPT_propagate_constraints);
Expand Down Expand Up @@ -892,7 +892,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,

Opts.DebugConstraintSolverAttempt = attempt;
}

if (const Arg *A = Args.getLastArg(OPT_debug_forbid_typecheck_prefix)) {
Opts.DebugForbidTypecheckPrefix = A->getValue();
}
Expand All @@ -904,7 +904,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
A->getAsString(Args), A->getValue());
return true;
}

Opts.SolverMemoryThreshold = threshold;
}

Expand All @@ -929,7 +929,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,

Opts.MaxCircularityDepth = threshold;
}

for (const Arg *A : Args.filtered(OPT_D)) {
Opts.addCustomConditionalCompilationFlag(A->getValue());
}
Expand Down Expand Up @@ -1102,9 +1102,9 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts,
// Opts.RuntimeIncludePath is set by calls to
// setRuntimeIncludePath() or setMainExecutablePath().
// Opts.RuntimeImportPath is set by calls to
// setRuntimeIncludePath() or setMainExecutablePath() and
// setRuntimeIncludePath() or setMainExecutablePath() and
// updated by calls to setTargetTriple() or parseArgs().
// Assumes exactly one of setMainExecutablePath() or setRuntimeIncludePath()
// Assumes exactly one of setMainExecutablePath() or setRuntimeIncludePath()
// is called before setTargetTriple() and parseArgs().
// TODO: improve the handling of RuntimeIncludePath.

Expand Down Expand Up @@ -1222,7 +1222,7 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
return true;
}
}

if (const Arg *A = Args.getLastArg(OPT_disable_sil_linking,
OPT_sil_link_all)) {
if (A->getOption().matches(OPT_disable_sil_linking))
Expand Down Expand Up @@ -1486,7 +1486,7 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,

if (Args.hasArg(OPT_use_jit))
Opts.UseJIT = true;

for (const Arg *A : Args.filtered(OPT_verify_type_layout)) {
Opts.VerifyTypeLayoutNames.push_back(A->getValue());
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/Platform/CMakeLists.txt
Expand Up @@ -71,8 +71,8 @@ foreach(sdk ${SWIFT_SDKS})
endif()

if(NOT "${sdk}" STREQUAL "HAIKU")
set(GLIBC_INCLUDE_PATH "${SWIFT_SDK_${sdk}_PATH}/${GLIBC_SYSROOT_RELATIVE_INCLUDE_PATH}")
set(GLIBC_ARCH_INCLUDE_PATH "${SWIFT_SDK_${sdk}_PATH}/${GLIBC_SYSROOT_RELATIVE_ARCH_INCLUDE_PATH}")
set(GLIBC_INCLUDE_PATH "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/${GLIBC_SYSROOT_RELATIVE_INCLUDE_PATH}")
set(GLIBC_ARCH_INCLUDE_PATH "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/${GLIBC_SYSROOT_RELATIVE_ARCH_INCLUDE_PATH}")
endif()

set(glibc_modulemap_source "glibc.modulemap.gyb")
Expand All @@ -94,7 +94,7 @@ foreach(sdk ${SWIFT_SDKS})

# If this SDK is a target for a non-native host, create a native modulemap
# without a sysroot prefix. This is the one we'll install instead.
if(NOT "${SWIFT_SDK_${sdk}_PATH}" STREQUAL "/")
if(NOT "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}" STREQUAL "/")

set(glibc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/glibc.modulemap")
handle_gyb_source_single(glibc_modulemap_native_target
Expand Down

0 comments on commit a499c41

Please sign in to comment.