2005-01-19 01:09:05 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#include "cmDependsC.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
2005-05-10 19:00:15 +04:00
|
|
|
#include <ctype.h> // isspace
|
|
|
|
|
2005-01-19 01:09:05 +03:00
|
|
|
//----------------------------------------------------------------------------
|
2005-08-17 19:43:58 +04:00
|
|
|
cmDependsC::cmDependsC():
|
|
|
|
m_IncludePath(0), m_GeneratedFiles(0)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-05-11 21:16:45 +04:00
|
|
|
// yummy look at all those constructor arguments
|
2005-07-27 17:49:37 +04:00
|
|
|
cmDependsC::cmDependsC(std::vector<std::string> const& includes,
|
2005-08-17 19:43:58 +04:00
|
|
|
const char* scanRegex, const char* complainRegex,
|
|
|
|
std::set<cmStdString> const& generatedFiles):
|
2005-01-19 01:09:05 +03:00
|
|
|
m_IncludePath(&includes),
|
2005-03-03 23:22:18 +03:00
|
|
|
m_IncludeRegexLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)([\">])"),
|
2005-02-08 00:11:01 +03:00
|
|
|
m_IncludeRegexScan(scanRegex),
|
2005-08-17 19:43:58 +04:00
|
|
|
m_IncludeRegexComplain(complainRegex),
|
|
|
|
m_GeneratedFiles(&generatedFiles)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmDependsC::~cmDependsC()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-07-27 17:49:37 +04:00
|
|
|
bool cmDependsC::WriteDependencies(const char *src,
|
|
|
|
const char *obj, std::ostream& os)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
// Make sure this is a scanning instance.
|
2005-07-27 17:49:37 +04:00
|
|
|
if(!src || src[0] == '\0')
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
2005-07-27 17:49:37 +04:00
|
|
|
cmSystemTools::Error("Cannot scan dependencies without a source file.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!obj || obj[0] == '\0')
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Cannot scan dependencies without an object file.");
|
2005-01-19 01:09:05 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!m_IncludePath)
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Cannot scan dependencies without an include path.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the dependency graph starting with the source file.
|
|
|
|
bool first = true;
|
2005-03-03 23:22:18 +03:00
|
|
|
UnscannedEntry root;
|
2005-07-27 17:49:37 +04:00
|
|
|
root.FileName = src;
|
2005-03-03 23:22:18 +03:00
|
|
|
m_Unscanned.push(root);
|
2005-02-08 00:11:01 +03:00
|
|
|
m_Encountered.clear();
|
2005-07-27 17:49:37 +04:00
|
|
|
m_Encountered.insert(src);
|
2005-01-19 01:09:05 +03:00
|
|
|
std::set<cmStdString> dependencies;
|
|
|
|
std::set<cmStdString> scanned;
|
|
|
|
while(!m_Unscanned.empty())
|
|
|
|
{
|
|
|
|
// Get the next file to scan.
|
2005-03-03 23:22:18 +03:00
|
|
|
UnscannedEntry current = m_Unscanned.front();
|
2005-01-19 01:09:05 +03:00
|
|
|
m_Unscanned.pop();
|
|
|
|
|
|
|
|
// If not a full path, find the file in the include path.
|
|
|
|
std::string fullName;
|
2005-03-03 23:22:18 +03:00
|
|
|
if(first || cmSystemTools::FileIsFullPath(current.FileName.c_str()))
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
2005-08-17 19:43:58 +04:00
|
|
|
if(this->FileExistsOrIsGenerated(current.FileName, scanned,
|
|
|
|
dependencies))
|
2005-02-08 00:11:01 +03:00
|
|
|
{
|
2005-03-03 23:22:18 +03:00
|
|
|
fullName = current.FileName;
|
2005-02-08 00:11:01 +03:00
|
|
|
}
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
2005-03-03 23:22:18 +03:00
|
|
|
else if(!current.QuotedLocation.empty() &&
|
2005-08-17 19:43:58 +04:00
|
|
|
this->FileExistsOrIsGenerated(current.QuotedLocation, scanned,
|
|
|
|
dependencies))
|
2005-03-03 23:22:18 +03:00
|
|
|
{
|
|
|
|
// The include statement producing this entry was a double-quote
|
|
|
|
// include and the included file is present in the directory of
|
|
|
|
// the source containing the include statement.
|
|
|
|
fullName = current.QuotedLocation;
|
|
|
|
}
|
2005-01-19 01:09:05 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for(std::vector<std::string>::const_iterator i = m_IncludePath->begin();
|
|
|
|
i != m_IncludePath->end(); ++i)
|
|
|
|
{
|
2005-02-24 20:44:56 +03:00
|
|
|
// Construct the name of the file as if it were in the current
|
|
|
|
// include directory. Avoid using a leading "./".
|
2005-01-19 01:09:05 +03:00
|
|
|
std::string temp = *i;
|
2005-02-24 20:44:56 +03:00
|
|
|
if(temp == ".")
|
|
|
|
{
|
|
|
|
temp = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp += "/";
|
|
|
|
}
|
2005-03-03 23:22:18 +03:00
|
|
|
temp += current.FileName;
|
2005-02-24 20:44:56 +03:00
|
|
|
|
|
|
|
// Look for the file in this location.
|
2005-08-17 19:43:58 +04:00
|
|
|
if(this->FileExistsOrIsGenerated(temp, scanned, dependencies))
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
fullName = temp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-08 00:11:01 +03:00
|
|
|
// Complain if the file cannot be found and matches the complain
|
|
|
|
// regex.
|
2005-03-03 23:22:18 +03:00
|
|
|
if(fullName.empty() &&
|
|
|
|
m_IncludeRegexComplain.find(current.FileName.c_str()))
|
2005-02-08 00:11:01 +03:00
|
|
|
{
|
2005-03-03 23:22:18 +03:00
|
|
|
cmSystemTools::Error("Cannot find file \"",
|
|
|
|
current.FileName.c_str(), "\".");
|
2005-02-08 00:11:01 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-19 01:09:05 +03:00
|
|
|
// Scan the file if it was found and has not been scanned already.
|
2005-02-08 00:11:01 +03:00
|
|
|
if(!fullName.empty() && (scanned.find(fullName) == scanned.end()))
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
// Record scanned files.
|
|
|
|
scanned.insert(fullName);
|
|
|
|
|
|
|
|
// Try to scan the file. Just leave it out if we cannot find
|
|
|
|
// it.
|
|
|
|
std::ifstream fin(fullName.c_str());
|
|
|
|
if(fin)
|
|
|
|
{
|
|
|
|
// Add this file as a dependency.
|
|
|
|
dependencies.insert(fullName);
|
|
|
|
|
2005-03-03 23:22:18 +03:00
|
|
|
// Scan this file for new dependencies. Pass the directory
|
|
|
|
// containing the file to handle double-quote includes.
|
|
|
|
std::string dir = cmSystemTools::GetFilenamePath(fullName);
|
|
|
|
this->Scan(fin, dir.c_str());
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the dependencies to the output stream.
|
|
|
|
for(std::set<cmStdString>::iterator i=dependencies.begin();
|
|
|
|
i != dependencies.end(); ++i)
|
|
|
|
{
|
2005-07-27 17:49:37 +04:00
|
|
|
os << obj << ": "
|
2005-01-19 01:09:05 +03:00
|
|
|
<< cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
os << std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDependsC::CheckDependencies(std::istream& is)
|
|
|
|
{
|
|
|
|
// Parse dependencies from the stream. If any dependee is missing
|
|
|
|
// or newer than the depender then dependencies should be
|
|
|
|
// regenerated.
|
|
|
|
bool okay = true;
|
|
|
|
std::string line;
|
|
|
|
std::string depender;
|
|
|
|
std::string dependee;
|
|
|
|
while(cmSystemTools::GetLineFromStream(is, line))
|
|
|
|
{
|
2005-05-10 19:00:15 +04:00
|
|
|
// Parse the dependency line.
|
|
|
|
if(!this->ParseDependency(line.c_str(), depender, dependee))
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dependencies must be regenerated if the dependee does not exist
|
|
|
|
// or if the depender exists and is older than the dependee.
|
2005-05-06 17:58:58 +04:00
|
|
|
bool regenerate = false;
|
|
|
|
if(!cmSystemTools::FileExists(dependee.c_str()))
|
|
|
|
{
|
|
|
|
// The dependee does not exist.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
|
|
|
if(m_Verbose)
|
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" does not exist for depender \""
|
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(cmSystemTools::FileExists(depender.c_str()))
|
|
|
|
{
|
|
|
|
// The dependee and depender both exist. Compare file times.
|
|
|
|
int result = 0;
|
|
|
|
if((!cmSystemTools::FileTimeCompare(depender.c_str(), dependee.c_str(),
|
|
|
|
&result) || result < 0))
|
|
|
|
{
|
|
|
|
// The depender is older than the dependee.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
|
|
|
if(m_Verbose)
|
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" is newer than depender \""
|
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(regenerate)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
// Dependencies must be regenerated.
|
|
|
|
okay = false;
|
|
|
|
|
|
|
|
// Remove the depender to be sure it is rebuilt.
|
|
|
|
cmSystemTools::RemoveFile(depender.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-03-03 23:22:18 +03:00
|
|
|
void cmDependsC::Scan(std::istream& is, const char* directory)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
// Read one line at a time.
|
|
|
|
std::string line;
|
|
|
|
while(cmSystemTools::GetLineFromStream(is, line))
|
|
|
|
{
|
2005-02-08 00:11:01 +03:00
|
|
|
// Match include directives.
|
|
|
|
if(m_IncludeRegexLine.find(line.c_str()))
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
// Get the file being included.
|
2005-03-03 23:22:18 +03:00
|
|
|
UnscannedEntry entry;
|
|
|
|
entry.FileName = m_IncludeRegexLine.match(1);
|
|
|
|
if(m_IncludeRegexLine.match(2) == "\"")
|
|
|
|
{
|
|
|
|
// This was a double-quoted include. We must check for the
|
|
|
|
// file in the directory containing the file we are scanning.
|
|
|
|
entry.QuotedLocation = directory;
|
|
|
|
entry.QuotedLocation += "/";
|
|
|
|
entry.QuotedLocation += entry.FileName;
|
|
|
|
}
|
2005-01-19 01:09:05 +03:00
|
|
|
|
2005-02-08 00:11:01 +03:00
|
|
|
// Queue the file if it has not yet been encountered and it
|
2005-03-03 23:22:18 +03:00
|
|
|
// matches the regular expression for recursive scanning. Note
|
|
|
|
// that this check does not account for the possibility of two
|
|
|
|
// headers with the same name in different directories when one
|
|
|
|
// is included by double-quotes and the other by angle brackets.
|
|
|
|
// This kind of problem will be fixed when a more
|
|
|
|
// preprocessor-like implementation of this scanner is created.
|
|
|
|
if(m_Encountered.find(entry.FileName) == m_Encountered.end() &&
|
|
|
|
m_IncludeRegexScan.find(entry.FileName.c_str()))
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
2005-03-03 23:22:18 +03:00
|
|
|
m_Encountered.insert(entry.FileName);
|
|
|
|
m_Unscanned.push(entry);
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-10 19:00:15 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDependsC::ParseDependency(const char* line, std::string& depender,
|
|
|
|
std::string& dependee)
|
|
|
|
{
|
|
|
|
// Start with empty names.
|
|
|
|
depender = "";
|
|
|
|
dependee = "";
|
|
|
|
|
|
|
|
// Get the left-hand-side of the dependency.
|
|
|
|
const char* c = this->ParseFileName(line, depender);
|
|
|
|
|
|
|
|
// Skip the ':' separator.
|
|
|
|
for(;c && *c && isspace(*c);++c);
|
|
|
|
if(!c || !*c || *c != ':')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++c;
|
|
|
|
|
|
|
|
// Get the right-hand-side of the dependency.
|
|
|
|
return this->ParseFileName(c, dependee)?true:false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
const char* cmDependsC::ParseFileName(const char* in, std::string& name)
|
|
|
|
{
|
|
|
|
// Skip leading whitespace.
|
|
|
|
const char* c = in;
|
|
|
|
for(;c && *c && isspace(*c);++c);
|
|
|
|
|
|
|
|
// If this is an empty line or a comment line return failure.
|
|
|
|
if(!c || !*c || *c == '#')
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the possibly quoted file name.
|
|
|
|
bool quoted = false;
|
2005-07-22 23:32:00 +04:00
|
|
|
char* buf = new char[strlen(in)+1];
|
|
|
|
char* pos = buf;
|
2005-07-27 17:49:37 +04:00
|
|
|
|
|
|
|
// for every character while we haven't hit the end of the string AND we
|
|
|
|
// are in a quoted string OR the current character isn't a : or the second
|
|
|
|
// character AND it isn't a space
|
2005-05-10 19:00:15 +04:00
|
|
|
for(;*c && (quoted ||
|
2005-08-10 18:01:43 +04:00
|
|
|
((*c != ':' || pos == buf+1) && !isspace(*c))); ++c)
|
2005-05-10 19:00:15 +04:00
|
|
|
{
|
|
|
|
if(*c == '"')
|
|
|
|
{
|
|
|
|
quoted = !quoted;
|
|
|
|
}
|
2005-08-10 18:01:43 +04:00
|
|
|
// handle unquoted escaped spaces
|
2005-05-10 19:00:15 +04:00
|
|
|
else if(!quoted && *c == '\\' && isspace(*(c+1)))
|
|
|
|
{
|
2005-07-22 23:32:00 +04:00
|
|
|
*pos = *(++c);
|
|
|
|
pos++;
|
2005-05-10 19:00:15 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-07-22 23:32:00 +04:00
|
|
|
*pos = *c;
|
|
|
|
pos++;
|
2005-05-10 19:00:15 +04:00
|
|
|
}
|
|
|
|
}
|
2005-07-22 23:32:00 +04:00
|
|
|
*pos =0;
|
2005-08-10 18:01:43 +04:00
|
|
|
name += buf;
|
2005-07-22 23:32:00 +04:00
|
|
|
delete [] buf;
|
2005-05-10 19:00:15 +04:00
|
|
|
// Return the ending position.
|
|
|
|
return c;
|
|
|
|
}
|
2005-08-17 19:43:58 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDependsC::FileExistsOrIsGenerated(const std::string& fname,
|
|
|
|
std::set<cmStdString>& scanned,
|
|
|
|
std::set<cmStdString>& dependencies)
|
|
|
|
{
|
|
|
|
// Check first for a generated file.
|
|
|
|
if(m_GeneratedFiles &&
|
2005-08-18 17:19:55 +04:00
|
|
|
std::set<cmStdString>::const_iterator(m_GeneratedFiles->find(fname)) !=
|
|
|
|
m_GeneratedFiles->end())
|
2005-08-17 19:43:58 +04:00
|
|
|
{
|
|
|
|
// If the file does not really exist yet pretend it has already
|
|
|
|
// been scanned. When it exists later then dependencies will be
|
|
|
|
// rescanned.
|
|
|
|
if(!cmSystemTools::FileExists(fname.c_str()))
|
|
|
|
{
|
|
|
|
scanned.insert(fname);
|
|
|
|
dependencies.insert(fname);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return cmSystemTools::FileExists(fname.c_str());
|
|
|
|
}
|
|
|
|
}
|