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