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