Skip to content

Commit 8231a2b

Browse files
committed
[build] rewrite #21707 and swiftCore/swiftRuntime CMake config, so that it doesn't break
cross-compilation Passing swift_core_private_link_libraries to PRIVATE_LINK_LIBRARIES only works either when building a single host SDK alone or when needed by all SDKs, so move all that configuration to a swift_core_private_libraries() function that's called from add_swift_target_library() for each SDK/arch instead. Similarly, call a swift_runtime_static_libraries() function to split off libraries for the static stdlib with linux. Finally, remove the last uses of SWIFT_CONFIGURED_SDKS for anything more than checking, using SWIFT_SDKS instead, and move the static-stdlib linker file generation to the stdlib/ directory.
1 parent c83ca6a commit 8231a2b

File tree

6 files changed

+89
-75
lines changed

6 files changed

+89
-75
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,6 @@ function(_add_variant_link_flags)
428428
swift_android_cxx_libraries_for_arch(${LFLAGS_ARCH} cxx_link_libraries)
429429
list(APPEND link_libraries ${cxx_link_libraries})
430430

431-
# link against the ICU libraries
432-
list(APPEND link_libraries
433-
${SWIFT_ANDROID_${LFLAGS_ARCH}_ICU_I18N}
434-
${SWIFT_ANDROID_${LFLAGS_ARCH}_ICU_UC})
435-
436431
swift_android_lib_for_arch(${LFLAGS_ARCH} ${LFLAGS_ARCH}_LIB)
437432
foreach(path IN LISTS ${LFLAGS_ARCH}_LIB)
438433
list(APPEND library_search_directories ${path})

lib/Driver/CMakeLists.txt

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,3 @@ target_link_libraries(swiftDriver PRIVATE
2727
swiftAST
2828
swiftBasic
2929
swiftOption)
30-
31-
# Generate the static-stdlib-args.lnk file used by -static-stdlib option
32-
# for 'GenericUnix' (eg linux)
33-
if(SWIFT_BUILD_STATIC_STDLIB)
34-
set(static_stdlib_lnk_file_list)
35-
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
36-
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
37-
string(TOLOWER "${sdk}" lowercase_sdk)
38-
if(SWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_STATICLIB)
39-
set(ICU_STATICLIB "TRUE")
40-
else()
41-
set(ICU_STATICLIB "FALSE")
42-
find_package(ICU REQUIRED COMPONENTS uc i18n)
43-
get_filename_component(ICU_UC_LIBDIR "${ICU_UC_LIBRARIES}" DIRECTORY)
44-
get_filename_component(ICU_I18N_LIBDIR "${ICU_I18N_LIBRARIES}" DIRECTORY)
45-
endif()
46-
set(linkfile "${lowercase_sdk}/static-stdlib-args.lnk")
47-
add_custom_command_target(swift_static_stdlib_${sdk}_args
48-
COMMAND
49-
"${SWIFT_SOURCE_DIR}/utils/gen-static-stdlib-link-args"
50-
"${sdk}"
51-
"${SWIFTSTATICLIB_DIR}/${linkfile}"
52-
"${ICU_STATICLIB}"
53-
"${ICU_UC_LIBDIR}"
54-
"${ICU_I18N_LIBDIR}"
55-
OUTPUT
56-
"${SWIFTSTATICLIB_DIR}/${linkfile}"
57-
DEPENDS
58-
"${SWIFT_SOURCE_DIR}/utils/gen-static-stdlib-link-args")
59-
60-
list(APPEND static_stdlib_lnk_file_list ${swift_static_stdlib_${sdk}_args})
61-
swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
62-
DESTINATION "lib/swift_static/${lowercase_sdk}"
63-
COMPONENT stdlib)
64-
endif()
65-
endforeach()
66-
add_custom_target(swift_static_lnk_args ALL DEPENDS ${static_stdlib_lnk_file_list})
67-
add_dependencies(stdlib swift_static_lnk_args)
68-
endif()

