Skip to content

[Driver] add -static-stdlib flag (Darwin platforms) #1817

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 1 commit into from
May 4, 2016
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
6 changes: 6 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ def no_link_objc_runtime : Flag<["-"], "no-link-objc-runtime">,
Flags<[HelpHidden, DoesNotAffectIncrementalBuild]>,
HelpText<"Don't link in additions to the Objective-C runtime">;

def static_stdlib: Flag<["-"], "static-stdlib">,
HelpText<"Statically link the Swift standard library">;
def no_static_stdlib: Flag<["-"], "no-static-stdlib">,
Flags<[HelpHidden]>,
HelpText<"Don't statically link the Swift standard library">;

def use_ld : Joined<["-"], "use-ld=">,
Flags<[DoesNotAffectIncrementalBuild]>,
HelpText<"Specifies the linker to be used">;
Expand Down
38 changes: 37 additions & 1 deletion lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,28 @@ static void getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
getPlatformNameForTriple(TC.getTriple()));
}

/// Get the runtime library link path for static linking,
/// which is platform-specific and found relative to the compiler.
static void getRuntimeStaticLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
const llvm::opt::ArgList &args,
const ToolChain &TC) {
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
// library link path and the standard library module import path don't
// need to be the same.
if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) {
StringRef value = A->getValue();
runtimeLibPath.append(value.begin(), value.end());
} else {
auto programPath = TC.getDriver().getSwiftProgramPath();
runtimeLibPath.append(programPath.begin(), programPath.end());
llvm::sys::path::remove_filename(runtimeLibPath); // remove /swift
llvm::sys::path::remove_filename(runtimeLibPath); // remove /bin
llvm::sys::path::append(runtimeLibPath, "lib", "swift_static");
}
llvm::sys::path::append(runtimeLibPath,
getPlatformNameForTriple(TC.getTriple()));
}

ToolChain::InvocationInfo
toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
const JobContext &context) const {
Expand Down Expand Up @@ -1054,8 +1076,22 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
// relative to the compiler.
SmallString<128> RuntimeLibPath;
getRuntimeLibraryPath(RuntimeLibPath, context.Args, *this);

// Link the standard library.
Arguments.push_back("-L");
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
if (context.Args.hasFlag(options::OPT_static_stdlib,
options::OPT_no_static_stdlib,
false)) {
SmallString<128> StaticRuntimeLibPath;
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
Arguments.push_back("-lc++");
Arguments.push_back("-framework");
Arguments.push_back("Foundation");
Arguments.push_back("-force_load_swift_libs");
} else {
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
}

if (context.Args.hasArg(options::OPT_profile_generate)) {
SmallString<128> LibProfile(RuntimeLibPath);
Expand Down
10 changes: 10 additions & 0 deletions test/Driver/static-stdlib.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Statically link a "hello world" program
// XFAIL: linux
// REQUIRES: static_stdlib
print("hello world!")
// RUN: rm -rf %t && mkdir %t
// RUN: %target-swiftc_driver -static-stdlib -o %t/static-stdlib %s
// RUN: %t/static-stdlib | FileCheck %s
// RUN: otool -L %t/static-stdlib | FileCheck %s --check-prefix=OTOOL
// CHECK: hello world!
// OTOOL-NOT: libswiftCore.dylib