From ad591767c9a7182564923d3a52e80993953621bb Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Thu, 31 Jan 2008 13:50:06 +0000 Subject: [PATCH] Make obj_dir only when needed, and use OS calls rather than system to clean up git-svn-id: file://localhost/svn/verilator/trunk/verilator@984 77ca24e4-aefa-0310-84f0-b9a241c72d87 --- src/V3File.cpp | 8 ++++++++ src/V3File.h | 6 ++++++ src/V3Options.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++ src/V3Options.h | 5 +++++ src/Verilator.cpp | 10 +++------- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/V3File.cpp b/src/V3File.cpp index 6c053c8dc..974b9c18d 100644 --- a/src/V3File.cpp +++ b/src/V3File.cpp @@ -232,6 +232,14 @@ bool V3File::checkTimes(const string& filename, const string& cmdline) { return dependImp.checkTimes(filename, cmdline); } +void V3File::createMakeDir() { + static bool created = false; + if (!created) { + created = true; + mkdir(v3Global.opt.makeDir().c_str(), 0777); + } +} + //###################################################################### // V3OutFile: A class for printing to a file, with automatic indentation of C++ code. diff --git a/src/V3File.h b/src/V3File.h index 6d5835005..84f0967e5 100644 --- a/src/V3File.h +++ b/src/V3File.h @@ -46,6 +46,7 @@ public: return new_ofstream_nodepend (filename, append); } static ofstream* new_ofstream_nodepend(const string& filename, bool append=false) { + createMakeDir(); if (append) { return new ofstream(filename.c_str(), ios::app); } else { @@ -53,15 +54,20 @@ public: } } static FILE* new_fopen_w(const string& filename) { + createMakeDir(); addTgtDepend(filename); return fopen(filename.c_str(),"w"); } + // Dependencies static void addSrcDepend(const string& filename); static void addTgtDepend(const string& filename); static void writeDepend(const string& filename); static void writeTimes(const string& filename, const string& cmdline); static bool checkTimes(const string& filename, const string& cmdline); + + // Directory utilities + static void createMakeDir(); }; //============================================================================ diff --git a/src/V3Options.cpp b/src/V3Options.cpp index 0ccf39c6b..5d3adb869 100644 --- a/src/V3Options.cpp +++ b/src/V3Options.cpp @@ -226,6 +226,55 @@ string V3Options::filePath (FileLine* fl, const string& modname, const string& e return ""; } +void V3Options::unlinkRegexp(const string& dir, const string& regexp) { + if (DIR* dirp = opendir(dir.c_str())) { + while (struct dirent* direntp = readdir(dirp)) { + if (wildmatch(direntp->d_name, regexp.c_str())) { + string fullname = dir + "/" + string(direntp->d_name); + unlink (fullname.c_str()); + } + } + closedir(dirp); + } +} + +// Double procedures, inlined, unrolls loop much better +inline bool V3Options::wildmatchi(const char* s, const char* p) { + for ( ; *p; s++, p++) { + if (*p!='*') { + if (((*s)!=(*p)) && *p != '?') + return false; + } + else { + // Trailing star matches everything. + if (!*++p) return true; + while (wildmatch(s, p) == false) + if (*++s == '\0') + return false; + return true; + } + } + return(*s == '\0' || *s == '['); +} + +bool V3Options::wildmatch(const char* s, const char* p) { + for ( ; *p; s++, p++) { + if (*p!='*') { + if (((*s)!=(*p)) && *p != '?') + return false; + } + else { + // Trailing star matches everything. + if (!*++p) return true; + while (wildmatchi(s, p) == false) + if (*++s == '\0') + return false; + return true; + } + } + return(*s == '\0' || *s == '['); +} + //###################################################################### // V3 Options accessors diff --git a/src/V3Options.h b/src/V3Options.h index 284330c01..01e33ac55 100644 --- a/src/V3Options.h +++ b/src/V3Options.h @@ -115,6 +115,7 @@ class V3Options { void optimize(int level); void coverage(bool flag) { m_coverageLine = m_coverageUser = flag; } bool onoff(const char* sw, const char* arg, bool& flag); + static bool wildmatchi(const char* s, const char* p); public: // CREATORS @@ -203,12 +204,16 @@ class V3Options { void parseOptsList (FileLine* fl, int argc, char** argv); void parseOptsFile (FileLine* fl, const string& filename); + // METHODS (generic string utilities) + static bool wildmatch(const char* s, const char* p); + // METHODS (generic file utilities) static string filenameFromDirBase (const string& dir, const string& basename); static string filenameNonDir (const string& filename); ///< Return non-directory part of filename static string filenameNonExt (const string& filename); ///< Return non-extensioned (no .) part of filename static string filenameNonDirExt (const string& filename) { return filenameNonExt(filenameNonDir(filename)); } ///< Return basename of filename static string filenameDir (const string& filename); ///< Return directory part of filename + static void unlinkRegexp(const string& dir, const string& regexp); static string getenvStr(const char* envvar, const char* defaultValue) { if (const char* envvalue = getenv(envvar)) { return envvalue; diff --git a/src/Verilator.cpp b/src/Verilator.cpp index 958b514fc..c1e399af7 100644 --- a/src/Verilator.cpp +++ b/src/Verilator.cpp @@ -521,13 +521,9 @@ int main(int argc, char** argv, char** env) { //--FRONTEND------------------ // Cleanup - mkdir(v3Global.opt.makeDir().c_str(), 0777); - string cleanFilename = "/bin/rm -rf "+v3Global.opt.makeDir()+"/"+v3Global.opt.prefix()+"_*.tree"; - system(cleanFilename.c_str()); - cleanFilename = "/bin/rm -rf "+v3Global.opt.makeDir()+"/"+v3Global.opt.prefix()+"_*.dot"; - system(cleanFilename.c_str()); - cleanFilename = "/bin/rm -rf "+v3Global.opt.makeDir()+"/"+v3Global.opt.prefix()+"_*.txt"; - system(cleanFilename.c_str()); + V3Options::unlinkRegexp(v3Global.opt.makeDir(), v3Global.opt.prefix()+"_*.tree"); + V3Options::unlinkRegexp(v3Global.opt.makeDir(), v3Global.opt.prefix()+"_*.dot"); + V3Options::unlinkRegexp(v3Global.opt.makeDir(), v3Global.opt.prefix()+"_*.txt"); // Read first filename v3Global.readFiles();