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 "sharedlibrary.h"
18 #include "staticlibrary.h"
23 Executable::Executable(Builder &b, const Component &c, const list<ObjectFile *> &objs):
24 Target(b, &c.get_package(), generate_target_name(c)),
28 for(list<ObjectFile *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
33 Finds and adds any required libraries to the dependencies.
35 void Executable::find_depends()
37 LibMode libmode=comp.get_package().get_library_mode();
38 if(dynamic_cast<SharedLibrary *>(this))
41 list<const Component *> queue;
42 list<Target *> dep_libs;
43 queue.push_back(&comp);
46 const Component *c=queue.front();
47 queue.erase(queue.begin());
49 const StringList &libpath=c->get_build_info().libpath;
51 const list<string> &libs=c->get_build_info().libs;
52 for(StringList::const_iterator i=libs.begin(); i!=libs.end(); ++i)
54 Target *lib=builder.get_library(*i, libpath, libmode);
57 dep_libs.push_back(lib);
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());
65 builder.problem(comp.get_package().get_name(), format("Couldn't find library %s for %s", *i, basename(name)));
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)
74 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
75 last=(j==i || *j!=*i);
83 Action *Executable::build()
85 return Target::build(new Link(builder, *this));;
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.
92 string Executable::generate_target_name(const Component &c)
94 const SourcePackage &pkg=c.get_package();
95 string prefix, suffix;
96 const string &arch=pkg.get_builder().get_current_arch().get_name();
98 if(c.get_type()==Component::LIBRARY)
106 else if(c.get_type()==Component::MODULE)
108 else if(c.get_type()==Component::PROGRAM)
114 return (pkg.get_out_dir()/(prefix+c.get_name()+suffix)).str();