]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
e5968c0de411d8c548b0365e69099adfc276aa8a
[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         for(list<Target *>::iterator j=queue.begin(); j!=queue.end(); ++j)
53         {
54                 Target *tgt = *j;
55
56                 BuildInfo binfo;
57                 tgt->collect_build_info(binfo);
58                 if(tgt!=this)
59                         static_binfo.libpath.insert(static_binfo.libpath.end(), binfo.libpath.begin(), binfo.libpath.end());
60
61                 list<Target *>::iterator insert_pos = j;
62                 ++insert_pos;
63                 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
64                 {
65                         if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
66                                 continue;
67
68                         BuildInfo::LibraryMode libmode = component->get_build_info().get_libmode_for(*i);
69                         Target *lib = builder.get_vfs().find_library(*i, binfo.libpath, libmode);
70                         if(lib)
71                         {
72                                 Target *real = lib->get_real_target();
73                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(real))
74                                         queue.insert(insert_pos, stlib);
75                                 else
76                                         dep_libs.push_back(lib);
77                         }
78                         else if(missing_libs.insert(*i).second)
79                                 problems.push_back(format("Required library %s not found", *i));
80                 }
81         }
82
83         queue.pop_front();
84         dep_libs.splice(dep_libs.begin(), queue);
85
86         /* Add only the last occurrence of each library to the actual dependencies.
87         This ensures that static library ordering is correct. */
88         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
89         {
90                 bool last = true;
91                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
92                         last = (j==i || *j!=*i);
93                 if(last)
94                         add_dependency(**i);
95         }
96 }