]> git.tdb.fi Git - builder.git/blob - source/executable.cpp
488dd2966a27279e1d85ac8100f1be51977fdafd
[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 "sharedlibrary.h"
18 #include "staticlibrary.h"
19
20 using namespace std;
21 using namespace Msp;
22
23 Executable::Executable(Builder &b, const Component &c, const list<ObjectFile *> &objs):
24         Target(b, &c.get_package(), generate_target_name(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 /**
33 Finds and adds any required libraries to the dependencies.
34 */
35 void Executable::find_depends()
36 {
37         LibMode libmode=comp.get_package().get_library_mode();
38         if(dynamic_cast<SharedLibrary *>(this))
39                 libmode=DYNAMIC;
40
41         list<const Component *> queue;
42         list<Target *> dep_libs;
43         queue.push_back(&comp);
44         while(!queue.empty())
45         {
46                 const Component *c=queue.front();
47                 queue.erase(queue.begin());
48
49                 const StringList &libpath=c->get_build_info().libpath;
50
51                 const list<string> &libs=c->get_build_info().libs;
52                 for(StringList::const_iterator i=libs.begin(); i!=libs.end(); ++i)
53                 {
54                         Target *lib=builder.get_library(*i, libpath, libmode);
55                         if(lib)
56                         {
57                                 dep_libs.push_back(lib);
58
59                                 if(dynamic_cast<Install *>(lib))
60                                         lib=lib->get_depends().front();
61                                 if(StaticLibrary *stlib=dynamic_cast<StaticLibrary *>(lib))
62                                         queue.push_back(&stlib->get_component());
63                         }
64                         else
65                                 builder.problem(comp.get_package().get_name(), format("Couldn't find library %s for %s", *i, basename(name)));
66                 }
67         }
68
69         /* Add only the last occurrence of each library to the actual dependencies.
70         This ensures that static library ordering is correct. */
71         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
72         {
73                 bool last=true;
74                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
75                         last=(j==i || *j!=*i);
76                 if(last)
77                         add_depend(*i);
78         }
79
80         deps_ready=true;
81 }
82
83 Action *Executable::build()
84 {
85         return Target::build(new Link(builder, *this));;
86 }
87
88 /**
89 Returns the name for the executable.  We can't do this in the constructor since
90 we need to pass the value to the Target c'tor.
91 */
92 string Executable::generate_target_name(const Component &c)
93 {
94         const SourcePackage &pkg=c.get_package();
95         string prefix, suffix;
96         const string &arch=pkg.get_builder().get_current_arch().get_name();
97
98         if(c.get_type()==Component::LIBRARY)
99         {
100                 prefix="lib";
101                 if(arch=="win32")
102                         suffix=".dll";
103                 else
104                         suffix=".so";
105         }
106         else if(c.get_type()==Component::MODULE)
107                 suffix=".m";
108         else if(c.get_type()==Component::PROGRAM)
109         {
110                 if(arch=="win32")
111                         suffix=".exe";
112         }
113
114         return (pkg.get_out_dir()/(prefix+c.get_name()+suffix)).str();
115 }