]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
Give targets the ability to gather their own build info
[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_output_directory()/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         nested_build_sig = true;
29         arch_in_build_sig = true;
30 }
31
32 void Binary::find_dependencies()
33 {
34         if(!component)
35                 return;
36
37         list<const Component *> queue;
38         list<Target *> dep_libs;
39         set<string> missing_libs;
40         queue.push_back(component);
41         while(!queue.empty())
42         {
43                 queue.erase(queue.begin());
44
45                 BuildInfo binfo;
46                 collect_build_info(binfo);
47
48                 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
49                 {
50                         if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
51                                 continue;
52
53                         BuildInfo::LibraryMode libmode = component->get_build_info().get_libmode_for(*i);
54                         Target *lib = builder.get_vfs().find_library(*i, binfo.libpath, libmode);
55                         if(lib)
56                         {
57                                 Target *real = lib->get_real_target();
58                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(real))
59                                 {
60                                         dep_libs.push_back(stlib);
61                                         if(stlib->get_component())
62                                                 queue.push_back(stlib->get_component());
63                                 }
64                                 else
65                                         dep_libs.push_back(lib);
66                         }
67                         else if(missing_libs.insert(*i).second)
68                                 problems.push_back(format("Required library %s not found", *i));
69                 }
70         }
71
72         /* Add only the last occurrence of each library to the actual dependencies.
73         This ensures that static library ordering is correct. */
74         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
75         {
76                 bool last = true;
77                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
78                         last = (j==i || *j!=*i);
79                 if(last)
80                         add_dependency(**i);
81         }
82 }