3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/core/error.h>
10 #include <msp/path/utils.h>
11 #include <msp/time/utils.h>
14 #include "sourcepackage.h"
19 Config::Config(SourcePackage &p):
25 Adds a configuration option.
28 @param v Default value
31 void Config::add_option(const string &n, const string &v, const string &d)
33 options.insert(OptionMap::value_type(n, Option(n, v, d)));
37 Gets the given option from the configuration. If the option doesn't exist,
38 an Exception is thrown.
40 const Config::Option &Config::get_option(const string &name) const
42 OptionMap::const_iterator i=options.find(name);
44 throw Exception("Tried to access nonexistent option "+name);
50 Checks whether an option with the given name exists.
52 bool Config::is_option(const string &name) const
54 return options.count(name);
58 Selects the last profile used. If the profile cache file is not present, the
59 default profile is assumed.
61 void Config::select_last_profile()
63 ifstream in((package.get_source()/".profile.cache").str().c_str());
68 set_option("profile", profile);
72 package.get_builder().apply_profile_template(*this, get_option("profile").value);
79 Selects the given profile. The profile cache file is updated as well, unless
82 void Config::select_profile(const string &profile)
84 set_option("profile", profile);
86 if(!package.get_builder().get_dry_run())
88 ofstream out((package.get_source()/".profile.cache").str().c_str());
94 package.get_builder().apply_profile_template(*this, profile);
101 Processes options from the given raw option map. Nonexistent options are
102 ignored. If any options were changed, the mtime of the configuration is updated
105 @param opts A map to process options from
107 @return Whether any option values were changed
109 bool Config::update(const StringMap &opts)
112 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
114 if(set_option(i->first, i->second) && i->first!="profile")
118 if(changed && !freeze_mtime)
125 Expands any variable references in options.
127 void Config::finish()
129 for(unsigned n=0; n<20; ++n)
132 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
134 Option &opt=i->second;
136 while((dollar=opt.value.find('$', dollar))!=string::npos)
140 if(opt.value[dollar+1]=='{')
142 end=opt.value.find('}', dollar+2);
143 if(end==string::npos)
144 throw Exception("Unterminated variable reference");
145 var=opt.value.substr(dollar+2, end-dollar-2);
150 for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end);
151 var=opt.value.substr(dollar+1, end-dollar-1);
156 value=get_option(var).value;
158 value=getenv(var.c_str());
160 opt.value.replace(dollar, end-dollar, value);
162 dollar+=value.size();
172 void Config::save() const
174 Path::Path fn=package.get_source()/".options.cache";
176 OptionMap::const_iterator i=options.find("profile");
178 fn=package.get_source()/(".options."+i->second.value+".cache");
180 ofstream out(fn.str().c_str());
183 for(i=options.begin(); i!=options.end(); ++i)
184 out<<"option \""<<i->second.name<<"\" \""<<i->second.value<<"\";\n";
187 bool Config::set_option(const string &opt, const string &val)
191 OptionMap::iterator i=options.find(opt);
194 if(i->second.value!=val)
204 Path::Path fn=package.get_source()/".options.cache";
206 OptionMap::iterator i=options.find("profile");
208 fn=package.get_source()/(".options."+i->second.value+".cache");
210 ifstream in(fn.str().c_str());
215 mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
217 DataFile::Parser parser(in, fn.str());
218 Loader loader(*this);
223 Config::Option::Option(const string &n, const string &v, const string &d):
231 Config::Loader::Loader(Config &c):
234 add("option", &Loader::option);
237 void Config::Loader::option(const string &n, const string &v)
239 conf.set_option(n, v);