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 /// Can answer questions about the manifest for the BuildLog.
00029 struct BuildLogUser {
00030   /// Return if a given output no longer part of the build manifest.
00031   /// This is only called during recompaction and doesn't have to be fast.
00032   virtual bool IsPathDead(StringPiece s) const = 0;
00033 };
00034 
00035 /// Store a log of every command ran for every build.
00036 /// It has a few uses:
00037 ///
00038 /// 1) (hashes of) command lines for existing output files, so we know
00039 ///    when we need to rebuild due to the command changing
00040 /// 2) timing information, perhaps for generating reports
00041 /// 3) restat information
00042 struct BuildLog {
00043   BuildLog();
00044   ~BuildLog();
00045 
00046   bool OpenForWrite(const string& path, const BuildLogUser& user, string* err);
00047   bool RecordCommand(Edge* edge, int start_time, int end_time,
00048                      TimeStamp restat_mtime = 0);
00049   void Close();
00050 
00051   /// Load the on-disk log.
00052   bool Load(const string& path, string* err);
00053 
00054   struct LogEntry {
00055     string output;
00056     uint64_t command_hash;
00057     int start_time;
00058     int end_time;
00059     TimeStamp restat_mtime;
00060 
00061     static uint64_t HashCommand(StringPiece command);
00062 
00063     // Used by tests.
00064     bool operator==(const LogEntry& o) {
00065       return output == o.output && command_hash == o.command_hash &&
00066           start_time == o.start_time && end_time == o.end_time &&
00067           restat_mtime == o.restat_mtime;
00068     }
00069 
00070     explicit LogEntry(const string& output);
00071     LogEntry(const string& output, uint64_t command_hash,
00072              int start_time, int end_time, TimeStamp restat_mtime);
00073   };
00074 
00075   /// Lookup a previously-run command by its output path.
00076   LogEntry* LookupByOutput(const string& path);
00077 
00078   /// Serialize an entry into a log file.
00079   bool WriteEntry(FILE* f, const LogEntry& entry);
00080 
00081   /// Rewrite the known log entries, throwing away old data.
00082   bool Recompact(const string& path, const BuildLogUser& user, string* err);
00083 
00084   typedef ExternalStringHashMap<LogEntry*>::Type Entries;
00085   const Entries& entries() const { return entries_; }
00086 
00087  private:
00088   Entries entries_;
00089   FILE* log_file_;
00090   bool needs_recompaction_;
00091 };
00092 
00093 #endif // NINJA_BUILD_LOG_H_