]> git.tdb.fi Git - builder.git/blob - source/objectfile.cpp
Redesign the library mode system
[builder.git] / source / objectfile.cpp
1 #include <algorithm>
2 #include <msp/fs/utils.h>
3 #include "builder.h"
4 #include "component.h"
5 #include "objectfile.h"
6 #include "sourcefile.h"
7 #include "sourcepackage.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 ObjectFile::ObjectFile(Builder &b, const Component &c, SourceFile &s):
13         FileTarget(b, c.get_package(), generate_target_path(c, FS::relative(s.get_path(), c.get_package().get_source()).str())),
14         source(s)       
15 {
16         component = &c;
17         add_depend(source);
18 }
19
20 FS::Path ObjectFile::generate_target_path(const Component &comp, const string &src)
21 {
22         const SourcePackage &pkg = comp.get_package();
23         string fn = FS::basepart(src)+".o";
24         if(!fn.compare(0, 2, "./"))
25                 fn.erase(0, 2);
26         for(string::iterator i=fn.begin(); i!=fn.end(); ++i)
27                 if(*i=='/')
28                         *i = '_';
29         return pkg.get_temp_dir()/comp.get_name()/fn;
30 }
31
32 void ObjectFile::find_depends()
33 {
34         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
35         {
36                 (*i)->prepare();
37                 find_depends(dynamic_cast<FileTarget *>(*i));
38         }
39 }
40
41 void ObjectFile::find_depends(FileTarget *tgt)
42 {
43         FileTarget *rtgt = dynamic_cast<FileTarget *>(tgt->get_real_target());
44         const Dependencies &tdeps = rtgt->get_depends();
45         Dependencies deps_to_add;
46         if(rtgt==tgt)
47         {
48                 /* We are using the target from its original location, so dependencies
49                 apply directly */
50                 deps_to_add = tdeps;
51         }
52         else
53         {
54                 /* The target has been displaced by installing it.  Displace any
55                 dependencies that come from the same package as well. */
56                 const SourcePackage *tpkg = rtgt->get_package();
57                 for(Dependencies::const_iterator i=tdeps.begin(); i!=tdeps.end(); ++i)
58                 {
59                         FileTarget *file = dynamic_cast<FileTarget *>(*i);
60                         if(file && file->get_package()==tpkg && FS::descendant_depth(file->get_path(), tpkg->get_source())>=0)
61                         {
62                                 FS::Path displaced = tgt->get_path()/FS::relative(file->get_path(), rtgt->get_path());
63                                 if(Target *ddep = builder.get_vfs().get_target(displaced))
64                                         deps_to_add.push_back(ddep);
65                         }
66                         else
67                                 deps_to_add.push_back(*i);
68                 }
69         }
70
71         for(Dependencies::const_iterator i=deps_to_add.begin(); i!=deps_to_add.end(); ++i)
72                 if(find(depends.begin(), depends.end(), *i)==depends.end())
73                         add_depend(**i);
74 }