2001-01-11 19:55:47 +00:00
|
|
|
/*=========================================================================
|
|
|
|
|
2002-10-23 22:03:27 +00:00
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
2001-01-11 19:55:47 +00:00
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
2002-10-23 22:03:27 +00:00
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
|
2001-01-11 19:55:47 +00:00
|
|
|
|
2006-03-10 18:06:26 +00:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
2002-01-21 20:30:43 +00:00
|
|
|
PURPOSE. See the above copyright notices for more information.
|
2001-01-11 19:55:47 +00:00
|
|
|
|
|
|
|
=========================================================================*/
|
2000-08-29 19:26:29 +00:00
|
|
|
#include "cmMakeDepend.h"
|
2000-09-12 09:30:35 +00:00
|
|
|
#include "cmSystemTools.h"
|
2000-08-29 19:26:29 +00:00
|
|
|
|
2003-06-23 18:10:12 +00:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2001-07-17 19:09:16 +00:00
|
|
|
|
|
|
|
void cmDependInformation::AddDependencies(cmDependInformation* info)
|
|
|
|
{
|
|
|
|
if(this != info)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->DependencySet.insert(info);
|
2001-07-17 19:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-29 19:26:29 +00:00
|
|
|
cmMakeDepend::cmMakeDepend()
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Verbose = false;
|
|
|
|
this->IncludeFileRegularExpression.compile("^.*$");
|
|
|
|
this->ComplainFileRegularExpression.compile("^$");
|
2000-09-12 09:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 19:26:29 +00:00
|
|
|
cmMakeDepend::~cmMakeDepend()
|
2006-03-15 16:02:08 +00:00
|
|
|
{
|
|
|
|
for(DependInformationMapType::iterator i =
|
|
|
|
this->DependInformationMap.begin();
|
|
|
|
i != this->DependInformationMap.end(); ++i)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2001-07-17 19:09:16 +00:00
|
|
|
delete i->second;
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the makefile that depends will be made from.
|
2001-04-25 13:37:57 +00:00
|
|
|
// The pointer is kept so the cmSourceFile array can
|
2000-09-12 09:30:35 +00:00
|
|
|
// be updated with the depend information in the cmMakefile.
|
2000-08-29 19:26:29 +00:00
|
|
|
|
2005-06-22 13:06:46 +00:00
|
|
|
void cmMakeDepend::SetMakefile(cmMakefile* makefile)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile = makefile;
|
2001-03-15 23:09:16 +00:00
|
|
|
|
|
|
|
// Now extract the include file regular expression from the makefile.
|
2006-03-15 16:02:08 +00:00
|
|
|
this->IncludeFileRegularExpression.compile(
|
|
|
|
this->Makefile->IncludeFileRegularExpression.c_str());
|
|
|
|
this->ComplainFileRegularExpression.compile(
|
|
|
|
this->Makefile->ComplainFileRegularExpression.c_str());
|
|
|
|
|
2000-09-12 09:30:35 +00:00
|
|
|
// Now extract any include paths from the makefile flags
|
2001-07-30 15:34:03 +00:00
|
|
|
const std::vector<std::string>& includes =
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile->GetIncludeDirectories();
|
2001-07-30 15:34:03 +00:00
|
|
|
for(std::vector<std::string>::const_iterator j = includes.begin();
|
2001-07-17 19:09:16 +00:00
|
|
|
j != includes.end(); ++j)
|
2000-09-12 09:30:35 +00:00
|
|
|
{
|
2001-07-17 19:41:04 +00:00
|
|
|
std::string path = *j;
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile->ExpandVariablesInString(path);
|
2001-07-17 19:41:04 +00:00
|
|
|
this->AddSearchPath(path.c_str());
|
2000-09-12 09:30:35 +00:00
|
|
|
}
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-17 19:09:16 +00:00
|
|
|
const cmDependInformation* cmMakeDepend::FindDependencies(const char* file)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2002-09-10 19:37:28 +00:00
|
|
|
cmDependInformation* info = this->GetDependInformation(file,0);
|
2001-07-17 19:09:16 +00:00
|
|
|
this->GenerateDependInformation(info);
|
|
|
|
return info;
|
2001-06-12 13:00:21 +00:00
|
|
|
}
|
|
|
|
|
2001-07-17 19:09:16 +00:00
|
|
|
void cmMakeDepend::GenerateDependInformation(cmDependInformation* info)
|
2001-06-12 13:00:21 +00:00
|
|
|
{
|
2001-07-17 19:09:16 +00:00
|
|
|
// If dependencies are already done, stop now.
|
2006-03-15 16:02:08 +00:00
|
|
|
if(info->DependDone)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2001-07-17 19:09:16 +00:00
|
|
|
return;
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
2001-07-17 19:09:16 +00:00
|
|
|
else
|
2001-06-12 13:00:21 +00:00
|
|
|
{
|
2001-07-17 19:09:16 +00:00
|
|
|
// Make sure we don't visit the same file more than once.
|
2006-03-15 16:02:08 +00:00
|
|
|
info->DependDone = true;
|
2001-06-12 13:00:21 +00:00
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
const char* path = info->FullPath.c_str();
|
2000-08-29 19:26:29 +00:00
|
|
|
if(!path)
|
|
|
|
{
|
2006-03-10 18:06:26 +00:00
|
|
|
cmSystemTools::Error(
|
|
|
|
"Attempt to find dependencies for file without path!");
|
2000-08-29 19:26:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-07-17 19:09:16 +00:00
|
|
|
|
2001-11-12 20:37:38 +00:00
|
|
|
bool found = false;
|
2001-11-16 14:03:31 +00:00
|
|
|
|
2001-03-08 21:12:16 +00:00
|
|
|
// If the file exists, use it to find dependency information.
|
2007-12-15 01:31:27 +00:00
|
|
|
if(cmSystemTools::FileExists(path, true))
|
2001-03-08 21:12:16 +00:00
|
|
|
{
|
|
|
|
// Use the real file to find its dependencies.
|
2001-07-17 19:09:16 +00:00
|
|
|
this->DependWalk(info);
|
2001-11-12 20:37:38 +00:00
|
|
|
found = true;
|
2001-03-08 21:12:16 +00:00
|
|
|
}
|
2001-11-16 14:03:31 +00:00
|
|
|
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2001-11-12 20:37:38 +00:00
|
|
|
// See if the cmSourceFile for it has any files specified as
|
|
|
|
// dependency hints.
|
2006-03-15 16:38:47 +00:00
|
|
|
if(info->SourceFile != 0)
|
2001-03-08 21:12:16 +00:00
|
|
|
{
|
2001-11-16 14:03:31 +00:00
|
|
|
|
2001-04-25 13:37:57 +00:00
|
|
|
// Get the cmSourceFile corresponding to this.
|
2006-03-15 16:38:47 +00:00
|
|
|
const cmSourceFile& cFile = *(info->SourceFile);
|
2001-03-08 21:12:16 +00:00
|
|
|
// See if there are any hints for finding dependencies for the missing
|
|
|
|
// file.
|
2001-04-24 20:49:12 +00:00
|
|
|
if(!cFile.GetDepends().empty())
|
2001-03-08 21:12:16 +00:00
|
|
|
{
|
2001-11-12 20:37:38 +00:00
|
|
|
// Dependency hints have been given. Use them to begin the
|
2001-03-08 21:12:16 +00:00
|
|
|
// recursion.
|
2001-06-12 13:00:21 +00:00
|
|
|
for(std::vector<std::string>::const_iterator file =
|
2006-03-10 18:06:26 +00:00
|
|
|
cFile.GetDepends().begin(); file != cFile.GetDepends().end();
|
2001-04-24 20:49:12 +00:00
|
|
|
++file)
|
2001-03-08 21:12:16 +00:00
|
|
|
{
|
|
|
|
this->AddDependency(info, file->c_str());
|
|
|
|
}
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2001-03-08 21:12:16 +00:00
|
|
|
// Found dependency information. We are done.
|
2001-11-12 20:37:38 +00:00
|
|
|
found = true;
|
2001-04-16 16:31:56 +00:00
|
|
|
}
|
2001-03-08 21:12:16 +00:00
|
|
|
}
|
2001-11-16 14:03:31 +00:00
|
|
|
|
|
|
|
if(!found)
|
|
|
|
{
|
|
|
|
// Try to find the file amongst the sources
|
2006-03-15 16:02:08 +00:00
|
|
|
cmSourceFile *srcFile = this->Makefile->GetSource
|
|
|
|
(cmSystemTools::GetFilenameWithoutExtension(path).c_str());
|
2002-06-27 19:57:09 +00:00
|
|
|
if (srcFile)
|
2001-11-16 14:03:31 +00:00
|
|
|
{
|
2002-06-27 19:57:09 +00:00
|
|
|
if (srcFile->GetFullPath() == path)
|
2001-11-16 14:03:31 +00:00
|
|
|
{
|
2002-06-27 19:57:09 +00:00
|
|
|
found=true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//try to guess which include path to use
|
2006-03-15 16:02:08 +00:00
|
|
|
for(std::vector<std::string>::iterator t =
|
|
|
|
this->IncludeDirectories.begin();
|
|
|
|
t != this->IncludeDirectories.end(); ++t)
|
2002-06-27 19:57:09 +00:00
|
|
|
{
|
|
|
|
std::string incpath = *t;
|
2002-11-06 16:20:12 +00:00
|
|
|
if (incpath.size() && incpath[incpath.size() - 1] != '/')
|
|
|
|
{
|
|
|
|
incpath = incpath + "/";
|
|
|
|
}
|
2002-06-27 19:57:09 +00:00
|
|
|
incpath = incpath + path;
|
|
|
|
if (srcFile->GetFullPath() == incpath)
|
2001-11-16 14:03:31 +00:00
|
|
|
{
|
2002-06-27 19:57:09 +00:00
|
|
|
// set the path to the guessed path
|
2006-03-15 16:02:08 +00:00
|
|
|
info->FullPath = incpath;
|
2002-06-27 19:57:09 +00:00
|
|
|
found=true;
|
2001-11-16 14:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2001-11-12 20:37:38 +00:00
|
|
|
if(!found)
|
2001-06-21 19:02:52 +00:00
|
|
|
{
|
2001-11-12 20:37:38 +00:00
|
|
|
// Couldn't find any dependency information.
|
2006-03-15 16:02:08 +00:00
|
|
|
if(this->ComplainFileRegularExpression.find(info->IncludeName.c_str()))
|
2001-11-12 20:37:38 +00:00
|
|
|
{
|
|
|
|
cmSystemTools::Error("error cannot find dependencies for ", path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Destroy the name of the file so that it won't be output as a
|
|
|
|
// dependency.
|
2006-03-15 16:02:08 +00:00
|
|
|
info->FullPath = "";
|
2001-11-12 20:37:38 +00:00
|
|
|
}
|
2001-06-21 19:02:52 +00:00
|
|
|
}
|
2001-03-08 21:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function actually reads the file specified and scans it for
|
|
|
|
// #include directives
|
2001-07-17 19:09:16 +00:00
|
|
|
void cmMakeDepend::DependWalk(cmDependInformation* info)
|
2001-03-08 21:12:16 +00:00
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
cmsys::RegularExpression includeLine
|
|
|
|
("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
|
|
|
|
std::ifstream fin(info->FullPath.c_str());
|
2000-08-29 19:26:29 +00:00
|
|
|
if(!fin)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
cmSystemTools::Error("Cannot open ", info->FullPath.c_str());
|
2000-08-29 19:26:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-07-17 19:09:16 +00:00
|
|
|
|
|
|
|
// TODO: Write real read loop (see cmSystemTools::CopyFile).
|
2003-03-27 17:24:30 +00:00
|
|
|
std::string line;
|
|
|
|
while( cmSystemTools::GetLineFromStream(fin, line) )
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2003-03-27 17:24:30 +00:00
|
|
|
if(includeLine.find(line.c_str()))
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
|
|
|
// extract the file being included
|
2001-06-21 19:02:52 +00:00
|
|
|
std::string includeFile = includeLine.match(1);
|
2000-08-29 19:26:29 +00:00
|
|
|
// see if the include matches the regular expression
|
2006-03-15 16:02:08 +00:00
|
|
|
if(!this->IncludeFileRegularExpression.find(includeFile))
|
2002-10-23 22:03:27 +00:00
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
if(this->Verbose)
|
2002-10-23 22:03:27 +00:00
|
|
|
{
|
2001-01-05 16:41:20 +00:00
|
|
|
std::string message = "Skipping ";
|
|
|
|
message += includeFile;
|
|
|
|
message += " for file ";
|
2006-03-15 16:02:08 +00:00
|
|
|
message += info->FullPath.c_str();
|
2002-10-23 22:03:27 +00:00
|
|
|
cmSystemTools::Error(message.c_str(), 0);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2001-03-08 21:12:16 +00:00
|
|
|
// Add this file and all its dependencies.
|
|
|
|
this->AddDependency(info, includeFile.c_str());
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
2001-03-08 21:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
cmDependInformation* dependInfo =
|
|
|
|
this->GetDependInformation(file, info->PathOnly.c_str());
|
2001-07-17 19:09:16 +00:00
|
|
|
this->GenerateDependInformation(dependInfo);
|
|
|
|
info->AddDependencies(dependInfo);
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 18:19:09 +00:00
|
|
|
cmDependInformation* cmMakeDepend::GetDependInformation(const char* file,
|
|
|
|
const char *extraPath)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2001-07-17 19:09:16 +00:00
|
|
|
// Get the full path for the file so that lookup is unambiguous.
|
2002-06-10 18:19:09 +00:00
|
|
|
std::string fullPath = this->FullPath(file, extraPath);
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2001-07-17 19:09:16 +00:00
|
|
|
// Try to find the file's instance of cmDependInformation.
|
2006-03-15 16:02:08 +00:00
|
|
|
DependInformationMapType::const_iterator result =
|
|
|
|
this->DependInformationMap.find(fullPath);
|
|
|
|
if(result != this->DependInformationMap.end())
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2001-07-17 19:09:16 +00:00
|
|
|
// Found an instance, return it.
|
|
|
|
return result->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Didn't find an instance. Create a new one and save it.
|
|
|
|
cmDependInformation* info = new cmDependInformation;
|
2006-03-15 16:02:08 +00:00
|
|
|
info->FullPath = fullPath;
|
|
|
|
info->PathOnly = cmSystemTools::GetFilenamePath(fullPath.c_str());
|
|
|
|
info->IncludeName = file;
|
|
|
|
this->DependInformationMap[fullPath] = info;
|
2001-07-17 19:09:16 +00:00
|
|
|
return info;
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-15 16:02:08 +00:00
|
|
|
// find the full path to fname by searching the this->IncludeDirectories array
|
2002-06-10 18:19:09 +00:00
|
|
|
std::string cmMakeDepend::FullPath(const char* fname, const char *extraPath)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
DirectoryToFileToPathMapType::iterator m;
|
2003-07-28 22:12:23 +00:00
|
|
|
if(extraPath)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
m = this->DirectoryToFileToPathMap.find(extraPath);
|
2003-07-28 22:12:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
m = this->DirectoryToFileToPathMap.find("");
|
2003-07-28 22:12:23 +00:00
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
|
|
|
|
if(m != this->DirectoryToFileToPathMap.end())
|
2003-07-28 22:12:23 +00:00
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
FileToPathMapType& map = m->second;
|
|
|
|
FileToPathMapType::iterator p = map.find(fname);
|
2003-07-28 22:12:23 +00:00
|
|
|
if(p != map.end())
|
|
|
|
{
|
|
|
|
return p->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-15 01:31:27 +00:00
|
|
|
if(cmSystemTools::FileExists(fname, true))
|
2000-11-22 16:02:30 +00:00
|
|
|
{
|
2003-07-28 22:12:23 +00:00
|
|
|
std::string fp = cmSystemTools::CollapseFullPath(fname);
|
2006-03-15 16:02:08 +00:00
|
|
|
this->DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
|
2003-07-28 22:12:23 +00:00
|
|
|
return fp;
|
2000-11-22 16:02:30 +00:00
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
|
|
|
|
for(std::vector<std::string>::iterator i = this->IncludeDirectories.begin();
|
|
|
|
i != this->IncludeDirectories.end(); ++i)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
|
|
|
std::string path = *i;
|
2002-11-06 16:20:12 +00:00
|
|
|
if (path.size() && path[path.size() - 1] != '/')
|
|
|
|
{
|
|
|
|
path = path + "/";
|
|
|
|
}
|
2000-08-29 19:26:29 +00:00
|
|
|
path = path + fname;
|
2007-12-15 01:31:27 +00:00
|
|
|
if(cmSystemTools::FileExists(path.c_str(), true)
|
2003-11-12 19:20:21 +00:00
|
|
|
&& !cmSystemTools::FileIsDirectory(path.c_str()))
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2003-07-28 22:12:23 +00:00
|
|
|
std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
|
2006-03-15 16:02:08 +00:00
|
|
|
this->DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
|
2003-07-28 22:12:23 +00:00
|
|
|
return fp;
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-25 13:37:57 +00:00
|
|
|
|
2002-06-10 18:19:09 +00:00
|
|
|
if (extraPath)
|
|
|
|
{
|
|
|
|
std::string path = extraPath;
|
2002-11-06 16:20:12 +00:00
|
|
|
if (path.size() && path[path.size() - 1] != '/')
|
|
|
|
{
|
|
|
|
path = path + "/";
|
|
|
|
}
|
2002-06-10 18:19:09 +00:00
|
|
|
path = path + fname;
|
2007-12-15 01:31:27 +00:00
|
|
|
if(cmSystemTools::FileExists(path.c_str(), true)
|
2003-11-12 19:20:21 +00:00
|
|
|
&& !cmSystemTools::FileIsDirectory(path.c_str()))
|
2002-06-10 18:19:09 +00:00
|
|
|
{
|
2003-07-28 22:12:23 +00:00
|
|
|
std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
|
2006-03-15 16:02:08 +00:00
|
|
|
this->DirectoryToFileToPathMap[extraPath][fname] = fp;
|
2003-07-28 22:12:23 +00:00
|
|
|
return fp;
|
2002-06-10 18:19:09 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2001-06-21 19:02:52 +00:00
|
|
|
// Couldn't find the file.
|
2000-08-29 19:26:29 +00:00
|
|
|
return std::string(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a directory to the search path
|
|
|
|
void cmMakeDepend::AddSearchPath(const char* path)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->IncludeDirectories.push_back(path);
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|