diff options
author | Ehsan Akhgari <ehsan.akhgari@gmail.com> | 2014-06-30 19:54:20 +0000 |
---|---|---|
committer | Ehsan Akhgari <ehsan.akhgari@gmail.com> | 2014-06-30 19:54:20 +0000 |
commit | 33d1ae53f7902c8a6ffc100f2b109ec2c5ebf69d (patch) | |
tree | 9c2b1ea78481a7c619232b332dbce604ae46c1f0 | |
parent | cf21875d41772a865c6a71ebeccca3d1a7afcfc2 (diff) | |
download | bcm5719-llvm-33d1ae53f7902c8a6ffc100f2b109ec2c5ebf69d.tar.gz bcm5719-llvm-33d1ae53f7902c8a6ffc100f2b109ec2c5ebf69d.zip |
Refactor the code in clang to find a file in a PATH like environment variable into a helper function
llvm-svn: 212057
-rw-r--r-- | llvm/include/llvm/Support/Process.h | 7 | ||||
-rw-r--r-- | llvm/lib/Support/Process.cpp | 30 |
2 files changed, 37 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/Process.h b/llvm/include/llvm/Support/Process.h index 4f98e4de812..30973de3aac 100644 --- a/llvm/include/llvm/Support/Process.h +++ b/llvm/include/llvm/Support/Process.h @@ -171,6 +171,13 @@ public: // string. \arg Name is assumed to be in UTF-8 encoding too. static Optional<std::string> GetEnv(StringRef name); + /// This function searches for an existing file in the list of directories + /// in a PATH like environment variable, and returns the first file found, + /// according to the order of the entries in the PATH like environment + /// variable. + static Optional<std::string> FindInEnvPath(const std::string& EnvName, + const std::string& FileName); + /// This function returns a SmallVector containing the arguments passed from /// the operating system to the program. This function expects to be handed /// the vector passed in from main. diff --git a/llvm/lib/Support/Process.cpp b/llvm/lib/Support/Process.cpp index 087c459e187..0d42e0e35b9 100644 --- a/llvm/lib/Support/Process.cpp +++ b/llvm/lib/Support/Process.cpp @@ -11,9 +11,12 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/StringExtras.h" #include "llvm/Config/config.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/Process.h" +#include "llvm/Support/Program.h" using namespace llvm; using namespace sys; @@ -66,6 +69,33 @@ TimeValue self_process::get_wall_time() const { return getElapsedWallTime(); } +Optional<std::string> Process::FindInEnvPath(const std::string& EnvName, + const std::string& FileName) +{ + Optional<std::string> FoundPath; + Optional<std::string> OptPath = Process::GetEnv(EnvName); + if (!OptPath.hasValue()) + return FoundPath; + + const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'}; + SmallVector<StringRef, 8> Dirs; + SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr); + + for (const auto &Dir : Dirs) { + if (Dir.empty()) + continue; + + SmallString<128> FilePath(Dir); + path::append(FilePath, FileName); + if (fs::exists(Twine(FilePath))) { + FoundPath = FilePath.str(); + break; + } + } + + return FoundPath; +} + #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" |