]> git.tdb.fi Git - builder.git/blob - source/condition.cpp
0b020ea5d74e247414368e58ac032b15c676a1d4
[builder.git] / source / condition.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/strings/utils.h>
9 #include "builder.h"
10 #include "condition.h"
11 #include "sourcepackage.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 Condition::Condition(SourcePackage &p, const string &expr):
17         pkg(p)
18 {
19         vector<string> parts = split(expr);
20
21         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
22         {
23                 if(*i=="and")
24                         continue;
25
26                 string::size_type token = i->find_first_of("=!");
27                 if(token==string::npos)
28                         expression.insert(StringMap::value_type(*i, "="));
29                 else if(token==0 && (*i)[0]=='!')
30                         expression.insert(StringMap::value_type(i->substr(1), "!"));
31                 else
32                         expression.insert(StringMap::value_type(i->substr(0, token), i->substr(token)));
33         }
34 }
35
36 bool Condition::eval()
37 {
38         const Config &conf = pkg.get_config();
39
40         bool result = true;
41         for(StringMap::iterator i=expression.begin(); i!=expression.end(); ++i)
42         {
43                 bool neg = (i->second[0]=='!');
44                 unsigned start = 1;
45                 if(i->second[1]=='=')
46                         ++start;
47                 string value = i->second.substr(start);
48
49                 bool match = false;
50                 if(conf.is_option(i->first))
51                 {
52                         if(value.empty())
53                                 match = lexical_cast<bool>(conf.get_option(i->first).value);
54                         else
55                                 match = (conf.get_option(i->first).value==value);
56                 }
57                 else if(i->first=="arch")
58                         match = pkg.get_builder().get_current_arch().match_name(value);
59
60                 if(match==neg)
61                         result = false;
62         }
63
64         return result;
65 }
66
67
68 Condition::Loader::Loader(Condition &c):
69         cond(c)
70 {
71         add("require",    &Loader::require);
72         add("build_info", &Loader::build_info);
73 }
74
75 void Condition::Loader::require(const string &pkg)
76 {
77         cond.requires.push_back(pkg);
78 }
79
80 void Condition::Loader::build_info()
81 {
82         load_sub(cond.build_info);
83 }