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