]> git.tdb.fi Git - builder.git/blob - source/dependencycache.cpp
Adapt to changes in msppath
[builder.git] / source / dependencycache.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/path/utils.h>
10 #include <msp/strings/utils.h>
11 #include "builder.h"
12 #include "dependencycache.h"
13 #include "sourcepackage.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 DependencyCache::DependencyCache(SourcePackage &p):
19         package(p),
20         changed(false)
21 { }
22
23 const StringList &DependencyCache::get_deps(const string &tgt) const
24 {
25         DepsMap::const_iterator i=deps.find(tgt);
26         if(i==deps.end())
27         {
28                 static StringList dummy;
29                 return dummy;
30         }
31         return i->second;
32 }
33
34 void DependencyCache::set_deps(const string &tgt, const StringList &d)
35 {
36         deps[tgt]=d;
37         changed=true;
38 }
39
40 /**
41 Saves the depencency cache.  If there are no cached dependencies or they
42 haven't been changed, does nothing.
43 */
44 void DependencyCache::save() const
45 {
46         if(deps.empty() || !changed || package.get_builder().get_dry_run())
47                 return;
48
49         ofstream out((package.get_source()/".deps").str().c_str());
50         if(!out)
51                 return;
52
53         for(DepsMap::const_iterator i=deps.begin(); i!=deps.end(); ++i)
54         {
55                 out<<i->first;
56                 for(StringList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
57                         out<<'|'<<*j;
58                 out<<'\n';
59         }
60 }
61
62 void DependencyCache::load()
63 {
64         string fn=(package.get_source()/".deps").str();
65         ifstream in(fn.c_str());
66         if(!in)
67                 return;
68
69         string line;
70         while(getline(in, line))
71         {
72                 vector<string> parts=split(line, '|');
73                 deps[parts[0]]=StringList(parts.begin()+1, parts.end());
74         }
75
76         mtime=Time::TimeStamp::from_unixtime(stat(fn).st_mtime);
77 }