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 FileReadable(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 IN_SUBST: optList.push_back("%in%"); break;
159 case OUT_SUBST: optList.push_back("%out%"); break;
160 case TIME_SUBST: optList.push_back("%time%"); break;
161 case STATS_SUBST: optList.push_back("%stats%"); break;
162 case OPT_SUBST: optList.push_back("%opt%"); break;
163 case TARGET_SUBST: optList.push_back("%target%"); break;
170 void parseOptionList(CompilerDriver::StringVector& optList ) {
171 if (next() == EQUALS) {
172 while (next_is_real()) {
173 if (token == STRING || token == OPTION)
174 optList.push_back(ConfigLexerState.StringVal);
175 else if (!parseSubstitution(optList)) {
176 error("Expecting a program argument or substitution", false);
181 error("Expecting '='");
184 void parseVersion() {
185 if (next() == EQUALS) {
186 while (next_is_real()) {
187 if (token == STRING || token == OPTION)
188 confDat->version = ConfigLexerState.StringVal;
190 error("Expecting a version string");
193 error("Expecting '='");
199 confDat->langName = parseName();
202 parseOptionList(confDat->opts[CompilerDriver::OPT_FAST_COMPILE]);
205 parseOptionList(confDat->opts[CompilerDriver::OPT_SIMPLE]);
208 parseOptionList(confDat->opts[CompilerDriver::OPT_AGGRESSIVE]);
211 parseOptionList(confDat->opts[CompilerDriver::OPT_LINK_TIME]);
215 confDat->opts[CompilerDriver::OPT_AGGRESSIVE_LINK_TIME]);
218 error("Expecting 'name' or 'optN' after 'lang.'");
223 void parseCommand(CompilerDriver::Action& action) {
224 if (next() == EQUALS) {
225 if (next() == EOLTOK) {
227 action.program.clear();
230 if (token == STRING || token == OPTION) {
231 action.program = ConfigLexerState.StringVal;
233 error("Expecting a program name");
235 while (next_is_real()) {
236 if (token == STRING || token == OPTION) {
237 action.args.push_back(ConfigLexerState.StringVal);
238 } else if (!parseSubstitution(action.args)) {
239 error("Expecting a program argument or substitution", false);
247 void parsePreprocessor() {
250 parseCommand(confDat->PreProcessor);
254 confDat->PreProcessor.set(CompilerDriver::REQUIRED_FLAG);
256 confDat->PreProcessor.clear(CompilerDriver::REQUIRED_FLAG);
259 error("Expecting 'command' or 'required'");
264 void parseTranslator() {
267 parseCommand(confDat->Translator);
271 confDat->Translator.set(CompilerDriver::REQUIRED_FLAG);
273 confDat->Translator.clear(CompilerDriver::REQUIRED_FLAG);
277 confDat->Translator.set(CompilerDriver::PREPROCESSES_FLAG);
279 confDat->Translator.clear(CompilerDriver::PREPROCESSES_FLAG);
283 confDat->Translator.set(CompilerDriver::OPTIMIZES_FLAG);
285 confDat->Translator.clear(CompilerDriver::OPTIMIZES_FLAG);
289 confDat->Translator.set(CompilerDriver::GROKS_DASH_O_FLAG);
291 confDat->Translator.clear(CompilerDriver::GROKS_DASH_O_FLAG);
295 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
297 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
301 error("Expecting 'command', 'required', 'preprocesses', "
302 "'groks_dash_O' or 'optimizes'");
307 void parseOptimizer() {
310 parseCommand(confDat->Optimizer);
314 confDat->Optimizer.set(CompilerDriver::PREPROCESSES_FLAG);
316 confDat->Optimizer.clear(CompilerDriver::PREPROCESSES_FLAG);
320 confDat->Optimizer.set(CompilerDriver::TRANSLATES_FLAG);
322 confDat->Optimizer.clear(CompilerDriver::TRANSLATES_FLAG);
326 confDat->Optimizer.set(CompilerDriver::GROKS_DASH_O_FLAG);
328 confDat->Optimizer.clear(CompilerDriver::GROKS_DASH_O_FLAG);
332 confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG);
334 confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG);
337 error("Expecting 'command' or 'groks_dash_O'");
342 void parseAssembler() {
345 parseCommand(confDat->Assembler);
348 error("Expecting 'command'");
356 parseCommand(confDat->Linker);
360 confDat->Linker.set(CompilerDriver::GROKS_DASH_O_FLAG);
362 confDat->Linker.clear(CompilerDriver::GROKS_DASH_O_FLAG);
365 error("Expecting 'command'");
370 void parseAssignment() {
372 case VERSION: parseVersion(); break;
373 case LANG: parseLang(); break;
374 case PREPROCESSOR: parsePreprocessor(); break;
375 case TRANSLATOR: parseTranslator(); break;
376 case OPTIMIZER: parseOptimizer(); break;
377 case ASSEMBLER: parseAssembler(); break;
378 case LINKER: parseLinker(); break;
379 case EOLTOK: break; // just ignore
382 error("Invalid top level configuration item");
388 while ( next() != EOFTOK ) {
389 if (token == ERRORTOK)
390 error("Invalid token");
391 else if (token != EOLTOK)
394 provider->checkErrors();
399 ParseConfigData(InputProvider& provider, CompilerDriver::ConfigData& confDat) {
402 p.provider = &provider;
403 p.confDat = &confDat;
408 CompilerDriver::ConfigData*
409 LLVMC_ConfigDataProvider::ReadConfigData(const std::string& ftype) {
410 CompilerDriver::ConfigData* result = 0;
411 std::string dir_name;
412 if (configDir.empty()) {
413 // Try the environment variable
414 const char* conf = getenv("LLVM_CONFIG_DIR");
418 if (!::sys::FileReadable(dir_name + ftype))
419 throw "Configuration file for '" + ftype + "' is not available.";
421 // Try the user's home directory
422 const char* home = getenv("HOME");
425 dir_name += "/.llvm/etc/";
426 if (!::sys::FileReadable(dir_name + ftype)) {
427 // Okay, try the LLVM installation directory
428 dir_name = LLVM_ETCDIR;
430 if (!::sys::FileReadable(dir_name + ftype)) {
431 // Okay, try the "standard" place
432 dir_name = "/etc/llvm/";
433 if (!::sys::FileReadable(dir_name + ftype)) {
434 throw "Configuration file for '" + ftype + "' is not available.";
441 dir_name = configDir + "/";
442 if (!::sys::FileReadable(dir_name + ftype)) {
443 throw "Configuration file for '" + ftype + "' is not available.";
446 FileInputProvider fip( dir_name + ftype );
448 throw "Configuration file for '" + ftype + "' is not available.";
450 result = new CompilerDriver::ConfigData();
451 ParseConfigData(fip,*result);
455 LLVMC_ConfigDataProvider::LLVMC_ConfigDataProvider()
459 Configurations.clear();
462 LLVMC_ConfigDataProvider::~LLVMC_ConfigDataProvider()
464 ConfigDataMap::iterator cIt = Configurations.begin();
465 while (cIt != Configurations.end()) {
466 CompilerDriver::ConfigData* cd = cIt->second;
470 Configurations.clear();
473 CompilerDriver::ConfigData*
474 LLVMC_ConfigDataProvider::ProvideConfigData(const std::string& filetype) {
475 CompilerDriver::ConfigData* result = 0;
476 if (!Configurations.empty()) {
477 ConfigDataMap::iterator cIt = Configurations.find(filetype);
478 if ( cIt != Configurations.end() ) {
479 // We found one in the case, return it.
480 result = cIt->second;
484 // The configuration data doesn't exist, we have to go read it.
485 result = ReadConfigData(filetype);
486 // If we got one, cache it
488 Configurations.insert(std::make_pair(filetype,result));
490 return result; // Might return 0
493 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab