[cfe-dev] diff for clang environment variables/paths

Kelly Wilson wilsonk at cpsc.ucalgary.ca
Wed Aug 22 12:44:22 CDT 2007


Hello fellow clangers ;)

I am just sending a quick diff for a couple clang.cpp "FIXME"'s. Not sure 
if the use of the system()/getenv() calls are appropriate but I just 
wanted this to work for my setup. Let me know if this is not an 
acceptable patch.

Not really tested on Mac OS X but the syscalls exist and 'gcc -v' replies 
with the correct info, I think.

Thanks,
K.Wilson
-------------- next part --------------
Index: Driver/clang.cpp
===================================================================
--- Driver/clang.cpp	(revision 40382)
+++ Driver/clang.cpp	(working copy)
@@ -35,8 +35,18 @@
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Signals.h"
 #include <memory>
+#include <fstream>
+#include <iostream>
 using namespace clang;
 
+
+inline char const *ChangeNullToEmpty( char const *PossibleNull )
+{
+  if (!PossibleNull)
+    return "";
+  return PossibleNull;
+}
+
 //===----------------------------------------------------------------------===//
 // Global options.
 //===----------------------------------------------------------------------===//
@@ -537,6 +547,37 @@
   }
 }
 
+//Strip out the includes from the "gcc -v" or "g++ -v" output and add them to the path
+void getIncludes(std::ifstream &InFile, FileManager &FM) {
+
+ std::string TmpInclude;
+
+ bool SearchStartFound = false;
+ while(InFile) {
+   
+   getline(InFile,TmpInclude);
+   
+   if(TmpInclude.find("End of search",0) != std::string::npos) {
+     SearchStartFound = false;
+   }
+   if(SearchStartFound) {
+     //There is a space at the start of the directories that gcc returns??? Aesthetics, I guess.
+     if(TmpInclude[0] == ' ') {
+       TmpInclude.erase(0,1);
+     }
+     //Add to the search path.
+     AddPath(TmpInclude, System, false, false, false, FM);
+     
+   }
+   if(TmpInclude.find("> search",0) != std::string::npos) {
+     SearchStartFound = true;
+   }
+   
+ }
+ 
+}
+
+
 /// InitializeIncludePaths - Process the -I options and set them in the
 /// HeaderSearch object.
 static void InitializeIncludePaths(HeaderSearch &Headers, FileManager &FM,
@@ -606,20 +647,53 @@
       }
     }
   }
-  
-  // FIXME: Add contents of the CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
+
+  std::string EnvVars;  
+  // Add contents of the CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
   // OBJC_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH environment variables.
-  
-  // FIXME: temporary hack: hard-coded paths.
-  // FIXME: get these from the target?
+  // EnvVars is not checked here for valid input. That happens in AddPath()
+  if ( (EnvVars = ChangeNullToEmpty(getenv("CPATH"))) != "") {
+    AddPath(EnvVars, System, false, true, false, FM);    
+  }
+  if ( (EnvVars = ChangeNullToEmpty(getenv("C_INCLUDE_PATH"))) != "") {
+    AddPath(EnvVars, System, false, true, false, FM);
+  }
+  if ( (EnvVars = ChangeNullToEmpty(getenv("CPLUS_INCLUDE_PATH"))) != "") {
+    AddPath(EnvVars, System, true, true, false, FM);    
+  }
+  if ( (EnvVars = ChangeNullToEmpty(getenv("OBJC_INCLUDE_PATH"))) != "") {
+    AddPath(EnvVars, System, false, true, false, FM);    
+  }
+  if ( (EnvVars = ChangeNullToEmpty(getenv("OBJCPLUS_INCLUDE_PATH"))) != "") {
+    AddPath(EnvVars, System, true, true, false, FM);    
+  }
+    
+  // Hard-coded paths may not be needed now, but I haven't adequately tested this on all
+  // platforms (Linux->yes...OSX->not so much) so I have left them in. Doubles are culled later. 
   if (!nostdinc) {
+
+    std::ifstream CppInFile;
+    std::ifstream CInFile;
+
     if (Lang.CPlusPlus) {
       AddPath("/usr/include/c++/4.0.0", System, true, false, false, FM);
       AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
               false, FM);
       AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false,FM);
+
+      std::system("touch empty.cpp");
+      std::system("gcc -v empty.cpp -f-syntax-only 2&> tmpCppFile");
+      CppInFile.open("./tmpCppFile");
+      if(!CppInFile) {
+	fprintf(stderr, "Error reading temporary file 'tmpCppFile'! No write access in this directory? \n");
+      }
+      getIncludes(CppInFile, FM);
+      CppInFile.close();
+      std::system("rm -f tmpCppFile");
+      
     }
     
+    
     AddPath("/usr/local/include", System, false, false, false, FM);
     // leopard
     AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System, 
@@ -642,6 +716,18 @@
     AddPath("/usr/include", System, false, false, false, FM);
     AddPath("/System/Library/Frameworks", System, true, false, true, FM);
     AddPath("/Library/Frameworks", System, true, false, true, FM);
+    
+
+    std::system("touch empty.c");
+    std::system("gcc -v empty.c -f-syntax-only 2&> tmpCFile");
+    CInFile.open("./tmpCFile");
+    if(!CInFile) {
+      fprintf(stderr, "Error reading temporary file 'tmpCFile'! No write access in this directory? \n");
+    }
+    getIncludes(CInFile, FM);
+    CInFile.close();
+    std::system("rm -f tmpCFile");
+
   }
 
   // Now that we have collected all of the include paths, merge them all


More information about the cfe-dev mailing list