]> git.tdb.fi Git - builder.git/blob - source/config.cpp
Externalize dry run handling from Config and DependencyCache
[builder.git] / source / config.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/io/file.h>
5 #include <msp/io/print.h>
6 #include <msp/time/utils.h>
7 #include "builder.h"
8 #include "config.h"
9 #include "sourcepackage.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 Config::Config(SourcePackage &p):
15         package(p),
16         freeze_mtime(false),
17         changed(false)
18 { }
19
20 void Config::add_option(const string &n, const string &v, const string &d)
21 {
22         options.insert(OptionMap::value_type(n, Option(n, v, d)));
23 }
24
25 const Config::Option &Config::get_option(const string &name) const
26 {
27         return get_item(options, name);
28 }
29
30 bool Config::is_option(const string &name) const
31 {
32         return options.count(name);
33 }
34
35 void Config::select_last_profile()
36 {
37         FS::Path profile_cache_fn = package.get_source()/".profile";
38         if(FS::exists(profile_cache_fn))
39         {
40                 IO::BufferedFile in(profile_cache_fn.str());
41                 string profile;
42                 in.getline(profile);
43                 set_option("profile", profile);
44         }
45
46         freeze_mtime = true;
47         package.get_builder().apply_profile_template(*this, get_option("profile").value);
48         freeze_mtime = false;
49
50         load();
51 }
52
53 void Config::select_profile(const string &profile)
54 {
55         set_option("profile", profile);
56
57         freeze_mtime = true;
58         package.get_builder().apply_profile_template(*this, profile);
59         freeze_mtime = false;
60
61         load();
62 }
63
64 bool Config::update(const StringMap &opts)
65 {
66         bool changed_now = false;
67         for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
68         {
69                 if(set_option(i->first, i->second) && i->first!="profile")
70                         changed_now = true;
71         }
72
73         if(changed_now && !freeze_mtime)
74         {
75                 mtime = Time::now();
76                 changed = true;
77         }
78
79         return changed_now;
80 }
81
82 void Config::finish()
83 {
84         for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
85                 i->second.value = package.expand_string(i->second.value);
86 }
87
88 void Config::save() const
89 {
90         if(!changed)
91                 return;
92
93         FS::Path fn = package.get_source()/".options";
94
95         OptionMap::const_iterator i = options.find("profile");
96         if(i!=options.end())
97         {
98                 fn = package.get_source()/(".options."+i->second.value);
99
100                 IO::BufferedFile profile_out((package.get_source()/".profile").str(), IO::M_WRITE);
101                 IO::print(profile_out, "%s\n", i->second.value);
102         }
103
104         IO::BufferedFile out(fn.str(), IO::M_WRITE);
105
106         for(i=options.begin(); i!=options.end(); ++i)
107                 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
108 }
109
110 bool Config::set_option(const string &opt, const string &val)
111 {
112         bool result = false;
113
114         OptionMap::iterator i = options.find(opt);
115         if(i!=options.end())
116         {
117                 if(i->second.value!=val)
118                         result = true;
119                 i->second.value = val;
120         }
121
122         return result;
123 }
124
125 void Config::load()
126 {
127         FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
128
129         FS::Stat stat = FS::stat(fn);
130         if(stat)
131         {
132                 IO::BufferedFile in(fn.str());
133
134                 mtime = stat.get_modify_time();
135
136                 DataFile::Parser parser(in, fn.str());
137                 Loader loader(*this);
138                 loader.load(parser);
139         }
140 }
141
142
143 Config::Option::Option(const string &n, const string &v, const string &d):
144         name(n),
145         defv(v),
146         descr(d),
147         value(v)
148 { }
149
150
151 Config::Loader::Loader(Config &c):
152         DataFile::ObjectLoader<Config>(c)
153 {
154         add("option", &Loader::option);
155 }
156
157 void Config::Loader::option(const string &n, const string &v)
158 {
159         obj.set_option(n, v);
160 }