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