1 //===- Configuration.cpp - Configuration Data Mgmt --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the parsing of configuration files for the LLVM Compiler
13 //===------------------------------------------------------------------------===
15 #include "Configuration.h"
16 #include "ConfigLexer.h"
17 #include "CompilerDriver.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/ADT/StringExtras.h"
27 // From CompilerDriver.cpp (for now)
28 extern bool FileIsReadable(const std::string& fname);
32 ConfigLexerInfo ConfigLexerState;
33 InputProvider* ConfigLexerInput = 0;
35 InputProvider::~InputProvider() {}
36 void InputProvider::error(const std::string& msg) {
37 std::cerr << name << ":" << ConfigLexerState.lineNum << ": Error: " <<
42 void InputProvider::checkErrors() {
44 std::cerr << name << " had " << errCount << " errors. Terminating.\n";
53 class FileInputProvider : public InputProvider {
55 FileInputProvider(const std::string & fname)
56 : InputProvider(fname)
58 ConfigLexerInput = this;
60 virtual ~FileInputProvider() { F.close(); ConfigLexerInput = 0; }
61 virtual unsigned read(char *buffer, unsigned max_size) {
63 F.read(buffer,max_size);
64 if ( F.gcount() ) return F.gcount() - 1;
69 bool okay() { return F.good(); }
74 cl::opt<bool> DumpTokens("dump-tokens", cl::Optional, cl::Hidden,
75 cl::init(false), cl::desc("Dump lexical tokens (debug use only)."));
83 ConfigLexerState.lineNum = 1;
84 ConfigLexerState.in_value = false;
85 ConfigLexerState.StringVal.clear();
86 ConfigLexerState.IntegerVal = 0;
89 ConfigLexerTokens token;
90 InputProvider* provider;
91 CompilerDriver::ConfigData* confDat;
96 std::cerr << token << "\n";
100 inline bool next_is_real() {
102 return (token != EOLTOK) && (token != ERRORTOK) && (token != 0);
105 inline void eatLineRemnant() {
106 while (next_is_real()) ;
109 void error(const std::string& msg, bool skip = true) {
110 provider->error(msg);
115 bool parseCompleteItem(std::string& result) {
117 while (next_is_real()) {
121 result += ConfigLexerState.StringVal;
135 std::string parseName() {
137 if (next() == EQUALS) {
138 if (parseCompleteItem(result))
141 error("Name exepected");
143 error("Expecting '='");
147 bool parseBoolean() {
149 if (next() == EQUALS) {
152 if (token == FALSETOK) {
154 } else if (token != TRUETOK) {
155 error("Expecting boolean value");
158 if (next() != EOLTOK && token != 0) {
159 error("Extraneous tokens after boolean");
163 error("Expecting '='");
167 bool parseSubstitution(CompilerDriver::StringVector& optList) {
169 case ARGS_SUBST: optList.push_back("%args%"); break;
170 case BINDIR_SUBST: optList.push_back("%bindir%"); break;
171 case DEFS_SUBST: optList.push_back("%defs%"); break;
172 case IN_SUBST: optList.push_back("%in%"); break;
173 case INCLS_SUBST: optList.push_back("%incls%"); break;
174 case LIBDIR_SUBST: optList.push_back("%libdir%"); break;
175 case LIBS_SUBST: optList.push_back("%libs%"); break;
176 case OPT_SUBST: optList.push_back("%opt%"); break;
177 case OUT_SUBST: optList.push_back("%out%"); break;
178 case TARGET_SUBST: optList.push_back("%target%"); break;
179 case STATS_SUBST: optList.push_back("%stats%"); break;
180 case TIME_SUBST: optList.push_back("%time%"); break;
181 case VERBOSE_SUBST: optList.push_back("%verbose%"); break;
182 case FOPTS_SUBST: optList.push_back("%fOpts%"); break;
183 case MOPTS_SUBST: optList.push_back("%Mopts%"); break;
184 case WOPTS_SUBST: optList.push_back("%Wopts%"); break;
191 void parseOptionList(CompilerDriver::StringVector& optList ) {
192 if (next() == EQUALS) {
193 while (next_is_real()) {
194 if (token == STRING || token == OPTION)
195 optList.push_back(ConfigLexerState.StringVal);
196 else if (!parseSubstitution(optList)) {
197 error("Expecting a program argument or substitution", false);
202 error("Expecting '='");
205 void parseVersion() {
206 if (next() != EQUALS)
207 error("Expecting '='");
208 while (next_is_real()) {
209 if (token == STRING || token == OPTION)
210 confDat->version = ConfigLexerState.StringVal;
212 error("Expecting a version string");
217 if (next() != EQUALS)
218 error("Expecting '='");
220 while (parseCompleteItem(lib)) {
222 confDat->libpaths.push_back(lib);
228 if (next() != SEPARATOR)
229 error("Expecting '.'");
235 confDat->langName = parseName();
238 parseOptionList(confDat->opts[CompilerDriver::OPT_FAST_COMPILE]);
241 parseOptionList(confDat->opts[CompilerDriver::OPT_SIMPLE]);
244 parseOptionList(confDat->opts[CompilerDriver::OPT_AGGRESSIVE]);
247 parseOptionList(confDat->opts[CompilerDriver::OPT_LINK_TIME]);
251 confDat->opts[CompilerDriver::OPT_AGGRESSIVE_LINK_TIME]);
254 error("Expecting 'name' or 'optN' after 'lang.'");
259 bool parseProgramName(std::string& str) {
279 str += ConfigLexerState.StringVal;
300 } while (token != SPACE && token != EOFTOK && token != EOLTOK &&
305 void parseCommand(CompilerDriver::Action& action) {
306 if (next() != EQUALS)
307 error("Expecting '='");
311 action.program.clear();
319 std::string progname;
320 if (parseProgramName(progname))
321 action.program.setFile(progname);
323 error("Expecting a program name");
326 std::string anOption;
327 while (next_is_real()) {
331 anOption += ConfigLexerState.StringVal;
334 anOption += "assembly";
337 anOption += "bytecode";
349 action.args.push_back(anOption);
353 if (!parseSubstitution(action.args))
354 error("Expecting a program argument or substitution", false);
362 void parsePreprocessor() {
363 if (next() != SEPARATOR)
364 error("Expecting '.'");
367 parseCommand(confDat->PreProcessor);
371 confDat->PreProcessor.set(CompilerDriver::REQUIRED_FLAG);
373 confDat->PreProcessor.clear(CompilerDriver::REQUIRED_FLAG);
376 error("Expecting 'command' or 'required' but found '" +
377 ConfigLexerState.StringVal);
382 bool parseOutputFlag() {
383 if (next() == EQUALS) {
386 if (token == ASSEMBLY) {
388 } else if (token == BYTECODE) {
391 error("Expecting output type value");
394 if (next() != EOLTOK && token != 0) {
395 error("Extraneous tokens after output value");
399 error("Expecting '='");
403 void parseTranslator() {
404 if (next() != SEPARATOR)
405 error("Expecting '.'");
408 parseCommand(confDat->Translator);
412 confDat->Translator.set(CompilerDriver::REQUIRED_FLAG);
414 confDat->Translator.clear(CompilerDriver::REQUIRED_FLAG);
418 confDat->Translator.set(CompilerDriver::PREPROCESSES_FLAG);
420 confDat->Translator.clear(CompilerDriver::PREPROCESSES_FLAG);
423 if (parseOutputFlag())
424 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
426 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
430 error("Expecting 'command', 'required', 'preprocesses', or "
431 "'output' but found '" + ConfigLexerState.StringVal +
437 void parseOptimizer() {
438 if (next() != SEPARATOR)
439 error("Expecting '.'");
442 parseCommand(confDat->Optimizer);
446 confDat->Optimizer.set(CompilerDriver::PREPROCESSES_FLAG);
448 confDat->Optimizer.clear(CompilerDriver::PREPROCESSES_FLAG);
452 confDat->Optimizer.set(CompilerDriver::TRANSLATES_FLAG);
454 confDat->Optimizer.clear(CompilerDriver::TRANSLATES_FLAG);
458 confDat->Optimizer.set(CompilerDriver::REQUIRED_FLAG);
460 confDat->Optimizer.clear(CompilerDriver::REQUIRED_FLAG);
463 if (parseOutputFlag())
464 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
466 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
469 error(std::string("Expecting 'command', 'preprocesses', "
470 "'translates' or 'output' but found '") +
471 ConfigLexerState.StringVal + "' instead");
476 void parseAssembler() {
477 if (next() != SEPARATOR)
478 error("Expecting '.'");
481 parseCommand(confDat->Assembler);
484 error("Expecting 'command'");
490 if (next() != SEPARATOR)
491 error("Expecting '.'");
498 error("Expecting 'libs' or 'libpaths'");
503 void parseAssignment() {
505 case VERSION_TOK: parseVersion(); break;
506 case LANG: parseLang(); break;
507 case PREPROCESSOR: parsePreprocessor(); break;
508 case TRANSLATOR: parseTranslator(); break;
509 case OPTIMIZER: parseOptimizer(); break;
510 case ASSEMBLER: parseAssembler(); break;
511 case LINKER: parseLinker(); break;
512 case EOLTOK: break; // just ignore
515 error("Invalid top level configuration item");
521 while ( next() != EOFTOK ) {
522 if (token == ERRORTOK)
523 error("Invalid token");
524 else if (token != EOLTOK)
527 provider->checkErrors();
532 ParseConfigData(InputProvider& provider, CompilerDriver::ConfigData& confDat) {
535 p.provider = &provider;
536 p.confDat = &confDat;
542 CompilerDriver::ConfigData*
543 LLVMC_ConfigDataProvider::ReadConfigData(const std::string& ftype) {
544 CompilerDriver::ConfigData* result = 0;
546 if (configDir.isEmpty()) {
547 // Try the environment variable
548 const char* conf = getenv("LLVM_CONFIG_DIR");
550 confFile.setDirectory(conf);
551 confFile.appendFile(ftype);
552 if (!confFile.readable())
553 throw std::string("Configuration file for '") + ftype +
554 "' is not available.";
556 // Try the user's home directory
557 confFile = sys::Path::GetUserHomeDirectory();
558 if (!confFile.isEmpty()) {
559 confFile.appendDirectory(".llvm");
560 confFile.appendDirectory("etc");
561 confFile.appendFile(ftype);
562 if (!confFile.readable())
565 if (confFile.isEmpty()) {
566 // Okay, try the LLVM installation directory
567 confFile = sys::Path::GetLLVMConfigDir();
568 confFile.appendFile(ftype);
569 if (!confFile.readable()) {
570 // Okay, try the "standard" place
571 confFile = sys::Path::GetLLVMDefaultConfigDir();
572 confFile.appendFile(ftype);
573 if (!confFile.readable()) {
574 throw std::string("Configuration file for '") + ftype +
575 "' is not available.";
581 confFile = configDir;
582 confFile.appendFile(ftype);
583 if (!confFile.readable())
584 throw std::string("Configuration file for '") + ftype +
585 "' is not available.";
587 FileInputProvider fip( confFile.toString() );
589 throw std::string("Configuration file for '") + ftype +
590 "' is not available.";
592 result = new CompilerDriver::ConfigData();
593 ParseConfigData(fip,*result);
597 LLVMC_ConfigDataProvider::~LLVMC_ConfigDataProvider()
599 ConfigDataMap::iterator cIt = Configurations.begin();
600 while (cIt != Configurations.end()) {
601 CompilerDriver::ConfigData* cd = cIt->second;
605 Configurations.clear();
608 CompilerDriver::ConfigData*
609 LLVMC_ConfigDataProvider::ProvideConfigData(const std::string& filetype) {
610 CompilerDriver::ConfigData* result = 0;
611 if (!Configurations.empty()) {
612 ConfigDataMap::iterator cIt = Configurations.find(filetype);
613 if ( cIt != Configurations.end() ) {
614 // We found one in the case, return it.
615 result = cIt->second;
619 // The configuration data doesn't exist, we have to go read it.
620 result = ReadConfigData(filetype);
621 // If we got one, cache it
623 Configurations.insert(std::make_pair(filetype,result));
625 return result; // Might return 0