Home | History | Annotate | Download | only in llvm-ndk-cc
      1 #include <cassert>
      2 #include <cstdlib>
      3 #include <list>
      4 #include <set>
      5 #include <string>
      6 #include <utility>
      7 #include <vector>
      8 
      9 #include "clang/Driver/Arg.h"
     10 #include "clang/Driver/ArgList.h"
     11 #include "clang/Driver/DriverDiagnostic.h"
     12 #include "clang/Driver/Option.h"
     13 #include "clang/Driver/OptTable.h"
     14 
     15 #include "clang/Frontend/DiagnosticOptions.h"
     16 #include "clang/Frontend/TextDiagnosticPrinter.h"
     17 
     18 #include "llvm/ADT/SmallVector.h"
     19 #include "llvm/ADT/StringRef.h"
     20 #include "llvm/ADT/IntrusiveRefCntPtr.h"
     21 #include "llvm/ADT/OwningPtr.h"
     22 
     23 #include "llvm/Support/CommandLine.h"
     24 #include "llvm/Support/ManagedStatic.h"
     25 #include "llvm/Support/MemoryBuffer.h"
     26 #include "llvm/Support/Path.h"
     27 #include "llvm/Support/raw_ostream.h"
     28 #include "llvm/Support/system_error.h"
     29 
     30 #include "Compiler.h"
     31 
     32 // FIXME: Add parameter feature '-D macro=xxx'
     33 static llvm::cl::opt<std::string>
     34 InputFilename(llvm::cl::Positional, llvm::cl::Required,
     35               llvm::cl::desc("<input file>"));
     36 
     37 static llvm::cl::list<std::string>
     38 HeaderSearchDirs("I", llvm::cl::desc("Header search directory"), llvm::cl::Prefix);
     39 
     40 static llvm::cl::list<std::string>
     41 PreDefinedSymbols("D", llvm::cl::desc("Pre-define symbol"));
     42 
     43 static llvm::cl::opt<std::string>
     44 OutputFilename(llvm::cl::Required, "o", llvm::cl::desc("Override output filename"));
     45 
     46 // split "xxx"     => "xxx" ""
     47 // split "xxx=yyy" => "xxx" "yyy"
     48 static void splitPreDefinedSymbol(const std::string& In,
     49                              std::string& Key, std::string& Value) {
     50   size_t FoundPos = In.find("=");
     51   if (FoundPos == std::string::npos) {
     52     Key = In;
     53     Value = "";
     54   } else {
     55     Key = In.substr(0, FoundPos);
     56     Value = In.substr(FoundPos+1, std::string::npos);
     57   }
     58 }
     59 
     60 
     61 int main(int argc, char** argv) {
     62   llvm::llvm_shutdown_obj _ShutdownObj;
     63   llvm::cl::ParseCommandLineOptions(argc, argv, "P-NDK Compile Tool");
     64 
     65   clang::TextDiagnosticPrinter* DiagClient =
     66       new clang::TextDiagnosticPrinter(llvm::errs(), clang::DiagnosticOptions());
     67   DiagClient->setPrefix(argv[0]);
     68 
     69   llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>
     70       DiagIDs(new clang::DiagnosticIDs());
     71   clang::Diagnostic Diags(DiagIDs, DiagClient, true);
     72 
     73   if (Diags.hasErrorOccurred())
     74     return 1;
     75 
     76   std::vector<std::string> IncludePaths;
     77   for(unsigned i = 0, e = HeaderSearchDirs.size(); i<e; ++i) {
     78     IncludePaths.push_back(HeaderSearchDirs[i]);
     79   }
     80 
     81   std::map<std::string, std::string> PreDefinedSymbolMap;
     82   for(unsigned i = 0, e = PreDefinedSymbols.size(); i<e; ++i) {
     83     std::string Key;
     84     std::string Value;
     85     splitPreDefinedSymbol(PreDefinedSymbols[i], Key, Value);
     86     PreDefinedSymbolMap.insert(
     87         std::pair<std::string, std::string>(Key,Value));
     88   }
     89 
     90   ndkpc::Compiler Compiler;
     91   Compiler.init(std::string(),
     92                 std::string(),
     93                 std::vector<std::string>(),
     94                 llvm::StringRef(InputFilename).endswith(".cpp"));
     95   Compiler.setInputSource(InputFilename);
     96   Compiler.setIncludePaths(IncludePaths);
     97   Compiler.setOutput(OutputFilename.c_str());
     98   Compiler.setPreDefinedSymbol(PreDefinedSymbolMap);
     99 
    100   int ret = Compiler.compile();
    101   return ret;
    102 }
    103