]> git.tdb.fi Git - builder.git/blob - source/sourcefile.cpp
718a09c48541152d3c15f484b5048ce81017f94e
[builder.git] / source / sourcefile.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <fstream>
9 #include <msp/strings/regex.h>
10 #include "builder.h"
11 #include "component.h"
12 #include "sourcefile.h"
13 #include "sourcepackage.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 SourceFile::SourceFile(Builder &b, const Component *c, const string &n):
19         Target(b, c?&c->get_package():0, n),
20         comp(c)
21 { }
22
23 /**
24 Parses include directives from the file and looks up the appropriate targets
25 from Builder.
26 */
27 void SourceFile::find_depends()
28 {
29         if(!comp)
30                 return;
31
32         DependencyCache &deps_cache=comp->get_package().get_deps_cache();
33         if(mtime>deps_cache.get_mtime())
34         {
35                 ifstream in(name.c_str());
36                 if(!in) return;
37
38                 Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
39
40                 string line;
41                 while(getline(in, line))
42                         if(RegMatch match=r_include.match(line))
43                                 includes.push_back(match[1].str);
44
45                 deps_cache.set_deps(name, includes);
46         }
47         else
48                 includes=deps_cache.get_deps(name);
49
50         const StringList &incpath=comp->get_build_info().incpath;
51         const string &arch=comp->get_package().get_arch();
52
53         string path=name.substr(0, name.rfind('/'));
54         for(list<string>::iterator i=includes.begin(); i!=includes.end(); ++i)
55         {
56                 Target *hdr=builder.get_header(*i, arch, path, incpath);
57                 if(hdr)
58                         add_depend(hdr);
59         }
60
61         deps_ready=true;
62 }