Skip to content

[5.9.0][CMake] Support Macros in Linux #68197

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

Merged
merged 20 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
97e9a2d
[CMake] Add paths for building Swift code against host toolchain
DougGregor May 29, 2023
4690e59
[CMake] Fix host library builds and rpaths for testing macros
DougGregor May 29, 2023
b3b1b7a
[SourceKit] Fix paths when including the new Swift compiler
DougGregor May 29, 2023
38b6f0e
[CMake] Support Macros in Linux
rintaro Aug 19, 2023
e5e63db
Avoid adding redundant temporary RUNPATH to builder's stdlib
rintaro Aug 24, 2023
791a83f
[CMake] Fatal error when bootstrapping is not "hosttools"
rintaro Aug 24, 2023
046a144
[CMake] Update for review comments
rintaro Aug 25, 2023
ed2b5df
[CMake] Fix weiredness in swift runtime linking for SourceKit libraries
rintaro Aug 25, 2023
a5d05ea
[CMake] Further cleanup for RUNPATH setting
rintaro Aug 25, 2023
24e7403
[CMake] Set LD_LIBRARY_PATH to builder's runtime when building stdlib
rintaro Aug 28, 2023
3c79ce5
[CMake] Fix install for macOS toolchain
rintaro Aug 28, 2023
43c4e2f
[CMake] Fix RUNPATH of lib_InternalSwiftScan in Linux
rintaro Aug 28, 2023
209e353
[CMake] Stop some exectuable link with swiftCore
rintaro Aug 28, 2023
56d0e65
[CMake] Use RPATH_CHANGE instead RPATH_SET
rintaro Aug 29, 2023
b383afc
[CMake] Always use builder's runtime for building Swift code in Linux
rintaro Aug 29, 2023
704987e
[CMake][SourceKit] Add RPATH to 'lib/swift/host'
rintaro Aug 30, 2023
462badb
[CMake] Update host platform check for macro support
rintaro Aug 30, 2023
78188b3
[CMake] Handle cases where Swift parser integration is disabled in Linux
rintaro Aug 30, 2023
f1869c0
[stdlib] Set PYTHONIOENCODING when building the stdlib
bnbarham Aug 30, 2023
d34a60d
[CMake] Instantiate missing C++ templates
rintaro Aug 29, 2023
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
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ include(SwiftConfigureSDK)
include(SwiftComponents)
include(SwiftList)
include(AddPureSwift)
include(SetRPATH)

# Configure swift include, install, build components.
swift_configure_components()
Expand Down Expand Up @@ -957,6 +958,12 @@ if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
else()
set(SWIFT_SWIFT_PARSER TRUE)
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS})

if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
endif()
endif()
endif()

Expand Down
8 changes: 7 additions & 1 deletion SwiftCompilerSources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ else()
#
# step 1: generate a dummy source file, which just includes all headers
# defined in include/swift/module.modulemap
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
"
#include \"Basic/BridgedSwiftObject.h\"
#include \"Basic/BasicBridging.h\"
Expand All @@ -247,6 +247,12 @@ else()

#include \"Parse/RegexParserBridging.h\"
")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
)

# step 2: build a library containing that source file. This library depends on all the included header files.
# The swift modules can now depend on that target.
Expand Down
34 changes: 30 additions & 4 deletions cmake/modules/AddPureSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ function(_add_host_swift_compile_options name)
_add_host_variant_swift_sanitizer_flags(${name})
endfunction()

function(_set_pure_swift_link_flags name relpath_to_lib_dir)
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
# Don't add builder's stdlib RPATH automatically.
target_compile_options(${name} PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:-no-toolchain-stdlib-rpath>
)

set_property(TARGET ${name}
APPEND PROPERTY INSTALL_RPATH
# At runtime, use swiftCore in the current just-built toolchain.
# NOTE: This relies on the ABI being the same as the builder.
"$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}"
)
# NOTE: At this point we don't have any pure swift executables/shared
# libraries required for building runtime/stdlib. So we don't need to add
# RPATH to the builder's runtime.
endif()
endfunction()

