]> git.tdb.fi Git - builder.git/blob - source/executable.cpp
0cae259f8b7b755bf74da0913a516ecafbd7d09a
[builder.git] / source / executable.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/path/utils.h>
9 #include <msp/strings/formatter.h>
10 #include "builder.h"
11 #include "component.h"
12 #include "executable.h"
13 #include "install.h"
14 #include "link.h"
15 #include "objectfile.h"
16 #include "package.h"
17 #include "staticlibrary.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 Executable::Executable(Builder &b, const Component &c, const list<ObjectFile *> &objs):
23         Target(b, &c.get_package(), generate_target_name(c)),
24         comp(c)
25 {
26         buildable=true;
27         for(list<ObjectFile *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
28                 add_depend(*i);
29 }
30
31 /**
32 Finds and adds any required libraries to the dependencies.
33 */
34 void Executable::find_depends()
35 {
36         LibMode libmode=comp.get_package().get_library_mode();
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                 const string &arch=c->get_package().get_arch();
48
49                 const list<string> &libs=c->get_build_info().libs;
50                 for(StringList::const_iterator i=libs.begin(); i!=libs.end(); ++i)
51                 {
52                         Target *lib=builder.get_library(*i, arch, libpath, libmode);
53                         if(lib)
54                         {
55                                 dep_libs.push_back(lib);
56
57                                 if(dynamic_cast<Install *>(lib))
58                                         lib=lib->get_depends().front();
59                                 if(StaticLibrary *stlib=dynamic_cast<StaticLibrary *>(lib))
60                                         queue.push_back(&stlib->get_component());
61                         }
62                         else
63                                 builder.problem(comp.get_package().get_name(), format("Couldn't find library %s for %s", *i, Path::basename(name)));
64                 }
65         }
66
67         /* Add only the last occurrence of each library to the actual dependencies.
68         This ensures that static library ordering is correct. */
69         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
70         {
71                 bool last=true;
72                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
73                         last=(j==i || *j!=*i);
74                 if(last)
75                         add_depend(*i);
76         }
77
78         deps_ready=true;
79 }
80
81 Action *Executable::build()
82 {
83         return Target::build(new Link(builder, *this));;
84 }
85
86 /**
87 Returns the name for the executable.  We can't do this in the constructor since
88 we need to pass the value to the Target c'tor.
89 */
90 string Executable::generate_target_name(const Component &c)
91 {
92         string prefix, suffix;
93         const string &arch=c.get_package().get_arch();
94
95         if(c.get_type()==Component::LIBRARY)
96         {
97                 prefix="lib";
98                 if(arch=="win32")
99                         suffix=".dll";
100                 else
101                         suffix=".so";
102         }
103         else if(c.get_type()==Component::MODULE)
104                 suffix=".m";
105         else if(c.get_type()==Component::PROGRAM)
106         {
107                 if(arch=="win32")
108                         suffix=".exe";
109         }
110
111         return (c.get_package().get_out_dir()/(prefix+c.get_name()+suffix)).str();
112 }