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