]> git.tdb.fi Git - builder.git/blob - source/lib/objectfile.cpp
46ec607119d82c10511eef32ef23e9d50657b958
[builder.git] / source / lib / objectfile.cpp
1 #include <msp/core/algorithm.h>
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 {
16         component = &c;
17         add_dependency(source);
18 }
19
20 FS::Path ObjectFile::generate_target_path(const Component &comp, const FS::Path &src)
21 {
22         string fn = comp.flatten_source_path(src);
23         const Architecture &arch = comp.get_package().get_builder().get_current_arch();
24         return comp.get_temp_directory()/arch.create_filename<ObjectFile>(FS::basepart(fn));
25 }
26
27 void ObjectFile::set_used_in_shared_library(bool u)
28 {
29         used_in_shlib = u;
30 }
31
32 void ObjectFile::collect_build_info(BuildInfo &binfo) const
33 {
34         Target::collect_build_info(binfo);
35         binfo.update_from(component->get_build_info_for_path(source.get_path()));
36 }
37
38 void ObjectFile::find_dependencies()
39 {
40         vector<FileTarget *> headers;
41         find_dependencies(source, headers);
42         for(FileTarget *h: headers)
43                 if(!any_equals(depends, static_cast<Target *>(h)))
44                 {
45                         add_dependency(*h);
46                         if(h->get_real_target()->is_buildable())
47                                 h->signal_modified.connect(sigc::mem_fun(this, static_cast<void (ObjectFile::*)()>(&ObjectFile::find_dependencies)));
48                 }
49 }
50
51 void ObjectFile::find_dependencies(FileTarget &tgt, vector<FileTarget *> &headers)
52 {
53         tgt.prepare();
54
55         FileTarget *rtgt = tgt.get_real_target();
56         Dependencies deps_to_add = rtgt->get_transitive_dependencies();
57         if(rtgt!=&tgt)
58         {
59                 FS::Path inst_dir = rtgt->get_component()->get_install_map().get_install_location(*rtgt);
60                 /* The target has been displaced by installing it.  Displace any
61                 dependencies that come from the same package as well. */
62                 const SourcePackage *tpkg = rtgt->get_package();
63                 for(Target *&d: deps_to_add)
64                 {
65                         FileTarget *file = dynamic_cast<FileTarget *>(d);
66                         if(file && file->get_package()==tpkg && FS::descendant_depth(file->get_path(), tpkg->get_source_directory())>=0)
67                         {
68                                 const Component *tcomp = file->get_component();
69                                 FS::Path dep_inst = tcomp->get_install_map().get_install_location(*file);
70                                 FS::Path displaced = FS::dirname(tgt.get_path())/FS::relative(dep_inst, inst_dir)/FS::basename(file->get_path());
71                                 d = builder.get_vfs().get_target(displaced);
72                                 if(!d)
73                                 {
74                                         /* If the target was in an overlay directory and the displaced
75                                         dependency is not found, try removing the overlay from the path. */
76                                         string last_dir = FS::basename(FS::dirname(displaced));
77                                         if(any_equals(tcomp->get_overlays(), last_dir))
78                                         {
79                                                 displaced = displaced.subpath(0, displaced.size()-2)/FS::basename(file->get_path());
80                                                 d = builder.get_vfs().get_target(displaced);
81                                         }
82                                 }
83                         }
84                 }
85         }
86
87         for(Target *d: deps_to_add)
88                 if(FileTarget *file = dynamic_cast<FileTarget *>(d))
89                 {
90                         auto i = lower_bound(headers, file);
91                         if(i==headers.end() || *i!=file)
92                         {
93                                 headers.insert(i, file);
94                                 find_dependencies(*file, headers);
95                         }
96                 }
97 }