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"
|
|
|
|
#include "cmSystemTools.h"
|
2003-06-23 22:10:12 +04:00
|
|
|
|
|
|
|
#include <cmsys/RegularExpression.hxx>
|
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;
|
|
|
|
}
|
|
|
|
|
2001-08-29 02:28:31 +04:00
|
|
|
std::ifstream fin(path);
|
|
|
|
if(!fin)
|
|
|
|
{
|
2002-04-12 01:02:10 +04:00
|
|
|
cmSystemTools::Error("cmListFileCache: error can not open file ", path);
|
2001-08-29 02:28:31 +04:00
|
|
|
return false;
|
|
|
|
}
|
2002-12-12 02:13:33 +03:00
|
|
|
long line=0;
|
2001-08-29 02:28:31 +04:00
|
|
|
cmListFile inFile;
|
|
|
|
inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
|
2001-11-30 00:44:22 +03:00
|
|
|
bool parseError;
|
2001-08-29 02:28:31 +04:00
|
|
|
while ( fin )
|
|
|
|
{
|
|
|
|
cmListFileFunction inFunction;
|
2002-12-12 02:13:33 +03:00
|
|
|
if(cmListFileCache::ParseFunction(fin, inFunction, path, parseError,
|
2002-12-12 19:36:28 +03:00
|
|
|
line))
|
2001-08-29 02:28:31 +04:00
|
|
|
{
|
2002-12-12 02:13:33 +03:00
|
|
|
inFunction.m_FilePath = path;
|
2001-08-29 02:28:31 +04:00
|
|
|
inFile.m_Functions.push_back(inFunction);
|
|
|
|
}
|
2001-11-30 00:44:22 +03:00
|
|
|
if (parseError)
|
|
|
|
{
|
|
|
|
inFile.m_ModifiedTime = 0;
|
|
|
|
}
|
2001-08-29 02:28:31 +04:00
|
|
|
}
|
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";
|
2002-12-12 02:13:33 +03:00
|
|
|
cmListFileArgument prj("Project", false);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2001-08-29 02:28:31 +04:00
|
|
|
m_ListFileCache[path] = inFile;
|
|
|
|
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-03-27 20:24:30 +03:00
|
|
|
inline void RemoveComments(std::string& line)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-03-27 20:24:30 +03:00
|
|
|
std::string::size_type pos = line.find("#");
|
|
|
|
if (pos != std::string::npos )
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-03-27 20:24:30 +03:00
|
|
|
line.erase(pos);
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmListFileCache::ParseFunction(std::ifstream& fin,
|
|
|
|
cmListFileFunction& function,
|
|
|
|
const char* filename,
|
|
|
|
bool& parseError,
|
2002-12-12 19:36:28 +03:00
|
|
|
long& line)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
|
|
|
parseError = false;
|
|
|
|
std::string& name = function.m_Name;
|
|
|
|
std::vector<cmListFileArgument>& arguments = function.m_Arguments;
|
|
|
|
name = "";
|
|
|
|
arguments = std::vector<cmListFileArgument>();
|
2003-03-27 20:24:30 +03:00
|
|
|
std::string inbuffer;
|
2002-12-12 02:13:33 +03:00
|
|
|
if(!fin)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2003-03-27 20:24:30 +03:00
|
|
|
if(cmSystemTools::GetLineFromStream(fin, inbuffer) )
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2002-12-12 19:36:28 +03:00
|
|
|
++line;
|
2002-12-12 02:13:33 +03:00
|
|
|
RemoveComments(inbuffer);
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression blankLine("^[ \t\r]*$");
|
|
|
|
cmsys::RegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
|
|
|
|
cmsys::RegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
|
|
|
|
cmsys::RegularExpression lastLine("^(.*)\\)[ \t\r]*$");
|
2002-12-12 02:13:33 +03:00
|
|
|
|
|
|
|
// check for blank line or comment
|
2003-03-27 20:24:30 +03:00
|
|
|
if(blankLine.find(inbuffer.c_str()) )
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// look for a oneline fun(arg arg2)
|
2003-03-27 20:24:30 +03:00
|
|
|
else if(oneLiner.find(inbuffer.c_str()))
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
|
|
|
// the arguments are the second match
|
|
|
|
std::string args = oneLiner.match(2);
|
|
|
|
name = oneLiner.match(1);
|
|
|
|
// break up the arguments
|
|
|
|
cmListFileCache::GetArguments(args, arguments);
|
2002-12-12 19:36:28 +03:00
|
|
|
function.m_Line = line;
|
2002-12-12 02:13:33 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// look for a start of a multiline with no trailing ")" fun(arg arg2
|
2003-03-27 20:24:30 +03:00
|
|
|
else if(multiLine.find(inbuffer.c_str()))
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
|
|
|
name = multiLine.match(1);
|
|
|
|
std::string args = multiLine.match(2);
|
|
|
|
cmListFileCache::GetArguments(args, arguments);
|
2002-12-12 19:36:28 +03:00
|
|
|
function.m_Line = line;
|
2002-12-12 02:13:33 +03:00
|
|
|
// Read lines until the closing paren is hit
|
|
|
|
bool done = false;
|
|
|
|
while(!done)
|
|
|
|
{
|
|
|
|
// read lines until the end paren is found
|
2003-03-27 20:24:30 +03:00
|
|
|
if(cmSystemTools::GetLineFromStream(fin, inbuffer) )
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2002-12-12 19:36:28 +03:00
|
|
|
++line;
|
2002-12-12 02:13:33 +03:00
|
|
|
RemoveComments(inbuffer);
|
|
|
|
// Check for comment lines and ignore them.
|
2003-03-27 20:24:30 +03:00
|
|
|
if(blankLine.find(inbuffer.c_str()))
|
2002-12-12 02:13:33 +03:00
|
|
|
{ continue; }
|
|
|
|
// Is this the last line?
|
2003-03-27 20:24:30 +03:00
|
|
|
if(lastLine.find(inbuffer.c_str()))
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
std::string gargs = lastLine.match(1);
|
|
|
|
cmListFileCache::GetArguments(gargs, arguments);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-03-27 20:24:30 +03:00
|
|
|
cmListFileCache::GetArguments(inbuffer, arguments);
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parseError = true;
|
2002-12-12 19:36:28 +03:00
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
|
|
|
<< filename << ":" << line << ":\n"
|
|
|
|
<< "Parse error. Function missing ending \")\".";
|
|
|
|
cmSystemTools::Error(error.str().c_str());
|
2002-12-12 02:13:33 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parseError = true;
|
2002-12-12 19:36:28 +03:00
|
|
|
cmOStringStream error;
|
|
|
|
error << "Error in cmake code at\n"
|
|
|
|
<< filename << ":" << line << ":\n"
|
|
|
|
<< "Parse error.";
|
|
|
|
cmSystemTools::Error(error.str().c_str());
|
2002-12-12 02:13:33 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmListFileCache::GetArguments(std::string& line,
|
|
|
|
std::vector<cmListFileArgument>& arguments)
|
|
|
|
{
|
|
|
|
// Match a normal argument (not quoted, no spaces).
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
|
2002-12-12 02:13:33 +03:00
|
|
|
// Match a quoted argument (surrounded by double quotes, spaces allowed).
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");
|
2002-12-12 02:13:33 +03:00
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while(!done)
|
|
|
|
{
|
|
|
|
std::string arg;
|
|
|
|
std::string::size_type endpos=0;
|
|
|
|
bool quoted = false;
|
|
|
|
bool foundQuoted = quotedArgument.find(line.c_str());
|
|
|
|
bool foundNormal = normalArgument.find(line.c_str());
|
|
|
|
|
|
|
|
if(foundQuoted && foundNormal)
|
|
|
|
{
|
|
|
|
// Both matches were found. Take the earlier one.
|
|
|
|
// Favor double-quoted version if there is a tie.
|
|
|
|
if(normalArgument.start(1) < quotedArgument.start(1))
|
|
|
|
{
|
|
|
|
arg = normalArgument.match(1);
|
|
|
|
endpos = normalArgument.end(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg = quotedArgument.match(1);
|
|
|
|
endpos = quotedArgument.end(1);
|
|
|
|
// Strip off the double quotes on the ends.
|
|
|
|
arg = arg.substr(1, arg.length()-2);
|
|
|
|
quoted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(foundQuoted)
|
|
|
|
{
|
|
|
|
arg = quotedArgument.match(1);
|
|
|
|
endpos = quotedArgument.end(1);
|
|
|
|
// Strip off the double quotes on the ends.
|
|
|
|
arg = arg.substr(1, arg.length()-2);
|
|
|
|
quoted = true;
|
|
|
|
}
|
|
|
|
else if(foundNormal)
|
|
|
|
{
|
|
|
|
arg = normalArgument.match(1);
|
|
|
|
endpos = normalArgument.end(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
if(!done)
|
|
|
|
{
|
|
|
|
cmListFileArgument a(cmSystemTools::RemoveEscapes(arg.c_str()), quoted);
|
|
|
|
arguments.push_back(a);
|
|
|
|
line = line.substr(endpos, line.length() - endpos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|