3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/core/except.h>
10 #include <msp/io/except.h>
11 #include <msp/io/file.h>
12 #include <msp/io/print.h>
13 #include <msp/path/utils.h>
14 #include <msp/time/utils.h>
17 #include "sourcepackage.h"
22 Config::Config(SourcePackage &p):
28 Adds a configuration option.
31 @param v Default value
34 void Config::add_option(const string &n, const string &v, const string &d)
36 options.insert(OptionMap::value_type(n, Option(n, v, d)));
40 Gets the given option from the configuration. If the option doesn't exist,
41 an Exception is thrown.
43 const Config::Option &Config::get_option(const string &name) const
45 OptionMap::const_iterator i=options.find(name);
47 throw Exception("Tried to access nonexistent option "+name);
53 Checks whether an option with the given name exists.
55 bool Config::is_option(const string &name) const
57 return options.count(name);
61 Selects the last profile used. If the profile cache file is not present, the
62 default profile is assumed.
64 void Config::select_last_profile()
68 IO::File in((package.get_source()/".profile.cache").str());
71 set_option("profile", profile);
73 catch(const IO::FileNotFound &)
77 package.get_builder().apply_profile_template(*this, get_option("profile").value);
84 Selects the given profile. The profile cache file is updated as well, unless
87 void Config::select_profile(const string &profile)
89 set_option("profile", profile);
91 if(!package.get_builder().get_dry_run())
93 IO::File out((package.get_source()/".profile.cache").str(), IO::M_WRITE);
94 IO::print(out, "%s\n", profile);
98 package.get_builder().apply_profile_template(*this, profile);
105 Processes options from the given raw option map. Nonexistent options are
106 ignored. If any options were changed, the mtime of the configuration is updated
109 @param opts A map to process options from
111 @return Whether any option values were changed
113 bool Config::update(const StringMap &opts)
116 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
118 if(set_option(i->first, i->second) && i->first!="profile")
122 if(changed && !freeze_mtime)
129 Expands any variable references in options.
131 void Config::finish()
133 for(unsigned n=0; n<20; ++n)
136 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
138 Option &opt=i->second;
140 while((dollar=opt.value.find('$', dollar))!=string::npos)
144 if(opt.value[dollar+1]=='{')
146 end=opt.value.find('}', dollar+2);
147 if(end==string::npos)
148 throw Exception("Unterminated variable reference");
149 var=opt.value.substr(dollar+2, end-dollar-2);
154 for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end) ;
155 var=opt.value.substr(dollar+1, end-dollar-1);
160 value=get_option(var).value;
162 value=package.get_builder().get_current_arch().get_name();
163 else if(const char *ptr=getenv(var.c_str()))
166 opt.value.replace(dollar, end-dollar, value);
168 dollar+=value.size();
178 void Config::save() const
180 Path fn=package.get_source()/".options.cache";
182 OptionMap::const_iterator i=options.find("profile");
184 fn=package.get_source()/(".options."+i->second.value+".cache");
186 IO::File out(fn.str(), IO::M_WRITE);
188 for(i=options.begin(); i!=options.end(); ++i)
189 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
192 bool Config::set_option(const string &opt, const string &val)
196 OptionMap::iterator i=options.find(opt);
199 if(i->second.value!=val)
209 Path fn=package.get_source()/".options.cache";
211 OptionMap::iterator i=options.find("profile");
213 fn=package.get_source()/(".options."+i->second.value+".cache");
217 IO::File inf(fn.str());
218 IO::Buffered in(inf);
220 mtime=Time::TimeStamp::from_unixtime(stat(fn).st_mtime);
222 DataFile::Parser parser(in, fn.str());
223 Loader loader(*this);
226 catch(const IO::FileNotFound &)
231 Config::Option::Option(const string &n, const string &v, const string &d):
239 Config::Loader::Loader(Config &c):
242 add("option", &Loader::option);
245 void Config::Loader::option(const string &n, const string &v)
247 conf.set_option(n, v);