]> git.tdb.fi Git - builder.git/blob - source/csourcefile.cpp
dee807b2306027aa1a503e261f241dfbe4b4a0d7
[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
10 using namespace std;
11 using namespace Msp;
12
13 CSourceFile::CSourceFile(Builder &b, const FS::Path &p):
14         SourceFile(b, 0, p)
15 { }
16
17 CSourceFile::CSourceFile(Builder &b, const Component &c, const FS::Path &p):
18         SourceFile(b, &c, p)
19 {
20         string ext = FS::extpart(FS::basename(path));
21         if(ext==".h" || ext==".H" || ext==".hpp")
22                 install_location = "include/"+comp->get_name();
23 }
24
25 void CSourceFile::find_depends()
26 {
27         if(!comp || !mtime)
28                 return;
29
30         const SourcePackage &spkg = comp->get_package();
31         string relname = FS::relative(path, spkg.get_source()).str();
32
33         DependencyCache &deps_cache = spkg.get_deps_cache();
34         if(mtime<deps_cache.get_mtime() && deps_cache.has_deps(relname))
35                 includes = deps_cache.get_deps(relname);
36         else
37         {
38                 IO::BufferedFile in(path.str());
39
40                 if(builder.get_verbose()>=4)
41                         IO::print("Reading includes from %s\n", path.str());
42
43                 Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
44
45                 string line;
46                 while(in.getline(line))
47                         if(RegMatch match = r_include.match(line))
48                                 includes.push_back(match[1].str);
49
50                 deps_cache.set_deps(relname, includes);
51         }
52
53         const StringList &incpath = comp->get_build_info().incpath;
54         StringList local_incpath = incpath;
55         local_incpath.push_front(FS::dirname(path).str());
56
57         for(list<string>::iterator i=includes.begin(); i!=includes.end(); ++i)
58         {
59                 Target *hdr = builder.get_vfs().find_header(i->substr(1), ((*i)[0]=='"' ? local_incpath : incpath));
60                 if(hdr)
61                         add_depend(hdr);
62         }
63 }