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 bool next_is_real() {
102 return (token != EOLTOK) && (token != ERRORTOK) && (token != 0);
105 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;
171 void parseOptionList(CompilerDriver::StringVector& optList ) {
172 if (next() == EQUALS) {
173 while (next_is_real()) {
174 if (token == STRING || token == OPTION)
175 optList.push_back(ConfigLexerState.StringVal);
176 else if (!parseSubstitution(optList)) {
177 error("Expecting a program argument or substitution", false);
182 error("Expecting '='");
185 void parseVersion() {
186 if (next() == EQUALS) {
187 while (next_is_real()) {
188 if (token == STRING || token == OPTION)
189 confDat->version = ConfigLexerState.StringVal;
191 error("Expecting a version string");
194 error("Expecting '='");
200 confDat->langName = parseName();
203 parseOptionList(confDat->opts[CompilerDriver::OPT_FAST_COMPILE]);
206 parseOptionList(confDat->opts[CompilerDriver::OPT_SIMPLE]);
209 parseOptionList(confDat->opts[CompilerDriver::OPT_AGGRESSIVE]);
212 parseOptionList(confDat->opts[CompilerDriver::OPT_LINK_TIME]);
216 confDat->opts[CompilerDriver::OPT_AGGRESSIVE_LINK_TIME]);
219 error("Expecting 'name' or 'optN' after 'lang.'");
224 void parseCommand(CompilerDriver::Action& action) {
225 if (next() == EQUALS) {
226 if (next() == EOLTOK) {
228 action.program.clear();
231 if (token == STRING || token == OPTION) {
232 action.program = ConfigLexerState.StringVal;
234 error("Expecting a program name");
236 while (next_is_real()) {
237 if (token == STRING || token == OPTION) {
238 action.args.push_back(ConfigLexerState.StringVal);
239 } else if (!parseSubstitution(action.args)) {
240 error("Expecting a program argument or substitution", false);
248 void parsePreprocessor() {
251 parseCommand(confDat->PreProcessor);
255 confDat->PreProcessor.set(CompilerDriver::REQUIRED_FLAG);
257 confDat->PreProcessor.clear(CompilerDriver::REQUIRED_FLAG);
260 error("Expecting 'command' or 'required' but found '" +
261 ConfigLexerState.StringVal);
266 bool parseOutputFlag() {
267 if (next() == EQUALS) {
268 if (next() == ASSEMBLY) {
270 } else if (token == BYTECODE) {
273 error("Expecting output type value");
276 if (next() != EOLTOK && token != 0) {
277 error("Extraneous tokens after output value");
281 error("Expecting '='");
285 void parseTranslator() {
288 parseCommand(confDat->Translator);
292 confDat->Translator.set(CompilerDriver::REQUIRED_FLAG);
294 confDat->Translator.clear(CompilerDriver::REQUIRED_FLAG);
298 confDat->Translator.set(CompilerDriver::PREPROCESSES_FLAG);
300 confDat->Translator.clear(CompilerDriver::PREPROCESSES_FLAG);
303 if (parseOutputFlag())
304 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
306 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
310 error("Expecting 'command', 'required', 'preprocesses', or "
311 "'output' but found '" + ConfigLexerState.StringVal +
317 void parseOptimizer() {
320 parseCommand(confDat->Optimizer);
324 confDat->Optimizer.set(CompilerDriver::PREPROCESSES_FLAG);
326 confDat->Optimizer.clear(CompilerDriver::PREPROCESSES_FLAG);
330 confDat->Optimizer.set(CompilerDriver::TRANSLATES_FLAG);
332 confDat->Optimizer.clear(CompilerDriver::TRANSLATES_FLAG);
336 confDat->Optimizer.set(CompilerDriver::REQUIRED_FLAG);
338 confDat->Optimizer.clear(CompilerDriver::REQUIRED_FLAG);
341 if (parseOutputFlag())
342 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
344 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
347 error(std::string("Expecting 'command', 'preprocesses', ") +
348 "'translates' or 'output' but found '" +
349 ConfigLexerState.StringVal + "' instead");
354 void parseAssembler() {
357 parseCommand(confDat->Assembler);
360 error("Expecting 'command'");
372 error("Expecting 'libs' or 'libpaths'");
377 void parseAssignment() {
379 case VERSION: parseVersion(); break;
380 case LANG: parseLang(); break;
381 case PREPROCESSOR: parsePreprocessor(); break;
382 case TRANSLATOR: parseTranslator(); break;
383 case OPTIMIZER: parseOptimizer(); break;
384 case ASSEMBLER: parseAssembler(); break;
385 case LINKER: parseLinker(); break;
386 case EOLTOK: break; // just ignore
389 error("Invalid top level configuration item");
395 while ( next() != EOFTOK ) {
396 if (token == ERRORTOK)
397 error("Invalid token");
398 else if (token != EOLTOK)
401 provider->checkErrors();
406 ParseConfigData(InputProvider& provider, CompilerDriver::ConfigData& confDat) {
409 p.provider = &provider;
410 p.confDat = &confDat;
415 CompilerDriver::ConfigData*
416 LLVMC_ConfigDataProvider::ReadConfigData(const std::string& ftype) {
417 CompilerDriver::ConfigData* result = 0;
418 std::string dir_name;
419 if (configDir.empty()) {
420 // Try the environment variable
421 const char* conf = getenv("LLVM_CONFIG_DIR");
425 if (!::sys::FileIsReadable(dir_name + ftype))
426 throw "Configuration file for '" + ftype + "' is not available.";
428 // Try the user's home directory
429 const char* home = getenv("HOME");
432 dir_name += "/.llvm/etc/";
433 if (!::sys::FileIsReadable(dir_name + ftype)) {
434 // Okay, try the LLVM installation directory
435 dir_name = LLVM_ETCDIR;
437 if (!::sys::FileIsReadable(dir_name + ftype)) {
438 // Okay, try the "standard" place
439 dir_name = "/etc/llvm/";
440 if (!::sys::FileIsReadable(dir_name + ftype)) {
441 throw "Configuration file for '" + ftype + "' is not available.";
448 dir_name = configDir + "/";
449 if (!::sys::FileIsReadable(dir_name + ftype)) {
450 throw "Configuration file for '" + ftype + "' is not available.";
453 FileInputProvider fip( dir_name + ftype );
455 throw "Configuration file for '" + ftype + "' is not available.";
457 result = new CompilerDriver::ConfigData();
458 ParseConfigData(fip,*result);
462 LLVMC_ConfigDataProvider::LLVMC_ConfigDataProvider()
466 Configurations.clear();
469 LLVMC_ConfigDataProvider::~LLVMC_ConfigDataProvider()
471 ConfigDataMap::iterator cIt = Configurations.begin();
472 while (cIt != Configurations.end()) {
473 CompilerDriver::ConfigData* cd = cIt->second;
477 Configurations.clear();
480 CompilerDriver::ConfigData*
481 LLVMC_ConfigDataProvider::ProvideConfigData(const std::string& filetype) {
482 CompilerDriver::ConfigData* result = 0;
483 if (!Configurations.empty()) {
484 ConfigDataMap::iterator cIt = Configurations.find(filetype);
485 if ( cIt != Configurations.end() ) {
486 // We found one in the case, return it.
487 result = cIt->second;
491 // The configuration data doesn't exist, we have to go read it.
492 result = ReadConfigData(filetype);
493 // If we got one, cache it
495 Configurations.insert(std::make_pair(filetype,result));
497 return result; // Might return 0
500 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab