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