Ninja
build_log.h
Go to the documentation of this file.
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 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;
58  int end_time;
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  void set_quiet(bool quiet) { quiet_ = quiet; }
84 
86  const Entries& entries() const { return entries_; }
87 
88  private:
90  FILE* log_file_;
92  bool quiet_;
93 };
94 
95 #endif // NINJA_BUILD_LOG_H_
ExternalStringHashMap< LogEntry * >::Type Entries
Definition: build_log.h:85
bool needs_recompaction_
Definition: build_log.h:91
const Entries & entries() const
Definition: build_log.h:86
TimeStamp restat_mtime
Definition: build_log.h:59
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
bool operator==(const LogEntry &o)
Definition: build_log.h:64
void set_quiet(bool quiet)
Definition: build_log.h:83
int TimeStamp
Definition: timestamp.h:22
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:146
Store a log of every command ran for every build.
Definition: build_log.h:42
uint64_t command_hash
Definition: build_log.h:56
bool quiet_
Definition: build_log.h:92
FILE * log_file_
Definition: build_log.h:90
A template for hash_maps keyed by a StringPiece whose string is owned externally (typically by the va...
Definition: hash_map.h:104
unsigned long long uint64_t
Definition: win32port.h:22
Entries entries_
Definition: build_log.h:89
Can answer questions about the manifest for the BuildLog.
Definition: build_log.h:29