Skip to content

Commit 178dfa9

Browse files
committed
Fix various linker paths, variables and dependencies related to architecture specific library paths
On non-Darwin platforms we are now linking against libraries in their architecture specific sub-directories. (e.g. lib/swift/linux/x86_64). Fix various paths, variables and dependencies in AddSwift related to this change as well as removing the lipo functionality from platforms without fat binaries. Remove lipo targets for non-MachO binaries Remove the copying of binaries from their architecture specific paths to the lipo target's path on platforms that don't have universal binaries.
1 parent c572dfe commit 178dfa9

File tree

2 files changed

+144
-50
lines changed

2 files changed

+144
-50
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 136 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,8 @@ function(_add_swift_lipo_target)
535535
DEPENDS ${source_targets})
536536
else()
537537
# We don't know how to create fat binaries for other platforms.
538-
add_custom_command_target(unused_var
539-
COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${source_binaries}" "${LIPO_OUTPUT}"
540-
CUSTOM_TARGET_NAME "${LIPO_TARGET}"
541-
OUTPUT "${LIPO_OUTPUT}"
542-
DEPENDS ${source_targets})
538+
message(FATAL_ERROR
539+
"It's not possible to build a universal binary for a non-MachO target")
543540
endif()
544541
endfunction()
545542

@@ -1147,8 +1144,14 @@ function(_add_swift_library_single target name)
11471144
set(link_flags ${SWIFTLIB_SINGLE_LINK_FLAGS})
11481145
set(library_search_directories
11491146
"${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
1150-
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
1151-
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
1147+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}")
1148+
1149+
if(IS_DARWIN)
1150+
# Since we lipo each arch together on Darwin, search in the common directory
1151+
list(APPEND library_search_directories
1152+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
1153+
endif()
1154+
11521155

11531156
# Add variant-specific flags.
11541157
if(SWIFTLIB_SINGLE_TARGET_LIBRARY)
@@ -1289,8 +1292,12 @@ function(_add_swift_library_single target name)
12891292
COMPILE_FLAGS " ${c_compile_flags}")
12901293
set(library_search_directories
12911294
"${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
1292-
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
1295+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}")
1296+
if(IS_DARWIN)
1297+
list(APPEND library_search_directories
12931298
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
1299+
endif()
1300+
12941301
swift_target_link_search_directories("${target_static}" "${library_search_directories}")
12951302
target_link_libraries("${target_static}" PRIVATE
12961303
${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES})
@@ -1592,6 +1599,7 @@ function(add_swift_library name)
15921599
continue()
15931600
endif()
15941601

1602+
is_darwin_based_sdk("${sdk}" IS_DARWIN)
15951603
set(THIN_INPUT_TARGETS)
15961604

15971605
# For each architecture supported by this SDK
@@ -1784,47 +1792,105 @@ function(add_swift_library name)
17841792
)
17851793

