Skip to content

Commit 9504de5

Browse files
authored
Merge pull request #71552 from finagolfin/cross-compile
[CMake] Add broader support for cross-compiling the portions of the compiler that are written in Swift to non-Darwin Unix
2 parents efbfeb5 + fcf7614 commit 9504de5

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
@@ -155,10 +155,9 @@ function(add_swift_compiler_modules_library name)
155155
list(APPEND sdk_option "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift")
156156
endif()
157157
elseif(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
158-
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
159158
# NOTE: prepending allows SWIFT_COMPILER_SOURCES_SDK_FLAGS to override the
160159
# resource directory if needed.
161-
list(PREPEND sdk_option "-resource-dir" "${swift_exec_bin_dir}/../lib/swift")
160+
list(PREPEND sdk_option "-resource-dir" "${SWIFTLIB_DIR}")
162161
endif()
163162
get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
164163
${SWIFT_HOST_VARIANT_ARCH} "${deployment_version}")
@@ -232,6 +231,9 @@ function(add_swift_compiler_modules_library name)
232231
importedHeaderDependencies
233232
COMMENT "Building swift module ${module}")
234233

234+
if(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
235+
add_dependencies(${dep_target} swift-stdlib-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH})
236+
endif()
235237
set("${module}_dep_target" ${dep_target})
236238
set(all_module_targets ${all_module_targets} ${dep_target})
237239
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(SWIFT_HOST_VARIANT_SDK STREQUAL "ANDROID" AND 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
@@ -535,15 +535,19 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
535535
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD|WINDOWS")
536536
set(swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}")
537537
if(ASRLF_BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|CROSSCOMPILE")
538-
# At build time and run time, link against the swift libraries in the
539-
# installed host toolchain.
540-
if(SWIFT_PATH_TO_SWIFT_SDK)
541-
set(swift_dir "${SWIFT_PATH_TO_SWIFT_SDK}/usr")
538+
if(ASRLF_BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
539+
# At build time and run time, link against the swift libraries in the
540+
# installed host toolchain.
541+
if(SWIFT_PATH_TO_SWIFT_SDK)
542+
set(swift_dir "${SWIFT_PATH_TO_SWIFT_SDK}/usr")
543+
else()
544+
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
545+
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
546+
endif()
547+
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
542548
else()
543-
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
544-
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
549+
set(host_lib_dir "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
545550
endif()
546-
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
547551
set(host_lib_arch_dir "${host_lib_dir}/${SWIFT_HOST_VARIANT_ARCH}")
548552

549553
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
@@ -72,6 +72,10 @@ endif()
7272
add_dependencies(swift-syntax-lib
7373
${SWIFT_SYNTAX_MODULES})
7474

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

stdlib/cmake/modules/SwiftSource.cmake

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

850850
set(swift_compiler_tool_dep)
851-
if(SWIFT_INCLUDE_TOOLS)
852-
# Depend on the binary itself, in addition to the symlink.
851+
if(SWIFT_INCLUDE_TOOLS AND NOT BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
852+
# Depend on the binary itself, in addition to the symlink, unless
853+
# cross-compiling the compiler.
853854
set(swift_compiler_tool_dep "swift-frontend${target_suffix}")
854855
endif()
855856

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)