3 This file is part of builder
4 Copyright © 2006-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/core/except.h>
10 #include <msp/fs/stat.h>
11 #include <msp/fs/utils.h>
12 #include <msp/io/except.h>
13 #include <msp/io/file.h>
14 #include <msp/io/print.h>
15 #include <msp/time/utils.h>
18 #include "sourcepackage.h"
23 Config::Config(SourcePackage &p):
28 void Config::add_option(const string &n, const string &v, const string &d)
30 options.insert(OptionMap::value_type(n, Option(n, v, d)));
33 const Config::Option &Config::get_option(const string &name) const
35 OptionMap::const_iterator i = options.find(name);
37 throw KeyError("Unknown option", name);
42 bool Config::is_option(const string &name) const
44 return options.count(name);
47 void Config::select_last_profile()
51 IO::BufferedFile in((package.get_source()/".profile").str());
54 set_option("profile", profile);
56 catch(const IO::FileNotFound &)
60 package.get_builder().apply_profile_template(*this, get_option("profile").value);
66 void Config::select_profile(const string &profile)
68 set_option("profile", profile);
70 if(!package.get_builder().get_dry_run())
72 IO::BufferedFile out((package.get_source()/".profile").str(), IO::M_WRITE);
73 IO::print(out, "%s\n", profile);
77 package.get_builder().apply_profile_template(*this, profile);
83 bool Config::update(const StringMap &opts)
86 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
88 if(set_option(i->first, i->second) && i->first!="profile")
92 if(changed && !freeze_mtime)
100 for(unsigned n=0; n<20; ++n)
102 bool changed = false;
103 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
105 Option &opt = i->second;
106 string::size_type dollar = 0;
107 while((dollar = opt.value.find('$', dollar))!=string::npos)
109 string::size_type end;
111 if(opt.value[dollar+1]=='{')
113 end = opt.value.find('}', dollar+2);
114 if(end==string::npos)
115 throw Exception("Unterminated variable reference");
116 var = opt.value.substr(dollar+2, end-dollar-2);
121 for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end) ;
122 var = opt.value.substr(dollar+1, end-dollar-1);
127 value = get_option(var).value;
129 value = package.get_builder().get_current_arch().get_name();
130 else if(const char *ptr = getenv(var.c_str()))
133 opt.value.replace(dollar, end-dollar, value);
135 dollar += value.size();
145 void Config::save() const
147 FS::Path fn = package.get_source()/".options";
149 OptionMap::const_iterator i = options.find("profile");
151 fn = package.get_source()/(".options."+i->second.value);
153 IO::BufferedFile out(fn.str(), IO::M_WRITE);
155 for(i=options.begin(); i!=options.end(); ++i)
156 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
159 bool Config::set_option(const string &opt, const string &val)
163 OptionMap::iterator i = options.find(opt);
166 if(i->second.value!=val)
168 i->second.value = val;
176 FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
180 IO::BufferedFile in(fn.str());
182 mtime = Time::TimeStamp::from_unixtime(FS::stat(fn).st_mtime);
184 DataFile::Parser parser(in, fn.str());
185 Loader loader(*this);
188 catch(const IO::FileNotFound &)
193 Config::Option::Option(const string &n, const string &v, const string &d):
201 Config::Loader::Loader(Config &c):
204 add("option", &Loader::option);
207 void Config::Loader::option(const string &n, const string &v)
209 conf.set_option(n, v);