Skip to content

Commit fdd41ae

Browse files
authored
Revert "Look up runtime libraries in SDK (swiftlang#25740)"
This reverts commit b818b44.
1 parent b818b44 commit fdd41ae

File tree

7 files changed

+94
-123
lines changed

7 files changed

+94
-123
lines changed

include/swift/Driver/ToolChain.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,17 @@ class ToolChain {
194194
file_types::ID InputType,
195195
const char *PrefixArgument = nullptr) const;
196196

197-
/// Get the resource dir link path, which is platform-specific and found
197+
/// Get the runtime library link path, which is platform-specific and found
198198
/// relative to the compiler.
199-
void getResourceDirPath(SmallVectorImpl<char> &runtimeLibPath,
200-
const llvm::opt::ArgList &args, bool shared) const;
201-
202-
/// Get the runtime library link paths, which typically include the resource
203-
/// dir path and the SDK.
204-
void getRuntimeLibraryPaths(SmallVectorImpl<std::string> &runtimeLibPaths,
205-
const llvm::opt::ArgList &args,
206-
StringRef SDKPath, bool shared) const;
199+
void getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
200+
const llvm::opt::ArgList &args, bool shared) const;
207201

208202
void addPathEnvironmentVariableIfNeeded(Job::EnvironmentVector &env,
209203
const char *name,
210204
const char *separator,
211205
options::ID optionID,
212206
const llvm::opt::ArgList &args,
213-
ArrayRef<std::string> extraEntries = {}) const;
207+
StringRef extraEntry = "") const;
214208

215209
/// Specific toolchains should override this to provide additional conditions
216210
/// under which the compiler invocation should be written into debug info. For

lib/Driver/DarwinToolChains.cpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
7272
const JobContext &context) const {
7373
InvocationInfo II = ToolChain::constructInvocation(job, context);
7474

75-
SmallVector<std::string, 4> runtimeLibraryPaths;
76-
getRuntimeLibraryPaths(runtimeLibraryPaths, context.Args, context.OI.SDKPath,
77-
/*Shared=*/true);
75+
SmallString<128> runtimeLibraryPath;
76+
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, /*Shared=*/true);
7877

