don't write out deps entries if nothing changed
[platform/upstream/ninja.git] / src / deps_log.cc
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "deps_log.h"
16
17 #include <assert.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <string.h>
21
22 #include "graph.h"
23 #include "metrics.h"
24 #include "state.h"
25 #include "util.h"
26
27 bool DepsLog::OpenForWrite(const string& path, string* err) {
28   file_ = fopen(path.c_str(), "ab");
29   if (!file_) {
30     *err = strerror(errno);
31     return false;
32   }
33   SetCloseOnExec(fileno(file_));
34
35   // Opening a file in append mode doesn't set the file pointer to the file's
36   // end on Windows. Do that explicitly.
37   fseek(file_, 0, SEEK_END);
38
39   /* XXX
40   if (ftell(log_file_) == 0) {
41     if (fprintf(log_file_, kFileSignature, kCurrentVersion) < 0) {
42       *err = strerror(errno);
43       return false;
44     }
45   }
46   */
47
48   return true;
49 }
50
51 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
52                          const vector<Node*>& nodes) {
53   // Track whether there's any new data to be recorded.
54   bool made_change = false;
55
56   // Assign ids to all nodes that are missing one.
57   if (node->id() < 0) {
58     RecordId(node);
59     made_change = true;
60   }
61   for (vector<Node*>::const_iterator i = nodes.begin();
62        i != nodes.end(); ++i) {
63     if ((*i)->id() < 0) {
64       RecordId(*i);
65       made_change = true;
66     }
67   }
68
69   // See if the new data is different than the existing data, if any.
70   if (!made_change) {
71     Deps* deps = GetDeps(node);
72     if (!deps ||
73         deps->mtime != mtime ||
74         deps->node_count != (int)nodes.size()) {
75       made_change = true;
76     } else {
77       for (int i = 0; i < (int)nodes.size(); ++i) {
78         if (deps->nodes[i] != nodes[i]) {
79           made_change = true;
80           break;
81         }
82       }
83     }
84   }
85
86   // Don't write anything if there's no new info.
87   if (!made_change)
88     return true;
89
90   uint16_t size = 4 * (1 + 1 + (uint16_t)nodes.size());
91   size |= 0x8000;  // Deps record: set high bit.
92   fwrite(&size, 2, 1, file_);
93   int id = node->id();
94   fwrite(&id, 4, 1, file_);
95   int timestamp = mtime;
96   fwrite(&timestamp, 4, 1, file_);
97   for (vector<Node*>::const_iterator i = nodes.begin();
98        i != nodes.end(); ++i) {
99     id = (*i)->id();
100     fwrite(&id, 4, 1, file_);
101   }
102
103   return true;
104 }
105
106 void DepsLog::Close() {
107   fclose(file_);
108   file_ = NULL;
109 }
110
111 bool DepsLog::Load(const string& path, State* state, string* err) {
112   METRIC_RECORD(".ninja_deps load");
113   char buf[32 << 10];
114   FILE* f = fopen(path.c_str(), "rb");
115   if (!f) {
116     if (errno == ENOENT)
117       return true;
118     *err = strerror(errno);
119     return false;
120   }
121
122   for (;;) {
123     uint16_t size;
124     if (fread(&size, 2, 1, f) < 1)
125       break;
126     bool is_deps = (size >> 15) != 0;
127     size = size & 0x7FFF;
128
129     if (fread(buf, size, 1, f) < 1)
130       break;
131
132     if (is_deps) {
133       assert(size % 4 == 0);
134       int* deps_data = reinterpret_cast<int*>(buf);
135       int out_id = deps_data[0];
136       int mtime = deps_data[1];
137       deps_data += 2;
138       int deps_count = (size / 4) - 2;
139
140       Deps* deps = new Deps;
141       deps->mtime = mtime;
142       deps->node_count = deps_count;
143       deps->nodes = new Node*[deps_count];
144       for (int i = 0; i < deps_count; ++i) {
145         assert(deps_data[i] < (int)nodes_.size());
146         assert(nodes_[deps_data[i]]);
147         deps->nodes[i] = nodes_[deps_data[i]];
148       }
149
150       if (out_id >= (int)deps_.size())
151         deps_.resize(out_id + 1);
152       if (deps_[out_id])
153         delete deps_[out_id];
154       deps_[out_id] = deps;
155     } else {
156       StringPiece path(buf, size);
157       Node* node = state->GetNode(path);
158       assert(node->id() < 0);
159       node->set_id(nodes_.size());
160       nodes_.push_back(node);
161     }
162   }
163   if (ferror(f)) {
164     *err = strerror(ferror(f));
165     return false;
166   }
167   fclose(f);
168   return true;
169 }
170
171 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
172   if (node->id() < 0)
173     return NULL;
174   return deps_[node->id()];
175 }
176
177 bool DepsLog::RecordId(Node* node) {
178   uint16_t size = (uint16_t)node->path().size();
179   fwrite(&size, 2, 1, file_);
180   fwrite(node->path().data(), node->path().size(), 1, file_);
181
182   node->set_id(nodes_.size());
183   nodes_.push_back(node);
184
185   return true;
186 }