]> git.tdb.fi Git - builder.git/blob - source/executable.cpp
Adjust requires to library changes
[builder.git] / source / executable.cpp
1 #include <iostream>
2 #include "builder.h"
3 #include "component.h"
4 #include "executable.h"
5 #include "install.h"
6 #include "link.h"
7 #include "objectfile.h"
8 #include "package.h"
9 #include "staticlibrary.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 Executable::Executable(Builder &b, const Component &c, const list<ObjectFile *> &objs):
15         Target(b, &c.get_package(), generate_target_name(c)),
16         comp(c)
17 {
18         buildable=true;
19         for(list<ObjectFile *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
20                 add_depend(*i);
21 }
22
23 /**
24 Finds and adds any required libraries to the dependencies.
25 */
26 void Executable::find_depends()
27 {
28         LibMode libmode=package->get_library_mode();
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                 const string &arch=c->get_package().get_arch();
40
41                 const list<string> &libs=c->get_build_info().libs;
42                 for(StringList::const_iterator i=libs.begin(); i!=libs.end(); ++i)
43                 {
44                         Target *lib=builder.get_library(*i, arch, libpath, libmode);
45                         if(lib)
46                         {
47                                 dep_libs.push_back(lib);
48
49                                 if(dynamic_cast<Install *>(lib))
50                                         lib=lib->get_depends().front();
51                                 if(StaticLibrary *stlib=dynamic_cast<StaticLibrary *>(lib))
52                                         queue.push_back(&stlib->get_component());
53                         }
54                         else
55                                 cout<<"Couldn't find library "<<*i<<" for "<<name<<'\n';
56                                 //XXX Throw an exception here or something
57                 }
58         }
59
60         /* Add only the last occurrence of each library to the actual dependencies.
61         This ensures that static library ordering is correct. */
62         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
63         {
64                 bool last=true;
65                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
66                         last=(j==i || *j!=*i);
67                 if(last)
68                         add_depend(*i);
69         }
70
71         deps_ready=true;
72 }
73
74 Action *Executable::build()
75 {
76         return Target::build(new Link(builder, *this));;
77 }
78
79 /**
80 Returns the name for the executable.  We can't do this in the constructor since
81 we need to pass the value to the Target c'tor.
82 */
83 string Executable::generate_target_name(const Component &c)
84 {
85         string prefix,suffix;
86         const string &arch=c.get_package().get_arch();
87
88         if(c.get_type()==Component::LIBRARY)
89         {
90                 prefix="lib";
91                 if(arch=="win32")
92                         suffix=".dll";
93                 else
94                         suffix=".so";
95         }
96         else if(c.get_type()==Component::MODULE)
97                 suffix=".m";
98         else if(c.get_type()==Component::PROGRAM)
99         {
100                 if(arch=="win32")
101                         suffix=".exe";
102         }
103
104         return (c.get_package().get_out_dir()/(prefix+c.get_name()+suffix)).str();
105 }