]> git.tdb.fi Git - builder.git/blob - source/csourcefile.cpp
Inline simple constructors
[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 Component &c, const FS::Path &p):
15         SourceFile(b, c, p)
16 {
17         string ext = FS::extpart(FS::basename(path));
18         if(ext==".h" || ext==".H" || ext==".hpp")
19                 install_location = FS::Path("include")/package->get_name();
20 }
21
22 void CSourceFile::parse_includes(IO::Base &in)
23 {
24         static Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
25
26         string line;
27         while(in.getline(line))
28                 if(RegMatch match = r_include.match(line))
29                         includes.push_back(match[1].str);
30 }
31
32 void CSourceFile::find_dependencies()
33 {
34         if(!component || !mtime)
35                 return;
36
37         const SourcePackage &spkg = component->get_package();
38
39         Cache &cache = spkg.get_cache();
40         if(mtime<cache.get_mtime() && cache.has_key(this, "includes"))
41                 includes = cache.get_values(this, "includes");
42         else
43         {
44                 IO::BufferedFile in(path.str());
45
46                 builder.get_logger().log("files", "Reading includes from %s", path.str());
47
48                 parse_includes(in);
49                 cache.set_values(this, "includes", includes);
50         }
51
52         const BuildInfo &build_info = component->get_build_info_for_path(path);
53         const auto &incpath = build_info.incpath;
54         VirtualFileSystem::SearchPath local_incpath;
55         local_incpath.reserve(1+build_info.local_incpath.size()+incpath.size());
56         local_incpath.push_back(FS::dirname(path).str());
57         local_incpath.insert(local_incpath.end(), build_info.local_incpath.begin(), build_info.local_incpath.end());
58         local_incpath.insert(local_incpath.end(), incpath.begin(), incpath.end());
59
60         Tool *compiler = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(path)), true);
61         if(compiler)
62                 compiler->prepare();
63         for(const string &i: includes)
64                 if(Target *hdr = builder.get_vfs().find_header(i.substr(1), compiler, (i[0]=='"' ? local_incpath : incpath)))
65                         add_transitive_dependency(*hdr);
66 }
67
68 void CSourceFile::modified()
69 {
70         includes.clear();
71         trans_depends.clear();
72         find_dependencies();
73 }