Imported Upstream version 1.7.1
[platform/upstream/ninja.git] / src / build_log.h
1 // Copyright 2011 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 #ifndef NINJA_BUILD_LOG_H_
16 #define NINJA_BUILD_LOG_H_
17
18 #include <string>
19 #include <stdio.h>
20 using namespace std;
21
22 #include "hash_map.h"
23 #include "timestamp.h"
24 #include "util.h"  // uint64_t
25
26 struct Edge;
27
28 /// Can answer questions about the manifest for the BuildLog.
29 struct BuildLogUser {
30   /// Return if a given output is no longer part of the build manifest.
31   /// This is only called during recompaction and doesn't have to be fast.
32   virtual bool IsPathDead(StringPiece s) const = 0;
33 };
34
35 /// Store a log of every command ran for every build.
36 /// It has a few uses:
37 ///
38 /// 1) (hashes of) command lines for existing output files, so we know
39 ///    when we need to rebuild due to the command changing
40 /// 2) timing information, perhaps for generating reports
41 /// 3) restat information
42 struct BuildLog {
43   BuildLog();
44   ~BuildLog();
45
46   bool OpenForWrite(const string& path, const BuildLogUser& user, string* err);
47   bool RecordCommand(Edge* edge, int start_time, int end_time,
48                      TimeStamp restat_mtime = 0);
49   void Close();
50
51   /// Load the on-disk log.
52   bool Load(const string& path, string* err);
53
54   struct LogEntry {
55     string output;
56     uint64_t command_hash;
57     int start_time;
58     int end_time;
59     TimeStamp restat_mtime;
60
61     static uint64_t HashCommand(StringPiece command);
62
63     // Used by tests.
64     bool operator==(const LogEntry& o) {
65       return output == o.output && command_hash == o.command_hash &&
66           start_time == o.start_time && end_time == o.end_time &&
67           restat_mtime == o.restat_mtime;
68     }
69
70     explicit LogEntry(const string& output);
71     LogEntry(const string& output, uint64_t command_hash,
72              int start_time, int end_time, TimeStamp restat_mtime);
73   };
74
75   /// Lookup a previously-run command by its output path.
76   LogEntry* LookupByOutput(const string& path);
77
78   /// Serialize an entry into a log file.
79   bool WriteEntry(FILE* f, const LogEntry& entry);
80
81   /// Rewrite the known log entries, throwing away old data.
82   bool Recompact(const string& path, const BuildLogUser& user, string* err);
83
84   typedef ExternalStringHashMap<LogEntry*>::Type Entries;
85   const Entries& entries() const { return entries_; }
86
87  private:
88   Entries entries_;
89   FILE* log_file_;
90   bool needs_recompaction_;
91 };
92
93 #endif // NINJA_BUILD_LOG_H_