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 "Config/config.h"
19 #include "Support/CommandLine.h"
20 #include "Support/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, cl::init(false),
75 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 std::string parseName() {
117 if (next() == EQUALS) {
118 while (next_is_real()) {
122 result += ConfigLexerState.StringVal + " ";
125 error("Invalid name");
130 error("Name exepected");
132 result.erase(result.size()-1,1);
138 bool parseBoolean() {
140 if (next() == EQUALS) {
141 if (next() == FALSETOK) {
143 } else if (token != TRUETOK) {
144 error("Expecting boolean value");
147 if (next() != EOLTOK && token != 0) {
148 error("Extraneous tokens after boolean");
152 error("Expecting '='");
156 bool parseSubstitution(CompilerDriver::StringVector& optList) {
158 case ARGS_SUBST: optList.push_back("%args%"); break;
159 case IN_SUBST: optList.push_back("%in%"); break;
160 case OUT_SUBST: optList.push_back("%out%"); break;
161 case TIME_SUBST: optList.push_back("%time%"); break;
162 case STATS_SUBST: optList.push_back("%stats%"); break;
163 case OPT_SUBST: optList.push_back("%opt%"); break;
164 case TARGET_SUBST: optList.push_back("%target%"); break;
165 case FORCE_SUBST: optList.push_back("%force%"); break;
166 case VERBOSE_SUBST: optList.push_back("%verbose%"); break;
173 void parseOptionList(CompilerDriver::StringVector& optList ) {
174 if (next() == EQUALS) {
175 while (next_is_real()) {
176 if (token == STRING || token == OPTION)
177 optList.push_back(ConfigLexerState.StringVal);
178 else if (!parseSubstitution(optList)) {
179 error("Expecting a program argument or substitution", false);
184 error("Expecting '='");
187 void parseVersion() {
188 if (next() == EQUALS) {
189 while (next_is_real()) {
190 if (token == STRING || token == OPTION)
191 confDat->version = ConfigLexerState.StringVal;
193 error("Expecting a version string");
196 error("Expecting '='");
202 confDat->langName = parseName();
205 parseOptionList(confDat->opts[CompilerDriver::OPT_FAST_COMPILE]);
208 parseOptionList(confDat->opts[CompilerDriver::OPT_SIMPLE]);
211 parseOptionList(confDat->opts[CompilerDriver::OPT_AGGRESSIVE]);
214 parseOptionList(confDat->opts[CompilerDriver::OPT_LINK_TIME]);
218 confDat->opts[CompilerDriver::OPT_AGGRESSIVE_LINK_TIME]);
221 error("Expecting 'name' or 'optN' after 'lang.'");
226 void parseCommand(CompilerDriver::Action& action) {
227 if (next() == EQUALS) {
228 if (next() == EOLTOK) {
230 action.program.clear();
233 if (token == STRING || token == OPTION) {
234 action.program.set_file(ConfigLexerState.StringVal);
236 error("Expecting a program name");
238 while (next_is_real()) {
239 if (token == STRING || token == OPTION) {
240 action.args.push_back(ConfigLexerState.StringVal);
241 } else if (!parseSubstitution(action.args)) {
242 error("Expecting a program argument or substitution", false);
250 void parsePreprocessor() {
253 parseCommand(confDat->PreProcessor);
257 confDat->PreProcessor.set(CompilerDriver::REQUIRED_FLAG);
259 confDat->PreProcessor.clear(CompilerDriver::REQUIRED_FLAG);
262 error("Expecting 'command' or 'required' but found '" +
263 ConfigLexerState.StringVal);
268 bool parseOutputFlag() {
269 if (next() == EQUALS) {
270 if (next() == ASSEMBLY) {
272 } else if (token == BYTECODE) {
275 error("Expecting output type value");
278 if (next() != EOLTOK && token != 0) {
279 error("Extraneous tokens after output value");
283 error("Expecting '='");
287 void parseTranslator() {
290 parseCommand(confDat->Translator);
294 confDat->Translator.set(CompilerDriver::REQUIRED_FLAG);
296 confDat->Translator.clear(CompilerDriver::REQUIRED_FLAG);
300 confDat->Translator.set(CompilerDriver::PREPROCESSES_FLAG);
302 confDat->Translator.clear(CompilerDriver::PREPROCESSES_FLAG);
305 if (parseOutputFlag())
306 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
308 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
312 error("Expecting 'command', 'required', 'preprocesses', or "
313 "'output' but found '" + ConfigLexerState.StringVal +
319 void parseOptimizer() {
322 parseCommand(confDat->Optimizer);
326 confDat->Optimizer.set(CompilerDriver::PREPROCESSES_FLAG);
328 confDat->Optimizer.clear(CompilerDriver::PREPROCESSES_FLAG);
332 confDat->Optimizer.set(CompilerDriver::TRANSLATES_FLAG);
334 confDat->Optimizer.clear(CompilerDriver::TRANSLATES_FLAG);
338 confDat->Optimizer.set(CompilerDriver::REQUIRED_FLAG);
340 confDat->Optimizer.clear(CompilerDriver::REQUIRED_FLAG);
343 if (parseOutputFlag())
344 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
346 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
349 error(std::string("Expecting 'command', 'preprocesses', ") +
350 "'translates' or 'output' but found '" +
351 ConfigLexerState.StringVal + "' instead");
356 void parseAssembler() {
359 parseCommand(confDat->Assembler);
362 error("Expecting 'command'");
374 error("Expecting 'libs' or 'libpaths'");
379 void parseAssignment() {
381 case VERSION: parseVersion(); break;
382 case LANG: parseLang(); break;
383 case PREPROCESSOR: parsePreprocessor(); break;
384 case TRANSLATOR: parseTranslator(); break;
385 case OPTIMIZER: parseOptimizer(); break;
386 case ASSEMBLER: parseAssembler(); break;
387 case LINKER: parseLinker(); break;
388 case EOLTOK: break; // just ignore
391 error("Invalid top level configuration item");
397 while ( next() != EOFTOK ) {
398 if (token == ERRORTOK)
399 error("Invalid token");
400 else if (token != EOLTOK)
403 provider->checkErrors();
408 ParseConfigData(InputProvider& provider, CompilerDriver::ConfigData& confDat) {
411 p.provider = &provider;
412 p.confDat = &confDat;
417 CompilerDriver::ConfigData*
418 LLVMC_ConfigDataProvider::ReadConfigData(const std::string& ftype) {
419 CompilerDriver::ConfigData* result = 0;
421 if (configDir.is_empty()) {
422 // Try the environment variable
423 const char* conf = getenv("LLVM_CONFIG_DIR");
425 confFile.set_directory(conf);
426 confFile.append_file(ftype);
427 if (!confFile.readable())
428 throw "Configuration file for '" + ftype + "' is not available.";
430 // Try the user's home directory
431 confFile = sys::Path::GetUserHomeDirectory();
432 if (!confFile.is_empty()) {
433 confFile.append_directory(".llvm");
434 confFile.append_directory("etc");
435 confFile.append_file(ftype);
436 if (!confFile.readable())
439 if (!confFile.is_empty()) {
440 // Okay, try the LLVM installation directory
441 confFile = sys::Path::GetLLVMConfigDir();
442 confFile.append_file(ftype);
443 if (!confFile.readable()) {
444 // Okay, try the "standard" place
445 confFile = sys::Path::GetLLVMDefaultConfigDir();
446 confFile.append_file(ftype);
447 if (!confFile.readable()) {
448 throw "Configuration file for '" + ftype + "' is not available.";
454 confFile = configDir;
455 confFile.append_file(ftype);
456 if (!confFile.readable())
457 throw "Configuration file for '" + ftype + "' is not available.";
459 FileInputProvider fip( confFile.get() );
461 throw "Configuration file for '" + ftype + "' is not available.";
463 result = new CompilerDriver::ConfigData();
464 ParseConfigData(fip,*result);
468 LLVMC_ConfigDataProvider::~LLVMC_ConfigDataProvider()
470 ConfigDataMap::iterator cIt = Configurations.begin();
471 while (cIt != Configurations.end()) {
472 CompilerDriver::ConfigData* cd = cIt->second;
476 Configurations.clear();
479 CompilerDriver::ConfigData*
480 LLVMC_ConfigDataProvider::ProvideConfigData(const std::string& filetype) {
481 CompilerDriver::ConfigData* result = 0;
482 if (!Configurations.empty()) {
483 ConfigDataMap::iterator cIt = Configurations.find(filetype);
484 if ( cIt != Configurations.end() ) {
485 // We found one in the case, return it.
486 result = cIt->second;
490 // The configuration data doesn't exist, we have to go read it.
491 result = ReadConfigData(filetype);
492 // If we got one, cache it
494 Configurations.insert(std::make_pair(filetype,result));
496 return result; // Might return 0
499 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab