mozilla-history/gconfig/makedep/makedep.cpp

895 строки
26 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
// Dependency building hack
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <ctype.h>
#include <afxcoll.h>
#include <afxtempl.h>
int mainReturn = 0;
BOOL b16 = FALSE;
BOOL bSimple = FALSE;
// freopen won't work on stdout in win16
FILE *pAltFile = stdout;
CStringArray includeDirectories;
// turn a file, relative path or other into an absolute path
// This function copied from MFC 1.52
BOOL PASCAL _AfxFullPath(LPSTR lpszPathOut, LPCSTR lpszFileIn)
// lpszPathOut = buffer of _MAX_PATH
// lpszFileIn = file, relative path or absolute path
// (both in ANSI character set)
{
OFSTRUCT of;
if (OpenFile(lpszFileIn, &of, OF_PARSE) != HFILE_ERROR)
{
// of.szPathName is in the OEM character set
OemToAnsi(of.szPathName, lpszPathOut);
AnsiUpper(lpszPathOut); // paths in upper case just to be sure
return TRUE;
}
else
{
TRACE1("Warning: could not parse the path %Fs\n", lpszFileIn);
lstrcpy(lpszPathOut, lpszFileIn); // take it literally
AnsiUpper(lpszPathOut); // paths in upper case just to be sure
return FALSE;
}
}
void AddIncludeDirectory( char *pString ){
CString s = pString;
int len = s.GetLength();
if(len > 0 && s[len - 1] != '\\' ){
s += "\\";
}
includeDirectories.Add(s);
}
BOOL FileExists( const char *pString ){
struct _stat buf;
int result;
result = _stat( pString, &buf );
return (result == 0);
}
void FixPathName(CString& str) {
str.MakeUpper(); // all upper case
// now switch all forward slashes to back slashes
int index;
while ((index = str.Find('/')) != -1) {
str.SetAt(index, '\\');
}
}
void FATName(CString& csLONGName)
{
// Only relevant for 16 bits.
if(b16) {
// Convert filename to FAT (8.3) equivalent.
char aBuffer[2048];
if(GetShortPathName(csLONGName, aBuffer, 2048)) {
csLONGName = aBuffer;
}
}
}
void FlipSlashes(CString& csName)
{
LPTSTR pStr = csName.GetBuffer( 10 );
int length = lstrlen(pStr);
for (int i = 0; i < length; i++)
{
if (pStr[i] == '\\')
pStr[i] = '/';
}
csName.ReleaseBuffer();
}
class CFileRecord {
public:
CString m_shortName;
CString m_shortObjectName;
CString m_pathName;
CPtrArray m_includes;
BOOL m_bVisited;
BOOL m_bSystem;
BOOL m_bSource;
static CMapStringToPtr fileMap;
static CStringArray orderedFileNames;
static CMapStringToPtr includeMap;
static CMapStringToPtr noDependMap;
CFileRecord( const char *shortName, const char* pFullName, BOOL bSystem, BOOL bSource):
m_shortName( shortName ),
m_pathName(),
m_includes(),
m_bVisited(FALSE),
m_bSource( bSource ),
m_bSystem(bSystem){
m_pathName = pFullName;
FixPathName(m_pathName); // all upper case for consistency
ASSERT(FindFileRecord(m_pathName) == NULL); // make sure it's not already in the map
fileMap[m_pathName] = this; // add this to the filemap, using the appropriate name as the key
if (bSource) {
orderedFileNames.Add(m_pathName); // remember the order we saw source files in
}
m_shortObjectName = m_shortName.Left(m_shortName.GetLength()-4)+".obj";
}
//
// open the file and grab all the includes.
//
void ProcessFile(){
FILE *f;
CString fullName;
BOOL bSystem;
DWORD lineCntr = 0;
char *a = new char[2048];
memset(a, 0, 2048);
char srcPath[_MAX_PATH];
// construct the full path
if (!_AfxFullPath(srcPath, m_pathName)) {
strcpy(srcPath, m_pathName);
}
// strip off the source filename to end up with just the path
LPSTR pTemp = strrchr(srcPath, '\\');
if (pTemp) {
*(pTemp + 1) = 0;
}
f = fopen(m_pathName, "r");
if(f != NULL && f != (FILE *)-1) {
setvbuf(f, NULL, _IOFBF, 32768); // use a large file buffer
while(fgets(a, 2047, f)) {
// if the string "//{{NO_DEPENDENCIES}}" is at the start of one of the
// first 10 lines of a file, don't build dependencies on it or any
// of the files it includes
if (lineCntr < 10) {
static char* pDependStr = "//{{NO_DEPENDENCIES}}";
if (strncmp(a, pDependStr, strlen(pDependStr)) == 0) {
noDependMap[m_pathName] = 0; // add it to the noDependMap
break; // no need for further processing of this file
}
}
++lineCntr;
// have to handle a variety of legal syntaxes that we find in our source files:
// #include
// # include
// #include
// if the first non-whitespace char is a '#', consider this line
pTemp = a;
pTemp += strspn(pTemp, " \t"); // skip whitespace
if (*pTemp == '#') {
++pTemp; // skip the '#'
pTemp += strspn(pTemp, " \t"); // skip more whitespace
if( !strncmp(pTemp, "include", 7) ){
pTemp += 7; // skip the "include"
pTemp += strspn(pTemp, " \t"); // skip more whitespace
bSystem = (*pTemp == '<'); // mark if it's a system include or not
if (bSystem || (*pTemp == '"')) {
LPSTR pStart = pTemp + 1; // mark the start of the string
pTemp = pStart + strcspn(pStart, ">\" "); // find the end of the string
*pTemp = 0; // terminate the string
// construct the full pathname from the path part of the
// source file and the name listed here
fullName = srcPath;
fullName += pStart;
CFileRecord *pAddMe = AddFile( pStart, fullName, bSystem );
if (pAddMe) {
m_includes.Add(pAddMe);
}
}
}
}
}
fclose(f);
}
delete [] a;
}
void PrintIncludes(CString string){
int i = 0;
while( i < m_includes.GetSize() ){
CFileRecord *pRec = (CFileRecord*) m_includes[i];
// Don't write out files that don't exist or are not in the namespace
// of the programs using it (netscape_AppletMozillaContext.h doesn't
// mix well with 16 bits).
// Also don't write out files that are in the noDependMap
void* lookupJunk;
if( !pRec->m_bVisited && pRec->m_pathName.GetLength() != 0 && !noDependMap.Lookup(pRec->m_pathName, lookupJunk)) {
// not supposed to have a file in the list that doesn't exist
ASSERT(FileExists(pRec->m_pathName));
CString csOutput;
csOutput = pRec->m_pathName;
FATName(csOutput);
//fprintf(pAltFile, "\\\n %s ", (const char *) csOutput );
FlipSlashes(csOutput);
LPTSTR pStr = csOutput.GetBuffer(1000);
if (pStr[1] == ':')
{
CString temp ;
temp = csOutput.Right(csOutput.GetLength()-2);
csOutput.ReleaseBuffer();
csOutput = temp;
} else {
csOutput.ReleaseBuffer();
}
fprintf(pAltFile, " %s", (const char *) csOutput );
// mark this one as done so we don't do it more than once
pRec->m_bVisited = TRUE;
pRec->PrintIncludes(string);
}
i++;
}
}
void PrintDepend(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
// clear all the m_bVisisted flags so we can use it to keep track
// of whether we've already output this file as a dependency
next = fileMap.GetStartPosition();
while( next ){
fileMap.GetNextAssoc( next, name, *(void**)&pRec );
pRec->m_bVisited = FALSE;
}
char fname[_MAX_FNAME];
if (pRec->m_pathName.GetLength() != 0) {
if( bSimple ){
//fprintf(pAltFile, "\n\n\n%s:\t", m_pathName );
}
else {
CString csOutput;
csOutput = m_pathName;
FATName(csOutput);
_splitpath( csOutput, NULL, NULL, fname, NULL );
//fprintf(pAltFile, "\n\n\n$(OUTDIR)\\%s.obj: %s ", fname, (const char*) csOutput );
}
m_bVisited = TRUE; // mark it as done so we won't do it again
fprintf(pAltFile, "\n\n\nWin32_D.OBJ/%s: ", (LPCSTR)m_shortObjectName);
PrintIncludes(m_shortObjectName);
}
}
static CString NormalizeFileName( const char* pName ){
return CString(pName);
}
static CFileRecord* FindFileRecord( const char *pName ){
CFileRecord* pRec = NULL;
CString name(pName);
FixPathName(name);
fileMap.Lookup(name, (void*&)pRec);
return(pRec);
}
public:
static CFileRecord* AddFile( const char* pShortName, const char* pFullName, BOOL bSystem = FALSE,
BOOL bSource = FALSE ){
char fullName[_MAX_PATH];
BOOL bFound = FALSE;
BOOL bFoundInInclude = FALSE;
CString foundName;
CString fixedShortName;
CString s;
// if it is source, we might be getting an obj file. If we do,
// convert it to a c or c++ file.
if( bSource && (strcmp(GetExt(pShortName),".obj") == 0) ){
char path_buffer[_MAX_PATH];
char fname[_MAX_FNAME] = "";
CString s;
_splitpath( pShortName, NULL, NULL, fname, NULL );
if( FileExists( s = CString(fname) + ".cpp") ){
pShortName = s;
pFullName = s;
}
else if( FileExists( s = CString(fname) + ".c" ) ){
pShortName = s;
pFullName = s;
}
else {
return 0;
}
}
// if pFullName was not constructed, construct it here based on the current directory
if (!pFullName) {
_AfxFullPath(fullName, pShortName);
pFullName = fullName;
}
// first check to see if we already have this exact file
CFileRecord *pRec = FindFileRecord(pFullName);
if (pRec) {
bFound = TRUE;
}
// check the fullname first
if (!bFound && FileExists(pFullName)) {
foundName = pFullName;
bFound = TRUE;
}
// if still not found, search the include paths
if (!bFound) {
// all files we've found in include directories are in the includeMap
// we can see if we've already found it in the include path before and
// save time by getting it from there
fixedShortName = pShortName;
FixPathName(fixedShortName); // normalize the name
includeMap.Lookup(fixedShortName, (void*&)pRec);
pShortName = fixedShortName; // set this to point to the normalized name for when we add it to the include map
if (!pRec) {
int i = 0;
while( i < includeDirectories.GetSize() ){
if( FileExists( includeDirectories[i] + pShortName ) ){
foundName = includeDirectories[i] + pShortName;
bFound = TRUE;
bFoundInInclude = TRUE;
break;
}
i++;
}
}
}
// if we found it and don't already have a fileRecord for it
// see if this name is already in there
if (bFound && !pRec) {
pRec = FindFileRecord(foundName);
}
// source files are not allowed to be missing
if (!pRec && !bFound && bSource) {
fprintf(stderr, "Source file: %s doesn't exist\n", pFullName);
mainReturn = -1; // exit with an error, don't write out the results
}
#ifdef _DEBUG
if (!pRec && !bFound && !bSystem) {
fprintf(stderr, "Header not found: %s (%s)\n", pShortName, pFullName);
}
#endif
// if none of the above logic found it already in the list, must be a new file, add it to the list
if (bFound && (pRec == NULL)) {
pRec = new CFileRecord( pShortName, foundName, bSystem, bSource);
// if we found this one in the include path, add it to the includeMap
// for performance reasons (so we can find it there next time rather
// than having to search the file system again)
if (bFoundInInclude) {
includeMap[pShortName] = pRec;
}
}
return pRec;
}
static void PrintDependancies(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
// use orderedFileNames to preserve order
for (int pos = 0; pos < orderedFileNames.GetSize(); pos++) {
pRec = FindFileRecord(orderedFileNames[pos]);
if(pRec && pRec->m_bSource ){
pRec->PrintDepend();
}
}
}
void PrintDepend2(){
CFileRecord *pRec;
int i;
if( m_includes.GetSize() != 0 ){
fprintf(pAltFile, "\n\n\n%s: \\\n",m_pathName );
i = 0;
while( i < m_includes.GetSize() ){
pRec = (CFileRecord*) m_includes[i];
fprintf(pAltFile, "\t\t\t%s\t\\\n",pRec->m_pathName );
i++;
}
}
}
static void PrintDependancies2(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
next = fileMap.GetStartPosition();
while( next ){
fileMap.GetNextAssoc( next, name, *(void**)&pRec );
pRec->PrintDepend2();
}
}
static void PrintTargets(const char *pMacroName, const char *pDelimeter){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
BOOL bNeedDelimeter = FALSE;
fprintf(pAltFile, "%s = ", pMacroName);
// use orderedFileNames to preserve target order
for (int pos = 0; pos < orderedFileNames.GetSize(); pos++) {
pRec = FindFileRecord(orderedFileNames[pos]);
ASSERT(pRec);
if( pRec && pRec->m_bSource && pRec->m_pathName.GetLength() != 0){
char fname[_MAX_FNAME];
CString csOutput;
csOutput = pRec->m_pathName;
FATName(csOutput);
_splitpath( csOutput, NULL, NULL, fname, NULL );
if(bNeedDelimeter) {
fprintf(pAltFile, "%s\n", pDelimeter);
bNeedDelimeter = FALSE;
}
fprintf(pAltFile, " $(OUTDIR)\\%s.obj ", fname );
bNeedDelimeter = TRUE;
}
}
fprintf(pAltFile, "\n\n\n");
}
static CString DirDefine( const char *pPath ){
char path_buffer[_MAX_PATH];
char dir[_MAX_DIR] = "";
char dir2[_MAX_DIR] = "";
char fname[_MAX_FNAME] = "";
char ext[_MAX_EXT] = "";
CString s;
_splitpath( pPath, 0, dir, 0, ext );
BOOL bDone = FALSE;
while( dir && !bDone){
// remove the trailing slash
dir[ strlen(dir)-1] = 0;
_splitpath( dir, 0, dir2, fname, 0 );
if( strcmp( fname, "SRC" ) == 0 ){
strcpy( dir, dir2 );
}
else {
bDone = TRUE;
}
}
s = CString(fname) + "_" + (ext+1);
return s;
}
static void PrintSources(){
int i;
CString dirName, newDirName;
for( i=0; i< orderedFileNames.GetSize(); i++ ){
newDirName= DirDefine( orderedFileNames[i] );
if( newDirName != dirName ){
fprintf( pAltFile, "\n\n\nFILES_%s= $(FILES_%s) \\",
(const char*)newDirName, (const char*)newDirName );
dirName = newDirName;
}
fprintf( pAltFile, "\n\t%s^", (const char*)orderedFileNames[i] );
}
}
static CString SourceDirName( const char *pPath, BOOL bFileName){
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE] = "";
char dir[_MAX_DIR] = "";
char fname[_MAX_FNAME] = "";
char ext[_MAX_EXT] = "";
CString s;
_splitpath( pPath, drive, dir, fname, ext );
s = CString(drive) + dir;
if( bFileName ){
s += CString("FNAME") + ext;
}
else {
// remove the trailing slash
s = s.Left( s.GetLength() - 1 );
}
return s;
}
static CString GetExt( const char *pPath){
char ext[_MAX_EXT] = "";
_splitpath( pPath, 0,0,0, ext );
CString s = CString(ext);
s.MakeLower();
return s;
}
static void PrintBuildRules(){
int i;
CString dirName;
CMapStringToPtr dirList;
for( i=0; i< orderedFileNames.GetSize(); i++ ){
dirList[ SourceDirName(orderedFileNames[i], TRUE) ]= 0;
}
POSITION next;
CString name;
void *pVal;
next = dirList.GetStartPosition();
while( next ){
dirList.GetNextAssoc( next, name, pVal);
CString dirDefine = DirDefine( name );
CString ext = GetExt( name );
name = SourceDirName( name, FALSE );
CString response = dirDefine.Left(8);
fprintf( pAltFile,
"\n\n\n{%s}%s{$(OUTDIR)}.obj:\n"
"\t@rem <<$(OUTDIR)\\%s.cl\n"
"\t$(CFILEFLAGS)\n"
"\t$(CFLAGS_%s)\n"
"<<KEEP\n"
"\t$(CPP) @$(OUTDIR)\\%s.cl %%s\n",
(const char*)name,
(const char*)ext,
(const char*)response,
(const char*)dirDefine,
(const char*)response
);
fprintf( pAltFile,
"\n\n\nBATCH_%s:\n"
"\t@rem <<$(OUTDIR)\\%s.cl\n"
"\t$(CFILEFLAGS)\n"
"\t$(CFLAGS_%s)\n"
"\t$(FILES_%s)\n"
"<<KEEP\n"
"\t$(TIMESTART)\n"
"\t$(CPP) @$(OUTDIR)\\%s.cl\n"
"\t$(TIMESTOP)\n",
(const char*)dirDefine,
(const char*)response,
(const char*)dirDefine,
(const char*)dirDefine,
(const char*)response
);
}
//
// Loop through one more time and build the final batch build
// rule
//
fprintf( pAltFile,
"\n\n\nBATCH_BUILD_OBJECTS:\t\t\\\n");
next = dirList.GetStartPosition();
while( next ){
dirList.GetNextAssoc( next, name, pVal);
CString dirDefine = DirDefine( name );
fprintf( pAltFile,
"\tBATCH_%s\t\t\\\n", dirDefine );
}
fprintf( pAltFile,
"\n\n");
}
static void ProcessFiles(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
// search all the files for headers, adding each one to the list when found
// rather than do it recursively, it simple marks each one it's done
// and starts over, stopping only when all are marked as done
next = fileMap.GetStartPosition();
while( next ){
fileMap.GetNextAssoc( next, name, *(void**)&pRec );
if( pRec->m_bVisited == FALSE && pRec->m_bSystem == FALSE ){
// mark this file as already done so we don't read it again
// to find its headers
pRec->m_bVisited = TRUE;
pRec->ProcessFile();
// Start searching from the beginning again
// because ProcessFile may have added new files
// and changed the GetNextAssoc order
next = fileMap.GetStartPosition();
}
}
}
};
CMapStringToPtr CFileRecord::fileMap;
CStringArray CFileRecord::orderedFileNames;
CMapStringToPtr CFileRecord::includeMap;
CMapStringToPtr CFileRecord::noDependMap;
int main( int argc, char** argv ){
int i = 1;
char *pStr;
static int iRecursion = 0; // Track levels of recursion.
static CString outputFileName;
// Entering.
iRecursion++;
while( i < argc ){
if( argv[i][0] == '-' || argv[i][0] == '/' ){
switch( argv[i][1] ){
case 'i':
case 'I':
if( argv[i][2] != 0 ){
pStr = &(argv[i][2]);
}
else {
i++;
pStr = argv[i];
}
if( pStr == 0 || *pStr == '-' || *pStr == '/' ){
goto usage;
}
else {
AddIncludeDirectory( pStr );
}
break;
case 'f':
case 'F':
if( argv[i][2] != 0 ){
pStr = &(argv[i][2]);
}
else {
i++;
pStr = argv[i];
}
if( pStr == 0 || *pStr == '-' || *pStr == '/'){
goto usage;
}
else {
CStdioFile f;
CString s;
if( f.Open( pStr, CFile::modeRead ) ){
while(f.ReadString(s)){
s.TrimLeft();
s.TrimRight();
if( s.GetLength() ){
CFileRecord::AddFile( s, NULL, FALSE, TRUE );
}
}
f.Close();
}
else {
fprintf(stderr,"makedep: file not found: %s", pStr );
exit(-1);
}
}
break;
case 'o':
case 'O':
if( argv[i][2] != 0 ){
pStr = &(argv[i][2]);
}
else {
i++;
pStr = argv[i];
}
if( pStr == 0 || *pStr == '-' || *pStr == '/'){
goto usage;
}
else {
CStdioFile f;
CString s;
outputFileName = pStr;
if(!(pAltFile = fopen(pStr, "w+"))) {
fprintf(stderr, "makedep: file not found: %s", pStr );
exit(-1);
}
}
break;
case '1':
if( argv[i][2] == '6') {
b16 = TRUE;
}
break;
case 's':
case 'S':
bSimple = TRUE;
break;
case 'h':
case 'H':
case '?':
usage:
fprintf(stderr, "usage: makedep -I <dirname> -F <filelist> <filename>\n"
" -I <dirname> Directory name, can be repeated\n"
" -F <filelist> List of files to scan, one per line\n"
" -O <outputFile> File to write output, default stdout\n");
exit(-1);
}
}
else if( argv[i][0] == '@' ){
// file contains our commands.
CStdioFile f;
CString s;
int iNewArgc = 0;
char **apNewArgv = new char*[5000];
memset(apNewArgv, 0, sizeof(apNewArgv));
// First one is always the name of the exe.
apNewArgv[0] = argv[0];
iNewArgc++;
const char *pTraverse;
const char *pBeginArg;
if( f.Open( &argv[i][1], CFile::modeRead ) ){
while( iNewArgc < 5000 && f.ReadString(s) ) {
// Scan the string for args, and do the right thing.
pTraverse = (const char *)s;
while(iNewArgc < 5000 && *pTraverse) {
if(isspace(*pTraverse)) {
pTraverse++;
continue;
}
// Extract to next space.
pBeginArg = pTraverse;
do {
pTraverse++;
}
while(*pTraverse && !isspace(*pTraverse));
apNewArgv[iNewArgc] = new char[pTraverse - pBeginArg + 1];
memset(apNewArgv[iNewArgc], 0, pTraverse - pBeginArg + 1);
strncpy(apNewArgv[iNewArgc], pBeginArg, pTraverse - pBeginArg);
iNewArgc++;
}
}
f.Close();
}
// Recurse if needed.
if(iNewArgc > 1) {
main(iNewArgc, apNewArgv);
}
// Free off the argvs (but not the very first one which we didn't allocate).
while(iNewArgc > 1) {
iNewArgc--;
delete [] apNewArgv[iNewArgc];
}
delete [] apNewArgv;
}
else {
CFileRecord::AddFile( argv[i], NULL, FALSE, TRUE );
}
i++;
}
// Only of the very bottom level of recursion do we do this.
if(iRecursion == 1) {
// only write the results out if no errors encountered
if (mainReturn == 0) {
CFileRecord::ProcessFiles();
if( !bSimple ){
CFileRecord::PrintTargets("OBJ_FILES", "\\");
if(b16) {
CFileRecord::PrintTargets("LINK_OBJS", "+\\");
}
else {
CFileRecord::PrintTargets("LINK_OBJS", "^");
}
CFileRecord::PrintSources();
CFileRecord::PrintBuildRules();
}
CFileRecord::PrintDependancies();
}
if(pAltFile != stdout) {
fclose(pAltFile);
if (mainReturn != 0) {
remove(outputFileName); // kill output file if returning an error
}
}
}
iRecursion--;
return mainReturn;
}