Skip to content

Commit 21f7d87

Browse files
committed
Merge getRuntimeLibraryPath and getRuntimeStaticLibraryPath
These two functions are duplicates except for one string. Merge them and add an argument for whether we want static or shared.
1 parent 2866f0e commit 21f7d87

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

lib/Driver/ToolChains.cpp

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,8 @@ static void addPathEnvironmentVariableIfNeeded(Job::EnvironmentVector &env,
10251025
/// relative to the compiler.
10261026
static void getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
10271027
const llvm::opt::ArgList &args,
1028-
const ToolChain &TC) {
1028+
const ToolChain &TC,
1029+
bool shared) {
10291030
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
10301031
// library link path and the standard library module import path don't
10311032
// need to be the same.
@@ -1037,7 +1038,7 @@ static void getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
10371038
runtimeLibPath.append(programPath.begin(), programPath.end());
10381039
llvm::sys::path::remove_filename(runtimeLibPath); // remove /swift
10391040
llvm::sys::path::remove_filename(runtimeLibPath); // remove /bin
1040-
llvm::sys::path::append(runtimeLibPath, "lib", "swift");
1041+
llvm::sys::path::append(runtimeLibPath, "lib", shared ? "swift" : "swift_static");
10411042
}
10421043
llvm::sys::path::append(runtimeLibPath,
10431044
getPlatformNameForTriple(TC.getTriple()));
@@ -1047,43 +1048,21 @@ static void getClangLibraryPath(const ToolChain &TC, const ArgList &Args,
10471048
SmallString<128> &LibPath) {
10481049
const llvm::Triple &T = TC.getTriple();
10491050

1050-
getRuntimeLibraryPath(LibPath, Args, TC);
1051+
getRuntimeLibraryPath(LibPath, Args, TC, /*Shared=*/ true);
10511052
// Remove platform name.
10521053
llvm::sys::path::remove_filename(LibPath);
10531054
llvm::sys::path::append(LibPath, "clang", "lib",
10541055
T.isOSDarwin() ? "darwin"
10551056
: getPlatformNameForTriple(T));
10561057
}
10571058

1058-
/// Get the runtime library link path for static linking,
1059-
/// which is platform-specific and found relative to the compiler.
1060-
static void getRuntimeStaticLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
1061-
const llvm::opt::ArgList &args,
1062-
const ToolChain &TC) {
1063-
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
1064-
// library link path and the standard library module import path don't
1065-
// need to be the same.
1066-
if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) {
1067-
StringRef value = A->getValue();
1068-
runtimeLibPath.append(value.begin(), value.end());
1069-
} else {
1070-
auto programPath = TC.getDriver().getSwiftProgramPath();
1071-
runtimeLibPath.append(programPath.begin(), programPath.end());
1072-
llvm::sys::path::remove_filename(runtimeLibPath); // remove /swift
1073-
llvm::sys::path::remove_filename(runtimeLibPath); // remove /bin
1074-
llvm::sys::path::append(runtimeLibPath, "lib", "swift_static");
1075-
}
1076-
llvm::sys::path::append(runtimeLibPath,
1077-
getPlatformNameForTriple(TC.getTriple()));
1078-
}
1079-
10801059
ToolChain::InvocationInfo
10811060
toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
10821061
const JobContext &context) const {
10831062
InvocationInfo II = ToolChain::constructInvocation(job, context);
10841063

10851064
SmallString<128> runtimeLibraryPath;
1086-
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, *this);
1065+
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, *this, /*Shared=*/ true);
10871066

10881067
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_LIBRARY_PATH",
10891068
":", options::OPT_L, context.Args,
@@ -1188,7 +1167,7 @@ addLinkRuntimeLibForLinux(const ArgList &Args, ArgStringList &Arguments,
11881167
StringRef LinuxLibName,
11891168
const ToolChain &TC) {
11901169
SmallString<128> Dir;
1191-
getRuntimeLibraryPath(Dir, Args, TC);
1170+
getRuntimeLibraryPath(Dir, Args, TC, /*Shared=*/ true);
11921171
// Remove platform name.
11931172
llvm::sys::path::remove_filename(Dir);
11941173
llvm::sys::path::append(Dir, "clang", "lib", "linux");
@@ -1397,15 +1376,15 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
13971376
// Add the runtime library link path, which is platform-specific and found
13981377
// relative to the compiler.
13991378
SmallString<128> RuntimeLibPath;
1400-
getRuntimeLibraryPath(RuntimeLibPath, context.Args, *this);
1379+
getRuntimeLibraryPath(RuntimeLibPath, context.Args, *this, /*Shared=*/ true);
14011380

14021381
// Link the standard library.
14031382
Arguments.push_back("-L");
14041383
if (context.Args.hasFlag(options::OPT_static_stdlib,
14051384
options::OPT_no_static_stdlib,
14061385
false)) {
14071386
SmallString<128> StaticRuntimeLibPath;
1408-
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
1387+
getRuntimeLibraryPath(StaticRuntimeLibPath, context.Args, *this, /*Shared=*/ false);
14091388
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
14101389
Arguments.push_back("-lc++");
14111390
Arguments.push_back("-framework");
@@ -1504,7 +1483,7 @@ toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job,
15041483
InvocationInfo II = ToolChain::constructInvocation(job, context);
15051484

15061485
SmallString<128> runtimeLibraryPath;
1507-
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, *this);
1486+
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, *this, /*Shared=*/ true);
15081487

15091488
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "LD_LIBRARY_PATH",
15101489
":", options::OPT_L, context.Args,
@@ -1638,10 +1617,10 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
16381617
}
16391618

16401619
SmallString<128> SharedRuntimeLibPath;
1641-
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args, *this);
1620+
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args, *this, /*Shared=*/ true);
16421621

16431622
SmallString<128> StaticRuntimeLibPath;
1644-
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
1623+
getRuntimeLibraryPath(StaticRuntimeLibPath, context.Args, *this, /*Shared=*/ false);
16451624

16461625
// Add the runtime library link path, which is platform-specific and found
16471626
// relative to the compiler.

0 commit comments

Comments
 (0)