From: Bill Wendling Date: Mon, 12 Mar 2012 21:22:35 +0000 (+0000) Subject: Have clang pay attention to the LIBRARY_PATH environment variable. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bdb8f3ce95e3e5c43497292fd05764faa6ad46b1;p=clang Have clang pay attention to the LIBRARY_PATH environment variable. The LIBRARY_PATH environment variable should be honored by clang. Have the driver pass the directories to the linker. and PR10296. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152578 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 7d2002bf9c..02b69b5057 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -88,6 +88,38 @@ static void QuoteTarget(StringRef Target, } } +static void AddDirectoryList(const ArgList &Args, + ArgStringList &CmdArgs, + const char *ArgName, + const char *DirList) { + if (!DirList) + return; // Nothing to do. + + StringRef Dirs(DirList); + if (Dirs.empty()) // Empty string should not add '.'. + return; + + StringRef::size_type Delim; + while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) { + if (Delim == 0) { // Leading colon. + CmdArgs.push_back(ArgName); + CmdArgs.push_back("."); + } else { + CmdArgs.push_back(ArgName); + CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim))); + } + Dirs = Dirs.substr(Delim + 1); + } + + if (Dirs.empty()) { // Trailing colon. + CmdArgs.push_back(ArgName); + CmdArgs.push_back("."); + } else { // Add the last path. + CmdArgs.push_back(ArgName); + CmdArgs.push_back(Args.MakeArgString(Dirs)); + } +} + static void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const ArgList &Args, ArgStringList &CmdArgs) { @@ -128,6 +160,9 @@ static void AddLinkerInputs(const ToolChain &TC, } else A.renderAsInput(Args, CmdArgs); } + + // LIBRARY_PATH - included following the user specified library paths. + AddDirectoryList(Args, CmdArgs, "-L", ::getenv("LIBRARY_PATH")); } /// \brief Determine whether Objective-C automated reference counting is @@ -162,38 +197,6 @@ static void addProfileRT(const ToolChain &TC, const ArgList &Args, CmdArgs.push_back(Args.MakeArgString(ProfileRT)); } -static void AddIncludeDirectoryList(const ArgList &Args, - ArgStringList &CmdArgs, - const char *ArgName, - const char *DirList) { - if (!DirList) - return; // Nothing to do. - - StringRef Dirs(DirList); - if (Dirs.empty()) // Empty string should not add '.'. - return; - - StringRef::size_type Delim; - while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) { - if (Delim == 0) { // Leading colon. - CmdArgs.push_back(ArgName); - CmdArgs.push_back("."); - } else { - CmdArgs.push_back(ArgName); - CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim))); - } - Dirs = Dirs.substr(Delim + 1); - } - - if (Dirs.empty()) { // Trailing colon. - CmdArgs.push_back(ArgName); - CmdArgs.push_back("."); - } else { // Add the last path. - CmdArgs.push_back(ArgName); - CmdArgs.push_back(Args.MakeArgString(Dirs)); - } -} - void Clang::AddPreprocessingOptions(Compilation &C, const Driver &D, const ArgList &Args, @@ -399,19 +402,19 @@ void Clang::AddPreprocessingOptions(Compilation &C, // frontend into the driver. It will allow deleting 4 otherwise unused flags. // CPATH - included following the user specified includes (but prior to // builtin and standard includes). - AddIncludeDirectoryList(Args, CmdArgs, "-I", ::getenv("CPATH")); + AddDirectoryList(Args, CmdArgs, "-I", ::getenv("CPATH")); // C_INCLUDE_PATH - system includes enabled when compiling C. - AddIncludeDirectoryList(Args, CmdArgs, "-c-isystem", - ::getenv("C_INCLUDE_PATH")); + AddDirectoryList(Args, CmdArgs, "-c-isystem", + ::getenv("C_INCLUDE_PATH")); // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. - AddIncludeDirectoryList(Args, CmdArgs, "-cxx-isystem", - ::getenv("CPLUS_INCLUDE_PATH")); + AddDirectoryList(Args, CmdArgs, "-cxx-isystem", + ::getenv("CPLUS_INCLUDE_PATH")); // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. - AddIncludeDirectoryList(Args, CmdArgs, "-objc-isystem", - ::getenv("OBJC_INCLUDE_PATH")); + AddDirectoryList(Args, CmdArgs, "-objc-isystem", + ::getenv("OBJC_INCLUDE_PATH")); // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. - AddIncludeDirectoryList(Args, CmdArgs, "-objcxx-isystem", - ::getenv("OBJCPLUS_INCLUDE_PATH")); + AddDirectoryList(Args, CmdArgs, "-objcxx-isystem", + ::getenv("OBJCPLUS_INCLUDE_PATH")); // Add C++ include arguments, if needed. if (types::isCXX(Inputs[0].getType())) diff --git a/test/Driver/linker-opts.c b/test/Driver/linker-opts.c new file mode 100644 index 0000000000..a8d176cc1b --- /dev/null +++ b/test/Driver/linker-opts.c @@ -0,0 +1,2 @@ +// RUN: env LIBRARY_PATH=%T/test1 %clang -x c %s -### -o foo 2> %t.log +// RUN: grep '".*ld.*" .*"-L" "%T/test1"' %t.log