]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
Replace per-file copyright notices with a single file
[builder.git] / source / binary.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/strings/format.h>
3 #include "binary.h"
4 #include "builder.h"
5 #include "component.h"
6 #include "install.h"
7 #include "link.h"
8 #include "objectfile.h"
9 #include "package.h"
10 #include "sharedlibrary.h"
11 #include "staticlibrary.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 Binary::Binary(Builder &b, const Component &c, const list<ObjectFile *> &objs):
17         FileTarget(b, &c.get_package(), generate_target_path(c)),
18         comp(c)
19 {
20         buildable = true;
21         for(list<ObjectFile *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
22                 add_depend(*i);
23 }
24
25 void Binary::find_depends()
26 {
27         LibMode libmode = comp.get_package().get_library_mode();
28         if(dynamic_cast<SharedLibrary *>(this))
29                 libmode = DYNAMIC;
30
31         list<const Component *> queue;
32         list<Target *> dep_libs;
33         queue.push_back(&comp);
34         while(!queue.empty())
35         {
36                 const Component *c = queue.front();
37                 queue.erase(queue.begin());
38
39                 const StringList &libpath = c->get_build_info().libpath;
40
41                 const list<string> &libs = c->get_build_info().libs;
42                 for(StringList::const_iterator i=libs.begin(); i!=libs.end(); ++i)
43                 {
44                         Target *lib = builder.get_library(*i, libpath, libmode);
45                         if(lib)
46                         {
47                                 dep_libs.push_back(lib);
48
49                                 lib = lib->get_real_target();
50                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(lib))
51                                         queue.push_back(&stlib->get_component());
52                         }
53                         else
54                                 builder.problem(comp.get_package().get_name(), format("Couldn't find library %s for %s", *i, name));
55                 }
56         }
57
58         /* Add only the last occurrence of each library to the actual dependencies.
59         This ensures that static library ordering is correct. */
60         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
61         {
62                 bool last = true;
63                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
64                         last = (j==i || *j!=*i);
65                 if(last)
66                         add_depend(*i);
67         }
68
69         deps_ready = true;
70 }
71
72 Action *Binary::create_action()
73 {
74         return new Link(builder, *this);
75 }
76
77 FS::Path Binary::generate_target_path(const Component &c)
78 {
79         const SourcePackage &pkg = c.get_package();
80         string prefix, suffix;
81         const string &sys = pkg.get_builder().get_current_arch().get_system();
82
83         if(c.get_type()==Component::LIBRARY)
84         {
85                 prefix = "lib";
86                 if(sys=="windows")
87                         suffix = ".dll";
88                 else
89                         suffix = ".so";
90         }
91         else if(c.get_type()==Component::MODULE)
92                 suffix = ".m";
93         else if(c.get_type()==Component::PROGRAM)
94         {
95                 if(sys=="windows")
96                         suffix = ".exe";
97         }
98
99         return pkg.get_out_dir()/(prefix+c.get_name()+suffix);
100 }