]> git.tdb.fi Git - builder.git/blob - source/condition.cpp
Rewrite the architecture system
[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, "!0"));
29                 else if(token==0 && (*i)[0]=='!')
30                         expression.insert(StringMap::value_type(*i, "=0"));
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                         match = (conf.get_option(i->first).value==value);
52                 else if(i->first=="arch")
53                         match = pkg.get_builder().get_current_arch().match_name(value);
54
55                 if(match==neg)
56                         result = false;
57         }
58
59         return result;
60 }
61
62
63 Condition::Loader::Loader(Condition &c):
64         cond(c)
65 {
66         add("require",    &Loader::require);
67         add("build_info", &Loader::build_info);
68 }
69
70 void Condition::Loader::require(const string &pkg)
71 {
72         cond.requires.push_back(pkg);
73 }
74
75 void Condition::Loader::build_info()
76 {
77         load_sub(cond.build_info);
78 }