Skip to content

Commit f3891f2

Browse files
authored
Merge pull request #68197 from rintaro/5.9.0-swift-swift-linux
[5.9.0][CMake] Support Macros in Linux
2 parents e4315e0 + d34a60d commit f3891f2

File tree

24 files changed

+322
-109
lines changed

24 files changed

+322
-109
lines changed

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ include(SwiftConfigureSDK)
799799
include(SwiftComponents)
800800
include(SwiftList)
801801
include(AddPureSwift)
802+
include(SetRPATH)
802803

803804
# Configure swift include, install, build components.
804805
swift_configure_components()
@@ -957,6 +958,12 @@ if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
957958
else()
958959
set(SWIFT_SWIFT_PARSER TRUE)
959960
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS})
961+
962+
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
963+
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
964+
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
965+
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
966+
endif()
960967
endif()
961968
endif()
962969

SwiftCompilerSources/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ else()
231231
#
232232
# step 1: generate a dummy source file, which just includes all headers
233233
# defined in include/swift/module.modulemap
234-
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
234+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
235235
"
236236
#include \"Basic/BridgedSwiftObject.h\"
237237
#include \"Basic/BasicBridging.h\"
@@ -247,6 +247,12 @@ else()
247247
248248
#include \"Parse/RegexParserBridging.h\"
249249
")
250+
add_custom_command(
251+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
252+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
253+
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
254+
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
255+
)
250256

251257
# step 2: build a library containing that source file. This library depends on all the included header files.
252258
# The swift modules can now depend on that target.

cmake/modules/AddPureSwift.cmake

+30-4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ function(_add_host_swift_compile_options name)
6565
_add_host_variant_swift_sanitizer_flags(${name})
6666
endfunction()
6767

68+
function(_set_pure_swift_link_flags name relpath_to_lib_dir)
69+
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
70+
# Don't add builder's stdlib RPATH automatically.
71+
target_compile_options(${name} PRIVATE
72+
$<$<COMPILE_LANGUAGE:Swift>:-no-toolchain-stdlib-rpath>
73+
)
74+
75+
set_property(TARGET ${name}
76+
APPEND PROPERTY INSTALL_RPATH
77+
# At runtime, use swiftCore in the current just-built toolchain.
78+
# NOTE: This relies on the ABI being the same as the builder.
79+
"$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}"
80+
)
81+
# NOTE: At this point we don't have any pure swift executables/shared
82+
# libraries required for building runtime/stdlib. So we don't need to add
83+
# RPATH to the builder's runtime.
84+
endif()
85+
endfunction()
86+
6887
# Add a new "pure" Swift host library.
6988
#
7089
# "Pure" Swift host libraries can only contain Swift code, and will be built
@@ -266,10 +285,17 @@ function(add_pure_swift_host_tool name)
266285
# Create the library.
267286
add_executable(${name} ${APSHT_SOURCES})
268287
_add_host_swift_compile_options(${name})
269-
270-
set_property(TARGET ${name}
271-
APPEND PROPERTY INSTALL_RPATH
272-
"@executable_path/../lib/swift/host")
288+
_set_pure_swift_link_flags(${name} "../lib")
289+
290+
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
291+
set_property(TARGET ${name}
292+
APPEND PROPERTY INSTALL_RPATH
293+
"@executable_path/../lib/swift/host")
294+
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
295+
set_property(TARGET ${name}
296+
APPEND PROPERTY INSTALL_RPATH
297+
"$ORIGIN/../lib/swift/host")
298+
endif()
273299

274300
set_property(TARGET ${name}
275301
PROPERTY BUILD_WITH_INSTALL_RPATH YES)

cmake/modules/AddSwift.cmake

+64-17
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
545545
target_link_libraries(${target} PRIVATE "swiftCore")
546546

