Skip to content

Commit ea62bcc

Browse files
authored
[cmake] Allow overriding SwiftCompilerSources arguments from CMake (#70134)
The calculated arguments for compiling the SwiftCompilerSources works in the cases where the sysroot of the machine building the code is `/` or can be provided as a single directory in `SWIFT_SDK_<HOST>_<ARCH>_PATH`. For some cases in which a sysroot is split into multiple directories or in some cross-compiling scenarios where the SDK layout is beyond our control, we can provide more flexibility in the build system. The changes in this commit moves the calculations around the SDK and libc++ outside the `add_swift_compiler_modules_library`, since they do not depend on the inputs. Some calculations relative to `-resource-dir` depend on the Swift compiler path, which cannot be moved outside the function. The calculations outside the function are stored in a cached variable, which is later used in the function. If the need arise, anyone can provide a custom value for that variable in their CMake invocation or cache files adapted for their specific case and override the automatically calculated default. I also rewrote a couple of `set(list ${list} …)` into `list(APPEND list …)`. This should be NFC for the Swift CI, because no value of `SWIFT_COMPILER_SOURCES_SDK_FLAGS` is provided, and the default should be the same value as before.
1 parent d8127eb commit ea62bcc

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,33 @@ function(swift_compiler_sources module)
5959
set(target_name "SwiftModule${module}")
6060
set_property(TARGET "SwiftModule${module}" APPEND PROPERTY SOURCES ${sources})
6161
endfunction()
62-
62+
63+
# Allow the override of the flags used to define the SDK used to compile the
64+
# Swift compiler sources from the CMake configuration (command line or cache
65+
# files). This allows supporting complicated sysroots and some cross-compilation
66+
# scenarios.
67+
set(SWIFT_COMPILER_SOURCES_SDK_FLAGS_default)
68+
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
69+
set(sdk_path "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
70+
list(APPEND SWIFT_COMPILER_SOURCES_SDK_FLAGS_default "-sdk" "${sdk_path}")
71+
if(NOT EXISTS "${sdk_path}/usr/include/c++")
72+
# Darwin SDKs in Xcode 12 or older do not include libc++, which prevents clang from finding libc++ when invoked
73+
# from ClangImporter. This results in build errors. To workaround this, let's explicitly pass the path to libc++
74+
# to clang.
75+
message(WARNING "Building with an outdated Darwin SDK: libc++ missing from the ${SWIFT_HOST_VARIANT_SDK} SDK. Will use libc++ from the toolchain.")
76+
get_filename_component(absolute_libcxx_path "${CMAKE_C_COMPILER}/../../include/c++/v1" REALPATH)
77+
if (EXISTS "${absolute_libcxx_path}")
78+
list(APPEND SWIFT_COMPILER_SOURCES_SDK_FLAGS_default "-Xcc" "-isystem" "-Xcc" "${absolute_libcxx_path}")
79+
else()
80+
message(ERROR "libc++ not found in the toolchain.")
81+
endif()
82+
endif()
83+
elseif(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
84+
list(APPEND SWIFT_COMPILER_SOURCES_SDK_FLAGS_default "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
85+
endif()
86+
set(SWIFT_COMPILER_SOURCES_SDK_FLAGS ${SWIFT_COMPILER_SOURCES_SDK_FLAGS_default}
87+
CACHE STRING "Swift flags used to compiler the Swift compiler sources")
88+
6389
# Add a library target for the swift compiler modules.
6490
#
6591
# Adds targets to compile all swift compiler modules and a target for the
@@ -103,35 +129,22 @@ function(add_swift_compiler_modules_library name)
103129

104130
get_bootstrapping_path(build_dir ${CMAKE_CURRENT_BINARY_DIR} "${ALS_BOOTSTRAPPING}")
105131

106-
set(sdk_option "")
132+
set(sdk_option ${SWIFT_COMPILER_SOURCES_SDK_FLAGS})
107133

108134
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
109135
set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
110-
set(sdk_path "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
111-
set(sdk_option "-sdk" "${sdk_path}")
112136
if(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS")
113137
# Let the cross-compiled compile don't pick up the compiled stdlib by providing
114138
# an (almost) empty resource dir.
115139
# The compiler will instead pick up the stdlib from the SDK.
116140
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
117-
set(sdk_option ${sdk_option} "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift")
118-
endif()
119-
if(NOT EXISTS "${sdk_path}/usr/include/c++")
120-
# Darwin SDKs in Xcode 12 or older do not include libc++, which prevents clang from finding libc++ when invoked
121-
# from ClangImporter. This results in build errors. To workaround this, let's explicitly pass the path to libc++
122-
# to clang.
123-
message(WARNING "Building with an outdated Darwin SDK: libc++ missing from the ${SWIFT_HOST_VARIANT_SDK} SDK. Will use libc++ from the toolchain.")
124-
get_filename_component(absolute_libcxx_path "${CMAKE_C_COMPILER}/../../include/c++/v1" REALPATH)
125-
if (EXISTS "${absolute_libcxx_path}")
126-
set(sdk_option ${sdk_option} "-Xcc" "-isystem" "-Xcc" "${absolute_libcxx_path}")
127-
else()
128-
message(ERROR "libc++ not found in the toolchain.")
129-
endif()
141+
list(APPEND sdk_option "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift")
130142
endif()
131143
elseif(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE")
132-
set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
133144
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
134-
set(sdk_option ${sdk_option} "-resource-dir" "${swift_exec_bin_dir}/../lib/swift")
145+
# NOTE: prepending allows SWIFT_COMPILER_SOURCES_SDK_FLAGS to override the
146+
# resource directory if needed.
147+
list(PREPEND sdk_option "-resource-dir" "${swift_exec_bin_dir}/../lib/swift")
135148
endif()
136149
get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
137150
${SWIFT_HOST_VARIANT_ARCH} "${deployment_version}")
@@ -140,7 +153,7 @@ function(add_swift_compiler_modules_library name)
140153
# under `include/swift`. These are either located next to the compiler (in case of open source toolchains) or
141154
# in the SDK (in case a Swift compiler from Xcode)
142155
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
143-
set(sdk_option ${sdk_option} "-I" "${swift_exec_bin_dir}/../lib" "-I" "${sdk_path}/usr/lib")
156+
list(APPEND sdk_option "-I" "${swift_exec_bin_dir}/../lib" "-I" "${sdk_path}/usr/lib")
144157

145158
set(all_obj_files)
146159
set(all_module_targets)

0 commit comments

Comments
 (0)