diff --git a/include/swift/Driver/Options.td b/include/swift/Driver/Options.td index 1f5a7c781b4..b0128ecede1 100644 --- a/include/swift/Driver/Options.td +++ b/include/swift/Driver/Options.td @@ -247,6 +247,11 @@ def Xllvm : Separate<["-"], "Xllvm">, Flags<[DriverOption, FrontendOption]>, MetaVarName<"">, HelpText<"Pass to llvm.">; +def resource_dir : Separate<["-"], "resource-dir">, + Flags<[DriverOption, FrontendOption, HelpHidden]>, + MetaVarName<"">, + HelpText<"The directory that holds the compiler resource files">; + def target : Joined<["--"], "target=">, Flags<[DriverOption, FrontendOption]>, HelpText<"Generate code for the given target">; def target_legacy_spelling : Separate<["-"], "target">, diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 0389b848f51..f3cc81e0595 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -110,6 +110,7 @@ static void addCommonFrontendArgs(const ToolChain &TC, inputArgs.AddAllArgs(arguments, options::OPT_I); inputArgs.AddLastArg(arguments, options::OPT_g); + inputArgs.AddLastArg(arguments, options::OPT_resource_dir); // Pass through any -Xllvm flags. inputArgs.AddAllArgs(arguments, options::OPT_Xllvm); @@ -431,10 +432,16 @@ Job *darwin::Linker::constructJob(const JobAction &JA, // 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. - llvm::SmallString<128> RuntimeLibPath(D.getSwiftProgramPath()); - llvm::sys::path::remove_filename(RuntimeLibPath); // remove /swift - llvm::sys::path::remove_filename(RuntimeLibPath); // remove /bin - llvm::sys::path::append(RuntimeLibPath, "lib", "swift"); + llvm::SmallString<128> RuntimeLibPath; + + if (const Arg *A = Args.getLastArg(options::OPT_resource_dir)) { + RuntimeLibPath = A->getValue(); + } else { + RuntimeLibPath = D.getSwiftProgramPath(); + llvm::sys::path::remove_filename(RuntimeLibPath); // remove /swift + llvm::sys::path::remove_filename(RuntimeLibPath); // remove /bin + llvm::sys::path::append(RuntimeLibPath, "lib", "swift"); + } llvm::sys::path::append(RuntimeLibPath, getPlatformNameForTriple(TC.getTriple())); Arguments.push_back("-L"); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index b16591bcb32..b4d3874ea46 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -550,6 +550,10 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args, Opts.SDKPath = A->getValue(); } + if (const Arg *A = Args.getLastArg(OPT_resource_dir)) { + Opts.RuntimeResourcePath = A->getValue(); + } + // Opts.RuntimeIncludePath is set by calls to // setRuntimeIncludePath() or setMainExecutablePath(). // Opts.RuntimeImportPath is set by calls to diff --git a/lib/Immediate/Immediate.cpp b/lib/Immediate/Immediate.cpp index 1a86cf31066..f746ac57ef6 100644 --- a/lib/Immediate/Immediate.cpp +++ b/lib/Immediate/Immediate.cpp @@ -71,10 +71,6 @@ #include #include -#ifndef SWIFT_TOOLCHAIN_SUBDIR -#define SWIFT_TOOLCHAIN_SUBDIR "swift" -#endif - using namespace swift; namespace { @@ -169,24 +165,18 @@ static void convertToUTF8(llvm::ArrayRef wide, } // end anonymous namespace -static bool loadRuntimeLib(StringRef sharedLibName, - const ProcessCmdLine &CmdLine) { +static bool loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) { // FIXME: Need error-checking. - llvm::SmallString<128> Path( - llvm::sys::fs::getMainExecutable(CmdLine[0].data(), - (void*)&swift::RunImmediately)); - llvm::sys::path::remove_filename(Path); // Remove /executable - llvm::sys::path::remove_filename(Path); // Remove /bin - llvm::sys::path::append(Path, "lib", SWIFT_TOOLCHAIN_SUBDIR, sharedLibName); + llvm::SmallString<128> Path = runtimeLibPath; + llvm::sys::path::append(Path, sharedLibName); return dlopen(Path.c_str(), 0); } -static bool loadSwiftRuntime(const ProcessCmdLine &CmdLine) { - // We rely on @rpath to find the core Swift stdlib. - return dlopen("libswift_stdlib_core.dylib", 0); +static bool loadSwiftRuntime(StringRef runtimeLibPath) { + return loadRuntimeLib("libswift_stdlib_core.dylib", runtimeLibPath); } -static bool tryLoadLibrary(LinkLibrary linkLib, const ProcessCmdLine &CmdLine, +static bool tryLoadLibrary(LinkLibrary linkLib, StringRef runtimeLibPath, DiagnosticEngine &diags) { // If we have an absolute path, just try to load it now. llvm::SmallString<128> path = linkLib.getName(); @@ -216,7 +206,7 @@ static bool tryLoadLibrary(LinkLibrary linkLib, const ProcessCmdLine &CmdLine, success = dlopen(path.c_str(), 0); if (!success && linkLib.getKind() == LibraryKind::Library) { // Try our runtime library path. - success = loadRuntimeLib(path, CmdLine); + success = loadRuntimeLib(path, runtimeLibPath); } } @@ -230,7 +220,6 @@ static bool tryLoadLibrary(LinkLibrary linkLib, const ProcessCmdLine &CmdLine, static bool IRGenImportedModules(CompilerInstance &CI, llvm::Module &Module, - const ProcessCmdLine &CmdLine, llvm::SmallPtrSet &ImportedModules, SmallVectorImpl &InitFns, @@ -242,7 +231,9 @@ static bool IRGenImportedModules(CompilerInstance &CI, // Perform autolinking. auto addLinkLibrary = [&](LinkLibrary linkLib) { - if (!tryLoadLibrary(linkLib, CmdLine, CI.getDiags())) + if (!tryLoadLibrary(linkLib, + CI.getASTContext().SearchPathOpts.RuntimeLibraryPath, + CI.getDiags())) hadError = true; }; std::for_each(IRGenOpts.LinkLibraries.begin(), IRGenOpts.LinkLibraries.end(), @@ -328,8 +319,8 @@ void swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine, SmallVector InitFns; llvm::SmallPtrSet ImportedModules; - if (IRGenImportedModules(CI, *Module, CmdLine, ImportedModules, - InitFns, IRGenOpts, SILOpts, /*IsREPL*/false)) + if (IRGenImportedModules(CI, *Module, ImportedModules, InitFns, + IRGenOpts, SILOpts, /*IsREPL*/false)) return; llvm::PassManagerBuilder PMBuilder; @@ -340,7 +331,7 @@ void swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine, PMBuilder.populateModulePassManager(ModulePasses); ModulePasses.run(*Module); - if (!loadSwiftRuntime(CmdLine)) { + if (!loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPath)) { CI.getDiags().diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib); return; @@ -1012,7 +1003,7 @@ private: llvm::Function *DumpModuleMain = DumpModule.getFunction("main"); DumpModuleMain->setName("repl.line"); - if (IRGenImportedModules(CI, Module, CmdLine, ImportedModules, InitFns, + if (IRGenImportedModules(CI, Module, ImportedModules, InitFns, IRGenOpts, SILOpts, sil.get())) return false; @@ -1056,14 +1047,16 @@ public: /*RanREPLApplicationMain*/ false } { - if (!loadSwiftRuntime(CmdLine)) { + ASTContext &Ctx = CI.getASTContext(); + if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath)) { CI.getDiags().diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib); return; } std::for_each(CI.getLinkLibraries().begin(), CI.getLinkLibraries().end(), [&](LinkLibrary linkLib) { - tryLoadLibrary(linkLib, CmdLine, CI.getDiags()); + tryLoadLibrary(linkLib, Ctx.SearchPathOpts.RuntimeLibraryPath, + CI.getDiags()); }); llvm::EngineBuilder builder(&Module); @@ -1092,7 +1085,7 @@ public: REPLInputFile, PersistentState, RC, llvm::MemoryBuffer::getMemBufferCopy(WarmUpStmt, "")); - if (CI.getASTContext().hadError()) + if (Ctx.hadError()) return; RC.CurElem = RC.CurIRGenElem = REPLInputFile.Decls.size(); diff --git a/tools/swift/CMakeLists.txt b/tools/swift/CMakeLists.txt index d47df77f7a8..cb59da36a6d 100644 --- a/tools/swift/CMakeLists.txt +++ b/tools/swift/CMakeLists.txt @@ -11,8 +11,7 @@ target_link_libraries(swift_old edit ${CORE_FOUNDATION}) if(MODULES_SDK) add_definitions( -DSWIFT_MODULES_SDK="${MODULES_SDK}" - -DSWIFT_MODULE_CACHE_PATH="${SWIFT_MODULE_CACHE_PATH}" - -DSWIFT_TOOLCHAIN_SUBDIR="${SWIFTLIB_SUBDIR}" ) + -DSWIFT_MODULE_CACHE_PATH="${SWIFT_MODULE_CACHE_PATH}" ) endif() if (SWIFT_SUBMIT_VERSION)