1 #include <msp/core/algorithm.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
8 #include "objectfile.h"
9 #include "sharedlibrary.h"
10 #include "sourcepackage.h"
11 #include "staticlibrary.h"
17 Binary::Binary(Builder &b, const Component &c, const string &p, const vector<ObjectFile *> &objs):
18 FileTarget(b, c.get_package(), c.get_package().get_output_directory()/p),
22 for(ObjectFile *o: objects)
25 nested_build_sig = true;
26 arch_in_build_sig = true;
29 void Binary::collect_build_info(BuildInfo &binfo) const
31 vector<const Tool *> tools;
32 for(ObjectFile *o: objects)
33 if(const Tool *obj_tool = o->get_tool())
34 if(!any_equals(tools, obj_tool))
35 tools.push_back(obj_tool);
36 for(const Tool *t: tools)
37 binfo.update_from(t->get_build_info());
39 Target::collect_build_info(binfo);
41 binfo.update_from(static_binfo);
44 void Binary::find_dependencies()
49 vector<Target *> static_libs;
50 vector<Target *> shared_libs;
51 vector<string> missing_libs;
52 find_dependencies(this, static_libs, shared_libs, missing_libs);
54 for(Target *t: static_libs)
56 for(Target *t: shared_libs)
58 for(const string &m: missing_libs)
59 problems.push_back(format("Required library %s not found", m));
62 void Binary::find_dependencies(Target *tgt, vector<Target *> &static_libs, vector<Target *> &shared_libs, vector<string> &missing_libs)
65 tgt->collect_build_info(binfo);
68 static_binfo.libpath.insert(static_binfo.libpath.end(), binfo.libpath.begin(), binfo.libpath.end());
69 static_binfo.keep_symbols.insert(static_binfo.keep_symbols.end(), binfo.keep_symbols.begin(), binfo.keep_symbols.end());
71 static_binfo.threads = true;
74 for(const string &l: binfo.libs)
76 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
79 BuildInfo::LibraryMode libmode = binfo.get_libmode_for(l);
80 Target *lib = builder.get_vfs().find_library(l, binfo.libpath, libmode);
83 Target *real = lib->get_real_target();
84 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(real))
86 /* Keep only the last occurrence of each static library. This
87 ensures the order is correct for linking. */
88 auto i = find(static_libs, stlib);
89 if(i!=static_libs.end())
91 static_libs.push_back(stlib);
92 find_dependencies(stlib, static_libs, shared_libs, missing_libs);
94 else if(!any_equals(shared_libs, lib))
95 shared_libs.push_back(lib);
97 else if(!any_equals(missing_libs, l))
98 missing_libs.push_back(l);