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
This commit is contained in:
Wilson Snyder 2008-01-31 13:50:06 +00:00
parent 8bc1e75d9f
commit ad591767c9
5 changed files with 71 additions and 7 deletions

View File

@ -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.

View File

@ -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();
};
//============================================================================

View File

@ -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

View File

@ -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;

View File

@ -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();