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/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):
29 Adds a configuration option.
32 @param v Default value
35 void Config::add_option(const string &n, const string &v, const string &d)
37 options.insert(OptionMap::value_type(n, Option(n, v, d)));
41 Gets the given option from the configuration. If the option doesn't exist,
42 an Exception is thrown.
44 const Config::Option &Config::get_option(const string &name) const
46 OptionMap::const_iterator i=options.find(name);
48 throw Exception("Tried to access nonexistent option "+name);
54 Checks whether an option with the given name exists.
56 bool Config::is_option(const string &name) const
58 return options.count(name);
62 Selects the last profile used. If the profile cache file is not present, the
63 default profile is assumed.
65 void Config::select_last_profile()
69 IO::File in((package.get_source()/".profile").str());
72 set_option("profile", profile);
74 catch(const IO::FileNotFound &)
78 package.get_builder().apply_profile_template(*this, get_option("profile").value);
85 Selects the given profile. The profile cache file is updated as well, unless
88 void Config::select_profile(const string &profile)
90 set_option("profile", profile);
92 if(!package.get_builder().get_dry_run())
94 IO::File out((package.get_source()/".profile").str(), IO::M_WRITE);
95 IO::print(out, "%s\n", profile);
99 package.get_builder().apply_profile_template(*this, profile);
106 Processes options from the given raw option map. Nonexistent options are
107 ignored. If any options were changed, the mtime of the configuration is updated
110 @param opts A map to process options from
112 @return Whether any option values were changed
114 bool Config::update(const StringMap &opts)
117 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
119 if(set_option(i->first, i->second) && i->first!="profile")
123 if(changed && !freeze_mtime)
130 Expands any variable references in options.
132 void Config::finish()
134 for(unsigned n=0; n<20; ++n)
137 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
139 Option &opt=i->second;
141 while((dollar=opt.value.find('$', dollar))!=string::npos)
145 if(opt.value[dollar+1]=='{')
147 end=opt.value.find('}', dollar+2);
148 if(end==string::npos)
149 throw Exception("Unterminated variable reference");
150 var=opt.value.substr(dollar+2, end-dollar-2);
155 for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end) ;
156 var=opt.value.substr(dollar+1, end-dollar-1);
161 value=get_option(var).value;
163 value=package.get_builder().get_current_arch().get_name();
164 else if(const char *ptr=getenv(var.c_str()))
167 opt.value.replace(dollar, end-dollar, value);
169 dollar+=value.size();
179 void Config::save() const
181 FS::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");
187 IO::File out(fn.str(), IO::M_WRITE);
189 for(i=options.begin(); i!=options.end(); ++i)
190 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
193 bool Config::set_option(const string &opt, const string &val)
197 OptionMap::iterator i=options.find(opt);
200 if(i->second.value!=val)
210 FS::Path fn=package.get_source()/(".options."+get_option("profile").value);
214 IO::File inf(fn.str());
215 IO::Buffered in(inf);
217 mtime=Time::TimeStamp::from_unixtime(FS::stat(fn).st_mtime);
219 DataFile::Parser parser(in, fn.str());
220 Loader loader(*this);
223 catch(const IO::FileNotFound &)
228 Config::Option::Option(const string &n, const string &v, const string &d):
236 Config::Loader::Loader(Config &c):
239 add("option", &Loader::option);
242 void Config::Loader::option(const string &n, const string &v)
244 conf.set_option(n, v);