Skip to content

Commit ff06812

Browse files
committed
[CMake] Add broader support for cross-compiling the portions of the compiler that are written in Swift to non-Darwin Unix
Add cross-compilation flags for the newly added Swift source in `lib/ASTGen/`, similar to how `SwiftCompilerSources/` is already cross-compiled for other platforms. Make sure the Swift source in the compiler builds and links against `SWIFTLIB_DIR` in this cross-compilation build directory, not the one that comes with the native host compiler. This requires changing the dependency chain in `CROSSCOMPILE` mode, as normally the Swift compiler is built first when building natively for the host, then it's used to build the stdlib. However, when cross-compiling the toolchain, the stdlib must be cross-compiled first by the host compiler, then the portions of the Swift compiler written in Swift must be cross-compiled with that new stdlib. All these dependency changes simply change that compilation order when cross-compiling, including removing the dependency that the Swift compiler is built before the stdlib when cross-compiling the Swift compiler. All changes in this pull are gated on the `CROSSCOMPILE` mode, so they will not affect any of the existing CI or build presets.
1 parent d8b5b88 commit ff06812

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ function(add_swift_compiler_modules_library name)
148148
list(APPEND sdk_option "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift")
149149
endif()
150150
elseif(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
151-
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
152151
# NOTE: prepending allows SWIFT_COMPILER_SOURCES_SDK_FLAGS to override the
153152
# resource directory if needed.
154-
list(PREPEND sdk_option "-resource-dir" "${swift_exec_bin_dir}/../lib/swift")
153+
list(PREPEND sdk_option "-resource-dir" "${SWIFTLIB_DIR}")
155154
endif()
156155
get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
157156
${SWIFT_HOST_VARIANT_ARCH} "${deployment_version}")
@@ -225,6 +224,9 @@ function(add_swift_compiler_modules_library name)
225224
importedHeaderDependencies
226225
COMMENT "Building swift module ${module}")
227226

227+
if(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
228+
add_dependencies(${dep_target} swift-stdlib-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH})
229+
endif()
228230
set("${module}_dep_target" ${dep_target})
229231
set(all_module_targets ${all_module_targets} ${dep_target})
230232
endforeach()

cmake/modules/AddPureSwift.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ function(_add_host_swift_compile_options name)
6060
$<$<COMPILE_LANGUAGE:Swift>:none>)
6161

6262
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${SWIFT_HOST_TRIPLE}>)
63+
if(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
64+
add_dependencies(${name} swift-stdlib-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH})
65+
target_compile_options(${name} PRIVATE
66+
$<$<COMPILE_LANGUAGE:Swift>:-sdk;${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH};>
67+
$<$<COMPILE_LANGUAGE:Swift>:-resource-dir;${SWIFTLIB_DIR};>)
68+
if (NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
69+
swift_android_tools_path(${SWIFT_HOST_VARIANT_ARCH} tools_path)
70+
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-tools-directory;${tools_path};>)
71+
endif()
72+
endif()
6373
_add_host_variant_swift_sanitizer_flags(${name})
6474

6575
target_compile_options(${name} PRIVATE

cmake/modules/AddSwift.cmake

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,19 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
529529
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD|WINDOWS")
530530
set(swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}")
531531
if(ASRLF_BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|CROSSCOMPILE")
532-
# At build time and run time, link against the swift libraries in the
533-
# installed host toolchain.
534-
if(SWIFT_PATH_TO_SWIFT_SDK)
535-
set(swift_dir "${SWIFT_PATH_TO_SWIFT_SDK}/usr")
532+
if(ASRLF_BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
533+
# At build time and run time, link against the swift libraries in the
534+
# installed host toolchain.
535+
if(SWIFT_PATH_TO_SWIFT_SDK)
536+
set(swift_dir "${SWIFT_PATH_TO_SWIFT_SDK}/usr")
537+
else()
538+
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
539+
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
540+
endif()
541+
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
536542
else()
537-
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
538-
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
543+
set(host_lib_dir "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
539544
endif()
540-
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
541545
set(host_lib_arch_dir "${host_lib_dir}/${SWIFT_HOST_VARIANT_ARCH}")
542546

543547
set(swiftrt "${host_lib_arch_dir}/swiftrt${CMAKE_C_OUTPUT_EXTENSION}")

lib/SwiftSyntax/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ endif()
7171
add_dependencies(swift-syntax-lib
7272
${SWIFT_SYNTAX_MODULES})
7373

74+
if(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
75+
add_dependencies(swift-syntax-lib swift-stdlib-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH})
76+
endif()
77+
7478
# Install Swift module interface files.
7579
foreach(module ${SWIFT_SYNTAX_MODULES})
7680
set(module_dir "${module}.swiftmodule")

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,9 @@ function(_compile_swift_files
836836
endif()
837837

838838
set(swift_compiler_tool_dep)
839-
if(SWIFT_INCLUDE_TOOLS)
840-
# Depend on the binary itself, in addition to the symlink.
839+
if(SWIFT_INCLUDE_TOOLS AND NOT BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
840+
# Depend on the binary itself, in addition to the symlink, unless
841+
# cross-compiling the compiler.
841842
set(swift_compiler_tool_dep "swift-frontend${target_suffix}")
842843
endif()
843844

tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES)
106106
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD" AND HAS_SWIFT_MODULES AND ASKD_BOOTSTRAPPING_MODE)
107107
set(swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}")
108108
if(ASKD_BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|CROSSCOMPILE")
109-
# At build time and run time, link against the swift libraries in the
110-
# installed host toolchain.
111-
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
112-
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
113-
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
109+
if(ASKD_BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS")
110+
# At build time and run time, link against the swift libraries in the
111+
# installed host toolchain.
112+
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
113+
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
114+
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
115+
else()
116+
set(host_lib_dir "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
117+
endif()
114118

115119
target_link_libraries(${target} PRIVATE ${swiftrt})
116120
target_link_libraries(${target} PRIVATE "swiftCore")

0 commit comments

Comments
 (0)