]> git.tdb.fi Git - builder.git/blob - source/csourcefile.cpp
Do dependency discovery in a single pass
[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)
28                 return;
29
30         const SourcePackage &spkg = comp->get_package();
31         string relname = FS::relative(path, spkg.get_source()).str();
32         DependencyCache &deps_cache = spkg.get_deps_cache();
33         bool deps_found = false;
34         if(mtime<deps_cache.get_mtime())
35         {
36                 try
37                 {
38                         includes = deps_cache.get_deps(relname);
39                         deps_found = true;
40                 }
41                 catch(const key_error &)
42                 { }
43         }
44
45         if(!deps_found)
46         {
47                 try
48                 {
49                         IO::BufferedFile in(path.str());
50
51                         if(builder.get_verbose()>=4)
52                                 IO::print("Reading includes from %s\n", path.str());
53
54                         Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
55
56                         string line;
57                         while(in.getline(line))
58                                 if(RegMatch match = r_include.match(line))
59                                         includes.push_back(match[1].str);
60
61                         deps_cache.set_deps(relname, includes);
62                 }
63                 catch(const IO::file_not_found &)
64                 {
65                         if(builder.get_verbose()>=4)
66                                 IO::print("Failed to read includes from %s\n", path.str());
67                         return;
68                 }
69         }
70
71         const StringList &incpath = comp->get_build_info().incpath;
72
73         FS::Path dir = FS::dirname(path);
74         for(list<string>::iterator i=includes.begin(); i!=includes.end(); ++i)
75         {
76                 Target *hdr = builder.get_vfs().find_header(*i, dir, incpath);
77                 if(hdr)
78                         add_depend(hdr);
79         }
80 }