Skip to content

Commit b08023a

Browse files
committed
SR-648: Allow swiftpm to statically link binaries on Linux
- If --build-swift-static-stdlib option is used then also produce static versions of libXCTest.a and libdispatch.a and put them into the lib/swift_static/linux/ toolchain directory. libFoundation.a is already being built and deployed there. - Binaries with the swift libs statically linked in can then be built with using the command: swift build -Xswiftc -static-stdlib -Xlinker -lcurl -Xlinker -l:libxml2.a -Xlinker -llzma -Xlinker /lib/x86_64-linux-gnu/libz.so.1 -Xlinker -lbsd -Xlinker --allow-multiple-definition Note: This is a dynamic binary with the libswiftCore, libFoundation and libdispatch libraries statically linked in. - Further fixes should reduce the complexity of the above command.
1 parent a0acfd6 commit b08023a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

utils/build-script-impl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,14 +2454,21 @@ for host in "${ALL_HOSTS[@]}"; do
24542454
else
24552455
dispatch_build_variant_arg="debug"
24562456
fi
2457+
2458+
if [ $(true_false "${BUILD_SWIFT_STATIC_STDLIB}") == "TRUE" ]; then
2459+
libdispatch_enable_static="--enable-static=yes"
2460+
else
2461+
libdispatch_enable_static=""
2462+
fi
2463+
24572464
call mkdir -p "${LIBDISPATCH_BUILD_DIR}"
24582465
with_pushd "${LIBDISPATCH_SOURCE_DIR}" \
24592466
call autoreconf -fvi
24602467
with_pushd "${LIBDISPATCH_BUILD_DIR}" \
24612468
call env CC="${LLVM_BIN}/clang" CXX="${LLVM_BIN}/clang++" SWIFTC="${SWIFTC_BIN}" \
24622469
"${LIBDISPATCH_SOURCE_DIR}"/configure --with-swift-toolchain="${SWIFT_BUILD_PATH}" \
24632470
--with-build-variant=$dispatch_build_variant_arg \
2464-
--prefix="$(get_host_install_destdir ${host})$(get_host_install_prefix ${host})"
2471+
--prefix="$(get_host_install_destdir ${host})$(get_host_install_prefix ${host})" ${libdispatch_enable_static}
24652472
else
24662473
echo "Skipping reconfiguration of libdispatch"
24672474
fi
@@ -3050,10 +3057,16 @@ for host in "${ALL_HOSTS[@]}"; do
30503057
echo "--- Installing ${product} ---"
30513058
XCTEST_BUILD_DIR=$(build_directory ${host} xctest)
30523059
XCTEST_INSTALL_PREFIX="${host_install_destdir}${host_install_prefix}/lib/swift/${LIB_TARGET}"
3060+
if [ $(true_false "${BUILD_SWIFT_STATIC_STDLIB}") == "TRUE" ]; then
3061+
XCTEST_STATIC_INSTALL_PREFIX="${host_install_destdir}${host_install_prefix}/lib/swift_static/${LIB_TARGET}"
3062+
xctest_static_install="--static-library-install-path=${XCTEST_STATIC_INSTALL_PREFIX}"
3063+
else
3064+
xctest_static_install=""
3065+
fi
30533066
# Note that installing directly to /usr/lib/swift usually
30543067
# requires root permissions.
30553068
call "${XCTEST_SOURCE_DIR}"/build_script.py install \
3056-
--library-install-path="${XCTEST_INSTALL_PREFIX}" \
3069+
--library-install-path="${XCTEST_INSTALL_PREFIX}" ${xctest_static_install} \
30573070
--module-install-path="${XCTEST_INSTALL_PREFIX}"/"${SWIFT_HOST_VARIANT_ARCH}" \
30583071
"${XCTEST_BUILD_DIR}"
30593072

@@ -3093,6 +3106,11 @@ for host in "${ALL_HOSTS[@]}"; do
30933106
LIBDISPATCH_BUILD_DIR=$(build_directory ${host} ${product})
30943107
with_pushd "${LIBDISPATCH_BUILD_DIR}" \
30953108
call make install
3109+
DISPATCH_LIBDIR="${host_install_destdir}${host_install_prefix}/lib/swift/${SWIFT_HOST_VARIANT}"
3110+
DISPATCH_LIBDIR_STATIC="${host_install_destdir}${host_install_prefix}/lib/swift_static/${SWIFT_HOST_VARIANT}"
3111+
if [ -f "$DISPATCH_LIBDIR/libdispatch.a" ]; then
3112+
mv "$DISPATCH_LIBDIR/libdispatch.a" "$DISPATCH_LIBDIR_STATIC"
3113+
fi
30963114

30973115
# As libdispatch installation is self-contained, we break early here.
30983116
continue

0 commit comments

Comments
 (0)