ENH: add -i(ignore) option to wmkdep, wmkdepend

- specifies headers that should be ignored. Provides a workaround
  for the fact that wmkdep/wmkdepend doesn't handle defines at all.

STYLE: code cleanup of wmkdep.l
This commit is contained in:
Mark Olesen
2010-03-09 16:16:17 +01:00
parent 27ecc9177c
commit d4054f6b1f
7 changed files with 273 additions and 208 deletions

View File

@ -22,7 +22,7 @@
# along with OpenFOAM; if not, write to the Free Software Foundation, # along with OpenFOAM; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# #
# Script # File
# Makefile # Makefile
# #
# Description # Description

56
wmake/src/makeParserCode Executable file
View File

@ -0,0 +1,56 @@
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
# \\/ M anipulation |
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM.
#
# OpenFOAM is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenFOAM; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script
# makeParserCode
#
# Description
# Use coco-cpp to create parser code
#
#------------------------------------------------------------------------------
cd ${0%/*} || exit 1 # run from this directory
bindir=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/coco-cpp/bin
frames=$WM_THIRD_PARTY_DIR/platforms/share/coco-cpp
[ -d "$bindir" -a -x "$bindir/coco-cpp" ] || {
echo "no coco-cpp binary found"
exit 1
}
[ -d "$frames" -a -f "$frames/Parser.frame" -a -f "$frames/Scanner.frame" ] || {
echo "no coco-cpp frames found"
exit 1
}
# run coco-cpp:
$bindir/coco-cpp -frames $frames wmkdependParser.atg
echo
echo Done
#------------------------------------------------------------------------------

View File

@ -36,7 +36,7 @@ Description
only once. This is why this program is faster than cpp. only once. This is why this program is faster than cpp.
Usage Usage
wmkdep [ -Idirectory ... -Idirectory ] filename wmkdep [ -Idir ... -Idir ] [ -iheader .. -iheader ] filename
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -50,7 +50,7 @@ Usage
void nextFile(const char* fileName); void nextFile(const char* fileName);
void importFile(const char* fileName); void importFile(const char* fileName);
void importDirectory(const char* dirName); void importDir(const char* dirName);
# undef yywrap /* sometimes a macro by default */ # undef yywrap /* sometimes a macro by default */
@ -76,7 +76,7 @@ void importDirectory(const char* dirName);
<JFNAME>sun.*; BEGIN(INITIAL); <JFNAME>sun.*; BEGIN(INITIAL);
<JFNAME>launcher.*; BEGIN(INITIAL); <JFNAME>launcher.*; BEGIN(INITIAL);
<JFNAME>[^"\n*]*; { BEGIN(INITIAL); importFile(yytext); } /*"*/ <JFNAME>[^"\n*]*; { BEGIN(INITIAL); importFile(yytext); } /*"*/
<JFNAME>[^"\n]*\*; { BEGIN(INITIAL); importDirectory(yytext); } /*"*/ <JFNAME>[^"\n]*\*; { BEGIN(INITIAL); importDir(yytext); } /*"*/
" "include[ \t]+\' BEGIN(FFNAME); /* FORTRAN-file name */ " "include[ \t]+\' BEGIN(FFNAME); /* FORTRAN-file name */
<FFNAME>[^']* { BEGIN(INITIAL); nextFile(yytext); } /*'*/ <FFNAME>[^']* { BEGIN(INITIAL); nextFile(yytext); } /*'*/
@ -86,160 +86,24 @@ void importDirectory(const char* dirName);
%% %%
int nDirectories; /* char* entry in hash table */
char** directories; struct HashEntry
char* sourceFile = NULL;
char* sourceExt = NULL;
char* objectFile = NULL;
char* classFile = NULL;
char* depFile = NULL;
int main(int argc, char* argv[])
{
char *dotPtr, *slashPtr;
int i;
if (argc == 1)
{
fprintf(stderr,"input file not supplied\n");
exit(1);
}
sourceFile = (char*)malloc(strlen(argv[argc-1]) + 1);
strcpy(sourceFile, argv[argc-1]);
fprintf(stderr, "Making dependency list for source file %s\n", sourceFile);
/* Get list of -I directories. */
nDirectories = 0;
for (i = 1; i < argc; i++)
{
if (strncmp(argv[i], "-I", 2) == 0)
{
nDirectories++;
}
}
directories = (char**)malloc(sizeof(char*)*nDirectories);
nDirectories = 0;
for (i = 1; i < argc; i++)
{
if (strncmp(argv[i], "-I", 2) == 0)
{
directories[nDirectories++] = strdup(argv[i] + 2);
}
}
if ((dotPtr = strrchr(sourceFile, '.')) == NULL)
{
fprintf
(
stderr,
"Cannot find extension in source file name %s\n",
sourceFile
);
exit(1);
}
if ((slashPtr = strrchr(sourceFile, '/')) == NULL)
{
slashPtr = sourceFile;
}
else
{
slashPtr++;
}
sourceExt = (char*)malloc(strlen(sourceFile));
sourceExt[0] = 0;
strncat
(
sourceExt,
dotPtr+1,
(&sourceFile[strlen(sourceFile) - 1] - dotPtr)/sizeof(char)
);
/*
* initialise depFile to zero and use strncat rather than strncpy
* because there is a bug in the SGI strncat that if 0 preceeds the '.'
* it inserts a space
*/
depFile = (char*)malloc(strlen(sourceFile) + 3);
depFile[0] = 0;
strncat(depFile, sourceFile, (dotPtr - sourceFile)/sizeof(char));
strcat(depFile, ".dep");
if (strcmp(sourceExt, "java") == 0)
{
classFile = (char*)malloc(strlen(sourceFile) + 17);
strcpy(classFile, "$(CLASSES_DIR)/");
strncat(classFile, sourceFile, (dotPtr - sourceFile)/sizeof(char));
strcat(classFile, ".class");
printf("%s: %s\n", classFile, depFile);
}
else
{
objectFile = (char*)malloc(strlen(sourceFile) + 16);
strcpy(objectFile, "$(OBJECTS_DIR)/");
strncat(objectFile, slashPtr, (dotPtr - slashPtr)/sizeof(char));
strcat(objectFile, ".o");
printf("%s: %s\n", objectFile, depFile);
}
nextFile(sourceFile);
yylex();
for (i = 0; i < nDirectories; i++)
{
free(directories[i]);
}
free(directories);
free(sourceFile);
free(sourceExt);
free(objectFile);
free(classFile);
free(depFile);
return 0;
}
int currentBuffer = 0; /* Buffer pointer stack counter */
YY_BUFFER_STATE buffers[FILE_STACK_SIZE]; /* Buffer pointer stack */
/* file name entry in hash table */
struct FileName
{ {
char* name; char* name;
struct FileName* next; struct HashEntry* next;
}; };
/* Set of files already visited */
struct FileName* visitedFiles[HASH_TABLE_SIZE];
/* Set of (java) directories already visited */
struct FileName* visitedDirs[HASH_TABLE_SIZE];
/* /*
* lookup name in hash table, if not found insert in table * lookup name in hash table.
* if found - return 1
* if not found - insert in table and return 0
*/ */
int lookUp(struct FileName** hashTable, const char* p) int lookUp(struct HashEntry** hashTable, const char* p)
{ {
int ii = 0; int ii = 0;
struct FileName* n; struct HashEntry* n;
struct FileName* nn; struct HashEntry* nn;
/* hash */ /* hash */
const char* pp = p; const char* pp = p;
@ -258,9 +122,8 @@ int lookUp(struct FileName** hashTable, const char* p)
} }
/* insert */ /* insert */
nn = (struct FileName*)malloc(sizeof(struct FileName)); nn = (struct HashEntry*)malloc(sizeof(struct HashEntry));
nn->name = (char*)malloc(strlen(p)+1); nn->name = strdup(p);
strcpy(nn->name, p);
nn->next = hashTable[ii]; nn->next = hashTable[ii];
hashTable[ii] = nn; hashTable[ii] = nn;
@ -269,17 +132,152 @@ int lookUp(struct FileName** hashTable, const char* p)
} }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int nDirectories = 0;
char** directories;
char* sourceFile = NULL;
char* depFile = NULL;
/* Set of files already visited */
struct HashEntry* visitedFiles[HASH_TABLE_SIZE];
/* Set of (java) directories already visited */
struct HashEntry* visitedDirs[HASH_TABLE_SIZE];
int main(int argc, char* argv[])
{
char *basePos, *dotPos;
int i;
if (argc == 1)
{
fprintf(stderr, "input file not supplied\n");
exit(1);
}
sourceFile = strdup(argv[argc-1]);
fprintf(stderr, "Making dependency list for source file %s\n", sourceFile);
if ((basePos = strrchr(sourceFile, '/')) == NULL)
{
basePos = sourceFile;
}
else
{
basePos++;
}
if
(
(dotPos = strrchr(sourceFile, '.')) == NULL
|| (dotPos < basePos)
)
{
fprintf
(
stderr,
"cannot find extension in source file name %s\n",
sourceFile
);
exit(1);
}
/* count number of -I directories */
nDirectories = 0;
for (i = 1; i < argc; i++)
{
if (strncmp(argv[i], "-I", 2) == 0)
{
if (strlen(argv[i]) > 2)
{
nDirectories++;
}
}
}
directories = (char**)malloc(sizeof(char*)*nDirectories);
/* build list of -I directories and add -i ignores */
nDirectories = 0;
for (i = 1; i < argc; i++)
{
if (strncmp(argv[i], "-I", 2) == 0)
{
if (strlen(argv[i]) > 2)
{
directories[nDirectories++] = strdup(argv[i] + 2);
}
}
else if (strncmp(argv[i], "-i", 2) == 0)
{
if (strlen(argv[i]) > 2)
{
lookUp(visitedFiles, (argv[i] + 2));
}
}
}
/*
* initialise depFile to zero and use strncat rather than strncpy
* because there is a bug in the SGI strncat that if 0 preceeds the '.'
* it inserts a space
*/
depFile = (char*)malloc(strlen(sourceFile) + 3);
depFile[0] = 0;
strncat(depFile, sourceFile, (dotPos - sourceFile)/sizeof(char));
strcat(depFile, ".dep");
if (strcmp(dotPos, ".java") == 0)
{
char *classFile = strdup(sourceFile);
classFile[(dotPos - sourceFile)/sizeof(char)] = 0;
printf("$(CLASSES_DIR)/%s.class: %s\n", classFile, depFile);
free(classFile);
}
else
{
char *objectFile = strdup(basePos);
objectFile[(dotPos - basePos)/sizeof(char)] = 0;
printf("$(OBJECTS_DIR)/%s.o: %s\n", objectFile, depFile);
free(objectFile);
}
nextFile(sourceFile);
yylex();
for (i = 0; i < nDirectories; i++)
{
free(directories[i]);
}
free(directories);
free(sourceFile);
free(depFile);
return 0;
}
int currentBuffer = 0; /* Buffer pointer stack counter */
YY_BUFFER_STATE buffers[FILE_STACK_SIZE]; /* Buffer pointer stack */
/* /*
* Add a directory name to the file name * Add a directory name to the file name
*/ */
char* addDirectoryName(const char* directoryName, const char* fileName) char* addDirectoryName(const char* dirName, const char* fileName)
{ {
char* pathName; char* pathName = (char*)malloc(strlen(dirName) + strlen(fileName) + 2);
pathName = (char*)malloc(strlen(directoryName) + strlen(fileName) + 2); strcpy(pathName, dirName);
strcpy(pathName, directoryName); if (dirName[strlen(dirName)-1] != '/')
if (directoryName[strlen(directoryName)-1] != '/')
{ {
strcat(pathName, "/"); strcat(pathName, "/");
} }
@ -295,10 +293,10 @@ char* addDirectoryName(const char* directoryName, const char* fileName)
*/ */
void nextFile(const char* fileName) void nextFile(const char* fileName)
{ {
int d; if (lookUp(visitedFiles, fileName))
char* pathName; {
return; /* already existed (did not insert) */
if (lookUp(visitedFiles, fileName)) return; }
if (currentBuffer >= FILE_STACK_SIZE) if (currentBuffer >= FILE_STACK_SIZE)
{ {
@ -317,9 +315,10 @@ void nextFile(const char* fileName)
if (!(newyyin = fopen(fileName, "r"))) if (!(newyyin = fopen(fileName, "r")))
{ {
int d;
for (d=0; d<nDirectories; d++) for (d=0; d<nDirectories; d++)
{ {
pathName = addDirectoryName(directories[d], fileName); char* pathName = addDirectoryName(directories[d], fileName);
if ((newyyin = fopen(pathName, "r"))) if ((newyyin = fopen(pathName, "r")))
{ {
@ -365,7 +364,6 @@ void nextFile(const char* fileName)
void dotToSlash(char* fileName) void dotToSlash(char* fileName)
{ {
int i, len; int i, len;
len = strlen(fileName); len = strlen(fileName);
for (i=0; i<len; i++) for (i=0; i<len; i++)
@ -399,19 +397,20 @@ void importFile(const char* fileName)
/* /*
* Import (java) directories * Import (java) directories
*/ */
void importDirectory(const char* dirName) void importDir(const char* dirName)
{ {
int dirNameLen; char *uDirName;
char *uDirName, *path;
DIR *source; DIR *source;
struct dirent *list;
if (lookUp(visitedDirs, dirName)) return; if (lookUp(visitedDirs, dirName))
{
return; /* already existed (did not insert) */
}
dirNameLen = strlen(dirName);
uDirName = strdup(dirName); uDirName = strdup(dirName);
dotToSlash(uDirName); dotToSlash(uDirName);
uDirName[dirNameLen-2] = 0;
uDirName[strlen(dirName)-2] = 0;
if ((source = opendir(uDirName)) == NULL) if ((source = opendir(uDirName)) == NULL)
{ {
@ -421,30 +420,27 @@ void importDirectory(const char* dirName)
"could not open directory %s\n", "could not open directory %s\n",
uDirName uDirName
); );
free(uDirName);
return;
} }
else else
{ {
/* Read and parse all the entries in the directory */ /* Read and parse all the entries in the directory */
struct dirent *list;
while ((list = readdir(source)) != NULL) while ((list = readdir(source)) != NULL)
{ {
if char* dotPos = strrchr(list->d_name, '.');
(
strstr(list->d_name, ".java") if (dotPos != NULL && strcmp(dotPos, ".java") == 0)
&& !strstr(list->d_name, ".java~")
)
{ {
path = addDirectoryName(uDirName, list->d_name); char* pathName = addDirectoryName(uDirName, list->d_name);
nextFile(path); nextFile(pathName);
free(path); free(pathName);
} }
} }
closedir(source); closedir(source);
free(uDirName);
} }
free(uDirName);
} }

View File

@ -35,12 +35,12 @@ Description
only once. This is why this program is faster than cpp. only once. This is why this program is faster than cpp.
Usage Usage
wmkdep [ -Idirectory ... -Idirectory ] filename wmkdepend [ -Idir ... -Idir ] [ -iheader .. -iheader ] filename
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include <cstdio> #include <cstdio>
#include <stdlib.h> #include <cstdlib>
#include <cstring> #include <cstring>
#include "wmkdependParser.h" #include "wmkdependParser.h"
@ -58,7 +58,10 @@ void printUsage(const char* message = NULL)
fwprintf fwprintf
( (
stderr, stderr,
L"Usage: wmkdepend [ -Idirectory ... -Idirectory ] filename\n" L"Usage: wmkdepend %s filename\nOptions:\n%s\n",
"[ -Idir ... -Idir ] [ -iheader .. -iheader ]",
" -Idir specify include directory\n"
" -iheader specify header name to ignore\n"
); );
} }
@ -67,13 +70,15 @@ int main(int argc, char* argv[])
{ {
if (argc == 1) if (argc == 1)
{ {
printUsage("Error: input file not supplied"); printUsage("input file not supplied");
::exit(1); ::exit(1);
} }
for (int i=1; i < argc; i++) for (int i=1; i < argc; i++)
{ {
if (strncmp(argv[i], "-I", 2) == 0 && strlen(argv[i]) > 2) if (strncmp(argv[i], "-I", 2) == 0)
{
if (strlen(argv[i]) > 2)
{ {
std::string dirName(argv[i] + 2); std::string dirName(argv[i] + 2);
@ -86,6 +91,14 @@ int main(int argc, char* argv[])
wmake::Parser::includeDirs.push_back(dirName); wmake::Parser::includeDirs.push_back(dirName);
} }
} }
else if (strncmp(argv[i], "-i", 2) == 0)
{
if (strlen(argv[i]) > 2)
{
wmake::Parser::visitedFiles.insert(argv[i] + 2);
}
}
}
std::string sourceFile(argv[argc-1]); std::string sourceFile(argv[argc-1]);
@ -117,7 +130,7 @@ int main(int argc, char* argv[])
fwprintf fwprintf
( (
stderr, stderr,
L"Cannot find extension in source file name %s\n", L"cannot find extension in source file name %s\n",
sourceFile.c_str() sourceFile.c_str()
); );
::exit(1); ::exit(1);

View File

@ -59,9 +59,6 @@ COMPILER wmkdepend
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
private: private:
//! Set of files already visited
static std::set<std::string> visitedFiles_;
//! Set of (java) directories already visited //! Set of (java) directories already visited
static std::set<std::string> visitedDirs_; static std::set<std::string> visitedDirs_;
@ -75,6 +72,9 @@ private:
static void importFile(const std::string& name); static void importFile(const std::string& name);
public: public:
//! Set of files already visited
static std::set<std::string> visitedFiles;
//! Include directories to search //! Include directories to search
static std::list<std::string> includeDirs; static std::list<std::string> includeDirs;
@ -95,9 +95,9 @@ public:
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
std::set<std::string> Parser::visitedFiles_;
std::set<std::string> Parser::visitedDirs_; std::set<std::string> Parser::visitedDirs_;
std::set<std::string> Parser::visitedFiles;
std::list<std::string> Parser::includeDirs; std::list<std::string> Parser::includeDirs;
std::string Parser::sourceFile; std::string Parser::sourceFile;
std::string Parser::depFile; std::string Parser::depFile;
@ -123,7 +123,7 @@ void Parser::ignoreDir(const std::string& name)
void Parser::includeFile(const std::string& name) void Parser::includeFile(const std::string& name)
{ {
if (!visitedFiles_.insert(name).second) if (!visitedFiles.insert(name).second)
{ {
return; // already existed (did not insert) return; // already existed (did not insert)
} }
@ -172,7 +172,7 @@ void Parser::includeFile(const std::string& name)
); );
// only report the first occurance // only report the first occurance
visitedFiles_.insert(name); visitedFiles.insert(name);
} }
} }

View File

@ -52,9 +52,9 @@ namespace wmake {
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
std::set<std::string> Parser::visitedFiles_;
std::set<std::string> Parser::visitedDirs_; std::set<std::string> Parser::visitedDirs_;
std::set<std::string> Parser::visitedFiles;
std::list<std::string> Parser::includeDirs; std::list<std::string> Parser::includeDirs;
std::string Parser::sourceFile; std::string Parser::sourceFile;
std::string Parser::depFile; std::string Parser::depFile;
@ -80,7 +80,7 @@ void Parser::ignoreDir(const std::string& name)
void Parser::includeFile(const std::string& name) void Parser::includeFile(const std::string& name)
{ {
if (!visitedFiles_.insert(name).second) if (!visitedFiles.insert(name).second)
{ {
return; // already existed (did not insert) return; // already existed (did not insert)
} }
@ -129,7 +129,7 @@ void Parser::includeFile(const std::string& name)
); );
// only report the first occurance // only report the first occurance
visitedFiles_.insert(name); visitedFiles.insert(name);
} }
} }

View File

@ -122,9 +122,6 @@ public:
private: private:
//! Set of files already visited
static std::set<std::string> visitedFiles_;
//! Set of (java) directories already visited //! Set of (java) directories already visited
static std::set<std::string> visitedDirs_; static std::set<std::string> visitedDirs_;
@ -138,6 +135,9 @@ private:
static void importFile(const std::string& name); static void importFile(const std::string& name);
public: public:
//! Set of files already visited
static std::set<std::string> visitedFiles;
//! Include directories to search //! Include directories to search
static std::list<std::string> includeDirs; static std::list<std::string> includeDirs;