Skip to content

Commit 2c7b518

Browse files
committed
Consolidate code that computes resource dir relative to swift executable.
1 parent 3c45041 commit 2c7b518

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ class CompilerInvocation {
202202

203203
void setRuntimeResourcePath(StringRef Path);
204204

205+
/// Computes the runtime resource path relative to the given Swift
206+
/// executable.
207+
static void computeRuntimeResourcePathFromExecutablePath(
208+
StringRef mainExecutablePath,
209+
llvm::SmallString<128> &runtimeResourcePath);
210+
205211
void setSDKPath(const std::string &Path);
206212

207213
StringRef getSDKPath() const {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ swift::CompilerInvocation::CompilerInvocation() {
3838
setTargetTriple(llvm::sys::getDefaultTargetTriple());
3939
}
4040

41+
void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
42+
StringRef mainExecutablePath, llvm::SmallString<128> &runtimeResourcePath) {
43+
runtimeResourcePath.assign(mainExecutablePath);
44+
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /swift
45+
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /bin
46+
llvm::sys::path::append(runtimeResourcePath, "lib", "swift");
47+
}
48+
4149
void CompilerInvocation::setMainExecutablePath(StringRef Path) {
42-
llvm::SmallString<128> LibPath(Path);
43-
llvm::sys::path::remove_filename(LibPath); // Remove /swift
44-
llvm::sys::path::remove_filename(LibPath); // Remove /bin
45-
llvm::sys::path::append(LibPath, "lib", "swift");
50+
llvm::SmallString<128> LibPath;
51+
computeRuntimeResourcePathFromExecutablePath(Path, LibPath);
4652
setRuntimeResourcePath(LibPath.str());
4753

4854
llvm::SmallString<128> DiagnosticDocsPath(Path);

tools/driver/modulewrap_main.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,9 @@ int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
156156
// Wrap the bitstream in a module object file. To use the ClangImporter to
157157
// create the module loader, we need to properly set the runtime library path.
158158
SearchPathOptions SearchPathOpts;
159-
// FIXME: This logic has been duplicated from
160-
// CompilerInvocation::setMainExecutablePath. ModuleWrapInvocation
161-
// should share its implementation.
162-
SmallString<128> RuntimeResourcePath(MainExecutablePath);
163-
llvm::sys::path::remove_filename(RuntimeResourcePath); // Remove /swift
164-
llvm::sys::path::remove_filename(RuntimeResourcePath); // Remove /bin
165-
llvm::sys::path::append(RuntimeResourcePath, "lib", "swift");
159+
SmallString<128> RuntimeResourcePath;
160+
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
161+
MainExecutablePath, RuntimeResourcePath);
166162
SearchPathOpts.RuntimeResourcePath = RuntimeResourcePath.str();
167163

168164
SourceManager SrcMgr;

0 commit comments

Comments
 (0)