Skip to content

[build] rewrite #21707 so that it doesn't break cross-compilation #30170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,12 @@ function(add_swift_target_library name)
set(swiftlib_module_dependency_targets)
set(swiftlib_private_link_libraries_targets)

if(name STREQUAL swiftCore)
# This initializes swiftlib_private_link_libraries_targets for swiftCore,
# so don't move it away from the variable declaration just above.
swift_core_private_libraries(${sdk} ${arch} swiftlib_private_link_libraries_targets)
endif()

if(NOT BUILD_STANDALONE)
foreach(mod ${swiftlib_module_depends_flattened})
if(DEFINED maccatalyst_build_flavor)
Expand Down
34 changes: 18 additions & 16 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,27 @@ set(SWIFTLIB_GYB_SOURCES
set(GROUP_INFO_JSON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/GroupInfo.json)
set(swift_core_link_flags "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}")
set(swift_core_framework_depends)
set(swift_core_private_link_libraries)
set(swift_stdlib_compile_flags "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}")

if(SWIFT_PRIMARY_VARIANT_SDK STREQUAL CYGWIN)
# TODO(compnerd) cache this variable to permit re-configuration
execute_process(COMMAND "cygpath" "-u" "$ENV{SYSTEMROOT}" OUTPUT_VARIABLE ENV_SYSTEMROOT)
list(APPEND swift_core_private_link_libraries "${ENV_SYSTEMROOT}/system32/psapi.dll")
elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL FREEBSD)
find_library(EXECINFO_LIBRARY execinfo)
list(APPEND swift_core_private_link_libraries ${EXECINFO_LIBRARY})
elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL LINUX)
if(SWIFT_BUILD_STATIC_STDLIB)
list(APPEND swift_core_private_link_libraries)
function(swift_core_private_libraries sdk arch libraries_var_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is the going in the exact opposite direction of where we want to go. We need to start using find_library and imported targets to create the dependencies rather than hard coded names and magic flags.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear why you placed this review comment here: this reorganizes a chunk of the following CMake config as a CMake function and is orthogonal to the issue of "imported targets" vs "magic flags." Maybe you meant to place it below, where the flags are?

As for the way those libraries and flags are added below, that predates this pull and has nothing to do with me. I realize you'd like to see it modernized, but that's not what this pull is offering: I simply refactor the way CMake calls the pre-existing "magic flags," so they can be used when cross-compiling too.

set(private_link_libraries)
if(${sdk} STREQUAL CYGWIN)
if(${sdk} STREQUAL ${SWIFT_HOST_VARIANT_SDK})
# TODO(compnerd) cache this variable to permit re-configuration
execute_process(COMMAND "cygpath" "-u" "$ENV{SYSTEMROOT}" OUTPUT_VARIABLE ENV_SYSTEMROOT)
list(APPEND private_link_libraries "${ENV_SYSTEMROOT}/system32/psapi.dll")
else()
message(FATAL_ERROR "CYGWIN cross-compilation doesn't know where to find psapi.dll.")
endif()
elseif(${sdk} STREQUAL FREEBSD)
find_library(EXECINFO_LIBRARY execinfo)
list(APPEND private_link_libraries ${EXECINFO_LIBRARY})
elseif(${sdk} STREQUAL WINDOWS)
list(APPEND private_link_libraries shell32;DbgHelp)
endif()
elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL WINDOWS)
list(APPEND swift_core_private_link_libraries shell32;DbgHelp)
endif()

set(${libraries_var_name} ${private_link_libraries} PARENT_SCOPE)
endfunction()

option(SWIFT_CHECK_ESSENTIAL_STDLIB
"Check core standard library layering by linking its essential subset"
Expand Down Expand Up @@ -298,8 +302,6 @@ set(swiftCore_common_options
${SWIFTLIB_GYB_SOURCES}
LINK_FLAGS
${swift_core_link_flags}
PRIVATE_LINK_LIBRARIES
${swift_core_private_link_libraries}
INCORPORATE_OBJECT_LIBRARIES
${swift_core_incorporate_object_libraries}
FRAMEWORK_DEPENDS
Expand Down
49 changes: 24 additions & 25 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,6 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags})
list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS)
list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/stdlib/include/llvm/Support -I${SWIFT_SOURCE_DIR}/include)

set(sdk "${SWIFT_HOST_VARIANT_SDK}")
if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
set(static_binary_lnk_file_list)
string(TOLOWER "${sdk}" lowercase_sdk)

# Generate the static-executable-args.lnk file used for ELF systems (eg linux)
set(linkfile "${lowercase_sdk}/static-executable-args.lnk")
add_custom_command_target(swift_static_binary_${sdk}_args
COMMAND
"${CMAKE_COMMAND}" -E copy
"${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk"
"${SWIFTSTATICLIB_DIR}/${linkfile}"
OUTPUT
"${SWIFTSTATICLIB_DIR}/${linkfile}"
DEPENDS
"${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk")

list(APPEND static_binary_lnk_file_list ${swift_static_binary_${sdk}_args})
swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
DESTINATION "lib/swift_static/${lowercase_sdk}"
COMPONENT stdlib)
add_dependencies(stdlib ${static_binary_lnk_file_list})
add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list})
endif()

add_swift_target_library(swiftRuntime OBJECT_LIBRARY
${swift_runtime_sources}
${swift_runtime_objc_sources}
Expand Down Expand Up @@ -148,6 +123,30 @@ add_swift_target_library(swiftImageRegistrationObjectCOFF
INSTALL_IN_COMPONENT none)

foreach(sdk ${SWIFT_SDKS})
if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than only generate this link file if the host SDK is linux, as was done above, create it for all ELF SDKs in the list of wanted SWIFT_SDKS.

set(static_binary_lnk_file_list)
string(TOLOWER "${sdk}" lowercase_sdk)

# Generate the static-executable-args.lnk file used for ELF systems (eg linux)
set(linkfile "${lowercase_sdk}/static-executable-args.lnk")
add_custom_command_target(swift_static_binary_${sdk}_args
COMMAND
"${CMAKE_COMMAND}" -E copy
"${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk"
"${SWIFTSTATICLIB_DIR}/${linkfile}"
OUTPUT
"${SWIFTSTATICLIB_DIR}/${linkfile}"
DEPENDS
"${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk")

list(APPEND static_binary_lnk_file_list ${swift_static_binary_${sdk}_args})
swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
DESTINATION "lib/swift_static/${lowercase_sdk}"
COMPONENT stdlib)
add_dependencies(stdlib ${static_binary_lnk_file_list})
add_custom_target(static_binary_${lowercase_sdk}_magic ALL DEPENDS ${static_binary_lnk_file_list})
endif()

foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
Expand Down