]> git.tdb.fi Git - builder.git/blob - source/buildinfo.cpp
Move library mode into BuildInfo
[builder.git] / source / buildinfo.cpp
1 #include <algorithm>
2 #include <set>
3 #include <msp/strings/format.h>
4 #include "buildinfo.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 namespace {
10
11 /** Removes any duplicate entries from a list, leaving only the first one.  The
12 order of other elements is preserved.  O(nlogn) efficiency. */
13 template<typename T>
14 void unique(list<T> &l)
15 {
16         set<T> seen;
17         for(typename list<T>::iterator i=l.begin(); i!=l.end(); )
18         {
19                 if(seen.count(*i))
20                         l.erase(i++);
21                 else
22                         seen.insert(*i++);
23         }
24 }
25
26 }
27
28
29 BuildInfo::BuildInfo():
30         libmode(DYNAMIC),
31         threads(false),
32         debug(false),
33         optimize(0),
34         strip(false)
35 { }
36
37 void BuildInfo::update_from(const BuildInfo &bi, UpdateLevel level)
38 {
39         for(DefineMap::const_iterator i=bi.defines.begin(); i!=bi.defines.end(); ++i)
40                 defines[i->first] = i->second;
41         incpath.insert(incpath.end(), bi.incpath.begin(), bi.incpath.end());
42         if(level!=CHAINED)
43         {
44                 libpath.insert(libpath.end(), bi.libpath.begin(), bi.libpath.end());
45                 libs.insert(libs.end(), bi.libs.begin(), bi.libs.end());
46         }
47         warnings.insert(warnings.end(), bi.warnings.begin(), bi.warnings.end());
48         threads = bi.threads;
49         if(level==LOCAL)
50         {
51                 libmode = bi.libmode;
52                 debug = bi.debug;
53                 optimize = bi.optimize;
54                 strip = bi.strip;
55         }
56
57         unique();
58 }
59
60 void BuildInfo::unique()
61 {
62         ::unique(incpath);
63         ::unique(libpath);
64         ::unique(libs);
65
66         for(WordList::iterator i=warnings.begin(); i!=warnings.end(); ++i)
67         {
68                 bool flag = i->compare(0, 3, "no-");
69
70                 string warn = (flag ? *i : i->substr(3));
71                 string no_warn = "no-"+warn;
72
73                 for(WordList::iterator j=i; j!=warnings.end();)
74                 {
75                         if(j!=i && (*j==warn || *j==no_warn))
76                         {
77                                 flag = (*j==warn);
78                                 j = warnings.erase(j);
79                         }
80                         else
81                                 ++j;
82                 }
83
84                 *i = (flag ? warn : no_warn);
85         }
86 }
87
88
89 BuildInfo::Loader::Loader(BuildInfo &bi):
90         DataFile::ObjectLoader<BuildInfo>(bi)
91 {
92         add("debug",    &BuildInfo::debug);
93         add("define",   &Loader::define);
94         add("incpath",  &Loader::incpath);
95         add("libpath",  &Loader::libpath);
96         add("library",  &Loader::library);
97         add("libmode",  &BuildInfo::libmode);
98         add("optimize", &BuildInfo::optimize);
99         add("strip",    &BuildInfo::strip);
100         add("threads",  &BuildInfo::threads);
101         add("warning",  &Loader::warning);
102 }
103
104 void BuildInfo::Loader::incpath(const string &s)
105 {
106         obj.incpath.push_back(s);
107 }
108
109 void BuildInfo::Loader::define(const string &d, const string &v)
110 {
111         obj.defines[d] = v;
112 }
113
114 void BuildInfo::Loader::libpath(const string &s)
115 {
116         obj.libpath.push_back(s);
117 }
118
119 void BuildInfo::Loader::library(const string &s)
120 {
121         obj.libs.push_back(s);
122 }
123
124 void BuildInfo::Loader::warning(const string &s)
125 {
126         obj.warnings.push_back(s);
127 }
128
129
130 void operator>>(LexicalConverter &conv, BuildInfo::LibraryMode &libmode)
131 {
132         if(conv.get()=="FORCE_STATIC")
133                 libmode = BuildInfo::FORCE_STATIC;
134         else if(conv.get()=="STATIC")
135                 libmode = BuildInfo::STATIC;
136         else if(conv.get()=="DYNAMIC")
137                 libmode = BuildInfo::DYNAMIC;
138         else if(conv.get()=="FORCE_DYNAMIC")
139                 libmode = BuildInfo::FORCE_DYNAMIC;
140         else
141                 throw lexical_error(format("Conversion of '%s' to LibraryMode", conv.get()));
142 }