[Compbench-devel] CompBenchmarks++/Benchmark Package.cpp, NONE, 1.1 Package.h, NONE, 1.1
Brought to you by:
xfred
From: Frederic T. <xf...@us...> - 2007-01-03 21:57:35
|
Update of /cvsroot/compbench/CompBenchmarks++/Benchmark In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv21385 Added Files: Package.cpp Package.h Log Message: First import. --- NEW FILE: Package.cpp --- #include <Benchmark/Package.h> #include <Benchmark/Benchmark.h> #include <Benchmark/Benchmark-DLLoader.h> #include <System/System.h> #include <UI/UI.h> #include <cloptions.h> #include <config.h> #include <fstream> #include <iostream> #include <string> CBMPackage::CBMPackage(CBMSystem *_system) { system=_system; status=Unknown; currentCompiler=0; currentOptions=0; } BenchmarkVector& CBMPackage::Benchmarks(void) { return(benchmarks); } CBMBenchmark *CBMPackage::Benchmark(std::string bid) { return(0); } CBMSystem *CBMPackage::System(void) { return(system); } std::string CBMPackage::packageName(void) { return(""); } int CBMPackage::readStatus(void) { char s; std::string r; std::string id; id="bm-status-"; id+=packageName(); r=system->read((char*) id.c_str()); if (r!="") { s=r.c_str()[0]; status=(Status) ((int) s - (int) '0'); return(1); } else { /* !!! */ return(0); } } int CBMPackage::resetContext(void) { std::string contextFile; contextFile=system->temporaryDirectory(CBMSystem::Status); contextFile+="/"; contextFile+="bm-context-"; contextFile+=packageName(); return(system->unlink(contextFile)); } std::string CBMPackage::localPackageAbsoluteName(void) { std::string localFile = system->temporaryDirectory(CBMSystem::Download); localFile+="/"; localFile+=localPackageName(); return(localFile); } std::string CBMPackage::localPackageAbsoluteDirectory(void) { std::string cmd; cmd+=system->temporaryDirectory(CBMSystem::Extract); cmd+="/"; cmd+=extractDirectory(); return(cmd); } int CBMPackage::hasPatch(void) { return(0); } int CBMPackage::patch(int _force) { return(1); } int CBMPackage::hasTest(void) { return(0); } int CBMPackage::test(int _force) { return(1); } int CBMPackage::uninstall(void) { std::string where = localPackageAbsoluteDirectory(); std::string cmd; cmd+=CBM_PROG_RM; cmd+=" -rf "; std::string sstdout; cmd+=where; return(system->exec(cmd, sstdout)==0); } int CBMPackage::storeStatus(Status _status) { int r; char c[16] = { 0 }; std::string tmp; std::string id; id="bm-status-"; id+=packageName(); sprintf(c, "%d", (int) _status); tmp=c; r=system->store((char*) id.c_str(), tmp); status=_status; return(r); } std::string CBMPackage::contextID(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions) { std::string what = _currentCompiler->Binary(); what+="\n"; what+=_currentOptions->Options(); return(what); } int CBMPackage::storeContext(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions) { std::string context = contextID(_currentCompiler, _currentOptions); std::string id; int r; id="bm-context-"; id+=packageName(); r=system->store((char*) id.c_str(), context); return(r); } int CBMPackage::ContextMatches(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions) { std::string previousContext; std::string context; std::string id; id="bm-context-"; id+=packageName(); previousContext=system->read((char*) id.c_str()); context=contextID(_currentCompiler, _currentOptions); if (context==previousContext) { currentCompiler=_currentCompiler; currentOptions=_currentOptions; return(1); } else { return(0); } } int CBMPackage::Download(int _force) { std::string localFile = localPackageAbsoluteName(); std::string expected_md5; std::string md5; std::string info; if ((!_force) && (getStatus()>=Downloaded)) { return(1); } info=packageName(); info+=" "; info+=packageVersion(); UI->Information(CBMUI::BenchDownload, info); try { system->download(downloadURL(), (char*) localFile.c_str()); } catch (...) { } if (cbmSystem->fileExists(localFile)) { expected_md5=expectedMD5(); md5=system->md5File(localFile); if (expected_md5==md5) { UI->Information(CBMUI::ChecksumOK, localFile); storeStatus(Downloaded); return(1); } else { UI->Information(CBMUI::ChecksumFailed, localFile); system->unlink((char*) localFile.c_str()); info="Unsuccesfull download "; info+=downloadURL(); info+=" to "; info+=localFile; UI->Fatal(info); return(0); } } else { info="Can't download "; info+=downloadURL(); info+=" to "; info+=localFile; UI->Fatal(info); system->unlink((char*) localFile.c_str()); } return(0); } int CBMPackage::Extract(int _force) { int r; if ((!_force) && (getStatus()>=Extracted)) { return(1); } UI->Information(CBMUI::BenchExtract, packageName()); r=extract(_force); if (r) storeStatus(Extracted); else { std::string info; info="Error extracting "; info+=localPackageAbsoluteName(); info+=" into "; info+=system->temporaryDirectory(CBMSystem::Extract); UI->Fatal(info); } return(r); } int CBMPackage::Patch(int _force) { int r; if (!hasPatch()) return(1); if ((!_force) && (getStatus()>=Patched)) { return(1); } UI->Information(CBMUI::BenchPatch, packageName()); r=patch(_force); if (r) storeStatus(Patched); else { std::string info; info="Error patching "; info+=localPackageAbsoluteName(); UI->Fatal(info); } return(r); } int CBMPackage::PreConfigure(int _force) { int r; if ((!_force) && (getStatus()>=Preconfigured)) { return(1); } if ( ((!hasPatch()) && (getStatus()<Extracted)) || ((hasPatch()) && (getStatus()<Patched)) ) { return(0); } UI->Information(CBMUI::BenchPreconfigure, packageName()); r=preConfigure(_force); if (r) storeStatus(Preconfigured); else { std::string info; info="Error in "; info+=packageName(); info+="'s pre-configuration"; UI->Fatal(info); } return(r); } int CBMPackage::Configure(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions) { int r; std::string info; if (getStatus()>=Configured) { currentCompiler=_currentCompiler; currentOptions=_currentOptions; return(1); } info=packageName(); info+=" with compiler "; info+=_currentCompiler->Binary(); if (_currentOptions->Options()=="") { info+=" without options"; } else { info+=" and options "; info+=_currentOptions->Options(); } if (language()!=_currentCompiler->language()) { storeContext(_currentCompiler, _currentOptions); info=_currentCompiler->Binary(); info+=" is a "; info+=_currentCompiler->language(); info+=" compiler, while benchmark is written is "; info+=language(); info+="."; UI->Fatal(info); } UI->Information(CBMUI::BenchConfigure, info); r=configure(_currentCompiler, _currentOptions); if (r) { storeStatus(Configured); currentCompiler=_currentCompiler; currentOptions=_currentOptions; } else { info="Configure failed for "; info+=packageName(); UI->Fatal(info); } return(r); } int CBMPackage::Make(int _force) { int r; std::string info; time_t beg; time_t end; if ((getStatus()>=Made) && (!_force)) { return(1); } UI->Information(CBMUI::BenchMake, packageName()); beg=system->Time(); r=make(); if (r) { end=system->Time(); std::cout << "Build time : " << (double) (end-beg) << std::endl; storeStatus(Made); storeContext(currentCompiler, currentOptions); } else { info="Make failed for "; info+=packageName(); UI->Fatal(info); } return(r); } int CBMPackage::Test(int _force) { int r; std::string dum; if (!hasTest()) return(1); if (getStatus()<Configured) { UI->Fatal("Package is not configured"); } if ((!_force) && (getStatus()>=Benchmarked)) { return(1); } if (getStatus()<Made) { return(0); } if (UO_enableTestSuite) { UI->Information(CBMUI::BenchTest, packageName()); r=test(_force); } else { dum=packageName(); dum+=" : passed (disabled by user option)."; UI->Information(CBMUI::BenchTest, dum); r=1; } if (r) storeStatus(Tested); else { std::string info; info="Error testing "; info+=localPackageAbsoluteName(); UI->Fatal(info); } return(r); } int CBMPackage::Release(void) { int r; std::string info; if (getStatus()<Configured) { return(0); } UI->Information(CBMUI::BenchClean, packageName()); r=release(); if (r) { resetContext(); storeStatus(Preconfigured); } else { info="Clean failed for "; info+=packageName(); /* UI->Fatal(info); */ } return(r); } int CBMPackage::Install(int _force) { int r; if (downloadURL()=="") { UI->Fatal("Package is not known to be hosted anymore on the Internet. Use --fetch option."); } resetContext(); r=Download(_force); if (!r) return(0); r=Extract(_force); if (!r) return(0); if (hasPatch()) { r=Patch(); if (!r) return(0); } r=PreConfigure(_force); return(r); } int CBMPackage::Fetch(char *_source, int _force) { std::string dest = localPackageAbsoluteName(); std::string source = _source; std::string expected_md5; std::string md5; if ((getStatus()>=Downloaded) && (!_force)) { return(1); } if (dest!=source) { cbmSystem->copy(source, dest); } if (cbmSystem->fileExists(dest)) { expected_md5=expectedMD5(); md5=system->md5File(dest); if (expected_md5==md5) { UI->Information(CBMUI::ChecksumOK, dest); storeStatus(Downloaded); return(1); } else { UI->Information(CBMUI::ChecksumFailed, dest); } } return(0); } int CBMPackage::Uninstall(void) { std::string statusFile; int r; UI->Information(CBMUI::BenchUninstall, packageName()); statusFile=system->temporaryDirectory(CBMSystem::Status); statusFile+="/"; statusFile+="bm-status-"; statusFile+=packageName(); if (getStatus()!=Unknown) { system->unlink(localPackageAbsoluteName().c_str()); system->unlink(statusFile.c_str()); resetContext(); r=uninstall(); storeStatus(Unknown); currentCompiler=0; currentOptions=0; return(r); } else return(0); } CBMPackage::Status CBMPackage::getStatus(void) { // Status result = Unknown; std::string localFile = localPackageAbsoluteName(); if (status==Unknown) { readStatus(); return(status); } else return(status); } CBMPackage::~CBMPackage() { std::vector<CBMPackage*>::iterator it; if (PACKAGE_AUTOPURGE) { for(it=cbmlib_packages.begin(); it!=cbmlib_packages.end();) { if (*it == this) { cbmlib_packages.erase(it); break; } } } } --- NEW FILE: Package.h --- /* ---------------------------------------------------------------------------- $Id: Package.h,v 1.1 2007/01/03 21:57:31 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #ifndef H_CBMPACKAGE #define H_CBMPACKAGE #include <Compiler/Compiler.h> #include <Compiler/Compiler-Options.h> #include <vector> class CBMSystem; /** Defines a vector of benchmarks. Basically owned by CBMPackage instance. */ typedef std::vector<std::string> BenchmarkVector; /** \brief Defines an abstract package. * * This class can be overloaded in order to get new external packages * supported by CompBenchmarks. Basically it sets up packages (like gzip), * and prepares related benchmarks for evaluation. * * \sa CBMBenchmark */ class CBMPackage { public: /** Package status * Defines typedef used to code internal status of a given package */ typedef enum Status { Unknown, /*!< Unkown; benchmark/package probably * not yet installed */ Downloaded, /*!< Package has been downloaded and checksum * is Ok. */ Extracted, /*!< Package has been successfuly extracted */ Patched, /*!< Package has been patched (optional step) */ Preconfigured, /*!< Package is now preconfigured. Also * optionnal, but each package goes throught * that step */ Configured, /*!< Package is configured (./configure) */ Made, /*!< Package has been made (make) */ Tested, /*!< Package has been tested */ Benchmarked }; /*!< Benchmarked */ private: /** Package status * \sa CBMBenchmark::Status * \sa storeStatus() * \sa getStatus() * \sa readStatus() */ Status status; /** Expected MD5 on downloaded package * \return MD5 expected, as an std::string */ virtual std::string expectedMD5(void) = 0; /** Package's status * Initialise the status from file in the directory defined * by CBMSystem::Status. * * \sa CBMBenchmark::Status * \return 1 if ok. */ virtual int readStatus(void); /** Remove context. * \sa storeContext() */ virtual int resetContext(void); protected: /** \sa Benchmarks() */ BenchmarkVector benchmarks; /** Stores current compiler. */ CBMCompiler *currentCompiler; /** Stores current (compiler) options. */ CBMCompilerOptions *currentOptions; /** System used */ class CBMSystem *system; /** Constructor * Initialise the CBMPackage::system variable. */ CBMPackage(class CBMSystem *_system); /** Pure method to extract package \return 1 if ok \sa Extract() \sa localPackageAbsoluteDirectory() */ virtual int extract(int _force = 0) = 0; /** Indicates if the package needs patching * Must be overloaded if so. Returns 0 by default. * \return 0 by default, 1 if patch is needed. * \sa patch() * \sa Patch() */ virtual int hasPatch(void); /** Method to patch package * Must be overloaded to patch package. * \sa Patch() */ virtual int patch(int _force = 0); /** Method to test package. * Must be overloaded to test package using a method provided by package * maintainers. * \sa Test() */ virtual int test(int _force = 0); /** Pure virtual method to pre-configure package * Pre-configuration creates any needed elements (files or what ever) to benchmark * package. * \return 1 if ok. * \sa PreConfigure() */ virtual int preConfigure(int _force) = 0; /** Pure virtual method to configure package * Broadly speaking, this is ./configure * \sa Configure() * \return 1 if ok */ virtual int configure(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions) = 0; /** Pure virtual method to build package * This could be a simple make, for instance. * \return 1 if ok * \sa Make() */ virtual int make(void) = 0; /** Pure virtual method to clean-up sources * Useful bettween two Make() to get all sources compiled with same environment. * \sa Release() * \sa Configure() * \sa Make() */ virtual int release(void) = 0; /** Uninstall benchmark * Removes information used by CompBenchmarks as well as any downloaded, extracted * or built element. Default behaviour should be ok in most case. * \return 1 if ok. */ virtual int uninstall(void); /** Gets a context ID * Give an unique ID according to compiler and options used. This can avoid useless * recompilations. * \return ID (MD5) as std::string * \sa Make() * \sa storeContext() * \sa ContextMatches() */ virtual std::string contextID(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions); /** Stores (compilation) context ID * Stores the context ID for the current benchmark. * \sa contextID() * \sa ContextMatches() * \return 1 if ok */ virtual int storeContext(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions); public: /** Get system abstraction object \return CBMSystem */ virtual CBMSystem *System(void); /** Get all supported benchmarks. \return Array of std::string. */ BenchmarkVector& Benchmarks(void); /** Get a benchmark according to its identifier. \return References to a CBMBenchmark object */ class CBMBenchmark *Benchmark(std::string bid); /** Stores benchmark status * Probably internal usage only. * \return 1 if ok * \sa readStatus() */ virtual int storeStatus(Status _status); /** Package name * \return a std::string like 'gzip', without quotes */ virtual std::string packageName(void); /** Package version * \return a std::string like '1.2.4', without quotes */ virtual std::string packageVersion(void) = 0; /** Package size, in bytes * \return an integer coding the size of package's archive (to download) in bytes. */ virtual int packageSize(void) = 0; /** Package comments * Gives comments about package. Short description, or what ever. * \return comments, as std::string */ virtual std::string packageComments(void) = 0; /** Package local filename * \return a relative filename containing the local name of the uncompressed * package (like 'gzip-1.2.4.tar.Z', without quotes) */ virtual std::string localPackageName(void) = 0; /** Benchmark's language * \return std::string (C or C++) */ virtual std::string language(void) = 0; /** Returns package or benchmark license \return a std::string describing license */ virtual std::string license(void) = 0; /** Returns package home page \return URL in std::string */ virtual std::string homePage(void) = 0; /** Author(s) of package \return std::string containing authors. */ virtual std::string author(void) = 0; /** Convenience method * \return local uncompressed absolute archive filename for benchmark. * \sa localPackageName() */ virtual std::string localPackageAbsoluteName(void); /** Convenience method * \return directory used for extracting package * \sa CBMSystem::Extract */ virtual std::string localPackageAbsoluteDirectory(void); /** Relative directory to extract package * \return relative directory to use (or used, depending) for package extraction * \sa CBMSystem::Extract */ virtual char* extractDirectory(void) = 0; /** Indicates if the package provides a test method. * Must be overloaded if so. Returns 0 by default. * \return 0 by default, 1 if patch is needed. * \sa test() * \sa Test() */ virtual int hasTest(void); /** URL used for downloading package. * \return std::string representing the URL of the package * \sa expectedMD5() */ virtual std::string downloadURL(void) = 0; /** Total size of the package * \return Total size, in bytes, of thepackage. This comprises data used * in pre-configuration step. * \sa preConfigure() * \sa packageSize() */ virtual int benchmarkSize(void) = 0; /** Compairs previous compilation context to current one * \return 1 if they match. * \sa contextID() * \sa storeContext() * \sa Make() */ virtual int ContextMatches(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions); /** Download package. * Gets downloadURL() object from Internet and verify checksum. * Uses download(). Overloading is unadvised. * This step won't be done and'll return 1 if getStatus()>=CBMPackage::Downloaded. * \param _force sets to 1 to force operation. May changes status. * \return 1 if ok. * \sa status */ virtual int Download(int _force = 0); /** Extract package. * Extracts downloaded package. * Uses extract(). Overloading is unadvised. * This step won't be done and'll return 1 if getStatus()>=CBMPackage::Extracted. * \param _force sets to 1 to force operation. May changes status. * \return 1 if ok. * \sa Download() * \sa status */ virtual int Extract(int _force = 0); /** Patch package. * Patches extracted package. * Overloading is unadvised, 1 is returned without any action, by default. * If hasPatch() returns 1, uses patch(). * This step won't be done and'll return 1 if getStatus()>=CBMPackage::Patched. * \param _force sets to 1 to force operation. May changes status. * \return 1 if ok. * \sa Extract() * \sa status */ virtual int Patch(int _force = 0); /** Preconfigure package. * Preconfigure downloaded package. * Uses preConfigure(). Overloading is unadvised. * This step won't be done and'll return 1 if * getStatus()>=CBMPackage::PreConfigured. May changes status. * \param _force sets to 1 to force operation. * \return 1 if ok. * \sa Patch() * \sa status */ virtual int PreConfigure(int _force = 0); /** Configure package. * Configure downloaded package. * Uses configure(). Overloading is unadvised. Argument are used to keep * compiler and options used for current bench (context), even * if ./configure script is not present : they'll be used by Make(). * This step won't be done and'll return 1 if getStatus()>=CBMPackage::Configured. May changes status. * \return 1 if ok. * \sa Download() * \sa Release() * \sa ContextMatches() * \sa Make(). * \sa status */ virtual int Configure(CBMCompiler *_currentCompiler, CBMCompilerOptions *_currentOptions); /** Build package. * Builds package. * Uses make(). Overloading is unadvised. * This step won't be done and'll return 1 if getStatus()>=CBMPackage::Made. * \param _force sets to 1 to force operation. May changes status. * \return 1 if ok. May changes status. * \sa Configure() * \sa Release() * \sa status * \sa ContextMatches() */ virtual int Make(int _force = 0); /** Test a package. * Test a built package. * Overloading is unadvised, 1 is returned without any action, by default. * If hasPatch() returns 1, uses patch(). * This step won't be done and'll return 1 if getStatus()>=CBMPackage::Benchmarked. * \param _force sets to 1 to force operation. May changes status. * \return 1 if ok. * \sa Make() * \sa status */ virtual int Test(int _force = 0); /** Clean objects. * Uses release(). Overloading is unadvised. * This step won't be done and'll return 0 if getStatus()<CBMPackage::Made. May changes status. * \return 1 if ok. * \sa Bench() */ virtual int Release(void); /** Install package. * Installs package, uses, in that order Download(), Extract(), Patch() (if needed) * and PreConfigure(). Overloading is unadvised. * This step won't be done and'll return 1 if getStatus()>=CBMPackage::PreConfigured. * \param _force assign to 1 for forcing operation. * \return 1 if ok. May changes status. * \sa status */ virtual int Install(int _force = 0); /** Copy a local archive. * Copy package to directory defined by CBMSystem::Download. It'll be ready * to be extracted using Install(). * \param _source the archive file * \param _force assign to 1 for forcing operation. * \return 1 if ok. May changes status. * \sa status * \sa extract */ virtual int Fetch(char *_source, int _force = 0); /** Uninstall package. * Uses uninstall(). Overloading is unadvised. * This step won't be done and'll return 1 if getStatus()==CBMPackage::Unknown. * \return 1 if ok. May changes status. * \sa status */ virtual int Uninstall(void); /** Returns benchmark's status * \return status (enum). * \sa CBMBenchmark::Status */ virtual Status getStatus(void); /** Virtual destructor */ virtual ~CBMPackage(); }; #endif |