2002-01-21 23:30:43 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
2002-10-24 02:03:27 +04:00
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
2002-01-21 23:30:43 +03:00
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
2002-10-24 02:03:27 +04:00
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
|
2002-01-21 23:30:43 +03:00
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
2001-08-29 02:28:31 +04:00
|
|
|
#include "cmListFileCache.h"
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
#include "cmListFileLexer.h"
|
2001-08-29 02:28:31 +04:00
|
|
|
#include "cmSystemTools.h"
|
2003-06-23 22:10:12 +04:00
|
|
|
|
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2001-08-29 02:28:31 +04:00
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
|
|
|
|
cmListFileFunction& function,
|
|
|
|
const char* filename);
|
|
|
|
|
2001-08-29 02:28:31 +04:00
|
|
|
cmListFileCache* cmListFileCache::Instance = 0;
|
|
|
|
|
|
|
|
|
|
|
|
cmListFileCache* cmListFileCache::GetInstance()
|
|
|
|
{
|
|
|
|
if(!cmListFileCache::Instance)
|
|
|
|
{
|
|
|
|
cmListFileCache::Instance = new cmListFileCache;
|
|
|
|
}
|
|
|
|
return cmListFileCache::Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cmListFileCache::ClearCache()
|
|
|
|
{
|
|
|
|
delete cmListFileCache::Instance;
|
|
|
|
cmListFileCache::Instance = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-12-02 23:30:59 +03:00
|
|
|
cmListFile* cmListFileCache::GetFileCache(const char* path,
|
|
|
|
bool requireProjectCommand)
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
|
|
|
ListFileMap::iterator sl = m_ListFileCache.find(path);
|
|
|
|
if (sl == m_ListFileCache.end())
|
|
|
|
{
|
|
|
|
// if not already in the map, then parse and store the
|
|
|
|
// file
|
2002-12-02 23:30:59 +03:00
|
|
|
if(!this->CacheFile(path, requireProjectCommand))
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sl = m_ListFileCache.find(path);
|
|
|
|
if (sl == m_ListFileCache.end())
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Fatal error, in cmListFileCache CacheFile failed",
|
|
|
|
path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmListFile& ret = sl->second;
|
|
|
|
if(cmSystemTools::ModifiedTime(path) > ret.m_ModifiedTime )
|
|
|
|
{
|
2002-12-02 23:30:59 +03:00
|
|
|
if(!this->CacheFile(path, requireProjectCommand))
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sl = m_ListFileCache.find(path);
|
|
|
|
return &sl->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-02 23:30:59 +03:00
|
|
|
bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
2002-04-12 01:02:10 +04:00
|
|
|
if(!cmSystemTools::FileExists(path))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-04 18:45:11 +04:00
|
|
|
// Get a pointer to a persistent copy of the name.
|
|
|
|
const char* filename = this->GetUniqueStringPointer(path);
|
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
// Create the scanner.
|
|
|
|
cmListFileLexer* lexer = cmListFileLexer_New();
|
|
|
|
if(!lexer)
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
cmSystemTools::Error("cmListFileCache: error allocating lexer ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file.
|
2004-08-04 18:45:11 +04:00
|
|
|
if(!cmListFileLexer_SetFileName(lexer, filename))
|
2003-12-08 21:36:59 +03:00
|
|
|
{
|
|
|
|
cmListFileLexer_Delete(lexer);
|
2004-08-04 18:45:11 +04:00
|
|
|
cmSystemTools::Error("cmListFileCache: error can not open file ", filename);
|
2001-08-29 02:28:31 +04:00
|
|
|
return false;
|
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
// Use a simple recursive-descent parser to process the token
|
|
|
|
// stream.
|
2001-08-29 02:28:31 +04:00
|
|
|
cmListFile inFile;
|
2004-08-04 18:45:11 +04:00
|
|
|
inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(filename);
|
2003-12-08 21:36:59 +03:00
|
|
|
bool parseError = false;
|
|
|
|
bool haveNewline = true;
|
|
|
|
cmListFileLexer_Token* token;
|
|
|
|
while(!parseError && (token = cmListFileLexer_Scan(lexer)))
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
if(token->type == cmListFileLexer_Token_Newline)
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
haveNewline = true;
|
2001-08-29 02:28:31 +04:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
else if(token->type == cmListFileLexer_Token_Identifier)
|
2001-11-30 00:44:22 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
if(haveNewline)
|
|
|
|
{
|
|
|
|
haveNewline = false;
|
|
|
|
cmListFileFunction inFunction;
|
|
|
|
inFunction.m_Name = token->text;
|
2004-08-04 18:45:11 +04:00
|
|
|
inFunction.m_FilePath = filename;
|
2003-12-08 21:36:59 +03:00
|
|
|
inFunction.m_Line = token->line;
|
2004-08-04 18:45:11 +04:00
|
|
|
if(cmListFileCacheParseFunction(lexer, inFunction, filename))
|
2003-12-08 21:36:59 +03:00
|
|
|
{
|
|
|
|
inFile.m_Functions.push_back(inFunction);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parseError = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
2004-08-04 18:45:11 +04:00
|
|
|
<< filename << ":" << token->line << ":\n"
|
2004-09-01 02:39:42 +04:00
|
|
|
<< "Parse error. Expected a newline, got "
|
|
|
|
<< cmListFileLexer_GetTypeAsString(lexer, token->type)
|
|
|
|
<< " with text \"" << token->text << "\".";
|
2003-12-08 21:36:59 +03:00
|
|
|
cmSystemTools::Error(error.str().c_str());
|
|
|
|
parseError = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
2004-08-04 18:45:11 +04:00
|
|
|
<< filename << ":" << token->line << ":\n"
|
2004-09-01 02:39:42 +04:00
|
|
|
<< "Parse error. Expected a command name, got "
|
|
|
|
<< cmListFileLexer_GetTypeAsString(lexer, token->type)
|
|
|
|
<< " with text \""
|
2003-12-08 21:36:59 +03:00
|
|
|
<< token->text << "\".";
|
|
|
|
cmSystemTools::Error(error.str().c_str());
|
|
|
|
parseError = true;
|
2001-11-30 00:44:22 +03:00
|
|
|
}
|
2001-08-29 02:28:31 +04:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
if (parseError)
|
|
|
|
{
|
|
|
|
inFile.m_ModifiedTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmListFileLexer_Delete(lexer);
|
|
|
|
|
2002-12-02 23:30:59 +03:00
|
|
|
if(requireProjectCommand)
|
|
|
|
{
|
|
|
|
bool hasProject = false;
|
|
|
|
// search for a project command
|
|
|
|
for(std::vector<cmListFileFunction>::iterator i
|
|
|
|
= inFile.m_Functions.begin();
|
|
|
|
i != inFile.m_Functions.end(); ++i)
|
|
|
|
{
|
|
|
|
if(i->m_Name == "PROJECT")
|
|
|
|
{
|
|
|
|
hasProject = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if no project command is found, add one
|
|
|
|
if(!hasProject)
|
|
|
|
{
|
|
|
|
cmListFileFunction project;
|
|
|
|
project.m_Name = "PROJECT";
|
2004-08-04 18:45:11 +04:00
|
|
|
cmListFileArgument prj("Project", false, filename, 0);
|
2002-12-12 02:13:33 +03:00
|
|
|
project.m_Arguments.push_back(prj);
|
2002-12-03 00:08:13 +03:00
|
|
|
inFile.m_Functions.insert(inFile.m_Functions.begin(),project);
|
2002-12-02 23:30:59 +03:00
|
|
|
}
|
|
|
|
}
|
2004-08-04 18:45:11 +04:00
|
|
|
m_ListFileCache[filename] = inFile;
|
2001-08-29 02:28:31 +04:00
|
|
|
return true;
|
|
|
|
}
|
2002-09-19 22:34:15 +04:00
|
|
|
|
|
|
|
void cmListFileCache::FlushCache(const char* path)
|
|
|
|
{
|
|
|
|
ListFileMap::iterator it = m_ListFileCache.find(path);
|
|
|
|
if ( it != m_ListFileCache.end() )
|
|
|
|
{
|
|
|
|
m_ListFileCache.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2002-12-12 02:13:33 +03:00
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
|
|
|
|
cmListFileFunction& function,
|
|
|
|
const char* filename)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
// Command name has already been parsed. Read the left paren.
|
|
|
|
cmListFileLexer_Token* token;
|
|
|
|
if(!(token = cmListFileLexer_Scan(lexer)))
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
|
|
|
<< filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
|
|
|
|
<< "Parse error. Function missing opening \"(\".";
|
|
|
|
cmSystemTools::Error(error.str().c_str());
|
|
|
|
return false;
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
if(token->type != cmListFileLexer_Token_ParenLeft)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
|
|
|
<< filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
|
2004-09-01 02:39:42 +04:00
|
|
|
<< "Parse error. Expected \"(\", got "
|
|
|
|
<< cmListFileLexer_GetTypeAsString(lexer, token->type)
|
|
|
|
<< " with text \"" << token->text << "\".";
|
2003-12-08 21:36:59 +03:00
|
|
|
cmSystemTools::Error(error.str().c_str());
|
2002-12-12 02:13:33 +03:00
|
|
|
return false;
|
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
// Arguments.
|
2004-09-01 02:51:35 +04:00
|
|
|
unsigned long lastLine = cmListFileLexer_GetCurrentLine(lexer);
|
2003-12-08 21:36:59 +03:00
|
|
|
while((token = cmListFileLexer_Scan(lexer)))
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
if(token->type == cmListFileLexer_Token_ParenRight)
|
2003-07-10 01:17:34 +04:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
return true;
|
2003-07-10 01:17:34 +04:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
else if(token->type == cmListFileLexer_Token_Identifier ||
|
|
|
|
token->type == cmListFileLexer_Token_ArgumentUnquoted)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
cmListFileArgument a(cmSystemTools::RemoveEscapes(token->text),
|
2004-08-04 18:45:11 +04:00
|
|
|
false, filename, token->line);
|
2003-12-08 21:36:59 +03:00
|
|
|
function.m_Arguments.push_back(a);
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
cmListFileArgument a(cmSystemTools::RemoveEscapes(token->text),
|
2004-08-04 18:45:11 +04:00
|
|
|
true, filename, token->line);
|
2003-12-08 21:36:59 +03:00
|
|
|
function.m_Arguments.push_back(a);
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
else if(token->type != cmListFileLexer_Token_Newline)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-08 21:36:59 +03:00
|
|
|
// Error.
|
2002-12-12 19:36:28 +03:00
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
2003-12-08 21:36:59 +03:00
|
|
|
<< filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
|
|
|
|
<< "Parse error. Function missing ending \")\". "
|
2004-09-01 02:39:42 +04:00
|
|
|
<< "Instead found "
|
|
|
|
<< cmListFileLexer_GetTypeAsString(lexer, token->type)
|
|
|
|
<< " with text \"" << token->text << "\".";
|
2002-12-12 19:36:28 +03:00
|
|
|
cmSystemTools::Error(error.str().c_str());
|
2002-12-12 02:13:33 +03:00
|
|
|
return false;
|
|
|
|
}
|
2004-09-01 02:51:35 +04:00
|
|
|
lastLine = cmListFileLexer_GetCurrentLine(lexer);
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
2004-09-01 02:51:35 +04:00
|
|
|
<< filename << ":" << lastLine << ":\n"
|
2003-12-08 21:36:59 +03:00
|
|
|
<< "Parse error. Function missing ending \")\". "
|
|
|
|
<< "End of file reached.";
|
|
|
|
cmSystemTools::Error(error.str().c_str());
|
2002-12-12 02:13:33 +03:00
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
return false;
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
2004-08-04 18:45:11 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
const char* cmListFileCache::GetUniqueStringPointer(const char* name)
|
|
|
|
{
|
|
|
|
UniqueStrings::iterator i = m_UniqueStrings.find(name);
|
|
|
|
if(i == m_UniqueStrings.end())
|
|
|
|
{
|
|
|
|
char* str = new char[strlen(name)+1];
|
|
|
|
strcpy(str, name);
|
|
|
|
i = m_UniqueStrings.insert(UniqueStrings::value_type(name, str)).first;
|
|
|
|
}
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmListFileCache::~cmListFileCache()
|
|
|
|
{
|
|
|
|
for(UniqueStrings::iterator i = m_UniqueStrings.begin();
|
|
|
|
i != m_UniqueStrings.end(); ++i)
|
|
|
|
{
|
|
|
|
delete [] i->second;
|
|
|
|
}
|
|
|
|
}
|