]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
Further changes for library compatibility
[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/format.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                                 lib = lib->get_real_target();
57                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(lib))
58                                         queue.push_back(&stlib->get_component());
59                         }
60                         else
61                                 builder.problem(comp.get_package().get_name(), format("Couldn't find library %s for %s", *i, name));
62                 }
63         }
64
65         /* Add only the last occurrence of each library to the actual dependencies.
66         This ensures that static library ordering is correct. */
67         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
68         {
69                 bool last = true;
70                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
71                         last = (j==i || *j!=*i);
72                 if(last)
73                         add_depend(*i);
74         }
75
76         deps_ready = true;
77 }
78
79 Action *Binary::create_action()
80 {
81         return new Link(builder, *this);
82 }
83
84 FS::Path Binary::generate_target_path(const Component &c)
85 {
86         const SourcePackage &pkg = c.get_package();
87         string prefix, suffix;
88         const string &sys = pkg.get_builder().get_current_arch().get_system();
89
90         if(c.get_type()==Component::LIBRARY)
91         {
92                 prefix = "lib";
93                 if(sys=="windows")
94                         suffix = ".dll";
95                 else
96                         suffix = ".so";
97         }
98         else if(c.get_type()==Component::MODULE)
99                 suffix = ".m";
100         else if(c.get_type()==Component::PROGRAM)
101         {
102                 if(sys=="windows")
103                         suffix = ".exe";
104         }
105
106         return pkg.get_out_dir()/(prefix+c.get_name()+suffix);
107 }