• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "load_status.h"
24 #include "timestamp.h"
25 #include "util.h"  // uint64_t
26 
27 struct DiskInterface;
28 struct Edge;
29 
30 /// Can answer questions about the manifest for the BuildLog.
31 struct BuildLogUser {
32   /// Return if a given output is no longer part of the build manifest.
33   /// This is only called during recompaction and doesn't have to be fast.
34   virtual bool IsPathDead(StringPiece s) const = 0;
35 };
36 
37 /// Store a log of every command ran for every build.
38 /// It has a few uses:
39 ///
40 /// 1) (hashes of) command lines for existing output files, so we know
41 ///    when we need to rebuild due to the command changing
42 /// 2) timing information, perhaps for generating reports
43 /// 3) restat information
44 struct BuildLog {
45   BuildLog();
46   ~BuildLog();
47 
48   /// Prepares writing to the log file without actually opening it - that will
49   /// happen when/if it's needed
50   bool OpenForWrite(const string& path, const BuildLogUser& user, string* err);
51 
52   bool RecordCommand(Edge* edge, int start_time, int end_time,
53                      TimeStamp mtime = 0);
54   void Close();
55 
56   /// Load the on-disk log.
57   LoadStatus Load(const string& path, string* err);
58 
59   struct LogEntry {
60     string output;
61     uint64_t command_hash;
62     int start_time;
63     int end_time;
64     TimeStamp mtime;
65 
66     static uint64_t HashCommand(StringPiece command);
67 
68     // Used by tests.
69     bool operator==(const LogEntry& o) {
70       return output == o.output && command_hash == o.command_hash &&
71           start_time == o.start_time && end_time == o.end_time &&
72           mtime == o.mtime;
73     }
74 
75     explicit LogEntry(const string& output);
76     LogEntry(const string& output, uint64_t command_hash,
77              int start_time, int end_time, TimeStamp restat_mtime);
78   };
79 
80   /// Lookup a previously-run command by its output path.
81   LogEntry* LookupByOutput(const string& path);
82 
83   /// Serialize an entry into a log file.
84   bool WriteEntry(FILE* f, const LogEntry& entry);
85 
86   /// Rewrite the known log entries, throwing away old data.
87   bool Recompact(const string& path, const BuildLogUser& user, string* err);
88 
89   /// Restat all outputs in the log
90   bool Restat(StringPiece path, const DiskInterface& disk_interface,
91               int output_count, char** outputs, std::string* err);
92 
93   typedef ExternalStringHashMap<LogEntry*>::Type Entries;
entriesBuildLog94   const Entries& entries() const { return entries_; }
95 
96  private:
97   /// Should be called before using log_file_. When false is returned, errno
98   /// will be set.
99   bool OpenForWriteIfNeeded();
100 
101   Entries entries_;
102   FILE* log_file_;
103   std::string log_file_path_;
104   bool needs_recompaction_;
105 };
106 
107 #endif // NINJA_BUILD_LOG_H_
108