Skip to content

Commit d7f4d4c

Browse files
drewcrawfordjrose-apple
authored andcommitted
[Driver] add -static-stdlib flag (Darwin platforms) (#1817)
This commit adds the flags -static-stdlib and -no-static-stdlib to create programs statically linked (or not) with the standard library. Not is the default, which is also the current behavior. These flags are currently placebos on non-Darwin platforms.
1 parent 7a27736 commit d7f4d4c

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

include/swift/Option/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ def no_link_objc_runtime : Flag<["-"], "no-link-objc-runtime">,
258258
Flags<[HelpHidden, DoesNotAffectIncrementalBuild]>,
259259
HelpText<"Don't link in additions to the Objective-C runtime">;
260260

261+
def static_stdlib: Flag<["-"], "static-stdlib">,
262+
HelpText<"Statically link the Swift standard library">;
263+
def no_static_stdlib: Flag<["-"], "no-static-stdlib">,
264+
Flags<[HelpHidden]>,
265+
HelpText<"Don't statically link the Swift standard library">;
266+
261267
def use_ld : Joined<["-"], "use-ld=">,
262268
Flags<[DoesNotAffectIncrementalBuild]>,
263269
HelpText<"Specifies the linker to be used">;

lib/Driver/ToolChains.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,28 @@ static void getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
832832
getPlatformNameForTriple(TC.getTriple()));
833833
}
834834

835+
/// Get the runtime library link path for static linking,
836+
/// which is platform-specific and found relative to the compiler.
837+
static void getRuntimeStaticLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
838+
const llvm::opt::ArgList &args,
839+
const ToolChain &TC) {
840+
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
841+
// library link path and the standard library module import path don't
842+
// need to be the same.
843+
if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) {
844+
StringRef value = A->getValue();
845+
runtimeLibPath.append(value.begin(), value.end());
846+
} else {
847+
auto programPath = TC.getDriver().getSwiftProgramPath();
848+
runtimeLibPath.append(programPath.begin(), programPath.end());
849+
llvm::sys::path::remove_filename(runtimeLibPath); // remove /swift
850+
llvm::sys::path::remove_filename(runtimeLibPath); // remove /bin
851+
llvm::sys::path::append(runtimeLibPath, "lib", "swift_static");
852+
}
853+
llvm::sys::path::append(runtimeLibPath,
854+
getPlatformNameForTriple(TC.getTriple()));
855+
}
856+
835857
ToolChain::InvocationInfo
836858
toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
837859
const JobContext &context) const {
@@ -1054,8 +1076,22 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
10541076
// relative to the compiler.
10551077
SmallString<128> RuntimeLibPath;
10561078
getRuntimeLibraryPath(RuntimeLibPath, context.Args, *this);
1079+
1080+
// Link the standard library.
10571081
Arguments.push_back("-L");
1058-
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1082+
if (context.Args.hasFlag(options::OPT_static_stdlib,
1083+
options::OPT_no_static_stdlib,
1084+
false)) {
1085+
SmallString<128> StaticRuntimeLibPath;
1086+
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
1087+
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
1088+
Arguments.push_back("-lc++");
1089+
Arguments.push_back("-framework");
1090+
Arguments.push_back("Foundation");
1091+
Arguments.push_back("-force_load_swift_libs");
1092+
} else {
1093+
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1094+
}
10591095

10601096
if (context.Args.hasArg(options::OPT_profile_generate)) {
10611097
SmallString<128> LibProfile(RuntimeLibPath);

test/Driver/static-stdlib.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Statically link a "hello world" program
2+
// XFAIL: linux
3+
// REQUIRES: static_stdlib
4+
print("hello world!")
5+
// RUN: rm -rf %t && mkdir %t
6+
// RUN: %target-swiftc_driver -static-stdlib -o %t/static-stdlib %s
7+
// RUN: %t/static-stdlib | FileCheck %s
8+
// RUN: otool -L %t/static-stdlib | FileCheck %s --check-prefix=OTOOL
9+
// CHECK: hello world!
10+
// OTOOL-NOT: libswiftCore.dylib

0 commit comments

Comments
 (0)