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