]> git.tdb.fi Git - builder.git/blob - source/sourcefile.cpp
70410355652ab34cb7d522d0100ef62e30462eff
[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/fs/utils.h>
11 #include <msp/strings/regex.h>
12 #include "builder.h"
13 #include "component.h"
14 #include "sourcefile.h"
15 #include "sourcepackage.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 SourceFile::SourceFile(Builder &b, const Component *c, const string &n):
21         Target(b, c?&c->get_package():0, n),
22         comp(c)
23 { }
24
25 /**
26 Parses include directives from the file and looks up the appropriate targets
27 from Builder.
28 */
29 void SourceFile::find_depends()
30 {
31         if(!comp)
32                 return;
33
34         const SourcePackage &spkg=comp->get_package();
35         string relname=FS::relative(name, spkg.get_source()).str();
36         DependencyCache &deps_cache=spkg.get_deps_cache();
37         bool deps_found=false;
38         if(mtime<deps_cache.get_mtime())
39         {
40                 try
41                 {
42                         includes=deps_cache.get_deps(relname);
43                         deps_found=true;
44                 }
45                 catch(const KeyError &)
46                 { }
47         }
48
49         if(!deps_found)
50         {
51                 ifstream in(name.c_str());
52                 if(!in) return;
53
54                 if(builder.get_verbose()>=4)
55                         cout<<"Reading includes from "<<name<<'\n';
56
57                 Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
58
59                 string line;
60                 while(getline(in, 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
67         const StringList &incpath=comp->get_build_info().incpath;
68
69         string path=name.substr(0, name.rfind('/'));
70         for(list<string>::iterator i=includes.begin(); i!=includes.end(); ++i)
71         {
72                 Target *hdr=builder.get_header(*i, path, incpath);
73                 if(hdr)
74                         add_depend(hdr);
75         }
76
77         deps_ready=true;
78 }