]> git.tdb.fi Git - builder.git/blob - source/sourcefile.cpp
Convert all fstreams to IO::Files
[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 <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 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                 try
52                 {
53                         IO::BufferedFile in(name);
54
55                         if(builder.get_verbose()>=4)
56                                 cout<<"Reading includes from "<<name<<'\n';
57
58                         Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
59
60                         string line;
61                         while(in.getline(line))
62                                 if(RegMatch match=r_include.match(line))
63                                         includes.push_back(match[1].str);
64
65                         deps_cache.set_deps(relname, includes);
66                 }
67                 catch(const IO::FileNotFound &)
68                 {
69                         return;
70                 }
71         }
72
73         const StringList &incpath=comp->get_build_info().incpath;
74
75         string path=name.substr(0, name.rfind('/'));
76         for(list<string>::iterator i=includes.begin(); i!=includes.end(); ++i)
77         {
78                 Target *hdr=builder.get_header(*i, path, incpath);
79                 if(hdr)
80                         add_depend(hdr);
81         }
82
83         deps_ready=true;
84 }