]> git.tdb.fi Git - builder.git/blob - source/dependencycache.cpp
Externalize dry run handling from Config and DependencyCache
[builder.git] / source / dependencycache.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/stat.h>
3 #include <msp/io/file.h>
4 #include <msp/io/print.h>
5 #include <msp/strings/utils.h>
6 #include "builder.h"
7 #include "dependencycache.h"
8 #include "sourcepackage.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 DependencyCache::DependencyCache(SourcePackage &p):
14         package(p),
15         changed(false)
16 { }
17
18 void DependencyCache::set_deps(const string &tgt, const StringList &d)
19 {
20         deps[tgt] = d;
21         changed = true;
22 }
23
24 bool DependencyCache::has_deps(const string &tgt) const
25 {
26         return deps.count(tgt);
27 }
28
29 const StringList &DependencyCache::get_deps(const string &tgt) const
30 {
31         return get_item(deps, tgt);
32 }
33
34 void DependencyCache::save() const
35 {
36         if(deps.empty() || !changed)
37                 return;
38
39         IO::BufferedFile out((package.get_source()/".deps").str(), IO::M_WRITE);
40
41         for(DepsMap::const_iterator i=deps.begin(); i!=deps.end(); ++i)
42         {
43                 IO::print(out, i->first);
44                 for(StringList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
45                         IO::print(out, "|%s", *j);
46                 IO::print(out, "\n");
47         }
48 }
49
50 void DependencyCache::load()
51 {
52         string fn = (package.get_source()/".deps").str();
53
54         if(FS::Stat st = FS::stat(fn))
55         {
56                 IO::BufferedFile in(fn);
57
58                 string line;
59                 while(in.getline(line))
60                 {
61                         vector<string> parts = split(line, '|');
62                         deps[parts[0]] = StringList(parts.begin()+1, parts.end());
63                 }
64
65                 mtime = st.get_modify_time();
66         }
67 }