stdlib/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,43 @@ if(SWIFT_BUILD_STATIC_STDLIB)
7878
else()
7979
list(APPEND SWIFT_STDLIB_LIBRARY_BUILD_TYPES STATIC)
8080
endif()
81+
82+
# Generate the static-stdlib-args.lnk file used by -static-stdlib option
83+
# for 'GenericUnix' (eg linux)
84+
set(static_stdlib_lnk_file_list)
85+
foreach(sdk ${SWIFT_SDKS})
86+
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
87+
string(TOLOWER "${sdk}" lowercase_sdk)
88+
if(SWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_STATICLIB)
89+
set(ICU_STATICLIB "TRUE")
90+
else()
91+
set(ICU_STATICLIB "FALSE")
92+
find_package(ICU REQUIRED COMPONENTS uc i18n)
93+
get_filename_component(ICU_UC_LIBDIR "${ICU_UC_LIBRARIES}" DIRECTORY)
94+
get_filename_component(ICU_I18N_LIBDIR "${ICU_I18N_LIBRARIES}" DIRECTORY)
95+
endif()
96+
set(linkfile "${lowercase_sdk}/static-stdlib-args.lnk")
97+
add_custom_command_target(swift_static_stdlib_${sdk}_args
98+
COMMAND
99+
"${SWIFT_SOURCE_DIR}/utils/gen-static-stdlib-link-args"
100+
"${sdk}"
101+
"${SWIFTSTATICLIB_DIR}/${linkfile}"
102+
"${ICU_STATICLIB}"
103+
"${ICU_UC_LIBDIR}"
104+
"${ICU_I18N_LIBDIR}"
105+
OUTPUT
106+
"${SWIFTSTATICLIB_DIR}/${linkfile}"
107+
DEPENDS
108+
"${SWIFT_SOURCE_DIR}/utils/gen-static-stdlib-link-args")
109+
110+
list(APPEND static_stdlib_lnk_file_list ${swift_static_stdlib_${sdk}_args})
111+
swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
112+
DESTINATION "lib/swift_static/${lowercase_sdk}"
113+
COMPONENT stdlib)
114+
endif()
115+
endforeach()
116+
add_custom_target(swift_static_lnk_args ALL DEPENDS ${static_stdlib_lnk_file_list})
117+
add_dependencies(stdlib swift_static_lnk_args)
81118
endif()
82119

83120
function(swift_create_stdlib_targets name variant define_all_alias)

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,14 @@ function(add_swift_target_library name)
13341334
endif()
13351335
endif()
13361336

1337+
set(SWIFTLIB_${sdk}_SOURCES ${SWIFTLIB_SOURCES})
1338+
if(name STREQUAL swiftRuntime)
1339+
if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
1340+
list(REMOVE_ITEM SWIFTLIB_${sdk}_SOURCES ImageInspectionELF.cpp)
1341+
swift_runtime_static_libraries(${sdk})
1342+
endif()
1343+
endif()
1344+
13371345
set(sdk_supported_archs
13381346
${SWIFT_SDK_${sdk}_ARCHITECTURES}
13391347
${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES})
@@ -1371,6 +1379,12 @@ function(add_swift_target_library name)
13711379
set(swiftlib_module_dependency_targets)
13721380
set(swiftlib_private_link_libraries_targets)
13731381

1382+
if(name STREQUAL swiftCore)
1383+
# This initializes swiftlib_private_link_libraries_targets for swiftCore,
1384+
# so don't move it away from the variable declaration just above.
1385+
swift_core_private_libraries(${sdk} ${arch} swiftlib_private_link_libraries_targets)
1386+
endif()
1387+
13741388
if(NOT BUILD_STANDALONE)
13751389
foreach(mod ${swiftlib_module_depends_flattened})
13761390
if(DEFINED maccatalyst_build_flavor)
@@ -1480,7 +1494,7 @@ function(add_swift_target_library name)
14801494
${SWIFTLIB_STATIC_keyword}
14811495
${SWIFTLIB_OBJECT_LIBRARY_keyword}
14821496
${SWIFTLIB_INSTALL_WITH_SHARED_keyword}
1483-
${SWIFTLIB_SOURCES}
1497+
${SWIFTLIB_${sdk}_SOURCES}
14841498
TARGET_LIBRARY
14851499
MODULE_TARGETS ${module_variant_names}
14861500
SDK ${sdk}

