Skip to content

SR-648: Allow swiftpm to statically link binaries on Linux #9958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,

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

context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
for (const Arg *arg : context.Args.filtered(options::OPT_F,
options::OPT_Fsystem)) {
if (arg->getOption().matches(options::OPT_Fsystem))
Expand All @@ -1550,6 +1548,14 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
}

// Add any autolinking scripts to the arguments
for (const Job *Cmd : context.Inputs) {
auto &OutputInfo = Cmd->getOutput();
if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile)
Arguments.push_back(context.Args.MakeArgString(
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
}

// Link the standard library.
Arguments.push_back("-L");

Expand Down Expand Up @@ -1601,14 +1607,8 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
}


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

// Just before the output option, allow GenericUnix toolchains to add
// additional inputs.
Expand Down
8 changes: 8 additions & 0 deletions test/Driver/linker-args-order-linux.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Statically link a "hello world" program
// REQUIRES: static_stdlib
print("hello world!")
// RUN: %empty-directory(%t)
// RUN: %target-swiftc_driver -driver-print-jobs -static-stdlib -o %t/static-stdlib %s -Xlinker --no-allow-multiple-definition 2>&1| %FileCheck %s
// CHECK: {{.*}}/swift -frontend -c -primary-file {{.*}}/linker-args-order-linux.swift -target x86_64-unknown-linux-gnu -disable-objc-interop
// CHECK: {{.*}}/swift-autolink-extract{{.*}}
// 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
22 changes: 20 additions & 2 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -2454,14 +2454,21 @@ for host in "${ALL_HOSTS[@]}"; do
else
dispatch_build_variant_arg="debug"
fi

if [ $(true_false "${BUILD_SWIFT_STATIC_STDLIB}") == "TRUE" ]; then
libdispatch_enable_static="--enable-static=yes"
else
libdispatch_enable_static=""
fi

call mkdir -p "${LIBDISPATCH_BUILD_DIR}"
with_pushd "${LIBDISPATCH_SOURCE_DIR}" \
call autoreconf -fvi
with_pushd "${LIBDISPATCH_BUILD_DIR}" \
call env CC="${LLVM_BIN}/clang" CXX="${LLVM_BIN}/clang++" SWIFTC="${SWIFTC_BIN}" \
"${LIBDISPATCH_SOURCE_DIR}"/configure --with-swift-toolchain="${SWIFT_BUILD_PATH}" \
--with-build-variant=$dispatch_build_variant_arg \
--prefix="$(get_host_install_destdir ${host})$(get_host_install_prefix ${host})"
--prefix="$(get_host_install_destdir ${host})$(get_host_install_prefix ${host})" ${libdispatch_enable_static}
else
echo "Skipping reconfiguration of libdispatch"
fi
Expand Down Expand Up @@ -3050,10 +3057,16 @@ for host in "${ALL_HOSTS[@]}"; do
echo "--- Installing ${product} ---"
XCTEST_BUILD_DIR=$(build_directory ${host} xctest)
XCTEST_INSTALL_PREFIX="${host_install_destdir}${host_install_prefix}/lib/swift/${LIB_TARGET}"
if [ $(true_false "${BUILD_SWIFT_STATIC_STDLIB}") == "TRUE" ]; then
XCTEST_STATIC_INSTALL_PREFIX="${host_install_destdir}${host_install_prefix}/lib/swift_static/${LIB_TARGET}"
xctest_static_install="--static-library-install-path=${XCTEST_STATIC_INSTALL_PREFIX}"
else
xctest_static_install=""
fi
# Note that installing directly to /usr/lib/swift usually
# requires root permissions.
call "${XCTEST_SOURCE_DIR}"/build_script.py install \
--library-install-path="${XCTEST_INSTALL_PREFIX}" \
--library-install-path="${XCTEST_INSTALL_PREFIX}" ${xctest_static_install} \
--module-install-path="${XCTEST_INSTALL_PREFIX}"/"${SWIFT_HOST_VARIANT_ARCH}" \
"${XCTEST_BUILD_DIR}"

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

# As libdispatch installation is self-contained, we break early here.
continue
Expand Down