From 234d9515e10faa621a64af091c7728a0018288da Mon Sep 17 00:00:00 2001 From: Shiz Date: Wed, 27 May 2015 00:34:27 +0000 Subject: [PATCH 2/4] Add ToolChain::GetRuntimeLibPath, modify existing driver code to use it. Given a compiler runtime library (from ToolChain::RuntimeLibraryType) and a component (e.g. "builtins" or "sanitize" for compiler-rt and "" or "_s" for libgcc), it will return the full path to it according to the toolchain settings. This moves the previous lib/Driver/Tools.cpp code to where it should belong, in the ToolChain class. --- include/clang/Driver/ToolChain.h | 2 ++ lib/Driver/ToolChain.cpp | 32 +++++++++++++++++++++ lib/Driver/Tools.cpp | 62 +++++++++------------------------------- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/include/clang/Driver/ToolChain.h b/include/clang/Driver/ToolChain.h index 560df19..794a4dd 100644 --- a/include/clang/Driver/ToolChain.h +++ b/include/clang/Driver/ToolChain.h @@ -170,6 +170,8 @@ public: std::string GetFilePath(const char *Name) const; std::string GetProgramPath(const char *Name) const; + std::string GetRuntimeLibPath(enum RuntimeLibType Type, const char *Component, + bool Shared = false) const; /// Returns the linker path, respecting the -fuse-ld= argument to determine /// the linker suffix or name. diff --git a/lib/Driver/ToolChain.cpp b/lib/Driver/ToolChain.cpp index f7b7402..b759336 100644 --- a/lib/Driver/ToolChain.cpp +++ b/lib/Driver/ToolChain.cpp @@ -187,6 +187,38 @@ std::string ToolChain::GetProgramPath(const char *Name) const { return D.GetProgramPath(Name, *this); } +std::string ToolChain::GetRuntimeLibPath(enum RuntimeLibType Type, + const char *Component, bool Shared) const { + bool IsOSWindows = Triple.isOSWindows(); + StringRef Suffix = + Shared ? (IsOSWindows ? ".dll" : ".so") : (IsOSWindows ? ".lib" : ".a"); + llvm::SmallString<128> Path; + + switch (Type) { + case RLT_CompilerRT: { + StringRef Prefix = IsOSWindows ? "" : "lib"; + StringRef Arch = Triple.getArchName(); + // FIXME: Handle 64-bit. + if (IsOSWindows && !Triple.isWindowsItaniumEnvironment()) + Arch = "i386"; + // All ARM runtimes are in one library. + if (Triple.getArch() == llvm::Triple::arm || Triple.getArch() == llvm::Triple::armeb) + Arch = "arm"; + StringRef Env = Triple.getEnvironment() == llvm::Triple::Android + ? "-android" + : ""; + + (Prefix + Twine("clang_rt.") + Component + "-" + Arch + Env + Suffix).toVector(Path); + break; + } + case RLT_Libgcc: { + (Twine("libgcc") + Component + Suffix).toVector(Path); + break; + } + } + return GetFilePath(Path.c_str()); +} + std::string ToolChain::GetLinkerPath() const { if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { StringRef Suffix = A->getValue(); diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 527038f..d0bae4d 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -2208,53 +2208,13 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, } } -// Until ARM libraries are build separately, we have them all in one library -static StringRef getArchNameForCompilerRTLib(const ToolChain &TC) { - // FIXME: handle 64-bit - if (TC.getTriple().isOSWindows() && - !TC.getTriple().isWindowsItaniumEnvironment()) - return "i386"; - if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb) - return "arm"; - return TC.getArchName(); -} - -static SmallString<128> getCompilerRTLibDir(const ToolChain &TC) { - // The runtimes are located in the OS-specific resource directory. - SmallString<128> Res(TC.getDriver().ResourceDir); - const llvm::Triple &Triple = TC.getTriple(); - // TC.getOS() yield "freebsd10.0" whereas "freebsd" is expected. - StringRef OSLibName = - (Triple.getOS() == llvm::Triple::FreeBSD) ? "freebsd" : TC.getOS(); - llvm::sys::path::append(Res, "lib", OSLibName); - return Res; -} - -static SmallString<128> getCompilerRT(const ToolChain &TC, StringRef Component, - bool Shared = false) { - const char *Env = TC.getTriple().getEnvironment() == llvm::Triple::Android - ? "-android" - : ""; - - bool IsOSWindows = TC.getTriple().isOSWindows(); - StringRef Arch = getArchNameForCompilerRTLib(TC); - const char *Prefix = IsOSWindows ? "" : "lib"; - const char *Suffix = - Shared ? (IsOSWindows ? ".dll" : ".so") : (IsOSWindows ? ".lib" : ".a"); - - SmallString<128> Path = getCompilerRTLibDir(TC); - llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" + - Arch + Env + Suffix); - - return Path; -} - // This adds the static libclang_rt.builtins-arch.a directly to the command line // FIXME: Make sure we can also emit shared objects if they're requested // and available, check for possible errors, etc. static void addClangRT(const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) { - CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, "builtins"))); + CmdArgs.push_back(Args.MakeArgString( + TC.GetRuntimeLibPath(ToolChain::RLT_CompilerRT, "builtins"))); if (!TC.getTriple().isOSWindows()) { // FIXME: why do we link against gcc when we are using compiler-rt? @@ -2275,7 +2235,8 @@ static void addProfileRT(const ToolChain &TC, const ArgList &Args, Args.hasArg(options::OPT_coverage))) return; - CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, "profile"))); + CmdArgs.push_back(Args.MakeArgString( + TC.GetRuntimeLibPath(ToolChain::RLT_CompilerRT, "profile"))); } static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args, @@ -2285,7 +2246,8 @@ static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args, // whole-archive. if (!IsShared) CmdArgs.push_back("-whole-archive"); - CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, Sanitizer, IsShared))); + CmdArgs.push_back(Args.MakeArgString(TC.GetRuntimeLibPath( + ToolChain::RLT_CompilerRT, Sanitizer.data(), IsShared))); if (!IsShared) CmdArgs.push_back("-no-whole-archive"); } @@ -2295,7 +2257,8 @@ static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args, static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs, StringRef Sanitizer) { - SmallString<128> SanRT = getCompilerRT(TC, Sanitizer); + std::string SanRT = TC.GetRuntimeLibPath(ToolChain::RLT_CompilerRT, + Sanitizer.data()); if (llvm::sys::fs::exists(SanRT + ".syms")) { CmdArgs.push_back(Args.MakeArgString("--dynamic-list=" + SanRT + ".syms")); return true; @@ -8611,19 +8574,22 @@ void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA, "asan_dynamic_runtime_thunk", }; for (const auto &Component : CompilerRTComponents) - CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, Component))); + CmdArgs.push_back(Args.MakeArgString(TC.GetRuntimeLibPath( + ToolChain::RLT_CompilerRT, Component))); // Make sure the dynamic runtime thunk is not optimized out at link time // to ensure proper SEH handling. CmdArgs.push_back(Args.MakeArgString("-include:___asan_seh_interceptor")); } else if (DLL) { - CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, "asan_dll_thunk"))); + CmdArgs.push_back(Args.MakeArgString(TC.GetRuntimeLibPath( + ToolChain::RLT_CompilerRT, "asan_dll_thunk"))); } else { static const char *CompilerRTComponents[] = { "asan", "asan_cxx", }; for (const auto &Component : CompilerRTComponents) - CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, Component))); + CmdArgs.push_back(Args.MakeArgString(TC.GetRuntimeLibPath( + ToolChain::RLT_CompilerRT, Component))); } } -- 2.3.6