]> git.tdb.fi Git - builder.git/blob - source/dependencycache.cpp
Adjust to library changes
[builder.git] / source / dependencycache.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/fs/stat.h>
9 #include <msp/io/except.h>
10 #include <msp/io/file.h>
11 #include <msp/io/print.h>
12 #include <msp/strings/utils.h>
13 #include "builder.h"
14 #include "dependencycache.h"
15 #include "sourcepackage.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 DependencyCache::DependencyCache(SourcePackage &p):
21         package(p),
22         changed(false)
23 { }
24
25 void DependencyCache::set_deps(const string &tgt, const StringList &d)
26 {
27         deps[tgt] = d;
28         changed = true;
29 }
30
31 const StringList &DependencyCache::get_deps(const string &tgt) const
32 {
33         DepsMap::const_iterator i = deps.find(tgt);
34         if(i==deps.end())
35                 throw KeyError("Unknown dependencies", tgt);
36
37         return i->second;
38 }
39
40 void DependencyCache::save() const
41 {
42         if(deps.empty() || !changed || package.get_builder().get_dry_run())
43                 return;
44
45         IO::BufferedFile out((package.get_source()/".deps").str(), IO::M_WRITE);
46
47         for(DepsMap::const_iterator i=deps.begin(); i!=deps.end(); ++i)
48         {
49                 IO::print(out, i->first);
50                 for(StringList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
51                         IO::print(out, "|%s", *j);
52                 IO::print(out, "\n");
53         }
54 }
55
56 void DependencyCache::load()
57 {
58         string fn = (package.get_source()/".deps").str();
59
60         try
61         {
62                 IO::BufferedFile in(fn);
63
64                 string line;
65                 while(in.getline(line))
66                 {
67                         vector<string> parts = split(line, '|');
68                         deps[parts[0]] = StringList(parts.begin()+1, parts.end());
69                 }
70
71                 mtime = Time::TimeStamp::from_unixtime(FS::stat(fn).st_mtime);
72         }
73         catch(const IO::file_not_found &)
74         { }
75 }