From abe69082514b61181c6bc15a341895c971ecdc43 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 4 Jan 2010 23:24:55 +0000 Subject: The cache works...really well. --- src/cache.cpp | 31 ++++++++++++++++++++++++++++--- src/cache.h | 7 +++++-- src/conditionfiletime.cpp | 5 ++++- src/main.cpp | 14 ++++++++++++++ src/target.cpp | 17 +++++++++++++++++ src/target.h | 6 ++++++ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/cache.cpp b/src/cache.cpp index 1646d0a..de505b8 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -1,14 +1,18 @@ #include "cache.h" #include #include +#include +using namespace Bu; Cache::Cache() : + bCacheChanged( false ), bIsLoaded( false ) { } Cache::~Cache() { + save(); } void Cache::bind( const Bu::FString &sCacheFile ) @@ -21,19 +25,40 @@ void Cache::load() { if( bIsLoaded ) return; - Bu::File fIn( sCacheFile, Bu::File::Read ); - Bu::Archive ar( fIn, Bu::Archive::load ); - ar >> hRequires >> hVariables; + try + { + Bu::File fIn( sCacheFile, Bu::File::Read ); + Bu::Archive ar( fIn, Bu::Archive::load ); + + ar >> hRequires >> hVariables; + } + catch(...) { } bIsLoaded = true; } void Cache::save() { + if( !bIsLoaded ) + return; + if( bCacheChanged == false ) + return; + Bu::File fIn( sCacheFile, Bu::File::WriteNew ); Bu::Archive ar( fIn, Bu::Archive::save ); ar << hRequires << hVariables; } +StrList Cache::getRequires( const Bu::FString &sOutput ) +{ + return hRequires.get( sOutput ); +} + +void Cache::setRequires( const Bu::FString &sOutput, StrList lReqs ) +{ + hRequires.insert( sOutput, lReqs ); + bCacheChanged = true; +} + diff --git a/src/cache.h b/src/cache.h index 31da981..8932091 100644 --- a/src/cache.h +++ b/src/cache.h @@ -7,6 +7,7 @@ #include #include "variable.h" +#include "types.h" class Cache : public Bu::Singleton { @@ -21,12 +22,14 @@ public: void load(); void save(); - + StrList getRequires( const Bu::FString &sOutput ); + void setRequires( const Bu::FString &sOutput, StrList lReqs ); private: + bool bCacheChanged; Bu::FString sCacheFile; bool bIsLoaded; - typedef Bu::Hash > ReqHash; + typedef Bu::Hash ReqHash; ReqHash hRequires; typedef Bu::Hash VarHash; VarHash hVariables; diff --git a/src/conditionfiletime.cpp b/src/conditionfiletime.cpp index 224caf1..148ffac 100644 --- a/src/conditionfiletime.cpp +++ b/src/conditionfiletime.cpp @@ -25,6 +25,7 @@ bool ConditionFileTime::shouldExec( class Runner &r, Target &rTarget ) //sio << "-- Target processed because '" << *j << "' doesn't exist." // << sio.nl; // Output doesn't exist + rTarget.buildRequires( r ); return true; } } @@ -48,10 +49,11 @@ bool ConditionFileTime::shouldExec( class Runner &r, Target &rTarget ) { //sio << "-- Target processed because '" << *j // << "' is newer than output." << sio.nl; + rTarget.buildRequires( r ); return true; } } - rTarget.buildRequires( r ); + rTarget.gatherRequires( r ); for( StrList::const_iterator j = rTarget.getRequiresList().begin(); j; j++ ) { @@ -60,6 +62,7 @@ bool ConditionFileTime::shouldExec( class Runner &r, Target &rTarget ) { //sio << "-- Target processed because '" << *j // << "' is newer than output." << sio.nl; + rTarget.buildRequires( r ); return true; } } diff --git a/src/main.cpp b/src/main.cpp index 142927d..1bed895 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,8 @@ #include "viewplugger.h" +#include "cache.h" + #include #include #include @@ -24,11 +26,13 @@ public: sView("default"), sAction("default"), sConfig("default.bld"), + sCacheFile(".build_cache"), bDot( false ), bDebug( false ), bAutoInclude( true ), bAstDump( false ), bEnviron( true ), + bCache( true ), iInfoLevel( 0 ) { bool bClean = false; @@ -54,6 +58,8 @@ public: "action is specified, this will modify it to run 'clean-action'."); addOption( slot(this, &Options::onChdir), 'C', "chdir", "Change to directory before doing anything else."); + addOption( sCacheFile, "cache", "Select a different cache file."); + addOption( bCache, "no-cache", "Disable using the cache."); addHelpBanner("\nThe following options control debugging:"); addOption( bEnviron, "no-env", "Do not import environment variables."); @@ -71,6 +77,7 @@ public: setOverride( "debug-ast", "true" ); setOverride( "info", "1" ); setOverride( 'c', "true" ); + setOverride( "no-cache", "false" ); addHelpOption(); @@ -111,11 +118,13 @@ public: Bu::FString sView; Bu::FString sAction; Bu::FString sConfig; + Bu::FString sCacheFile; bool bDot; bool bDebug; bool bAutoInclude; bool bAstDump; bool bEnviron; + bool bCache; int iInfoLevel; }; @@ -155,6 +164,11 @@ int main( int argc, char *argv[] ) return 1; } + if( opts.bCache ) + { + Cache::getInstance().bind( opts.sCacheFile ); + } + // Load up the environment as vars. if( opts.bEnviron ) { diff --git a/src/target.cpp b/src/target.cpp index 1846044..4f9dd77 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -7,6 +7,7 @@ #include "context.h" #include "profile.h" #include "view.h" +#include "cache.h" #include #include @@ -76,6 +77,19 @@ const StrList &Target::getRequiresList() const return lsRequires; } +void Target::gatherRequires( Runner &r ) +{ + Cache &c = Cache::getInstance(); + try + { + lsRequires = c.getRequires( lsOutput.first() ); + } + catch( Bu::HashException &e ) + { + buildRequires( r ); + } +} + void Target::buildRequires( Runner &r ) { r.getContext().getView()->buildRequires( *this ); @@ -108,6 +122,9 @@ void Target::buildRequires( Runner &r ) } } r.getContext().popScope(); + + Cache &c = Cache::getInstance(); + c.setRequires( lsOutput.first(), lsRequires ); } void Target::addOutput( const Bu::FString &sOutput ) diff --git a/src/target.h b/src/target.h index 766366a..25af1f4 100644 --- a/src/target.h +++ b/src/target.h @@ -19,6 +19,12 @@ public: void addRequires( const Bu::FString &sReq ); void addRequires( const AstBranch *pBr ); const StrList &getRequiresList() const; + /** + * This function will get the cached requires if they exist, build them + * if they don't. Use this is conditions, but use buildRequires to force + * a rebuild even if cached data exists. + */ + void gatherRequires( class Runner &r ); void buildRequires( class Runner &r ); void addOutput( const Bu::FString &sOutput ); -- cgit v1.2.3