]> git.tdb.fi Git - builder.git/blob - source/buildinfo.cpp
Make BuildInfo uniquify things implicitly after update
[builder.git] / source / buildinfo.cpp
1 #include <algorithm>
2 #include "buildinfo.h"
3
4 using namespace std;
5 using namespace Msp;
6
7 namespace {
8
9 /** Removes any duplicate entries from a list, leaving only the first one.  The
10 order of other elements is preserved.  O(n²) efficiency. */
11 void unique(StringList &l)
12 {
13         for(StringList::iterator i=l.begin(); i!=l.end(); ++i)
14                 for(StringList::iterator j=i; j!=l.end();)
15                 {
16                         if(j!=i && *j==*i)
17                                 j = l.erase(j);
18                         else
19                                 ++j;
20                 }
21 }
22
23 }
24
25
26 BuildInfo::BuildInfo():
27         threads(false),
28         debug(false),
29         optimize(0),
30         strip(false)
31 { }
32
33 void BuildInfo::update_from(const BuildInfo &bi, UpdateLevel level)
34 {
35         for(DefineMap::const_iterator i=bi.defines.begin(); i!=bi.defines.end(); ++i)
36                 defines[i->first] = i->second;
37         incpath.insert(incpath.end(), bi.incpath.begin(), bi.incpath.end());
38         if(level!=CHAINED)
39         {
40                 libpath.insert(libpath.end(), bi.libpath.begin(), bi.libpath.end());
41                 libs.insert(libs.end(), bi.libs.begin(), bi.libs.end());
42         }
43         warnings.insert(warnings.end(), bi.warnings.begin(), bi.warnings.end());
44         threads = bi.threads;
45         if(level==LOCAL)
46         {
47                 debug = bi.debug;
48                 optimize = bi.optimize;
49                 strip = bi.strip;
50         }
51
52         unique();
53 }
54
55 void BuildInfo::unique()
56 {
57         ::unique(incpath);
58         ::unique(libpath);
59         ::unique(libs);
60
61         for(StringList::iterator i=warnings.begin(); i!=warnings.end(); ++i)
62         {
63                 bool flag = i->compare(0, 3, "no-");
64
65                 string warn = (flag ? *i : i->substr(3));
66                 string no_warn = "no-"+warn;
67
68                 for(StringList::iterator j=i; j!=warnings.end();)
69                 {
70                         if(j!=i && (*j==warn || *j==no_warn))
71                         {
72                                 flag = (*j==warn);
73                                 j = warnings.erase(j);
74                         }
75                         else
76                                 ++j;
77                 }
78
79                 *i = (flag ? warn : no_warn);
80         }
81 }
82
83
84 BuildInfo::Loader::Loader(BuildInfo &bi):
85         DataFile::ObjectLoader<BuildInfo>(bi)
86 {
87         add("debug",    &BuildInfo::debug);
88         add("define",   &Loader::define);
89         add("incpath",  &Loader::incpath);
90         add("libpath",  &Loader::libpath);
91         add("library",  &Loader::library);
92         add("optimize", &BuildInfo::optimize);
93         add("strip",    &BuildInfo::strip);
94         add("threads",  &BuildInfo::threads);
95         add("warning",  &Loader::warning);
96 }
97
98 void BuildInfo::Loader::incpath(const std::string &s)
99 {
100         obj.incpath.push_back(s);
101 }
102
103 void BuildInfo::Loader::define(const std::string &d, const std::string &v)
104 {
105         obj.defines[d] = v;
106 }
107
108 void BuildInfo::Loader::libpath(const std::string &s)
109 {
110         obj.libpath.push_back(s);
111 }
112
113 void BuildInfo::Loader::library(const std::string &s)
114 {
115         obj.libs.push_back(s);
116 }
117
118 void BuildInfo::Loader::warning(const std::string &s)
119 {
120         obj.warnings.push_back(s);
121 }