# Add a new "pure" Swift host library.
#
# "Pure" Swift host libraries can only contain Swift code, and will be built
Expand Down Expand Up @@ -266,10 +285,17 @@ function(add_pure_swift_host_tool name)
# Create the library.
add_executable(${name} ${APSHT_SOURCES})
_add_host_swift_compile_options(${name})

set_property(TARGET ${name}
APPEND PROPERTY INSTALL_RPATH
"@executable_path/../lib/swift/host")
_set_pure_swift_link_flags(${name} "../lib")

if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
set_property(TARGET ${name}
APPEND PROPERTY INSTALL_RPATH
"@executable_path/../lib/swift/host")
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
set_property(TARGET ${name}
APPEND PROPERTY INSTALL_RPATH
"$ORIGIN/../lib/swift/host")
endif()

set_property(TARGET ${name}
PROPERTY BUILD_WITH_INSTALL_RPATH YES)
Expand Down
81 changes: 64 additions & 17 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
target_link_libraries(${target} PRIVATE "swiftCore")

target_link_directories(${target} PRIVATE ${host_lib_dir})
if(ASRLF_BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
set(swift_runtime_rpath "${host_lib_dir}")
else()
set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
endif()

# At runtime, use swiftCore in the current toolchain.
# For building stdlib, LD_LIBRARY_PATH will be set to builder's stdlib
# FIXME: This assumes the ABI hasn't changed since the builder.
set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")

elseif(ASRLF_BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING")
# At build time link against the built swift libraries from the
Expand All @@ -573,10 +573,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
endif()

if(SWIFT_SWIFT_PARSER)
# Make sure we can find the early SwiftSyntax libraries.
target_link_directories(${target} PRIVATE "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")

# For the "end step" of bootstrapping configurations on Darwin, need to be
# For the "end step" of bootstrapping configurations, we need to be
# able to fall back to the SDK directory for libswiftCore et al.
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
if (NOT "${bootstrapping}" STREQUAL "1")
Expand All @@ -590,6 +587,13 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ABSOLUTE)
target_link_directories(${target} PUBLIC ${TOOLCHAIN_LIB_DIR})
else()
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
target_link_directories(${target} PRIVATE ${host_lib_dir})

set(swift_runtime_rpath "${host_lib_dir}")
endif()
endif()
endif()
Expand Down Expand Up @@ -822,8 +826,43 @@ macro(add_swift_lib_subdirectory name)
add_llvm_subdirectory(SWIFT LIB ${name})
endmacro()

# Add a new Swift host executable.
#
# Usage:
# add_swift_host_tool(name
# [HAS_SWIFT_MODULES | DOES_NOT_USE_SWIFT]
# [THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY]
#
# [BOOTSTRAPPING 0|1]
# [SWIFT_COMPONENT component]
# [LLVM_LINK_COMPONENTS comp1 ...]
# source1 [source2 source3 ...])
#
# name
# Name of the executable (e.g., swift-frontend).
#
# HAS_SWIFT_MODULES
# Whether to link with SwiftCompilerSources library
#
# DOES_NOT_USE_SWIFT
# Do not link with swift runtime
#
# THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
# Opt-out of LLVM IR optimizations when linking ThinLTO with ld64
#
# BOOTSTRAPPING
# Bootstrapping stage.
#
# SWIFT_COMPONENT
# Installation component where this tool belongs to.
#
# LLVM_LINK_COMPONENTS
# LLVM components this library depends on.
#
# source1 ...
# Sources to add into this executable.
function(add_swift_host_tool executable)
set(options HAS_SWIFT_MODULES THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
set(options HAS_SWIFT_MODULES DOES_NOT_USE_SWIFT THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
set(single_parameter_options SWIFT_COMPONENT BOOTSTRAPPING)
set(multiple_parameter_options LLVM_LINK_COMPONENTS)

Expand Down Expand Up @@ -879,12 +918,12 @@ function(add_swift_host_tool executable)
endif()

# Once the new Swift parser is linked in, every host tool has Swift modules.
if (SWIFT_SWIFT_PARSER)
if (SWIFT_SWIFT_PARSER AND NOT ASHT_DOES_NOT_USE_SWIFT)
set(ASHT_HAS_SWIFT_MODULES ON)
endif()

if (ASHT_HAS_SWIFT_MODULES)
_add_swift_runtime_link_flags(${executable} "../lib" "${ASHT_BOOTSTRAPPING}")
_add_swift_runtime_link_flags(${executable} "../lib" "${ASHT_BOOTSTRAPPING}")
endif()

llvm_update_compile_flags(${executable})
Expand Down Expand Up @@ -926,10 +965,17 @@ function(add_swift_host_tool executable)
endif()
endif()

set_property(
TARGET ${executable}
APPEND PROPERTY INSTALL_RPATH
"@executable_path/../${extra_relative_rpath}lib/swift/host")
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
set_property(
TARGET ${executable}
APPEND PROPERTY INSTALL_RPATH
"@executable_path/../${extra_relative_rpath}lib/swift/host")
else()
set_property(
TARGET ${executable}
APPEND PROPERTY INSTALL_RPATH
"$ORIGIN/../${extra_relative_rpath}lib/swift/host")
endif()
endif()

if(ASHT_THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
Expand All @@ -950,7 +996,8 @@ function(add_swift_host_tool executable)
swift_install_in_component(TARGETS ${executable}
RUNTIME
DESTINATION bin
COMPONENT ${ASHT_SWIFT_COMPONENT})
COMPONENT ${ASHT_SWIFT_COMPONENT}
)

swift_is_installing_component(${ASHT_SWIFT_COMPONENT} is_installing)
endif()
Expand Down
27 changes: 25 additions & 2 deletions cmake/modules/AddSwiftUnittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,32 @@ add_custom_target(SwiftUnitTests)

set_target_properties(SwiftUnitTests PROPERTIES FOLDER "Tests")

# Add a new Swift unit test executable.
#
# Usage:
# add_swift_unittest(name
# [IS_TARGET_TEST]
# source1 [source2 source3 ...])
#
# name
# Name of the test (e.g., SwiftASTTest).
#
# IS_TARGET_TEST
# Indicates this is a test for target libraries. Not host library.
# This avoids linking with toolchains stdlib.
#
# source1 ...
# Sources to add into this executable.
function(add_swift_unittest test_dirname)
cmake_parse_arguments(ASU
"IS_TARGET_TEST"
""
""
${ARGN})

# *NOTE* Even though "add_unittest" does not have llvm in its name, it is a
# function defined by AddLLVM.cmake.
add_unittest(SwiftUnitTests ${test_dirname} ${ARGN})
add_unittest(SwiftUnitTests ${test_dirname} ${ASU_UNPARSED_ARGUMENTS})

set_target_properties(${test_dirname} PROPERTIES LINKER_LANGUAGE CXX)

Expand Down Expand Up @@ -89,7 +111,8 @@ function(add_swift_unittest test_dirname)
endif()
endif()

if (SWIFT_SWIFT_PARSER)
if (SWIFT_SWIFT_PARSER AND NOT ASU_IS_TARGET_TEST)
# Link to stdlib the compiler uses.
_add_swift_runtime_link_flags(${test_dirname} "../../lib" "")
set_property(TARGET ${test_dirname} PROPERTY BUILD_WITH_INSTALL_RPATH OFF)
endif()
Expand Down
24 changes: 24 additions & 0 deletions cmake/modules/SetRPATH.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set(SWIFT_SET_RPATH_SCRIPT_FILE "${CMAKE_CURRENT_LIST_FILE}")

function(swift_get_set_rpath_script_file out_var)
set(${out_var} "${SWIFT_SET_RPATH_SCRIPT_FILE}" PARENT_SCOPE)
endfunction()

# Actual RPATH_CHANGE operation to the file.
function(_swift_set_rpath_impl file new_rpath)
# FIXME: Handle non-ELF files. We can't use RPATH_SET because it's only available CMake 3.21.0
execute_process(
COMMAND readelf -Wd "${file}"
COMMAND grep -Po "R(UN)?PATH.*\\[\\K[^\\]]*"
OUTPUT_VARIABLE current_rpath
)
string(STRIP "${current_rpath}" current_rpath)

# NOTE: RPATH_CHANGE is not documented, and works only for ELF and XCOFF.
file(RPATH_CHANGE FILE "${file}" OLD_RPATH "${current_rpath}" NEW_RPATH "${new_rpath}")
endfunction()

# For 'cmake -P <scirpt>'.
if (SWIFT_SET_RPATH_FILE AND SWIFT_SET_RPATH_NEW_RPATH)
_swift_set_rpath_impl("${SWIFT_SET_RPATH_FILE}" "${SWIFT_SET_RPATH_NEW_RPATH}")
endif()
2 changes: 1 addition & 1 deletion cmake/modules/SwiftImplicitImport.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function(swift_supports_implicit_module module_name out_var)
"${CMAKE_Swift_COMPILER}"
-Xfrontend -disable-implicit-${module_name}-module-import
-Xfrontend -parse-stdlib
-c - -o /dev/null
-parse -
INPUT_FILE
"${CMAKE_BINARY_DIR}/tmp/empty-check-${module_name}.swift"
OUTPUT_QUIET ERROR_QUIET
Expand Down
10 changes: 10 additions & 0 deletions cmake/modules/SwiftUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ function(get_bootstrapping_swift_lib_dir bs_lib_dir bootstrapping)
elseif("${bootstrapping}" STREQUAL "")
get_bootstrapping_path(bs_lib_dir ${lib_dir} "1")
endif()
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
# Compiler's INSTALL_RPATH is set to libs in the build directory
# For building stdlib, use stdlib in the builder's resource directory
# because the runtime may not be built yet.
# FIXME: This assumes the ABI hasn't changed since the builder.
get_filename_component(swift_bin_dir ${CMAKE_Swift_COMPILER} DIRECTORY)
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
set(bs_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
endif()
endif()
set(bs_lib_dir ${bs_lib_dir} PARENT_SCOPE)
endfunction()
Expand Down
32 changes: 28 additions & 4 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (SWIFT_SWIFT_PARSER)
list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "SwiftSyntax::"
OUTPUT_VARIABLE SWIFT_SYNTAX_TARGETS)

set(SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR
set(SWIFT_SYNTAX_LIBRARIES_BUILD_DIR
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
set(SWIFT_HOST_LIBRARIES_DEST_DIR
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
Expand All @@ -56,17 +56,35 @@ if (SWIFT_SWIFT_PARSER)
# Copy over all of the shared libraries from earlyswiftsyntax so they can
# be found via RPATH.
foreach (sharedlib ${SWIFT_SYNTAX_SHARED_LIBRARIES})
set(add_origin_rpath)
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
# At runtime, use swiftCore in the current toolchain.
swift_get_set_rpath_script_file(setrpath_command)
set(add_origin_rpath COMMAND ${CMAKE_COMMAND}
"-DSWIFT_SET_RPATH_FILE=${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
"-DSWIFT_SET_RPATH_NEW_RPATH='$$ORIGIN:$$ORIGIN/../${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}'"
-P "${setrpath_command}"
)
endif()

add_custom_command(
OUTPUT "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
DEPENDS "${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}
DEPENDS "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}
${add_origin_rpath}
)

add_custom_target(copy_swiftSyntaxLibrary_${sharedlib}
DEPENDS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
COMMENT "Copying ${sharedlib}"
)

swift_install_in_component(
PROGRAMS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host"
COMPONENT compiler
)

add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${sharedlib})
endforeach()

Expand All @@ -79,7 +97,7 @@ if (SWIFT_SWIFT_PARSER)
foreach(module_dir ${SWIFT_SYNTAX_MODULE_DIRS})
# Find all of the source module files.
file(GLOB module_files
"${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${module_dir}/*.swiftinterface")
"${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${module_dir}/*.swiftinterface")

# Determine the destination module files.
set(dest_module_files)
Expand All @@ -101,6 +119,12 @@ if (SWIFT_SWIFT_PARSER)
COMMENT "Copying ${module_dir}"
)

swift_install_in_component(
FILES ${dest_module_files}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/${module_dir}"
COMPONENT compiler
)

add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxModule_${module_dir})
endforeach()

Expand Down
Loading