]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
Collect libpath for static library dependencies from the libs themselves
[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::collect_build_info(BuildInfo &binfo) const
33 {
34         for(list<ObjectFile *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
35                 if(const Tool *obj_tool = (*i)->get_tool())
36                         binfo.update_from(obj_tool->get_build_info());
37
38         Target::collect_build_info(binfo);
39
40         binfo.update_from(static_binfo);
41 }
42
43 void Binary::find_dependencies()
44 {
45         if(!component)
46                 return;
47
48         list<Target *> queue;
49         list<Target *> dep_libs;
50         set<string> missing_libs;
51         queue.push_back(this);
52         while(!queue.empty())
53         {
54                 Target *tgt = queue.front();
55                 queue.pop_front();
56
57                 BuildInfo binfo;
58                 tgt->collect_build_info(binfo);
59                 if(tgt!=this)
60                         static_binfo.libpath.insert(static_binfo.libpath.end(), binfo.libpath.begin(), binfo.libpath.end());
61
62                 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
63                 {
64                         if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
65                                 continue;
66
67                         BuildInfo::LibraryMode libmode = component->get_build_info().get_libmode_for(*i);
68                         Target *lib = builder.get_vfs().find_library(*i, binfo.libpath, libmode);
69                         if(lib)
70                         {
71                                 Target *real = lib->get_real_target();
72                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(real))
73                                 {
74                                         dep_libs.push_back(stlib);
75                                         queue.push_back(stlib);
76                                 }
77                                 else
78                                         dep_libs.push_back(lib);
79                         }
80                         else if(missing_libs.insert(*i).second)
81                                 problems.push_back(format("Required library %s not found", *i));
82                 }
83         }
84
85         /* Add only the last occurrence of each library to the actual dependencies.
86         This ensures that static library ordering is correct. */
87         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
88         {
89                 bool last = true;
90                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
91                         last = (j==i || *j!=*i);
92                 if(last)
93                         add_dependency(**i);
94         }
95 }