]> git.tdb.fi Git - builder.git/blob - source/condition.cpp
Change arch and prefix to global options
[builder.git] / source / condition.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 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                 unsigned 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
48                 string value;
49                 if(conf.is_option(i->first))
50                         value=conf.get_option(i->first).value;
51                 else if(i->first=="arch")
52                         value=pkg.get_builder().get_current_arch().get_name();
53
54                 if((value==i->second.substr(start))==neg)
55                         result=false;
56         }
57
58         return result;
59 }
60
61
62 Condition::Loader::Loader(Condition &c):
63         cond(c)
64 {
65         add("require",    &Loader::require);
66         add("build_info", &Loader::build_info);
67 }
68
69 void Condition::Loader::require(const string &pkg)
70 {
71         cond.requires.push_back(pkg);
72 }
73
74 void Condition::Loader::build_info()
75 {
76         load_sub(cond.build_info);
77 }