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