]> git.tdb.fi Git - builder.git/blob - source/csourcefile.cpp
Refactor transitive dependencies to work on all targets
[builder.git] / source / csourcefile.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/regex.h>
5 #include "builder.h"
6 #include "component.h"
7 #include "csourcefile.h"
8 #include "sourcepackage.h"
9 #include "tool.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 CSourceFile::CSourceFile(Builder &b, const FS::Path &p):
15         SourceFile(b, p)
16 { }
17
18 CSourceFile::CSourceFile(Builder &b, const Component &c, const FS::Path &p):
19         SourceFile(b, c, p)
20 {
21         string ext = FS::extpart(FS::basename(path));
22         if(ext==".h" || ext==".H" || ext==".hpp")
23                 install_location = FS::Path("include")/package->get_name();
24 }
25
26 void CSourceFile::parse_includes(IO::Base &in)
27 {
28         static Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
29
30         string line;
31         while(in.getline(line))
32                 if(RegMatch match = r_include.match(line))
33                         includes.push_back(match[1].str);
34 }
35
36 void CSourceFile::find_dependencies()
37 {
38         if(!component || !mtime)
39                 return;
40
41         const SourcePackage &spkg = component->get_package();
42
43         Cache &cache = spkg.get_cache();
44         if(mtime<cache.get_mtime() && cache.has_key(this, "includes"))
45                 includes = cache.get_values(this, "includes");
46         else
47         {
48                 IO::BufferedFile in(path.str());
49
50                 builder.get_logger().log("files", format("Reading includes from %s", path.str()));
51
52                 parse_includes(in);
53                 cache.set_values(this, "includes", includes);
54         }
55
56         const BuildInfo &build_info = component->get_build_info_for_path(path);
57         const BuildInfo::PathList &incpath = build_info.incpath;
58         BuildInfo::PathList local_incpath = incpath;
59         local_incpath.insert(local_incpath.begin(), build_info.local_incpath.begin(), build_info.local_incpath.end());
60         local_incpath.push_front(FS::dirname(path).str());
61
62         Tool *compiler = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(path)), true);
63         if(compiler)
64                 compiler->prepare();
65         for(IncludeList::iterator i=includes.begin(); i!=includes.end(); ++i)
66         {
67                 Target *hdr = builder.get_vfs().find_header(i->substr(1), compiler, ((*i)[0]=='"' ? local_incpath : incpath));
68                 if(hdr)
69                         add_transitive_dependency(*hdr);
70         }
71 }
72
73 void CSourceFile::modified()
74 {
75         includes.clear();
76         trans_depends.clear();
77         find_dependencies();
78 }