stdlib/public/core/CMakeLists.txt

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,9 @@ set(SWIFTLIB_GYB_SOURCES
230230
set(GROUP_INFO_JSON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/GroupInfo.json)
231231
set(swift_core_link_flags "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}")
232232
set(swift_core_framework_depends)
233-
set(swift_core_private_link_libraries)
234233
set(swift_stdlib_compile_flags "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}")
235234
if(SWIFT_PRIMARY_VARIANT_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
236235
list(APPEND swift_core_link_flags "-all_load")
237-
list(APPEND swift_core_private_link_libraries icucore)
238236
else()
239237
# With the GNU linker the equivalent of -all_load is to tell the linker
240238
# --whole-archive before the archive and --no-whole-archive after (without
@@ -247,29 +245,40 @@ else()
247245
# effort has been completed.
248246
#set(LINK_FLAGS
249247
# -Wl,--whole-archive swiftRuntime -Wl,--no-whole-archive)
250-
if("${SWIFT_PATH_TO_LIBICU_BUILD}" STREQUAL "")
251-
list(APPEND swift_core_private_link_libraries
252-
${SWIFT_${SWIFT_PRIMARY_VARIANT_SDK}_${SWIFT_PRIMARY_VARIANT_ARCH}_ICU_UC}
253-
${SWIFT_${SWIFT_PRIMARY_VARIANT_SDK}_${SWIFT_PRIMARY_VARIANT_ARCH}_ICU_I18N})
248+
endif()
249+
250+
function(swift_core_private_libraries sdk arch libraries_var_name)
251+
set(private_link_libraries)
252+
if(${sdk} IN_LIST SWIFT_APPLE_PLATFORMS)
253+
list(APPEND private_link_libraries icucore)
254254
else()
255-
list(APPEND swift_core_private_link_libraries -licui18nswift -licuucswift -licudataswift)
255+
if(${sdk} STREQUAL ${SWIFT_HOST_VARIANT_SDK} AND NOT "${SWIFT_PATH_TO_LIBICU_BUILD}" STREQUAL "")
256+
list(APPEND private_link_libraries -licui18nswift -licuucswift -licudataswift)
257+
else()
258+
list(APPEND private_link_libraries ${SWIFT_${sdk}_${arch}_ICU_UC}
259+
${SWIFT_${sdk}_${arch}_ICU_I18N})
260+
endif()
256261
endif()
257-
endif()
258262

259-
if(SWIFT_PRIMARY_VARIANT_SDK STREQUAL CYGWIN)
260-
# TODO(compnerd) cache this variable to permit re-configuration
261-
execute_process(COMMAND "cygpath" "-u" "$ENV{SYSTEMROOT}" OUTPUT_VARIABLE ENV_SYSTEMROOT)
262-
list(APPEND swift_core_private_link_libraries "${ENV_SYSTEMROOT}/system32/psapi.dll")
263-
elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL FREEBSD)
264-
find_library(EXECINFO_LIBRARY execinfo)
265-
list(APPEND swift_core_private_link_libraries ${EXECINFO_LIBRARY})
266-
elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL LINUX)
267-
if(SWIFT_BUILD_STATIC_STDLIB)
268-
list(APPEND swift_core_private_link_libraries swiftImageInspectionShared)
263+
if(${sdk} STREQUAL CYGWIN)
264+
if(${sdk} STREQUAL ${SWIFT_HOST_VARIANT_SDK})
265+
# TODO(compnerd) cache this variable to permit re-configuration
266+
execute_process(COMMAND "cygpath" "-u" "$ENV{SYSTEMROOT}" OUTPUT_VARIABLE ENV_SYSTEMROOT)
267+
list(APPEND private_link_libraries "${ENV_SYSTEMROOT}/system32/psapi.dll")
268+
else()
269+
message(FATAL_ERROR "CYGWIN cross-compilation doesn't know where to find psapi.dll.")
270+
endif()
271+
elseif(${sdk} STREQUAL FREEBSD)
272+
find_library(EXECINFO_LIBRARY execinfo)
273+
list(APPEND private_link_libraries ${EXECINFO_LIBRARY})
274+
elseif(${sdk} STREQUAL LINUX AND SWIFT_BUILD_STATIC_STDLIB)
275+
list(APPEND private_link_libraries swiftImageInspectionShared)
276+
elseif(${sdk} STREQUAL WINDOWS)
277+
list(APPEND private_link_libraries shell32;DbgHelp)
269278
endif()
270-
elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL WINDOWS)
271-
list(APPEND swift_core_private_link_libraries shell32;DbgHelp)
272-
endif()
279+
280+
set(${libraries_var_name} ${private_link_libraries} PARENT_SCOPE)
281+
endfunction()
273282

