Skip to content

Commit 4c5b57f

Browse files
committed
[static] ELF platforms can build static binaries.
Static binaries can be built on other platforms, not just Linux; we use the same check for ELF output to condition static binaries like for static stdlib. However, since some ELF platforms don't need -ldl, pull this out into a conditionally set variable. As a side note: on OpenBSD, lld at HEAD does not work to link the test binary, but the platform lld works fine, so XFAIL this temporarily.
1 parent 02a7936 commit 4c5b57f

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

stdlib/public/runtime/CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ else()
9696
endif()
9797
endif()
9898

99+
# TODO: Put the logic for finding libdl in a module, similar to ICU.
100+
set(libdl)
101+
if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
102+
set(libdl -ldl)
103+
endif()
104+
99105

100106
set(sdk "${SWIFT_HOST_VARIANT_SDK}")
101-
if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
107+
if(SWIFT_BUILD_STATIC_STDLIB AND ${SWIFT_SDK_${sdk}_OBJECT_FORMAT} STREQUAL ELF)
102108
list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp)
103109
set(static_binary_lnk_file_list)
104110
string(TOLOWER "${sdk}" lowercase_sdk)
@@ -157,7 +163,7 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
157163
${libicu_i18n_a}
158164
${libicu_uc_a}
159165
${libicu_data_a}
160-
-ldl
166+
${libdl}
161167
-lstdc++
162168
-lm")
163169

@@ -166,7 +172,7 @@ ${libicu_data_a}
166172
DESTINATION "lib/swift_static/${lowercase_sdk}"
167173
COMPONENT stdlib)
168174
add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list})
169-
foreach(arch IN LISTS SWIFT_SDK_LINUX_ARCHITECTURES)
175+
foreach(arch IN LISTS SWIFT_SDK_${sdk}_ARCHITECTURES)
170176
add_dependencies(static_binary_magic ${swift_image_inspection_${arch}_static})
171177
endforeach()
172178
add_dependencies(static_binary_magic ${swift_image_inspection_static_primary_arch})
@@ -286,7 +292,7 @@ foreach(sdk ${SWIFT_CONFIGURED_SDKS})
286292
string(TOLOWER "${sdk}" lowercase_sdk)
287293
set(linkfile ${lowercase_sdk}/static-stdlib-args.lnk)
288294
file(WRITE "${SWIFTSTATICLIB_DIR}/${linkfile}" "
289-
-ldl
295+
${libdl}
290296
-lpthread
291297
-lswiftCore
292298
-lswiftImageInspectionShared

test/Driver/static-executable.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Create a self contained binary
22
// REQUIRES: OS=linux-gnu
33
// REQUIRES: static_stdlib
4+
// XFAIL: OS=openbsd
45
print("hello world!")
56
// RUN: %empty-directory(%t)
67
// RUN: %target-swiftc_driver -static-executable -o %t/static-executable %s

test/Driver/static-stdlib-linux.swift renamed to test/Driver/static-stdlib.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Statically link a "hello world" program
2-
// REQUIRES: OS=linux-gnu
2+
// REQUIRES: OS=linux-gnu || OS=openbsd
33
// REQUIRES: static_stdlib
44
print("hello world!")
55
// RUN: %empty-directory(%t)

unittests/runtime/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
2929
endif()
3030

3131
set(swift_runtime_test_extra_libraries)
32-
if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX")
32+
if(SWIFT_BUILD_STATIC_STDLIB AND
33+
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT} STREQUAL ELF)
3334
list(APPEND swift_runtime_test_extra_libraries
3435
$<TARGET_FILE:swiftImageInspectionShared-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}>)
3536
endif()

0 commit comments

Comments
 (0)