17861794
if(NOT SWIFTLIB_OBJECT_LIBRARY)
1787-
# Add dependencies on the (not-yet-created) custom lipo target.
1788-
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
1789-
if (NOT "${DEP}" STREQUAL "icucore")
1790-
add_dependencies(${VARIANT_NAME}
1791-
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
1795+
# On Darwin we use lipo'd libraries for dependencies.
1796+
# On other targets without universal binaries we just install the target lib
1797+
if (IS_DARWIN)
1798+
# Add dependencies on the (not-yet-created) custom lipo target.
1799+
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
1800+
if (NOT "${DEP}" STREQUAL "icucore")
1801+
add_dependencies(${VARIANT_NAME}
1802+
"${DEP}-${SWIFT_SDK_{sdk}_LIB_SUBDIR}")
1803+
endif()
1804+
endforeach()
1805+
1806+
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
1807+
# Add dependencies on the (not-yet-created) custom lipo target.
1808+
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
1809+
if (NOT "${DEP}" STREQUAL "icucore")
1810+
add_dependencies("${VARIANT_NAME}-static"
1811+
"${DEP}-${SWIFT_SDK_${SDK}_LIB_SUBDIR}-static")
1812+
endif()
1813+
endforeach()
17921814
endif()
1793-
endforeach()
17941815

1795-
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
1796-
# Add dependencies on the (not-yet-created) custom lipo target.
1816+
list(APPEND THIN_INPUT_TARGETS ${VARIANT_NAME})
1817+
else()
17971818
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
17981819
if (NOT "${DEP}" STREQUAL "icucore")
1799-
add_dependencies("${VARIANT_NAME}-static"
1800-
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-static")
1820+
add_dependencies(${VARIANT_NAME}
1821+
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
18011822
endif()
18021823
endforeach()
1803-
endif()
18041824

1805-
# Note this thin library.
1806-
list(APPEND THIN_INPUT_TARGETS ${VARIANT_NAME})
1825+
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
1826+
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
1827+
if (NOT "${DEP}" STREQUAL "icucore")
1828+
add_dependencies("${VARIANT_NAME}-static"
1829+
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}-static")
1830+
endif()
1831+
endforeach()
1832+
endif()
1833+
1834+
if(SWIFTLIB_TARGET_LIBRARY)
1835+
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
1836+
set(VARIANT_SUFFIX "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
1837+
if(TARGET "swift-stdlib${VARIANT_SUFFIX}" AND TARGET "swift-test-stdlib${VARIANT_SUFFIX}")
1838+
add_dependencies("swift-stdlib${VARIANT_SUFFIX}"
1839+
"${VARIANT_NAME}")
1840+
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
1841+
add_dependencies("swift-stdlib${VARIANT_SUFFIX}"
1842+
"${VARIANT_NAME}-static")
1843+
endif()
1844+
1845+
if((NOT "${name}" STREQUAL "swiftStdlibCollectionUnittest") AND
1846+
(NOT "${name}" STREQUAL "swiftStdlibUnicodeUnittest"))
1847+
add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}"
1848+
"${VARIANT_NAME}")
1849+
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
1850+
add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}"
1851+
"${VARIANT_NAME}-static")
1852+
endif()
1853+
endif()
1854+
endif()
1855+
1856+
if(SWIFTLIB_SHARED)
1857+
set(resource_dir "swift")
1858+
set(file_permissions
1859+
OWNER_READ OWNER_WRITE
1860+
GROUP_READ
1861+
WORLD_READ)
1862+
set(install_libpath
1863+
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
1864+
else()
1865+
set(resource_dir "swift_static")
1866+
set(file_permissions
1867+
OWNER_READ OWNER_WRITE
1868+
GROUP_READ
1869+
WORLD_READ)
1870+
set(install_libpath
1871+
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
1872+
endif()
1873+
1874+
swift_install_in_component("${SWIFTLIB_INSTALL_IN_COMPONENT}"
1875+
FILES "${install_libpath}"
1876+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}"
1877+
PERMISSIONS ${file_permissions})
1878+
endforeach()
1879+
endif()
1880+
endif()
18071881
endif()
18081882
endforeach()
1883+
18091884

1810-
if(NOT SWIFTLIB_OBJECT_LIBRARY)
1885+
# Only Darwin supports universal binaries.
1886+
if (NOT SWIFTLIB_OBJECT_LIBRARY AND IS_DARWIN)
18111887
# Determine the name of the universal library.
18121888
if(SWIFTLIB_SHARED)
1813-
if("${sdk}" STREQUAL "WINDOWS")
1814-
set(UNIVERSAL_LIBRARY_NAME
1815-
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${name}.dll")
1816-
else()
1817-
set(UNIVERSAL_LIBRARY_NAME
1818-
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
1819-
endif()
1889+
set(UNIVERSAL_LIBRARY_NAME
1890+
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
18201891
else()
1821-
if("${sdk}" STREQUAL "WINDOWS")
1822-
set(UNIVERSAL_LIBRARY_NAME
1823-
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${name}.lib")
1824-
else()
1825-
set(UNIVERSAL_LIBRARY_NAME
1826-
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
1827-
endif()
1892+
set(UNIVERSAL_LIBRARY_NAME
1893+
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
18281894
endif()
18291895

18301896
set(lipo_target "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
@@ -1866,6 +1932,7 @@ function(add_swift_library name)
18661932
WORLD_READ)
18671933
endif()
18681934

1935+
# On Darwin we install the lipo'd universal binary
18691936
swift_install_in_component("${SWIFTLIB_INSTALL_IN_COMPONENT}"
18701937
FILES "${UNIVERSAL_LIBRARY_NAME}"
18711938
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}"
@@ -2039,9 +2106,16 @@ function(_add_swift_executable_single name)
20392106
set(c_compile_flags)
20402107
set(link_flags)
20412108

2042-
# Prepare linker search directories.
2043-
set(library_search_directories
2109+
# Prepare linker search directories. On Darwin, we want the fat library paths. On
2110+
# non-Darwin we want the architecture specific versions.
2111+
is_darwin_based_sdk("${SWIFTEXE_SINGLE_SDK}" IS_DARWIN)
2112+
if (IS_DARWIN)
2113+
set(library_search_directories
20442114
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
2115+
else()
2116+
set(library_search_directories
2117+
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}/${SWIFTEXE_SINGLE_ARCHITECTURE}")
2118+
endif()
20452119

20462120
# Add variant-specific flags.
20472121
_add_variant_c_compile_flags(
@@ -2067,7 +2141,6 @@ function(_add_swift_executable_single name)
20672141
list(APPEND link_flags "-Wl,-no_pie")
20682142
endif()
20692143

2070-
is_darwin_based_sdk("${SWIFTEXE_SINGLE_SDK}" IS_DARWIN)
20712144
if(IS_DARWIN)
20722145
list(APPEND link_flags
20732146
"-Xlinker" "-rpath"
@@ -2076,12 +2149,19 @@ function(_add_swift_executable_single name)
20762149

20772150
# Find the names of dependency library targets.
20782151
#
2079-
# We don't add the ${ARCH} to the target suffix because we want to link
2152+
# We don't add the ${ARCH} to the target suffix on Darwin because we want to link
20802153
# against fat libraries.
2081-
_list_add_string_suffix(
2082-
"${SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES}"
2083-
"-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}"
2084-
SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES_TARGETS)
2154+
if (IS_DARWIN)
2155+
_list_add_string_suffix(
2156+
"${SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES}"
2157+
"-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}"
2158+
SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES_TARGETS)
2159+
else()
2160+
_list_add_string_suffix(
2161+
"${SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES}"
2162+
"-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}-${SWIFTEXE_SINGLE_ARCHITECTURE}"
2163+
SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES_TARGETS)
2164+
endif()
20852165

20862166
handle_swift_sources(
20872167
dependency_target
@@ -2191,12 +2271,19 @@ function(add_swift_target_executable name)
21912271
add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}" ${VARIANT_NAME})
21922272
endif()
21932273

2194-
# Don't add the ${arch} to the suffix. We want to link against fat
2274+
# On Darwin, don't add the ${arch} to the suffix. We want to link against fat
21952275
# libraries.
2196-
_list_add_string_suffix(
2197-
"${SWIFTEXE_TARGET_DEPENDS}"
2198-
"-${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
2199-
SWIFTEXE_TARGET_DEPENDS_with_suffix)
2276+
if (IS_DARWIN)
2277+
_list_add_string_suffix(
2278+
"${SWIFTEXE_TARGET_DEPENDS}"
2279+
"-${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
2280+
SWIFTEXE_TARGET_DEPENDS_with_suffix)
2281+
else()
2282+
_list_add_string_suffix(
2283+
"${SWIFTEXE_TARGET_DEPENDS}"
2284+
"${VARIANT_SUFFIX}"
2285+
SWIFTEXE_TARGET_DEPENDS_with_suffix)
2286+
endif()
22002287
_add_swift_executable_single(
22012288
${VARIANT_NAME}
22022289
${SWIFTEXE_TARGET_SOURCES}

test/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,16 @@ foreach(SDK ${SWIFT_SDKS})
203203
list(APPEND test_dependencies "touch-covering-tests")
204204
endif()
205205

206-
set(validation_test_dependencies
206+
is_darwin_based_sdk("${SDK}" IS_DARWIN)
207+
if(IS_DARWIN)
208+
set(validation_test_dependencies
207209
"swiftStdlibCollectionUnittest-${SWIFT_SDK_${SDK}_LIB_SUBDIR}"
208210
"swiftStdlibUnicodeUnittest-${SWIFT_SDK_${SDK}_LIB_SUBDIR}")
211+
else()
212+
set(validation_test_dependencies
213+
"swiftStdlibCollectionUnittest${VARIANT_SUFFIX}"
214+
"swiftStdlibUnicodeUnittest${VARIANT_SUFFIX}")
215+
endif()
209216

210217
set(command_upload_stdlib)
211218
set(command_upload_swift_reflection_test)

0 commit comments

Comments
 (0)