]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
Replace basic for loops with range-based loops or algorithms
[builder.git] / source / binary.cpp
1 #include <algorithm>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
5 #include "binary.h"
6 #include "builder.h"
7 #include "component.h"
8 #include "objectfile.h"
9 #include "sharedlibrary.h"
10 #include "sourcepackage.h"
11 #include "staticlibrary.h"
12 #include "tool.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 Binary::Binary(Builder &b, const FS::Path &p):
18         FileTarget(b, p)
19 { }
20
21 Binary::Binary(Builder &b, const Component &c, const string &p, const list<ObjectFile *> &objs):
22         FileTarget(b, c.get_package(), c.get_package().get_output_directory()/p),
23         objects(objs)
24 {
25         component = &c;
26         for(ObjectFile *o: objects)
27                 add_dependency(*o);
28
29         nested_build_sig = true;
30         arch_in_build_sig = true;
31 }
32
33 void Binary::collect_build_info(BuildInfo &binfo) const
34 {
35         for(ObjectFile *o: objects)
36                 if(const Tool *obj_tool = o->get_tool())
37                         binfo.update_from(obj_tool->get_build_info());
38
39         Target::collect_build_info(binfo);
40
41         binfo.update_from(static_binfo);
42 }
43
44 void Binary::find_dependencies()
45 {
46         if(!component)
47                 return;
48
49         list<Target *> queue;
50         list<Target *> dep_libs;
51         set<string> missing_libs;
52         queue.push_back(this);
53         for(auto j=queue.begin(); j!=queue.end(); ++j)
54         {
55                 Target *tgt = *j;
56
57                 BuildInfo binfo;
58                 tgt->collect_build_info(binfo);
59                 if(tgt!=this)
60                 {
61                         static_binfo.libpath.insert(static_binfo.libpath.end(), binfo.libpath.begin(), binfo.libpath.end());
62                         static_binfo.keep_symbols.insert(static_binfo.keep_symbols.end(), binfo.keep_symbols.begin(), binfo.keep_symbols.end());
63                         if(binfo.threads)
64                                 static_binfo.threads = true;
65                 }
66
67                 auto insert_pos = j;
68                 ++insert_pos;
69                 for(const string &l: binfo.libs)
70                 {
71                         if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
72                                 continue;
73
74                         BuildInfo::LibraryMode libmode = component->get_build_info().get_libmode_for(l);
75                         Target *lib = builder.get_vfs().find_library(l, binfo.libpath, libmode);
76                         if(lib)
77                         {
78                                 Target *real = lib->get_real_target();
79                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(real))
80                                         queue.insert(insert_pos, stlib);
81                                 else
82                                         dep_libs.push_back(lib);
83                         }
84                         else if(missing_libs.insert(l).second)
85                                 problems.push_back(format("Required library %s not found", l));
86                 }
87         }
88
89         queue.pop_front();
90         dep_libs.splice(dep_libs.begin(), queue);
91
92         /* Add only the last occurrence of each library to the actual dependencies.
93         This ensures that static library ordering is correct. */
94         for(auto i=dep_libs.begin(); i!=dep_libs.end(); ++i)
95                 if(!any_of(next(i), dep_libs.end(), [i](Target *d){ return d==*i; }))
96                         add_dependency(**i);
97 }