3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/path/utils.h>
9 #include <msp/strings/formatter.h>
11 #include "component.h"
12 #include "executable.h"
15 #include "objectfile.h"
17 #include "staticlibrary.h"
22 Executable::Executable(Builder &b, const Component &c, const list<ObjectFile *> &objs):
23 Target(b, &c.get_package(), generate_target_name(c)),
27 for(list<ObjectFile *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
32 Finds and adds any required libraries to the dependencies.
34 void Executable::find_depends()
36 LibMode libmode=comp.get_package().get_library_mode();
38 list<const Component *> queue;
39 list<Target *> dep_libs;
40 queue.push_back(&comp);
43 const Component *c=queue.front();
44 queue.erase(queue.begin());
46 const StringList &libpath=c->get_build_info().libpath;
48 const list<string> &libs=c->get_build_info().libs;
49 for(StringList::const_iterator i=libs.begin(); i!=libs.end(); ++i)
51 Target *lib=builder.get_library(*i, libpath, libmode);
54 dep_libs.push_back(lib);
56 if(dynamic_cast<Install *>(lib))
57 lib=lib->get_depends().front();
58 if(StaticLibrary *stlib=dynamic_cast<StaticLibrary *>(lib))
59 queue.push_back(&stlib->get_component());
62 builder.problem(comp.get_package().get_name(), format("Couldn't find library %s for %s", *i, basename(name)));
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)
71 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
72 last=(j==i || *j!=*i);
80 Action *Executable::build()
82 return Target::build(new Link(builder, *this));;
86 Returns the name for the executable. We can't do this in the constructor since
87 we need to pass the value to the Target c'tor.
89 string Executable::generate_target_name(const Component &c)
91 const SourcePackage &pkg=c.get_package();
92 string prefix, suffix;
93 const string &arch=pkg.get_builder().get_current_arch().get_name();
95 if(c.get_type()==Component::LIBRARY)
103 else if(c.get_type()==Component::MODULE)
105 else if(c.get_type()==Component::PROGRAM)
111 return (pkg.get_out_dir()/(prefix+c.get_name()+suffix)).str();