3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/except.h>
9 #include <msp/io/except.h>
10 #include <msp/io/file.h>
11 #include <msp/io/print.h>
12 #include <msp/path/utils.h>
13 #include <msp/time/utils.h>
16 #include "sourcepackage.h"
21 Config::Config(SourcePackage &p):
27 Adds a configuration option.
30 @param v Default value
33 void Config::add_option(const string &n, const string &v, const string &d)
35 options.insert(OptionMap::value_type(n, Option(n, v, d)));
39 Gets the given option from the configuration. If the option doesn't exist,
40 an Exception is thrown.
42 const Config::Option &Config::get_option(const string &name) const
44 OptionMap::const_iterator i=options.find(name);
46 throw Exception("Tried to access nonexistent option "+name);
52 Checks whether an option with the given name exists.
54 bool Config::is_option(const string &name) const
56 return options.count(name);
60 Selects the last profile used. If the profile cache file is not present, the
61 default profile is assumed.
63 void Config::select_last_profile()
67 IO::File in((package.get_source()/".profile.cache").str());
70 set_option("profile", profile);
72 catch(const IO::FileNotFound &)
76 package.get_builder().apply_profile_template(*this, get_option("profile").value);
83 Selects the given profile. The profile cache file is updated as well, unless
86 void Config::select_profile(const string &profile)
88 set_option("profile", profile);
90 if(!package.get_builder().get_dry_run())
92 IO::File out((package.get_source()/".profile.cache").str(), IO::M_WRITE);
93 IO::print(out, "%s\n", profile);
97 package.get_builder().apply_profile_template(*this, profile);
104 Processes options from the given raw option map. Nonexistent options are
105 ignored. If any options were changed, the mtime of the configuration is updated
108 @param opts A map to process options from
110 @return Whether any option values were changed
112 bool Config::update(const StringMap &opts)
115 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
117 if(set_option(i->first, i->second) && i->first!="profile")
121 if(changed && !freeze_mtime)
128 Expands any variable references in options.
130 void Config::finish()
132 for(unsigned n=0; n<20; ++n)
135 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
137 Option &opt=i->second;
139 while((dollar=opt.value.find('$', dollar))!=string::npos)
143 if(opt.value[dollar+1]=='{')
145 end=opt.value.find('}', dollar+2);
146 if(end==string::npos)
147 throw Exception("Unterminated variable reference");
148 var=opt.value.substr(dollar+2, end-dollar-2);
153 for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end);
154 var=opt.value.substr(dollar+1, end-dollar-1);
159 value=get_option(var).value;
161 value=getenv(var.c_str());
163 opt.value.replace(dollar, end-dollar, value);
165 dollar+=value.size();
175 void Config::save() const
177 Path fn=package.get_source()/".options.cache";
179 OptionMap::const_iterator i=options.find("profile");
181 fn=package.get_source()/(".options."+i->second.value+".cache");
183 IO::File out(fn.str(), IO::M_WRITE);
185 for(i=options.begin(); i!=options.end(); ++i)
186 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
189 bool Config::set_option(const string &opt, const string &val)
193 OptionMap::iterator i=options.find(opt);
196 if(i->second.value!=val)
206 Path fn=package.get_source()/".options.cache";
208 OptionMap::iterator i=options.find("profile");
210 fn=package.get_source()/(".options."+i->second.value+".cache");
214 IO::File in(fn.str());
216 mtime=Time::TimeStamp::from_unixtime(stat(fn).st_mtime);
218 DataFile::Parser parser(in, fn.str());
219 Loader loader(*this);
222 catch(const SystemError &)
227 Config::Option::Option(const string &n, const string &v, const string &d):
235 Config::Loader::Loader(Config &c):
238 add("option", &Loader::option);
241 void Config::Loader::option(const string &n, const string &v)
243 conf.set_option(n, v);