deptool.cpp revision 2a885aae09c02caec9cb454957821206e8635110
/**
* DepTool dependency tool
*
* This is a simple dependency generator coded in C++
*
* Authors:
* Bob Jamison
*
* Copyright (C) 2006 Bob Jamison
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
INSTRUCTIONS
1. First you optionally create a file named 'make.exclude.' This
lists the file names relative to the current directory. Lines starting
with a '#' are ignored. Here is an example:
========= SNIP ========
######################################################################
# File: make.exclude
#
# This is a list of files to exclude from building, using DepTool
#
######################################################################
ast
bonobo
#next line is ignored
========= SNIP ========
2. Run deptool. This will take a few seconds to scan all of your files
and make its dependency lists. Three files are created:
make.files: lists all of the files that were considered
make.dep: contains the output INCLUDE and OBJECT declarations
for you to include and use in your makefile. It also contains
the dependency listings.
make.ref: contains a reverse-dependency listing. This takes each file
and recursively determines which other files include it, and how
far the nesting is.
3. Include deptool.mk in your makefile.
*/
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <dirent.h>
#include <string>
#include <set>
#include <map>
#include <vector>
//# This will allow us to redefine the string in the future
/**
* Class which holds information for each file.
*/
class FileRec
{
public:
typedef enum
{
} FileType;
/**
* Constructor
*/
FileRec()
/**
* Copy constructor
*/
/**
* Constructor
*/
/**
* Assignment operator
*/
/**
* Destructor
*/
~FileRec()
{}
/**
* Directory part of the file name
*/
/**
* Base name, sans directory and suffix
*/
/**
* File extension, such as cpp or h
*/
/**
* Type of file: CFILE, HFILE, OFILE
*/
int type;
/**
* 'Distance' of inclusion from a file that depends on this one.
*/
int distance;
/**
* Used to list files ref'd by this one, in the case of allFiles,
* or other files which reference this one, such as refFiles;
*/
bool checked;
private:
void init()
{
checked = false;
}
{
}
};
/**
* Main class which does the work.
*/
class DepTool
{
public:
/**
* Constructor
*/
DepTool();
/**
* Destructor
*/
virtual ~DepTool();
/**
* Creates the list of all file names which will be
* candidates for further processing. Reads make.exclude
* to see which files for directories to leave out.
*/
virtual bool createFileList();
/**
* Generates the forward and reverse dependency lists
*/
virtual bool generateDependencies();
/**
* Calls the other two methods, then generates the files.
*/
virtual bool run();
private:
/**
*
*/
void reset();
/**
*
*/
/**
*
*/
/**
* Removes whitespace from beginning and end of a string
*/
/**
*
*/
/**
*
*/
/**
*
*/
/**
*
*/
/**
*
*/
/**
*
*/
/**
*
*/
/**
*
*/
bool saveFileList();
/**
*
*/
bool saveDepFile(bool doXml);
/**
*
*/
bool saveRefFile(bool doXml);
/**
*
*/
/**
*
*/
/**
*
*/
FileRec *include,
int depth);
/**
*
*/
int depth);
/**
*
*/
/**
*
*/
/**
*
*/
/**
*
*/
/**
* A list of all files which will be processed for
* dependencies.
*/
/**
* A list of .h files, with a list for each one
* of which other files include them.
*/
/**
* The list of .o files, and the
* dependencies upon them.
*/
char *fileBuf;
int fileSize;
static const int readBufLen = 8192;
char readBuf[8193];
};
//########################################################################
//# M A I N
//########################################################################
/**
* Constructor
*/
{
}
/**
* Destructor
*/
{
reset();
}
/**
* Clean up after processing. Called by the destructor, but should
* also be called before the object is reused.
*/
{
{
delete frec;
}
{
delete frec;
}
{
delete frec;
}
}
/**
* Format an error message in printf() style
*/
{
}
/**
* Format an trace message in printf() style
*/
{
}
//########################################################################
//# U T I L I T Y
//########################################################################
/**
* Removes whitespace from beginning and end of a string
*/
{
if (s.size() < 1)
return s;
//Find first non-ws char
unsigned int begin = 0;
{
break;
}
//Find first non-ws char, going in reverse
{
break;
}
//trace("begin:%d end:%d", begin, end);
return res;
}
/**
* Parse a full path name into path, base name, and suffix
*/
{
return;
{
pos++;
}
else
{
path = "";
}
{
}
//trace("parsename:%s %s %s", path.c_str(),
// basename.c_str(), suffix.c_str());
}
/**
* Return the suffix, if any, of a file name
*/
{
return "";
return "";
pos++;
//trace("suffix:%s", res.c_str());
return res;
}
//########################################################################
//# P R O C E S S I N G
//########################################################################
/**
* Recursively list all files and directories under 'dirname', except
* those in the exclude list.
*/
{
//trace("### listFilesInDirectory(%s, %d)", dirname.c_str(), depth);
int cFiles = 0;
int hFiles = 0;
while (true)
{
if (!de)
break;
if (s.size() == 0 || s[0] == '.')
continue;
{
}
{
}
{
}
{
//trace("directory: %s", fname.c_str());
}
{
}
else
{
{
cFiles++;
}
{
hFiles++;
}
}
}
if (hFiles > 0)
{
}
return true;
}
/**
*
*/
bool DepTool::createFileList()
{
if (!f)
{
trace("'make.exclude not found");
}
else
{
char readBuf[256];
while (!feof(f))
{
break;
s = trim(s);
if (s.size() > 0 && s[0]!='#')
}
fclose(f);
}
listFilesInDirectory(".", 0);
// Note which files in the exclude list were not used.
{
}
saveFileList();
return true;
}
/**
* Get a character from the buffer at pos. If out of range,
* return -1 for safety
*/
{
return -1;
}
/**
* Skip over all whitespace characters beginning at pos. Return
* the position of the first non-whitespace character.
*/
{
{
if (ch < 0)
break;
break;
pos++;
}
return pos;
}
/**
* Parse the buffer beginning at pos, for a word. Fill
* 'ret' with the result. Return the position after the
* word.
*/
{
{
if (ch < 0)
break;
break;
pos++;
}
return pos;
}
/**
* Return whether the sequence of characters in the buffer
* beginning at pos match the key, for the length of the key
*/
{
while (*key)
{
return false;
}
return true;
}
/**
* Add an include file name to a file record. If the name
* is not found in allFiles explicitly, try prepending include
* directory names to it and try again.
*/
{
{
//h file in same dir
//trace("local: '%s'", iname.c_str());
return true;
}
else
{
//look in other dirs
{
{
//trace("other: '%s'", iname.c_str());
return true;
}
}
}
return true;
}
/**
* Lightly parse a file to find the #include directives. Do
* a bit of state machine stuff to make sure that the directive
* is valid. (Like not in a comment).
*/
{
if (!f)
{
return false;
}
while (!feof(f))
{
}
fclose(f);
int pos = 0;
{
//trace("p:%c", get(pos));
//# Block comment
{
pos += 2;
{
{
pos += 2;
break;
}
else
pos++;
}
}
//# Line comment
{
pos += 2;
{
{
pos++;
break;
}
else
pos++;
}
}
//# #include! yaay
{
pos += 8;
{
}
}
else
{
pos++;
}
}
return true;
}
/**
* Recursively check include lists to find all files in allFiles to which
* a given file is dependent.
*/
FileRec *include,
int depth)
{
{
{
//trace("file '%s' already seen", fname.c_str());
continue;
}
}
return true;
}
/**
* Recursively check include lists to find all files in allFiles which
* will eventually have a dependency on this file. This is basically
* the inverse of processDependency().
*/
int depth)
{
{
{
{
//trace("%d reffile '%s' already seen", depth, cfname.c_str());
continue;
}
}
}
return true;
}
/**
* Generate the file dependency and reference lists.
*/
bool DepTool::generateDependencies()
{
//# First pass. Scan for all includes
{
{
//quit?
}
}
//# Second pass. Scan for all includes
{
{
//add the .c file first, of course
//trace("ofile:%s", fname.c_str());
processDependency(ofile, include, 0);
}
/*
else if (include->type == FileRec::HFILE)
{
String fname = iter->first;
FileRec *hfile = new FileRec(FileRec::HFILE);
hfile->path = include->path;
hfile->baseName = include->baseName;
hfile->suffix = include->suffix;
refFiles[fname] = hfile;
//trace("hfile:%s", fname.c_str());
processReference(hfile, fname, 0);
}
*/
}
return true;
}
/**
* High-level call to do what DepTool does.
*/
{
reset();
if (!createFileList())
return false;
if (!generateDependencies())
return false;
saveDepFile(false);
//saveRefFile(true);
return true;
}
//########################################################################
//# O U T P U T S
//########################################################################
/**
* Save the allFiles list. This is basically all files in a directory
* except those denied in the exclude list.
*/
bool DepTool::saveFileList()
{
if (!f)
{
trace("cannot open 'make.files' for writing");
}
fprintf(f, "########################################################\n");
fprintf(f, "## File: make.files\n");
fprintf(f, "########################################################\n");
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## F I L E S\n");
fprintf(f, "########################################################\n");
{
}
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## E X C L U D E D\n");
fprintf(f, "########################################################\n");
{
}
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## E X C L U D E entries unused\n");
fprintf(f, "########################################################\n");
{
}
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## E N D\n");
fprintf(f, "########################################################\n");
fclose(f);
return true;
}
/**
* This is the main product. This file lists the Include directives,
* the Object list, and the dependency list.
*/
{
if (!f)
{
trace("cannot open 'make.dep' for writing");
}
if (doXml)
{
fprintf(f, "<?xml version='1.0'?>\n");
fprintf(f, "<deptool>\n");
fprintf(f, "\n");
fprintf(f, "<!--\n");
fprintf(f, "########################################################\n");
fprintf(f, "## File: make.dep\n");
fprintf(f, "########################################################\n");
fprintf(f, "-->\n");
fprintf(f, "\n\n");
fprintf(f, "\n");
fprintf(f, "<!--\n");
fprintf(f, "########################################################\n");
fprintf(f, "## I N C L U D E\n");
fprintf(f, "########################################################\n");
fprintf(f, "-->\n");
fprintf(f, "<includes>\n");
{
}
fprintf(f, "</includes>\n");
fprintf(f, "\n\n\n");
fprintf(f, "<!--\n");
fprintf(f, "########################################################\n");
fprintf(f, "## O B J E C T S\n");
fprintf(f, "########################################################\n");
fprintf(f, "-->\n");
fprintf(f, "<objects>\n");
{
{
}
}
fprintf(f, "</objects>\n");
fprintf(f, "\n\n\n");
fprintf(f, "<!--\n");
fprintf(f, "########################################################\n");
fprintf(f, "## D E P E N D E N C I E S\n");
fprintf(f, "########################################################\n");
fprintf(f, "-->\n");
fprintf(f, "<dependencies>\n\n");
{
{
{
}
fprintf(f, "</file>\n\n");
}
}
fprintf(f, "</dependencies>\n");
fprintf(f, "\n\n\n");
fprintf(f, "</deptool>\n");
fprintf(f, "<!--\n");
fprintf(f, "########################################################\n");
fprintf(f, "## E N D\n");
fprintf(f, "########################################################\n");
fprintf(f, "-->\n");
}
else // ######### !XML
{
fprintf(f, "########################################################\n");
fprintf(f, "## File: make.dep\n");
fprintf(f, "########################################################\n");
fprintf(f, "\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## I N C L U D E\n");
fprintf(f, "########################################################\n");
fprintf(f, "DEPTOOL_INCLUDE =");
{
fprintf(f, " \\\n");
}
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## O B J E C T S\n");
fprintf(f, "########################################################\n");
fprintf(f, "DEPTOOL_OBJECTS =");
{
{
fprintf(f, " \\\n");
}
}
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## D E P E N D E N C I E S\n");
fprintf(f, "########################################################\n");
{
{
{
fprintf(f, " \\\n");
}
fprintf(f, "\n\n\n");
}
}
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## E N D\n");
fprintf(f, "########################################################\n");
}
fclose(f);
return true;
}
/**
* Save the "reference" file, which lists each include file, and any files
* that are judged to be dependent upon it.
*/
{
if (!f)
{
trace("cannot open 'make.ref' for writing");
}
if (doXml)
{
fprintf(f, "<?xml version='1.0'?>\n");
fprintf(f, "<deptool>\n\n");
fprintf(f, "<!--\n");
fprintf(f, "########################################################\n");
fprintf(f, "## File: make.ref\n");
fprintf(f, "## Note: The metric is the 'distance' of inclusion from\n");
fprintf(f, "## the given file.\n");
fprintf(f, "########################################################\n");
fprintf(f, "-->\n");
fprintf(f, "\n\n");
{
{
{
fprintf(f, " <ref d='%d' name='%s'/>\n",
}
fprintf(f, "</file>\n\n");
}
}
fprintf(f, "\n\n\n");
fprintf(f, "</deptool>\n");
fprintf(f, "<!--\n");
fprintf(f, "########################################################\n");
fprintf(f, "## E N D\n");
fprintf(f, "########################################################\n");
fprintf(f, "-->\n");
}
else //######### not xml
{
fprintf(f, "########################################################\n");
fprintf(f, "## File: make.ref\n");
fprintf(f, "## Note: The metric is the 'distance' of inclusion from\n");
fprintf(f, "## the given file.\n");
fprintf(f, "########################################################\n");
fprintf(f, "\n\n");
{
{
{
}
fprintf(f, "\n");
}
}
fprintf(f, "\n\n\n");
fprintf(f, "########################################################\n");
fprintf(f, "## E N D\n");
fprintf(f, "########################################################\n");
}
fclose(f);
return true;
}
//########################################################################
//# M A I N
//########################################################################
/**
* Run the DepTool's main functions
*/
static bool runTool()
{
return false;
return true;
}
/**
* Console main()
*/
{
if (!runTool())
return 1;
return 0;
}
//########################################################################
//# E N D O F F I L E
//########################################################################