]> git.tdb.fi Git - builder.git/blob - source/dependencycache.cpp
978a0e37b8ccb3bb96d7a56d43f0b9248fb3dbd2
[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/fs/stat.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                 throw KeyError("Unknown dependencies", tgt);
28
29         return i->second;
30 }
31
32 void DependencyCache::set_deps(const string &tgt, const StringList &d)
33 {
34         deps[tgt]=d;
35         changed=true;
36 }
37
38 /**
39 Saves the depencency cache.  If there are no cached dependencies or they
40 haven't been changed, does nothing.
41 */
42 void DependencyCache::save() const
43 {
44         if(deps.empty() || !changed || package.get_builder().get_dry_run())
45                 return;
46
47         ofstream out((package.get_source()/".deps").str().c_str());
48         if(!out)
49                 return;
50
51         for(DepsMap::const_iterator i=deps.begin(); i!=deps.end(); ++i)
52         {
53                 out<<i->first;
54                 for(StringList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
55                         out<<'|'<<*j;
56                 out<<'\n';
57         }
58 }
59
60 void DependencyCache::load()
61 {
62         string fn=(package.get_source()/".deps").str();
63         ifstream in(fn.c_str());
64         if(!in)
65                 return;
66
67         string line;
68         while(getline(in, line))
69         {
70                 vector<string> parts=split(line, '|');
71                 deps[parts[0]]=StringList(parts.begin()+1, parts.end());
72         }
73
74         mtime=Time::TimeStamp::from_unixtime(FS::stat(fn).st_mtime);
75 }