]> git.tdb.fi Git - builder.git/blob - source/objectfile.cpp
More flexible way to manage filename patterns
[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, s.get_path())),
14         source(s),
15         used_in_shlib(false)
16 {
17         component = &c;
18         add_dependency(source);
19 }
20
21 FS::Path ObjectFile::generate_target_path(const Component &comp, const FS::Path &src)
22 {
23         const SourcePackage &pkg = comp.get_package();
24         FS::Path temp_dir = pkg.get_temp_directory()/comp.get_name();
25         FS::Path rel_src;
26         if(FS::descendant_depth(src, temp_dir)>=0)
27                 rel_src = FS::relative(src, temp_dir);
28         else
29                 rel_src = FS::relative(src, pkg.get_source_directory());
30         string fn;
31         for(FS::Path::Iterator i=rel_src.begin(); i!=rel_src.end(); ++i)
32         {
33                 if(!fn.empty())
34                         fn += '_';
35                 if(*i!=".")
36                         fn += *i;
37         }
38         const Architecture &arch = comp.get_package().get_builder().get_current_arch();
39         return temp_dir/arch.create_filename<ObjectFile>(FS::basepart(fn));
40 }
41
42 void ObjectFile::set_used_in_shared_library(bool u)
43 {
44         used_in_shlib = u;
45 }
46
47 void ObjectFile::collect_build_info(BuildInfo &binfo) const
48 {
49         Target::collect_build_info(binfo);
50         binfo.update_from(component->get_build_info_for_path(source.get_path()));
51 }
52
53 void ObjectFile::find_dependencies()
54 {
55         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
56         {
57                 (*i)->prepare();
58                 find_dependencies(dynamic_cast<FileTarget *>(*i));
59         }
60 }
61
62 void ObjectFile::find_dependencies(FileTarget *tgt)
63 {
64         FileTarget *rtgt = dynamic_cast<FileTarget *>(tgt->get_real_target());
65         const Dependencies &tdeps = rtgt->get_transitive_dependencies();
66         Dependencies deps_to_add;
67         if(rtgt==tgt)
68         {
69                 /* We are using the target from its original location, so dependencies
70                 apply directly */
71                 deps_to_add = tdeps;
72         }
73         else
74         {
75                 FS::Path inst_dir = rtgt->get_component()->get_install_map().get_install_location(*rtgt);
76                 /* The target has been displaced by installing it.  Displace any
77                 dependencies that come from the same package as well. */
78                 const SourcePackage *tpkg = rtgt->get_package();
79                 for(Dependencies::const_iterator i=tdeps.begin(); i!=tdeps.end(); ++i)
80                 {
81                         FileTarget *file = dynamic_cast<FileTarget *>(*i);
82                         if(file && file->get_package()==tpkg && FS::descendant_depth(file->get_path(), tpkg->get_source_directory())>=0)
83                         {
84                                 const Component *tcomp = file->get_component();
85                                 FS::Path dep_inst = tcomp->get_install_map().get_install_location(*file);
86                                 FS::Path displaced = FS::dirname(tgt->get_path())/FS::relative(dep_inst, inst_dir)/FS::basename(file->get_path());
87                                 if(Target *ddep = builder.get_vfs().get_target(displaced))
88                                         deps_to_add.push_back(ddep);
89                                 else
90                                 {
91                                         const Component::OverlayList &overlays = tcomp->get_overlays();
92                                         string last_dir = FS::basename(FS::dirname(displaced));
93                                         for(Component::OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
94                                                 if(last_dir==*j)
95                                                 {
96                                                         displaced = displaced.subpath(0, displaced.size()-2)/FS::basename(file->get_path());
97                                                         if((ddep = builder.get_vfs().get_target(displaced)))
98                                                                 deps_to_add.push_back(ddep);
99                                                 }
100                                 }
101                         }
102                         else
103                                 deps_to_add.push_back(*i);
104                 }
105         }
106
107         for(Dependencies::const_iterator i=deps_to_add.begin(); i!=deps_to_add.end(); ++i)
108                 if(find(depends.begin(), depends.end(), *i)==depends.end())
109                 {
110                         add_dependency(**i);
111                         if((*i)->get_real_target()->is_buildable())
112                                 (*i)->signal_modified.connect(sigc::mem_fun(this, static_cast<void (ObjectFile::*)()>(&ObjectFile::find_dependencies)));
113                 }
114 }