]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
Track build options to rebuild primary targets when build type changes
[builder.git] / source / binary.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "binary.h"
5 #include "builder.h"
6 #include "component.h"
7 #include "objectfile.h"
8 #include "sharedlibrary.h"
9 #include "sourcepackage.h"
10 #include "staticlibrary.h"
11 #include "tool.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 Binary::Binary(Builder &b, const FS::Path &p):
17         FileTarget(b, p)
18 { }
19
20 Binary::Binary(Builder &b, const Component &c, const string &p, const list<ObjectFile *> &objs):
21         FileTarget(b, c.get_package(), c.get_package().get_out_dir()/p),
22         objects(objs)
23 {
24         component = &c;
25         for(list<ObjectFile *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
26                 add_dependency(**i);
27 }
28
29 void Binary::find_dependencies()
30 {
31         if(!component)
32                 return;
33
34         BuildInfo::LibraryMode libmode = component->get_build_info().libmode;
35
36         list<const Component *> queue;
37         list<Target *> dep_libs;
38         queue.push_back(component);
39         while(!queue.empty())
40         {
41                 const Component *c = queue.front();
42                 queue.erase(queue.begin());
43
44                 const BuildInfo &binfo = c->get_build_info();
45                 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
46                 {
47                         Target *lib = builder.get_vfs().find_library(*i, binfo.libpath, libmode);
48                         if(lib)
49                         {
50                                 dep_libs.push_back(lib);
51
52                                 lib = lib->get_real_target();
53                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(lib))
54                                         if(stlib->get_component())
55                                                 queue.push_back(stlib->get_component());
56                         }
57                         else
58                                 builder.problem(package->get_name(), format("Couldn't find library %s for %s", *i, name));
59                 }
60         }
61
62         /* Add only the last occurrence of each library to the actual dependencies.
63         This ensures that static library ordering is correct. */
64         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
65         {
66                 bool last = true;
67                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
68                         last = (j==i || *j!=*i);
69                 if(last)
70                         add_dependency(**i);
71         }
72 }
73
74 string Binary::create_build_signature() const
75 {
76         set<const Tool *> object_tools;
77         for(list<ObjectFile *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
78                 object_tools.insert((*i)->get_tool());
79
80         list<string> sigs;
81         sigs.push_back(tool->create_build_signature(component->get_build_info()));
82         for(set<const Tool *>::const_iterator i=object_tools.begin(); i!=object_tools.end(); ++i)
83                 sigs.push_back((*i)->create_build_signature(component->get_build_info()));
84         sigs.sort();
85
86         return join(sigs.begin(), sigs.end(), ";");
87 }