]> git.tdb.fi Git - builder.git/blob - source/sourcefile.cpp
Bugfixes
[builder.git] / source / sourcefile.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <iostream>
9 #include <msp/fs/utils.h>
10 #include <msp/io/except.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 FS::Path &p):
21         FileTarget(b, (c ? &c->get_package() : 0), p),
22         comp(c)
23 { }
24
25 void SourceFile::find_depends()
26 {
27         if(!comp)
28                 return;
29
30         const SourcePackage &spkg=comp->get_package();
31         string relname=FS::relative(name, 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 KeyError &)
42                 { }
43         }
44
45         if(!deps_found)
46         {
47                 try
48                 {
49                         IO::BufferedFile in(name);
50
51                         if(builder.get_verbose()>=4)
52                                 cout<<"Reading includes from "<<name<<'\n';
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::FileNotFound &)
64                 {
65                         return;
66                 }
67         }
68
69         const StringList &incpath=comp->get_build_info().incpath;
70
71         FS::Path dir=FS::dirname(path);
72         for(list<string>::iterator i=includes.begin(); i!=includes.end(); ++i)
73         {
74                 Target *hdr=builder.get_header(*i, dir, incpath);
75                 if(hdr)
76                         add_depend(hdr);
77         }
78
79         deps_ready=true;
80 }