547547
target_link_directories(${target} PRIVATE ${host_lib_dir})
548-
if(ASRLF_BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
549-
set(swift_runtime_rpath "${host_lib_dir}")
550-
else()
551-
set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
552-
endif()
548+
549+
# At runtime, use swiftCore in the current toolchain.
550+
# For building stdlib, LD_LIBRARY_PATH will be set to builder's stdlib
551+
# FIXME: This assumes the ABI hasn't changed since the builder.
552+
set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
553553

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

575575
if(SWIFT_SWIFT_PARSER)
576-
# Make sure we can find the early SwiftSyntax libraries.
577-
target_link_directories(${target} PRIVATE "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
578-
579-
# For the "end step" of bootstrapping configurations on Darwin, need to be
576+
# For the "end step" of bootstrapping configurations, we need to be
580577
# able to fall back to the SDK directory for libswiftCore et al.
581578
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
582579
if (NOT "${bootstrapping}" STREQUAL "1")
@@ -590,6 +587,13 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
590587
get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
591588
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ABSOLUTE)
592589
target_link_directories(${target} PUBLIC ${TOOLCHAIN_LIB_DIR})
590+
else()
591+
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
592+
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
593+
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
594+
target_link_directories(${target} PRIVATE ${host_lib_dir})
595+
596+
set(swift_runtime_rpath "${host_lib_dir}")
593597
endif()
594598
endif()
595599
endif()
@@ -822,8 +826,43 @@ macro(add_swift_lib_subdirectory name)
822826
add_llvm_subdirectory(SWIFT LIB ${name})
823827
endmacro()
824828

829+
# Add a new Swift host executable.
830+
#
831+
# Usage:
832+
# add_swift_host_tool(name
833+
# [HAS_SWIFT_MODULES | DOES_NOT_USE_SWIFT]
834+
# [THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY]
835+
#
836+
# [BOOTSTRAPPING 0|1]
837+
# [SWIFT_COMPONENT component]
838+
# [LLVM_LINK_COMPONENTS comp1 ...]
839+
# source1 [source2 source3 ...])
840+
#
841+
# name
842+
# Name of the executable (e.g., swift-frontend).
843+
#
844+
# HAS_SWIFT_MODULES
845+
# Whether to link with SwiftCompilerSources library
846+
#
847+
# DOES_NOT_USE_SWIFT
848+
# Do not link with swift runtime
849+
#
850+
# THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY
851+
# Opt-out of LLVM IR optimizations when linking ThinLTO with ld64
852+
#
853+
# BOOTSTRAPPING
854+
# Bootstrapping stage.
855+
#
856+
# SWIFT_COMPONENT
857+
# Installation component where this tool belongs to.
858+
#
859+
# LLVM_LINK_COMPONENTS
860+
# LLVM components this library depends on.
861+
#
862+
# source1 ...
863+
# Sources to add into this executable.
825864
function(add_swift_host_tool executable)
826-
set(options HAS_SWIFT_MODULES THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
865+
set(options HAS_SWIFT_MODULES DOES_NOT_USE_SWIFT THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
827866
set(single_parameter_options SWIFT_COMPONENT BOOTSTRAPPING)
828867
set(multiple_parameter_options LLVM_LINK_COMPONENTS)
829868

@@ -879,12 +918,12 @@ function(add_swift_host_tool executable)
879918
endif()
880919

881920
# Once the new Swift parser is linked in, every host tool has Swift modules.
882-
if (SWIFT_SWIFT_PARSER)
921+
if (SWIFT_SWIFT_PARSER AND NOT ASHT_DOES_NOT_USE_SWIFT)
883922
set(ASHT_HAS_SWIFT_MODULES ON)
884923
endif()
885924

886925
if (ASHT_HAS_SWIFT_MODULES)
887-
_add_swift_runtime_link_flags(${executable} "../lib" "${ASHT_BOOTSTRAPPING}")
926+
_add_swift_runtime_link_flags(${executable} "../lib" "${ASHT_BOOTSTRAPPING}")
888927
endif()
889928

890929
llvm_update_compile_flags(${executable})
@@ -926,10 +965,17 @@ function(add_swift_host_tool executable)
926965
endif()
927966
endif()
928967

929-
set_property(
930-
TARGET ${executable}
931-
APPEND PROPERTY INSTALL_RPATH
932-
"@executable_path/../${extra_relative_rpath}lib/swift/host")
968+
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
969+
set_property(
970+
TARGET ${executable}
971+
APPEND PROPERTY INSTALL_RPATH
972+
"@executable_path/../${extra_relative_rpath}lib/swift/host")
973+
else()
974+
set_property(
975+
TARGET ${executable}
976+
APPEND PROPERTY INSTALL_RPATH
977+
"$ORIGIN/../${extra_relative_rpath}lib/swift/host")
978+
endif()
933979
endif()
934980

935981
if(ASHT_THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
@@ -950,7 +996,8 @@ function(add_swift_host_tool executable)
950996
swift_install_in_component(TARGETS ${executable}
951997
RUNTIME
952998
DESTINATION bin
953-
COMPONENT ${ASHT_SWIFT_COMPONENT})
999+
COMPONENT ${ASHT_SWIFT_COMPONENT}
1000+
)
9541001

9551002
swift_is_installing_component(${ASHT_SWIFT_COMPONENT} is_installing)
9561003
endif()

cmake/modules/AddSwiftUnittests.cmake

+25-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,32 @@ add_custom_target(SwiftUnitTests)
55

66
set_target_properties(SwiftUnitTests PROPERTIES FOLDER "Tests")
77

8+
# Add a new Swift unit test executable.
9+
#
10+
# Usage:
11+
# add_swift_unittest(name
12+
# [IS_TARGET_TEST]
13+
# source1 [source2 source3 ...])
14+
#
15+
# name
16+
# Name of the test (e.g., SwiftASTTest).
17+
#
18+
# IS_TARGET_TEST
19+
# Indicates this is a test for target libraries. Not host library.
20+
# This avoids linking with toolchains stdlib.
21+
#
22+
# source1 ...
23+
# Sources to add into this executable.
824
function(add_swift_unittest test_dirname)
25+
cmake_parse_arguments(ASU
26+
"IS_TARGET_TEST"
27+
""
28+
""
29+
${ARGN})
30+
931
# *NOTE* Even though "add_unittest" does not have llvm in its name, it is a
1032
# function defined by AddLLVM.cmake.
11-
add_unittest(SwiftUnitTests ${test_dirname} ${ARGN})
33+
add_unittest(SwiftUnitTests ${test_dirname} ${ASU_UNPARSED_ARGUMENTS})
1234

1335
set_target_properties(${test_dirname} PROPERTIES LINKER_LANGUAGE CXX)
1436

@@ -89,7 +111,8 @@ function(add_swift_unittest test_dirname)
89111
endif()
90112
endif()
91113

92-
if (SWIFT_SWIFT_PARSER)
114+
if (SWIFT_SWIFT_PARSER AND NOT ASU_IS_TARGET_TEST)
115+
# Link to stdlib the compiler uses.
93116
_add_swift_runtime_link_flags(${test_dirname} "../../lib" "")
94117
set_property(TARGET ${test_dirname} PROPERTY BUILD_WITH_INSTALL_RPATH OFF)
95118
endif()

cmake/modules/SetRPATH.cmake

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
set(SWIFT_SET_RPATH_SCRIPT_FILE "${CMAKE_CURRENT_LIST_FILE}")
2+
3+
function(swift_get_set_rpath_script_file out_var)
4+
set(${out_var} "${SWIFT_SET_RPATH_SCRIPT_FILE}" PARENT_SCOPE)
5+
endfunction()
6+
7+
# Actual RPATH_CHANGE operation to the file.
8+
function(_swift_set_rpath_impl file new_rpath)
9+
# FIXME: Handle non-ELF files. We can't use RPATH_SET because it's only available CMake 3.21.0
10+
execute_process(
11+
COMMAND readelf -Wd "${file}"
12+
COMMAND grep -Po "R(UN)?PATH.*\\[\\K[^\\]]*"
13+
OUTPUT_VARIABLE current_rpath
14+
)
15+
string(STRIP "${current_rpath}" current_rpath)
16+
17+
# NOTE: RPATH_CHANGE is not documented, and works only for ELF and XCOFF.
18+
file(RPATH_CHANGE FILE "${file}" OLD_RPATH "${current_rpath}" NEW_RPATH "${new_rpath}")
19+
endfunction()
20+
21+
# For 'cmake -P <scirpt>'.
22+
if (SWIFT_SET_RPATH_FILE AND SWIFT_SET_RPATH_NEW_RPATH)
23+
_swift_set_rpath_impl("${SWIFT_SET_RPATH_FILE}" "${SWIFT_SET_RPATH_NEW_RPATH}")
24+
endif()

cmake/modules/SwiftImplicitImport.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function(swift_supports_implicit_module module_name out_var)
66
"${CMAKE_Swift_COMPILER}"
77
-Xfrontend -disable-implicit-${module_name}-module-import
88
-Xfrontend -parse-stdlib
9-
-c - -o /dev/null
9+
-parse -
1010
INPUT_FILE
1111
"${CMAKE_BINARY_DIR}/tmp/empty-check-${module_name}.swift"
1212
OUTPUT_QUIET ERROR_QUIET

cmake/modules/SwiftUtils.cmake

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ function(get_bootstrapping_swift_lib_dir bs_lib_dir bootstrapping)
107107
elseif("${bootstrapping}" STREQUAL "")
108108
get_bootstrapping_path(bs_lib_dir ${lib_dir} "1")
109109
endif()
110+
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
111+
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
112+
# Compiler's INSTALL_RPATH is set to libs in the build directory
113+
# For building stdlib, use stdlib in the builder's resource directory
114+
# because the runtime may not be built yet.
115+
# FIXME: This assumes the ABI hasn't changed since the builder.
116+
get_filename_component(swift_bin_dir ${CMAKE_Swift_COMPILER} DIRECTORY)
117+
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
118+
set(bs_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
119+
endif()
110120
endif()
111121
set(bs_lib_dir ${bs_lib_dir} PARENT_SCOPE)
112122
endfunction()

lib/CMakeLists.txt

+28-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ if (SWIFT_SWIFT_PARSER)
3636
list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "SwiftSyntax::"
3737
OUTPUT_VARIABLE SWIFT_SYNTAX_TARGETS)
3838

39-
set(SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR
39+
set(SWIFT_SYNTAX_LIBRARIES_BUILD_DIR
4040
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
4141
set(SWIFT_HOST_LIBRARIES_DEST_DIR
4242
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
@@ -56,17 +56,35 @@ if (SWIFT_SWIFT_PARSER)
5656
# Copy over all of the shared libraries from earlyswiftsyntax so they can
5757
# be found via RPATH.
5858
foreach (sharedlib ${SWIFT_SYNTAX_SHARED_LIBRARIES})
59+
set(add_origin_rpath)
60+
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
61+
# At runtime, use swiftCore in the current toolchain.
62+
swift_get_set_rpath_script_file(setrpath_command)
63+
set(add_origin_rpath COMMAND ${CMAKE_COMMAND}
64+
"-DSWIFT_SET_RPATH_FILE=${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
65+
"-DSWIFT_SET_RPATH_NEW_RPATH='$$ORIGIN:$$ORIGIN/../${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}'"
66+
-P "${setrpath_command}"
67+
)
68+
endif()
69+
5970
add_custom_command(
6071
OUTPUT "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
61-
DEPENDS "${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib}"
62-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}
72+
DEPENDS "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib}"
73+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}
74+
${add_origin_rpath}
6375
)
6476

6577
add_custom_target(copy_swiftSyntaxLibrary_${sharedlib}
6678
DEPENDS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
6779
COMMENT "Copying ${sharedlib}"
6880
)
6981

82+
swift_install_in_component(
83+
PROGRAMS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
84+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host"
85+
COMPONENT compiler
86+
)
87+
7088
add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${sharedlib})
7189
endforeach()
7290

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

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

122+
swift_install_in_component(
123+
FILES ${dest_module_files}
124+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/${module_dir}"
125+
COMPONENT compiler
126+
)
127+
104128
add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxModule_${module_dir})
105129
endforeach()
106130

0 commit comments

Comments
 (0)