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/file.h>
10 #include <msp/io/print.h>
11 #include <msp/path/utils.h>
12 #include <msp/time/utils.h>
15 #include "sourcepackage.h"
20 Config::Config(SourcePackage &p):
26 Adds a configuration option.
29 @param v Default value
32 void Config::add_option(const string &n, const string &v, const string &d)
34 options.insert(OptionMap::value_type(n, Option(n, v, d)));
38 Gets the given option from the configuration. If the option doesn't exist,
39 an Exception is thrown.
41 const Config::Option &Config::get_option(const string &name) const
43 OptionMap::const_iterator i=options.find(name);
45 throw Exception("Tried to access nonexistent option "+name);
51 Checks whether an option with the given name exists.
53 bool Config::is_option(const string &name) const
55 return options.count(name);
59 Selects the last profile used. If the profile cache file is not present, the
60 default profile is assumed.
62 void Config::select_last_profile()
66 IO::File in((package.get_source()/".profile.cache").str());
69 set_option("profile", profile);
71 catch(const SystemError &)
75 package.get_builder().apply_profile_template(*this, get_option("profile").value);
82 Selects the given profile. The profile cache file is updated as well, unless
85 void Config::select_profile(const string &profile)
87 set_option("profile", profile);
89 if(!package.get_builder().get_dry_run())
93 IO::File out((package.get_source()/".profile.cache").str().c_str());
94 IO::print(out, "%s\n", profile);
96 catch(const SystemError &)
101 package.get_builder().apply_profile_template(*this, profile);
108 Processes options from the given raw option map. Nonexistent options are
109 ignored. If any options were changed, the mtime of the configuration is updated
112 @param opts A map to process options from
114 @return Whether any option values were changed
116 bool Config::update(const StringMap &opts)
119 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
121 if(set_option(i->first, i->second) && i->first!="profile")
125 if(changed && !freeze_mtime)
132 Expands any variable references in options.
134 void Config::finish()
136 for(unsigned n=0; n<20; ++n)
139 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
141 Option &opt=i->second;
143 while((dollar=opt.value.find('$', dollar))!=string::npos)
147 if(opt.value[dollar+1]=='{')
149 end=opt.value.find('}', dollar+2);
150 if(end==string::npos)
151 throw Exception("Unterminated variable reference");
152 var=opt.value.substr(dollar+2, end-dollar-2);
157 for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end);
158 var=opt.value.substr(dollar+1, end-dollar-1);
163 value=get_option(var).value;
165 value=getenv(var.c_str());
167 opt.value.replace(dollar, end-dollar, value);
169 dollar+=value.size();
179 void Config::save() const
181 Path fn=package.get_source()/".options.cache";
183 OptionMap::const_iterator i=options.find("profile");
185 fn=package.get_source()/(".options."+i->second.value+".cache");
189 IO::File out(fn.str());
191 for(i=options.begin(); i!=options.end(); ++i)
192 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
194 catch(const SystemError &)
198 bool Config::set_option(const string &opt, const string &val)
202 OptionMap::iterator i=options.find(opt);
205 if(i->second.value!=val)
215 Path fn=package.get_source()/".options.cache";
217 OptionMap::iterator i=options.find("profile");
219 fn=package.get_source()/(".options."+i->second.value+".cache");
223 IO::File in(fn.str());
225 mtime=Time::TimeStamp::from_unixtime(stat(fn).st_mtime);
227 DataFile::Parser parser(in, fn.str());
228 Loader loader(*this);
231 catch(const SystemError &)
236 Config::Option::Option(const string &n, const string &v, const string &d):
244 Config::Loader::Loader(Config &c):
247 add("option", &Loader::option);
250 void Config::Loader::option(const string &n, const string &v)
252 conf.set_option(n, v);