Skip to content

Commit c27d95d

Browse files
committed
[core] Update CMake to use TARGET_SDKS (NFC)
The `add_swift_library` CMake function takes an optional `TARGET_SDKS` parameter. When used, only CMake targets for the specified SDKs are added. Refactor `stdlib/public/core` to use this parameter. This also eliminates logic that determines additional flags or source files to include based on `CMAKE_SYSTEM_NAME`, which makes it easier for hosts to add targets for different platforms. In addition, prevent libicu from being linked to object library public/stdlib/stubs on Linux -- attempting to link libraries to object libraries results in a CMake error.
1 parent 7fbf501 commit c27d95d

File tree

2 files changed

+86
-54
lines changed

2 files changed

+86
-54
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 86 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -156,73 +156,106 @@ set(SWIFTLIB_SOURCES
156156
VarArgs.swift
157157
Zip.swift
158158
)
159-
set(GROUP_INFO_JSON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/GroupInfo.json)
160-
set(swift_core_link_flags "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}")
161-
set(swift_core_framework_depends)
162-
set(swift_core_private_link_libraries)
163-
set(swift_stdlib_compile_flags "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}")
164-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
165-
list(APPEND swift_core_link_flags "-all_load")
166-
list(APPEND swift_core_framework_depends Foundation)
167-
list(APPEND swift_core_framework_depends CoreFoundation)
168-
else()
169-
# With the GNU linker the equivalent of -all_load is to tell the linker
170-
# --whole-archive before the archive and --no-whole-archive after (without
171-
# the second, it causes errors when the system libraries are told to
172-
# include everything). The best way to get it in there, according to the
173-
# documentation, is to put the flags in the target_link_libraries setting.
174-
175-
# TODO: However, for the moment this actually makes things explode with an
176-
# incomplete runtime. This should be turned back on when more of the porting
177-
# effort has been completed.
178-
#set(LINK_FLAGS
179-
# -Wl,--whole-archive swiftRuntime -Wl,--no-whole-archive)
180-
find_package(ICU REQUIRED COMPONENTS uc i18n)
181-
list(APPEND swift_core_private_link_libraries
182-
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY})
183-
endif()
184-
185-
if("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
186-
execute_process(COMMAND "cygpath" "-u" "$ENV{SYSTEMROOT}"
187-
OUTPUT_VARIABLE ENV_SYSTEMROOT)
188-
189-
list(APPEND swift_core_link_flags "${ENV_SYSTEMROOT}/system32/psapi.dll")
190-
endif()
191159

192-
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
193-
find_library(EXECINFO_LIBRARY execinfo)
194-
list(APPEND swift_core_private_link_libraries
195-
${EXECINFO_LIBRARY})
196-
endif()
160+
set(GROUP_INFO_JSON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/GroupInfo.json)
197161

198162
option(SWIFT_CHECK_ESSENTIAL_STDLIB
199163
"Check core standard library layering by linking its essential subset"
200164
FALSE)
165+
if(SWIFT_CHECK_ESSENTIAL_STDLIB)
166+
add_swift_library(swift_stdlib_essential ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
167+
${SWIFTLIB_ESSENTIAL})
168+
target_link_libraries(swift_stdlib_essential ${RUNTIME_DEPENDENCY})
169+
endif()
201170

171+
set(swift_stdlib_compile_flags "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}")
202172
if(SWIFT_STDLIB_SIL_DEBUGGING)
203173
list(APPEND swift_stdlib_compile_flags "-Xfrontend" "-gsil")
204174
endif()
205175

206-
if(SWIFT_CHECK_ESSENTIAL_STDLIB)
207-
add_swift_library(swift_stdlib_essential ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
208-
${SWIFTLIB_ESSENTIAL})
209-
target_link_libraries(swift_stdlib_essential ${RUNTIME_DEPENDENCY})
176+
# The copy_shim_headers target dependency is required to let the
177+
# build system know that there's a rule to produce the shims
178+
# directory, but is not sufficient to cause the object file to be rebuilt
179+
# when the shim header changes. Therefore, we pass both the target
180+
# and the generated directory as dependencies.
181+
set(swift_core_file_depends
182+
FILE_DEPENDS
183+
copy_shim_headers "${SWIFTLIB_DIR}/shims"
184+
section_magic
185+
${GROUP_INFO_JSON_FILE})
186+
187+
add_swift_library(swiftCore ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
188+
${SWIFTLIB_SOURCES}
189+
${swift_core_file_depends}
190+
SWIFT_COMPILE_FLAGS ${swift_stdlib_compile_flags}
191+
LINK_FLAGS ${SWIFT_RUNTIME_SWIFT_LINK_FLAGS} "-all_load"
192+
INCORPORATE_OBJECT_LIBRARIES swiftRuntime swiftStdlibStubs
193+
FRAMEWORK_DEPENDS Foundation CoreFoundation
194+
TARGET_SDKS ALL_APPLE_PLATFORMS
195+
INSTALL_IN_COMPONENT stdlib)
196+
197+
# The linker flags for swiftCore for Apple targets includes -all_load. With the
198+
# GNU linker the equivalent of -all_load is to tell the linker
199+
# --whole-archive before the archive and --no-whole-archive after (without
200+
# the second, it causes errors when the system libraries are told to
201+
# include everything). The best way to get it in there, according to the
202+
# documentation, is to put the flags in the target_link_libraries setting.
203+
# TODO: However, for the moment this actually makes things explode with an
204+
# incomplete runtime. This should be turned back on when more of the porting
205+
# effort has been completed.
206+
set(swift_runtime_swift_gnu_link_flags
207+
${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}
208+
# -Wl,--whole-archive swiftRuntime -Wl,--no-whole-archive
209+
)
210+
211+
212+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
213+
find_package(ICU REQUIRED COMPONENTS uc i18n)
214+
endif()
215+
add_swift_library(swiftCore ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
216+
${SWIFTLIB_SOURCES}
217+
${swift_core_file_depends}
218+
SWIFT_COMPILE_FLAGS ${swift_stdlib_compile_flags}
219+
LINK_FLAGS ${swift_runtime_swift_gnu_link_flags}
220+
PRIVATE_LINK_LIBRARIES ${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
221+
INCORPORATE_OBJECT_LIBRARIES swiftRuntime swiftStdlibStubs
222+
TARGET_SDKS LINUX
223+
INSTALL_IN_COMPONENT stdlib)
224+
225+
add_swift_library(swiftCore ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
226+
${SWIFTLIB_SOURCES}
227+
${swift_core_file_depends}
228+
SWIFT_COMPILE_FLAGS ${swift_stdlib_compile_flags}
229+
LINK_FLAGS ${swift_runtime_swift_gnu_link_flags}
230+
PRIVATE_LINK_LIBRARIES ${SWIFT_ANDROID_ICU_UC} ${SWIFT_ANDROID_ICU_I18N}
231+
INCORPORATE_OBJECT_LIBRARIES swiftRuntime swiftStdlibStubs
232+
TARGET_SDKS ANDROID
233+
INSTALL_IN_COMPONENT stdlib)
234+
235+
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
236+
find_library(EXECINFO_LIBRARY execinfo)
210237
endif()
238+
add_swift_library(swiftCore ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
239+
${SWIFTLIB_SOURCES}
240+
${swift_core_file_depends}
241+
SWIFT_COMPILE_FLAGS ${swift_stdlib_compile_flags}
242+
LINK_FLAGS ${swift_runtime_swift_gnu_link_flags}
243+
PRIVATE_LINK_LIBRARIES ${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY} ${EXECINFO_LIBRARY}
244+
INCORPORATE_OBJECT_LIBRARIES swiftRuntime swiftStdlibStubs
245+
TARGET_SDKS FREEBSD
246+
INSTALL_IN_COMPONENT stdlib)
211247

248+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
249+
execute_process(COMMAND "cygpath" "-u" "$ENV{SYSTEMROOT}"
250+
OUTPUT_VARIABLE ENV_SYSTEMROOT)
251+
endif()
212252
add_swift_library(swiftCore ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
213253
${SWIFTLIB_SOURCES}
214-
# The copy_shim_headers target dependency is required to let the
215-
# build system know that there's a rule to produce the shims
216-
# directory, but is not sufficient to cause the object file to be rebuilt
217-
# when the shim header changes. Therefore, we pass both the target
218-
# and the generated directory as dependencies.
219-
FILE_DEPENDS
220-
copy_shim_headers "${SWIFTLIB_DIR}/shims"
221-
section_magic
222-
${GROUP_INFO_JSON_FILE}
254+
${swift_core_file_depends}
223255
SWIFT_COMPILE_FLAGS ${swift_stdlib_compile_flags}
224-
LINK_FLAGS ${swift_core_link_flags}
225-
PRIVATE_LINK_LIBRARIES ${swift_core_private_link_libraries}
256+
LINK_FLAGS ${swift_runtime_swift_gnu_link_flags} "${ENV_SYSTEMROOT}/system32/psapi.dll"
257+
PRIVATE_LINK_LIBRARIES ${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
226258
INCORPORATE_OBJECT_LIBRARIES swiftRuntime swiftStdlibStubs
227-
FRAMEWORK_DEPENDS ${swift_core_framework_depends}
259+
TARGET_SDKS CYGWIN
228260
INSTALL_IN_COMPONENT stdlib)
261+

stdlib/public/stubs/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ add_swift_library(swiftStdlibStubs OBJECT_LIBRARY TARGET_LIBRARY
3434
${swift_stubs_unicode_normalization_sources}
3535
C_COMPILE_FLAGS ${swift_stubs_c_compile_flags}
3636
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
37-
LINK_LIBRARIES ${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
3837
TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX
3938
INSTALL_IN_COMPONENT stdlib)
4039

0 commit comments

Comments
 (0)