]> git.tdb.fi Git - builder.git/blob - source/binary.cpp
Redesign the library mode system
[builder.git] / source / binary.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/strings/format.h>
3 #include "binary.h"
4 #include "builder.h"
5 #include "component.h"
6 #include "objectfile.h"
7 #include "sharedlibrary.h"
8 #include "sourcepackage.h"
9 #include "staticlibrary.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 Binary::Binary(Builder &b, const FS::Path &p):
15         FileTarget(b, p)
16 { }
17
18 Binary::Binary(Builder &b, const Component &c, const string &p, const list<ObjectFile *> &objs):
19         FileTarget(b, c.get_package(), c.get_package().get_out_dir()/p)
20 {
21         component = &c;
22         for(list<ObjectFile *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
23                 add_depend(**i);
24 }
25
26 void Binary::find_depends()
27 {
28         if(!component)
29                 return;
30
31         const SourcePackage &spkg = component->get_package();
32         LibMode libmode = spkg.get_library_mode();
33         if(dynamic_cast<SharedLibrary *>(this) && libmode<DYNAMIC)
34                 libmode = DYNAMIC;
35
36         list<const Component *> queue;
37         list<Target *> dep_libs;
38         queue.push_back(component);
39         while(!queue.empty())
40         {
41                 const Component *c = queue.front();
42                 queue.erase(queue.begin());
43
44                 const BuildInfo::PathList &libpath = c->get_build_info().libpath;
45
46                 const BuildInfo::WordList &libs = c->get_build_info().libs;
47                 for(BuildInfo::WordList::const_iterator i=libs.begin(); i!=libs.end(); ++i)
48                 {
49                         Target *lib = builder.get_vfs().find_library(*i, libpath, libmode);
50                         if(lib)
51                         {
52                                 dep_libs.push_back(lib);
53
54                                 lib = lib->get_real_target();
55                                 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(lib))
56                                         if(stlib->get_component())
57                                                 queue.push_back(stlib->get_component());
58                         }
59                         else
60                                 builder.problem(spkg.get_name(), format("Couldn't find library %s for %s", *i, name));
61                 }
62         }
63
64         /* Add only the last occurrence of each library to the actual dependencies.
65         This ensures that static library ordering is correct. */
66         for(list<Target *>::iterator i=dep_libs.begin(); i!=dep_libs.end(); ++i)
67         {
68                 bool last = true;
69                 for(list<Target *>::iterator j=i; (last && j!=dep_libs.end()); ++j)
70                         last = (j==i || *j!=*i);
71                 if(last)
72                         add_depend(**i);
73         }
74 }