274283
option(SWIFT_CHECK_ESSENTIAL_STDLIB
275284
"Check core standard library layering by linking its essential subset"
@@ -310,8 +319,6 @@ add_swift_target_library(swiftCore
310319
${swift_stdlib_compile_flags} -Xcc -DswiftCore_EXPORTS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
311320
LINK_FLAGS
312321
${swift_core_link_flags}
313-
PRIVATE_LINK_LIBRARIES
314-
${swift_core_private_link_libraries}
315322
INCORPORATE_OBJECT_LIBRARIES
316323
swiftRuntime swiftStdlibStubs
317324
FRAMEWORK_DEPENDS

stdlib/public/runtime/CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags})
8989
list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS)
9090
list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include)
9191

92-
set(sdk "${SWIFT_HOST_VARIANT_SDK}")
93-
if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
94-
list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp)
92+
function(swift_runtime_static_libraries sdk)
9593
set(static_binary_lnk_file_list)
9694
string(TOLOWER "${sdk}" lowercase_sdk)
9795

@@ -100,6 +98,7 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
10098
ImageInspectionELF.cpp
10199
C_COMPILE_FLAGS ${swift_runtime_library_compile_flags}
102100
LINK_FLAGS ${swift_runtime_linker_flags}
101+
TARGET_SDKS ${sdk}
103102
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
104103
INSTALL_IN_COMPONENT stdlib)
105104

@@ -150,7 +149,7 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
150149
DESTINATION "lib/swift_static/${lowercase_sdk}"
151150
COMPONENT stdlib)
152151
add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list})
153-
foreach(arch IN LISTS SWIFT_SDK_LINUX_ARCHITECTURES)
152+
foreach(arch IN LISTS SWIFT_SDK_${sdk}_ARCHITECTURES)
154153
add_dependencies(static_binary_magic ${swift_image_inspection_${arch}_static})
155154
endforeach()
156155
add_dependencies(static_binary_magic ${swift_image_inspection_static_primary_arch})
@@ -160,9 +159,10 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
160159
ImageInspectionELF.cpp
161160
C_COMPILE_FLAGS ${swift_runtime_library_compile_flags}
162161
LINK_FLAGS ${swift_runtime_linker_flags}
162+
TARGET_SDKS ${sdk}
163163
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
164164
INSTALL_IN_COMPONENT never_install)
165-
endif()
165+
endfunction()
166166

167167
if(SWIFT_STDLIB_USE_NONATOMIC_RC)
168168
set(_RUNTIME_NONATOMIC_FLAGS -DSWIFT_STDLIB_USE_NONATOMIC_RC)
@@ -180,7 +180,7 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY
180180

181181
set(ELFISH_SDKS)
182182
set(COFF_SDKS)
183-
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
183+
foreach(sdk ${SWIFT_SDKS})
184184
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
185185
list(APPEND ELFISH_SDKS ${sdk})
186186
elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
@@ -208,7 +208,7 @@ add_swift_target_library(swiftImageRegistrationObjectCOFF
208208
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
209209
INSTALL_IN_COMPONENT none)
210210

211-
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
211+
foreach(sdk ${SWIFT_SDKS})
212212
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
213213
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
214214
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")

0 commit comments

Comments
 (0)