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