]> git.tdb.fi Git - builder.git/blob - source/objectfile.cpp
Binary packages can't have files, so avoid some dynamic casts
[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         comp(c),
15         source(s)       
16 {
17         add_depend(&source);
18 }
19
20 void ObjectFile::find_depends()
21 {
22         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
23         {
24                 (*i)->prepare();
25                 find_depends(dynamic_cast<FileTarget *>(*i));
26         }
27 }
28
29
30 void ObjectFile::find_depends(FileTarget *tgt)
31 {
32         FileTarget *rtgt = dynamic_cast<FileTarget *>(tgt->get_real_target());
33         const Dependencies &tdeps = rtgt->get_depends();
34         Dependencies deps_to_add;
35         if(rtgt==tgt)
36         {
37                 /* We are using the target from its original location, so dependencies
38                 apply directly */
39                 deps_to_add = tdeps;
40         }
41         else
42         {
43                 /* The target has been displaced by installing it.  Displace any
44                 dependencies that come from the same package as well. */
45                 const SourcePackage *tpkg = rtgt->get_package();
46                 for(Dependencies::const_iterator i=tdeps.begin(); i!=tdeps.end(); ++i)
47                 {
48                         FileTarget *file = dynamic_cast<FileTarget *>(*i);
49                         if(file && file->get_package()==tpkg && FS::descendant_depth(file->get_path(), tpkg->get_source())>=0)
50                         {
51                                 FS::Path displaced = tgt->get_path()/FS::relative(file->get_path(), rtgt->get_path());
52                                 if(Target *ddep = builder.get_vfs().get_target(displaced))
53                                         deps_to_add.push_back(ddep);
54                         }
55                         else
56                                 deps_to_add.push_back(*i);
57                 }
58         }
59
60         for(Dependencies::const_iterator i=deps_to_add.begin(); i!=deps_to_add.end(); ++i)
61                 if(find(depends.begin(), depends.end(), *i)==depends.end())
62                         add_depend(*i);
63 }
64
65 FS::Path ObjectFile::generate_target_path(const Component &comp, const string &src)
66 {
67         const SourcePackage &pkg = comp.get_package();
68         string fn = FS::basepart(src)+".o";
69         if(!fn.compare(0, 2, "./"))
70                 fn.erase(0, 2);
71         for(string::iterator i=fn.begin(); i!=fn.end(); ++i)
72                 if(*i=='/')
73                         *i = '_';
74         return pkg.get_temp_dir()/comp.get_name()/fn;
75 }