7978
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_LIBRARY_PATH",
8079
":", options::OPT_L, context.Args,
81-
runtimeLibraryPaths);
80+
runtimeLibraryPath);
8281
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_FRAMEWORK_PATH",
8382
":", options::OPT_F, context.Args);
8483
// FIXME: Add options::OPT_Fsystem paths to DYLD_FRAMEWORK_PATH as well.
@@ -391,10 +390,13 @@ toolchains::Darwin::constructInvocation(const DynamicLinkJobAction &job,
391390
Arguments.push_back("-arch");
392391
Arguments.push_back(context.Args.MakeArgString(getTriple().getArchName()));
393392

393+
// Add the runtime library link path, which is platform-specific and found
394+
// relative to the compiler.
395+
SmallString<128> RuntimeLibPath;
396+
getRuntimeLibraryPath(RuntimeLibPath, context.Args, /*Shared=*/true);
397+
394398
// Link compatibility libraries, if we're deploying back to OSes that
395399
// have an older Swift runtime.
396-
SmallString<128> SharedResourceDirPath;
397-
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
398400
Optional<llvm::VersionTuple> runtimeCompatibilityVersion;
399401

400402
if (context.Args.hasArg(options::OPT_runtime_compatibility_version)) {
@@ -416,7 +418,7 @@ toolchains::Darwin::constructInvocation(const DynamicLinkJobAction &job,
416418
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 0)) {
417419
// Swift 5.0 compatibility library
418420
SmallString<128> BackDeployLib;
419-
BackDeployLib.append(SharedResourceDirPath);
421+
BackDeployLib.append(RuntimeLibPath);
420422
llvm::sys::path::append(BackDeployLib, "libswiftCompatibility50.a");
421423

422424
if (llvm::sys::fs::exists(BackDeployLib)) {
@@ -431,7 +433,7 @@ toolchains::Darwin::constructInvocation(const DynamicLinkJobAction &job,
431433
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 0)) {
432434
// Swift 5.0 dynamic replacement compatibility library.
433435
SmallString<128> BackDeployLib;
434-
BackDeployLib.append(SharedResourceDirPath);
436+
BackDeployLib.append(RuntimeLibPath);
435437
llvm::sys::path::append(BackDeployLib,
436438
"libswiftCompatibilityDynamicReplacements.a");
437439

@@ -442,34 +444,23 @@ toolchains::Darwin::constructInvocation(const DynamicLinkJobAction &job,
442444
}
443445
}
444446

445-
bool wantsStaticStdlib =
446-
context.Args.hasFlag(options::OPT_static_stdlib,
447-
options::OPT_no_static_stdlib, false);
448-
449-
SmallVector<std::string, 4> RuntimeLibPaths;
450-
getRuntimeLibraryPaths(RuntimeLibPaths, context.Args,
451-
context.OI.SDKPath, /*Shared=*/!wantsStaticStdlib);
452-
453-
// Add the runtime library link path, which is platform-specific and found
454-
// relative to the compiler.
455-
for (auto path : RuntimeLibPaths) {
456-
Arguments.push_back("-L");
457-
Arguments.push_back(context.Args.MakeArgString(path));
458-
}
459-
460447
// Link the standard library.
461-
if (wantsStaticStdlib) {
448+
Arguments.push_back("-L");
449+
if (context.Args.hasFlag(options::OPT_static_stdlib,
450+
options::OPT_no_static_stdlib, false)) {
451+
SmallString<128> StaticRuntimeLibPath;
452+
getRuntimeLibraryPath(StaticRuntimeLibPath, context.Args, /*Shared=*/false);
453+
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
462454
Arguments.push_back("-lc++");
463455
Arguments.push_back("-framework");
464456
Arguments.push_back("Foundation");
465457
Arguments.push_back("-force_load_swift_libs");
466458
} else {
459+
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
467460
// FIXME: We probably shouldn't be adding an rpath here unless we know ahead
468461
// of time the standard library won't be copied. SR-1967
469-
for (auto path : RuntimeLibPaths) {
470-
Arguments.push_back("-rpath");
471-
Arguments.push_back(context.Args.MakeArgString(path));
472-
}
462+
Arguments.push_back("-rpath");
463+
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
473464
}
474465

475466
if (context.Args.hasArg(options::OPT_profile_generate)) {

lib/Driver/ToolChains.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,17 +1110,16 @@ ToolChain::constructInvocation(const StaticLinkJobAction &job,
11101110

11111111
void ToolChain::addPathEnvironmentVariableIfNeeded(
11121112
Job::EnvironmentVector &env, const char *name, const char *separator,
1113-
options::ID optionID, const ArgList &args,
1114-
ArrayRef<std::string> extraEntries) const {
1113+
options::ID optionID, const ArgList &args, StringRef extraEntry) const {
11151114
auto linkPathOptions = args.filtered(optionID);
1116-
if (linkPathOptions.begin() == linkPathOptions.end() && extraEntries.empty())
1115+
if (linkPathOptions.begin() == linkPathOptions.end() && extraEntry.empty())
11171116
return;
11181117

11191118
std::string newPaths;
11201119
interleave(linkPathOptions,
11211120
[&](const Arg *arg) { newPaths.append(arg->getValue()); },
11221121
[&] { newPaths.append(separator); });
1123-
for (auto extraEntry : extraEntries) {
1122+
if (!extraEntry.empty()) {
11241123
if (!newPaths.empty())
11251124
newPaths.append(separator);
11261125
newPaths.append(extraEntry.data(), extraEntry.size());
@@ -1144,7 +1143,7 @@ void ToolChain::getClangLibraryPath(const ArgList &Args,
11441143
SmallString<128> &LibPath) const {
11451144
const llvm::Triple &T = getTriple();
11461145

1147-
getResourceDirPath(LibPath, Args, /*Shared=*/true);
1146+
getRuntimeLibraryPath(LibPath, Args, /*Shared=*/true);
11481147
// Remove platform name.
11491148
llvm::sys::path::remove_filename(LibPath);
11501149
llvm::sys::path::append(LibPath, "clang", "lib",
@@ -1154,41 +1153,27 @@ void ToolChain::getClangLibraryPath(const ArgList &Args,
11541153

11551154
/// Get the runtime library link path, which is platform-specific and found
11561155
/// relative to the compiler.
1157-
void ToolChain::getResourceDirPath(SmallVectorImpl<char> &resourceDirPath,
1158-
const llvm::opt::ArgList &args,
1159-
bool shared) const {
1156+
void ToolChain::getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
1157+
const llvm::opt::ArgList &args,
1158+
bool shared) const {
11601159
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
11611160
// library link path and the standard library module import path don't
11621161
// need to be the same.
11631162
if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) {
11641163
StringRef value = A->getValue();
1165-
resourceDirPath.append(value.begin(), value.end());
1164+
runtimeLibPath.append(value.begin(), value.end());
11661165
} else {
11671166
auto programPath = getDriver().getSwiftProgramPath();
1168-
resourceDirPath.append(programPath.begin(), programPath.end());
1169-
llvm::sys::path::remove_filename(resourceDirPath); // remove /swift
1170-
llvm::sys::path::remove_filename(resourceDirPath); // remove /bin
1171-
llvm::sys::path::append(resourceDirPath, "lib",
1167+
runtimeLibPath.append(programPath.begin(), programPath.end());
1168+
llvm::sys::path::remove_filename(runtimeLibPath); // remove /swift
1169+
llvm::sys::path::remove_filename(runtimeLibPath); // remove /bin
1170+
llvm::sys::path::append(runtimeLibPath, "lib",
11721171
shared ? "swift" : "swift_static");
11731172
}
1174-
llvm::sys::path::append(resourceDirPath,
1173+
llvm::sys::path::append(runtimeLibPath,
11751174
getPlatformNameForTriple(getTriple()));
11761175
}
11771176

1178-
void ToolChain::getRuntimeLibraryPaths(SmallVectorImpl<std::string> &runtimeLibPaths,
1179-
const llvm::opt::ArgList &args,
1180-
StringRef SDKPath, bool shared) const {
1181-
SmallString<128> scratchPath;
1182-
getResourceDirPath(scratchPath, args, shared);
1183-
runtimeLibPaths.push_back(scratchPath.str());
1184-
1185-
if (!SDKPath.empty()) {
1186-
scratchPath = SDKPath;
1187-
llvm::sys::path::append(scratchPath, "usr", "lib", "swift");
1188-
runtimeLibPaths.push_back(scratchPath.str());
1189-
}
1190-
}
1191-
11921177
bool ToolChain::sanitizerRuntimeLibExists(const ArgList &args,
11931178
StringRef sanitizerName,
11941179
bool shared) const {

lib/Driver/UnixToolChains.cpp

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job,
5151
const JobContext &context) const {
5252
InvocationInfo II = ToolChain::constructInvocation(job, context);
5353

54-
SmallVector<std::string, 4> runtimeLibraryPaths;
55-
getRuntimeLibraryPaths(runtimeLibraryPaths, context.Args, context.OI.SDKPath,
56-
/*Shared=*/true);
54+
SmallString<128> runtimeLibraryPath;
55+
getRuntimeLibraryPath(runtimeLibraryPath, context.Args,
56+
/*Shared=*/true);
5757

5858
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "LD_LIBRARY_PATH",
5959
":", options::OPT_L, context.Args,
60-
runtimeLibraryPaths);
60+
runtimeLibraryPath);
6161
return II;
6262
}
6363

@@ -190,25 +190,24 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
190190
staticStdlib = true;
191191
}
192192

193-
SmallVector<std::string, 4> RuntimeLibPaths;
194-
getRuntimeLibraryPaths(RuntimeLibPaths, context.Args, context.OI.SDKPath,
195-
/*Shared=*/!(staticExecutable || staticStdlib));
193+
SmallString<128> SharedRuntimeLibPath;
194+
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args, /*Shared=*/true);
196195

196+
SmallString<128> StaticRuntimeLibPath;
197+
getRuntimeLibraryPath(StaticRuntimeLibPath, context.Args, /*Shared=*/false);
198+
199+
// Add the runtime library link path, which is platform-specific and found
200+
// relative to the compiler.
197201
if (!(staticExecutable || staticStdlib) && shouldProvideRPathToLinker()) {
198202
// FIXME: We probably shouldn't be adding an rpath here unless we know
199203
// ahead of time the standard library won't be copied.
200-
for (auto path : RuntimeLibPaths) {
201-
Arguments.push_back("-Xlinker");
202-
Arguments.push_back("-rpath");
203-
Arguments.push_back("-Xlinker");
204-
Arguments.push_back(context.Args.MakeArgString(path));
205-
}
204+
Arguments.push_back("-Xlinker");
205+
Arguments.push_back("-rpath");
206+
Arguments.push_back("-Xlinker");
207+
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
206208
}
207209

208-
SmallString<128> SharedResourceDirPath;
209-
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
210-
211-
SmallString<128> swiftrtPath = SharedResourceDirPath;
210+
SmallString<128> swiftrtPath = SharedRuntimeLibPath;
212211
llvm::sys::path::append(swiftrtPath,
213212
swift::getMajorArchitectureName(getTriple()));
214213
llvm::sys::path::append(swiftrtPath, "swiftrt.o");
@@ -240,34 +239,36 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
240239
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
241240
}
242241

243-
// Add the runtime library link paths.
244-
for (auto path : RuntimeLibPaths) {
245-
Arguments.push_back("-L");
246-
Arguments.push_back(context.Args.MakeArgString(path));
247-
}
248-
249-
// Link the standard library. In two paths, we do this using a .lnk file;
250-
// if we're going that route, we'll set `linkFilePath` to the path to that
251-
// file.
252-
SmallString<128> linkFilePath;
253-
getResourceDirPath(linkFilePath, context.Args, /*Shared=*/false);
242+
// Link the standard library.
243+
Arguments.push_back("-L");
254244

255245
if (staticExecutable) {
246+
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
247+
248+
SmallString<128> linkFilePath = StaticRuntimeLibPath;
256249
llvm::sys::path::append(linkFilePath, "static-executable-args.lnk");
250+
auto linkFile = linkFilePath.str();
251+
252+
if (llvm::sys::fs::is_regular_file(linkFile)) {
253+
Arguments.push_back(context.Args.MakeArgString(Twine("@") + linkFile));
254+
} else {
255+
llvm::report_fatal_error(
256+
"-static-executable not supported on this platform");
257+
}
257258
} else if (staticStdlib) {
258-
llvm::sys::path::append(linkFilePath, "static-stdlib-args.lnk");
259-
} else {
260-
linkFilePath.clear();
261-
Arguments.push_back("-lswiftCore");
262-
}
259+
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
263260

264-
if (!linkFilePath.empty()) {
261+
SmallString<128> linkFilePath = StaticRuntimeLibPath;
262+
llvm::sys::path::append(linkFilePath, "static-stdlib-args.lnk");
265263
auto linkFile = linkFilePath.str();
266264
if (llvm::sys::fs::is_regular_file(linkFile)) {
267265
Arguments.push_back(context.Args.MakeArgString(Twine("@") + linkFile));
268266
} else {
269267
llvm::report_fatal_error(linkFile + " not found");
270268
}
269+
} else {
270+
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
271+
Arguments.push_back("-lswiftCore");
271272
}
272273

273274
// Explicitly pass the target to the linker
@@ -288,7 +289,7 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
288289
}
289290

290291
if (context.Args.hasArg(options::OPT_profile_generate)) {
291-
SmallString<128> LibProfile(SharedResourceDirPath);
292+
SmallString<128> LibProfile(SharedRuntimeLibPath);
292293
llvm::sys::path::remove_filename(LibProfile); // remove platform name
293294
llvm::sys::path::append(LibProfile, "clang", "lib");
294295

lib/Driver/WindowsToolChains.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,28 @@ toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
108108
// driver rather than the `clang-cl` driver.
109109
Arguments.push_back("-nostartfiles");
110110

111-
bool wantsStaticStdlib =
112-
context.Args.hasFlag(options::OPT_static_stdlib,
113-
options::OPT_no_static_stdlib, false);
111+
SmallString<128> SharedRuntimeLibPath;
112+
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args,
113+
/*Shared=*/true);
114+
115+
// Link the standard library.
116+
Arguments.push_back("-L");
117+
if (context.Args.hasFlag(options::OPT_static_stdlib,
118+
options::OPT_no_static_stdlib, false)) {
119+
SmallString<128> StaticRuntimeLibPath;
120+
getRuntimeLibraryPath(StaticRuntimeLibPath, context.Args,
121+
/*Shared=*/false);
114122

115-
SmallVector<std::string, 4> RuntimeLibPaths;
116-
getRuntimeLibraryPaths(RuntimeLibPaths, context.Args, context.OI.SDKPath,
117-
/*Shared=*/!wantsStaticStdlib);
118-
119-
for (auto path : RuntimeLibPaths) {
120-
Arguments.push_back("-L");
121123
// Since Windows has separate libraries per architecture, link against the
122124
// architecture specific version of the static library.
123-
Arguments.push_back(context.Args.MakeArgString(path + "/" +
125+
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath + "/" +
126+
getTriple().getArchName()));
127+
} else {
128+
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath + "/" +
124129
getTriple().getArchName()));
125130
}
126131

127-
SmallString<128> SharedResourceDirPath;
128-
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
129-
130-
SmallString<128> swiftrtPath = SharedResourceDirPath;
132+
SmallString<128> swiftrtPath = SharedRuntimeLibPath;
131133
llvm::sys::path::append(swiftrtPath,
132134
swift::getMajorArchitectureName(getTriple()));
133135
llvm::sys::path::append(swiftrtPath, "swiftrt.obj");
@@ -162,7 +164,7 @@ toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
162164
}
163165

164166
if (context.Args.hasArg(options::OPT_profile_generate)) {
165-
SmallString<128> LibProfile(SharedResourceDirPath);
167+
SmallString<128> LibProfile(SharedRuntimeLibPath);
166168
llvm::sys::path::remove_filename(LibProfile); // remove platform name
167169
llvm::sys::path::append(LibProfile, "clang", "lib");
168170

0 commit comments

Comments
 (0)