Ninja
build_log.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #ifndef NINJA_BUILD_LOG_H_
00016 #define NINJA_BUILD_LOG_H_
00017 
00018 #include <string>
00019 #include <stdio.h>
00020 using namespace std;
00021 
00022 #include "hash_map.h"
00023 #include "timestamp.h"
00024 #include "util.h"  // uint64_t
00025 
00026 struct Edge;
00027 
00028 /// Store a log of every command ran for every build.
00029 /// It has a few uses:
00030 ///
00031 /// 1) (hashes of) command lines for existing output files, so we know
00032 ///    when we need to rebuild due to the command changing
00033 /// 2) timing information, perhaps for generating reports
00034 /// 3) restat information
00035 struct BuildLog {
00036   BuildLog();
00037   ~BuildLog();
00038 
00039   bool OpenForWrite(const string& path, string* err);
00040   bool RecordCommand(Edge* edge, int start_time, int end_time,
00041                      TimeStamp restat_mtime = 0);
00042   void Close();
00043 
00044   /// Load the on-disk log.
00045   bool Load(const string& path, string* err);
00046 
00047   struct LogEntry {
00048     string output;
00049     uint64_t command_hash;
00050     int start_time;
00051     int end_time;
00052     TimeStamp restat_mtime;
00053 
00054     static uint64_t HashCommand(StringPiece command);
00055 
00056     // Used by tests.
00057     bool operator==(const LogEntry& o) {
00058       return output == o.output && command_hash == o.command_hash &&
00059           start_time == o.start_time && end_time == o.end_time &&
00060           restat_mtime == o.restat_mtime;
00061     }
00062 
00063     explicit LogEntry(const string& output);
00064     LogEntry(const string& output, uint64_t command_hash,
00065              int start_time, int end_time, TimeStamp restat_mtime);
00066   };
00067 
00068   /// Lookup a previously-run command by its output path.
00069   LogEntry* LookupByOutput(const string& path);
00070 
00071   /// Serialize an entry into a log file.
00072   bool WriteEntry(FILE* f, const LogEntry& entry);
00073 
00074   /// Rewrite the known log entries, throwing away old data.
00075   bool Recompact(const string& path, string* err);
00076 
00077   typedef ExternalStringHashMap<LogEntry*>::Type Entries;
00078   const Entries& entries() const { return entries_; }
00079 
00080  private:
00081   Entries entries_;
00082   FILE* log_file_;
00083   bool needs_recompaction_;
00084 };
00085 
00086 #endif // NINJA_BUILD_LOG_H_