Skip to content

Commit e994f61

Browse files
authored
Merge pull request #9958 from spevans/pr_static_libs
SR-648: Allow swiftpm to statically link binaries on Linux
2 parents 32d15d8 + fc93261 commit e994f61

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

lib/Driver/ToolChains.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
15991599

16001600
// Add the runtime library link path, which is platform-specific and found
16011601
// relative to the compiler.
1602-
if (!staticExecutable && shouldProvideRPathToLinker()) {
1602+
if (!(staticExecutable || staticStdlib) && shouldProvideRPathToLinker()) {
16031603
// FIXME: We probably shouldn't be adding an rpath here unless we know
16041604
// ahead of time the standard library won't be copied.
16051605
Arguments.push_back("-Xlinker");
@@ -1615,8 +1615,6 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
16151615
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
16161616
addInputsOfType(Arguments, context.InputActions, types::TY_Object);
16171617

1618-
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
1619-
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
16201618
for (const Arg *arg : context.Args.filtered(options::OPT_F,
16211619
options::OPT_Fsystem)) {
16221620
if (arg->getOption().matches(options::OPT_Fsystem))
@@ -1631,6 +1629,14 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
16311629
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
16321630
}
16331631

1632+
// Add any autolinking scripts to the arguments
1633+
for (const Job *Cmd : context.Inputs) {
1634+
auto &OutputInfo = Cmd->getOutput();
1635+
if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile)
1636+
Arguments.push_back(context.Args.MakeArgString(
1637+
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
1638+
}
1639+
16341640
// Link the standard library.
16351641
Arguments.push_back("-L");
16361642

@@ -1692,14 +1698,8 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
16921698
Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
16931699
}
16941700

1695-
1696-
// Add any autolinking scripts to the arguments
1697-
for (const Job *Cmd : context.Inputs) {
1698-
auto &OutputInfo = Cmd->getOutput();
1699-
if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile)
1700-
Arguments.push_back(context.Args.MakeArgString(
1701-
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
1702-
}
1701+
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
1702+
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
17031703

17041704
// Just before the output option, allow GenericUnix toolchains to add
17051705
// additional inputs.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Statically link a "hello world" program
2+
// REQUIRES: static_stdlib
3+
print("hello world!")
4+
// RUN: %empty-directory(%t)
5+
// RUN: %target-swiftc_driver -driver-print-jobs -static-stdlib -o %t/static-stdlib %s -Xlinker --no-allow-multiple-definition 2>&1| %FileCheck %s
6+
// CHECK: {{.*}}/swift -frontend -c -primary-file {{.*}}/linker-args-order-linux.swift -target x86_64-unknown-linux-gnu -disable-objc-interop
7+
// CHECK: {{.*}}/swift-autolink-extract{{.*}}
8+
// CHECK: {{.*}}swift_begin.o /{{.*}}/linker-args-order-linux-{{[a-z0-9]+}}.o @/{{.*}}/linker-args-order-linux-{{[a-z0-9]+}}.autolink {{.*}} @{{.*}}/static-stdlib-args.lnk {{.*}} -Xlinker --no-allow-multiple-definition {{.*}}/swift_end.o

utils/build-script-impl

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

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

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

0 commit comments

Comments
 (0)