2001-01-11 22:55:47 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: Insight Segmentation & Registration Toolkit
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
2002-01-21 23:30:43 +03:00
|
|
|
Copyright (c) 2002 Insight Consortium. All rights reserved.
|
|
|
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
|
2001-01-11 22:55:47 +03:00
|
|
|
|
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-01-11 22:55:47 +03:00
|
|
|
|
|
|
|
=========================================================================*/
|
2000-08-29 23:26:29 +04:00
|
|
|
#include "cmMakeDepend.h"
|
2001-01-05 19:41:20 +03:00
|
|
|
#include "cmStandardIncludes.h"
|
2000-09-12 13:30:35 +04:00
|
|
|
#include "cmSystemTools.h"
|
2000-08-29 23:26:29 +04:00
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
|
|
|
|
void cmDependInformation::AddDependencies(cmDependInformation* info)
|
|
|
|
{
|
|
|
|
if(this != info)
|
|
|
|
{
|
|
|
|
m_DependencySet.insert(info);
|
|
|
|
for (cmDependInformation::DependencySet::const_iterator
|
|
|
|
d = info->m_DependencySet.begin();
|
|
|
|
d != info->m_DependencySet.end(); ++d)
|
|
|
|
{
|
|
|
|
m_DependencySet.insert(*d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:26:29 +04:00
|
|
|
cmMakeDepend::cmMakeDepend()
|
|
|
|
{
|
|
|
|
m_Verbose = false;
|
2001-06-21 23:02:52 +04:00
|
|
|
m_IncludeFileRegularExpression.compile("^.*$");
|
|
|
|
m_ComplainFileRegularExpression.compile("^$");
|
2000-09-12 13:30:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 23:26:29 +04:00
|
|
|
cmMakeDepend::~cmMakeDepend()
|
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
for(DependInformationMap::iterator i = m_DependInformationMap.begin();
|
|
|
|
i != m_DependInformationMap.end(); ++i)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
delete i->second;
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the makefile that depends will be made from.
|
2001-04-25 17:37:57 +04:00
|
|
|
// The pointer is kept so the cmSourceFile array can
|
2000-09-12 13:30:35 +04:00
|
|
|
// be updated with the depend information in the cmMakefile.
|
2000-08-29 23:26:29 +04:00
|
|
|
|
2001-06-12 17:00:21 +04:00
|
|
|
void cmMakeDepend::SetMakefile(const cmMakefile* makefile)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
|
|
|
m_Makefile = makefile;
|
2001-03-16 02:09:16 +03:00
|
|
|
|
|
|
|
// Now extract the include file regular expression from the makefile.
|
|
|
|
m_IncludeFileRegularExpression.compile(
|
|
|
|
m_Makefile->m_IncludeFileRegularExpression.c_str());
|
2001-06-21 23:02:52 +04:00
|
|
|
m_ComplainFileRegularExpression.compile(
|
|
|
|
m_Makefile->m_ComplainFileRegularExpression.c_str());
|
2000-09-12 13:30:35 +04:00
|
|
|
|
|
|
|
// Now extract any include paths from the makefile flags
|
2001-07-30 19:34:03 +04:00
|
|
|
const std::vector<std::string>& includes =
|
2001-07-17 23:09:16 +04:00
|
|
|
m_Makefile->GetIncludeDirectories();
|
2001-07-30 19:34:03 +04:00
|
|
|
for(std::vector<std::string>::const_iterator j = includes.begin();
|
2001-07-17 23:09:16 +04:00
|
|
|
j != includes.end(); ++j)
|
2000-09-12 13:30:35 +04:00
|
|
|
{
|
2001-07-17 23:41:04 +04:00
|
|
|
std::string path = *j;
|
|
|
|
m_Makefile->ExpandVariablesInString(path);
|
|
|
|
this->AddSearchPath(path.c_str());
|
2000-09-12 13:30:35 +04:00
|
|
|
}
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
const cmDependInformation* cmMakeDepend::FindDependencies(const char* file)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
cmDependInformation* info = this->GetDependInformation(file);
|
|
|
|
this->GenerateDependInformation(info);
|
|
|
|
return info;
|
2001-06-12 17:00:21 +04:00
|
|
|
}
|
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
void cmMakeDepend::GenerateDependInformation(cmDependInformation* info)
|
2001-06-12 17:00:21 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
// If dependencies are already done, stop now.
|
|
|
|
if(info->m_DependDone)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
return;
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
2001-07-17 23:09:16 +04:00
|
|
|
else
|
2001-06-12 17:00:21 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
// Make sure we don't visit the same file more than once.
|
|
|
|
info->m_DependDone = true;
|
2001-06-12 17:00:21 +04:00
|
|
|
}
|
2000-08-29 23:26:29 +04:00
|
|
|
const char* path = info->m_FullPath.c_str();
|
|
|
|
if(!path)
|
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
cmSystemTools::Error("Attempt to find dependencies for file without path!");
|
2000-08-29 23:26:29 +04:00
|
|
|
return;
|
|
|
|
}
|
2001-07-17 23:09:16 +04:00
|
|
|
|
2001-11-12 23:37:38 +03:00
|
|
|
bool found = false;
|
2001-11-16 17:03:31 +03:00
|
|
|
|
2001-03-09 00:12:16 +03:00
|
|
|
// If the file exists, use it to find dependency information.
|
|
|
|
if(cmSystemTools::FileExists(path))
|
|
|
|
{
|
|
|
|
// Use the real file to find its dependencies.
|
2001-07-17 23:09:16 +04:00
|
|
|
this->DependWalk(info);
|
2001-11-12 23:37:38 +03:00
|
|
|
found = true;
|
2001-03-09 00:12:16 +03:00
|
|
|
}
|
2001-11-16 17:03:31 +03:00
|
|
|
|
|
|
|
|
2001-11-12 23:37:38 +03:00
|
|
|
// See if the cmSourceFile for it has any files specified as
|
|
|
|
// dependency hints.
|
2001-07-17 23:09:16 +04:00
|
|
|
if(info->m_cmSourceFile != 0)
|
2001-03-09 00:12:16 +03:00
|
|
|
{
|
2001-11-16 17:03:31 +03:00
|
|
|
|
2001-04-25 17:37:57 +04:00
|
|
|
// Get the cmSourceFile corresponding to this.
|
2001-07-17 23:09:16 +04:00
|
|
|
const cmSourceFile& cFile = *(info->m_cmSourceFile);
|
2001-03-09 00:12:16 +03:00
|
|
|
// See if there are any hints for finding dependencies for the missing
|
|
|
|
// file.
|
2001-04-25 00:49:12 +04:00
|
|
|
if(!cFile.GetDepends().empty())
|
2001-03-09 00:12:16 +03:00
|
|
|
{
|
2001-11-12 23:37:38 +03:00
|
|
|
// Dependency hints have been given. Use them to begin the
|
2001-03-09 00:12:16 +03:00
|
|
|
// recursion.
|
2001-06-12 17:00:21 +04:00
|
|
|
for(std::vector<std::string>::const_iterator file =
|
2001-04-25 00:49:12 +04:00
|
|
|
cFile.GetDepends().begin(); file != cFile.GetDepends().end();
|
|
|
|
++file)
|
2001-03-09 00:12:16 +03:00
|
|
|
{
|
|
|
|
this->AddDependency(info, file->c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found dependency information. We are done.
|
2001-11-12 23:37:38 +03:00
|
|
|
found = true;
|
2001-04-16 20:31:56 +04:00
|
|
|
}
|
2001-03-09 00:12:16 +03:00
|
|
|
}
|
2001-11-16 17:03:31 +03:00
|
|
|
|
|
|
|
if(!found)
|
|
|
|
{
|
|
|
|
// Try to find the file amongst the sources
|
|
|
|
cmMakefile::SourceMap srcmap = m_Makefile->GetSources();
|
|
|
|
cmMakefile::SourceMap::iterator l;
|
|
|
|
for (l= srcmap.begin() ; l!=srcmap.end() ; l++)
|
|
|
|
{
|
|
|
|
for(std::vector<cmSourceFile>::iterator i = l->second.begin();
|
|
|
|
i != l->second.end(); i++)
|
|
|
|
{
|
|
|
|
if (i->GetFullPath() == path)
|
|
|
|
{
|
|
|
|
found=true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//try to guess which include path to use
|
|
|
|
for(std::vector<std::string>::iterator t =
|
|
|
|
m_IncludeDirectories.begin();
|
|
|
|
t != m_IncludeDirectories.end(); ++t)
|
|
|
|
{
|
|
|
|
std::string incpath = *t;
|
|
|
|
incpath = incpath + "/";
|
|
|
|
incpath = incpath + path;
|
|
|
|
if (i->GetFullPath() == incpath)
|
|
|
|
{
|
|
|
|
// set the path to the guessed path
|
|
|
|
info->m_FullPath = incpath;
|
|
|
|
found=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-12 23:37:38 +03:00
|
|
|
if(!found)
|
2001-06-21 23:02:52 +04:00
|
|
|
{
|
2001-11-12 23:37:38 +03:00
|
|
|
// Couldn't find any dependency information.
|
|
|
|
if(m_ComplainFileRegularExpression.find(info->m_IncludeName.c_str()))
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
info->m_FullPath = "";
|
|
|
|
}
|
2001-06-21 23:02:52 +04:00
|
|
|
}
|
2001-03-09 00:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function actually reads the file specified and scans it for
|
|
|
|
// #include directives
|
2001-07-17 23:09:16 +04:00
|
|
|
void cmMakeDepend::DependWalk(cmDependInformation* info)
|
2001-03-09 00:12:16 +03:00
|
|
|
{
|
2001-06-21 23:02:52 +04:00
|
|
|
cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
|
2001-07-17 23:09:16 +04:00
|
|
|
std::ifstream fin(info->m_FullPath.c_str());
|
2000-08-29 23:26:29 +04:00
|
|
|
if(!fin)
|
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
cmSystemTools::Error("Cannot open ", info->m_FullPath.c_str());
|
2000-08-29 23:26:29 +04:00
|
|
|
return;
|
|
|
|
}
|
2001-07-17 23:09:16 +04:00
|
|
|
|
|
|
|
// TODO: Write real read loop (see cmSystemTools::CopyFile).
|
2000-08-29 23:26:29 +04:00
|
|
|
char line[255];
|
2001-07-17 23:09:16 +04:00
|
|
|
for(fin.getline(line, 255); fin; fin.getline(line, 255))
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-06-21 23:02:52 +04:00
|
|
|
if(includeLine.find(line))
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
|
|
|
// extract the file being included
|
2001-06-21 23:02:52 +04:00
|
|
|
std::string includeFile = includeLine.match(1);
|
2000-08-29 23:26:29 +04:00
|
|
|
// see if the include matches the regular expression
|
|
|
|
if(!m_IncludeFileRegularExpression.find(includeFile))
|
|
|
|
{
|
|
|
|
if(m_Verbose)
|
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
std::string message = "Skipping ";
|
|
|
|
message += includeFile;
|
|
|
|
message += " for file ";
|
2001-07-17 23:09:16 +04:00
|
|
|
message += info->m_FullPath.c_str();
|
2001-01-05 19:41:20 +03:00
|
|
|
cmSystemTools::Error(message.c_str(), 0);
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2001-03-09 00:12:16 +03:00
|
|
|
|
|
|
|
// Add this file and all its dependencies.
|
|
|
|
this->AddDependency(info, includeFile.c_str());
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
2001-03-09 00:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
|
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
cmDependInformation* dependInfo = this->GetDependInformation(file);
|
|
|
|
this->GenerateDependInformation(dependInfo);
|
|
|
|
info->AddDependencies(dependInfo);
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
cmDependInformation* cmMakeDepend::GetDependInformation(const char* file)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
// Get the full path for the file so that lookup is unambiguous.
|
|
|
|
std::string fullPath = this->FullPath(file);
|
2000-08-29 23:26:29 +04:00
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
// Try to find the file's instance of cmDependInformation.
|
|
|
|
DependInformationMap::const_iterator result =
|
|
|
|
m_DependInformationMap.find(fullPath);
|
|
|
|
if(result != m_DependInformationMap.end())
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04: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;
|
|
|
|
info->m_FullPath = fullPath;
|
|
|
|
info->m_IncludeName = file;
|
|
|
|
m_DependInformationMap[fullPath] = info;
|
|
|
|
return info;
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
void cmMakeDepend::GenerateMakefileDependencies()
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
// Now create cmDependInformation objects for files in the directory
|
|
|
|
const cmTargets &tgts = m_Makefile->GetTargets();
|
|
|
|
for(cmTargets::const_iterator l = tgts.begin();
|
|
|
|
l != tgts.end(); l++)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
const std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
|
|
|
|
for(std::vector<cmSourceFile>::const_iterator i = classes.begin();
|
|
|
|
i != classes.end(); ++i)
|
2001-06-12 17:00:21 +04:00
|
|
|
{
|
2001-07-17 23:09:16 +04:00
|
|
|
if(!i->GetIsAHeaderFileOnly())
|
|
|
|
{
|
|
|
|
cmDependInformation* info =
|
|
|
|
this->GetDependInformation(i->GetFullPath().c_str());
|
|
|
|
this->AddFileToSearchPath(info->m_FullPath.c_str());
|
|
|
|
info->m_cmSourceFile = &*i;
|
|
|
|
this->GenerateDependInformation(info);
|
|
|
|
}
|
2001-06-12 17:00:21 +04:00
|
|
|
}
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
|
2000-08-29 23:26:29 +04:00
|
|
|
// find the full path to fname by searching the m_IncludeDirectories array
|
|
|
|
std::string cmMakeDepend::FullPath(const char* fname)
|
|
|
|
{
|
2000-11-22 19:02:30 +03:00
|
|
|
if(cmSystemTools::FileExists(fname))
|
|
|
|
{
|
|
|
|
return std::string(fname);
|
|
|
|
}
|
2001-01-05 19:41:20 +03:00
|
|
|
|
2000-08-29 23:26:29 +04:00
|
|
|
for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
|
|
|
|
i != m_IncludeDirectories.end(); ++i)
|
|
|
|
{
|
|
|
|
std::string path = *i;
|
|
|
|
path = path + "/";
|
|
|
|
path = path + fname;
|
2000-09-12 13:30:35 +04:00
|
|
|
if(cmSystemTools::FileExists(path.c_str()))
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
2001-04-25 17:37:57 +04:00
|
|
|
|
2001-06-21 23:02:52 +04:00
|
|
|
// Couldn't find the file.
|
2000-08-29 23:26:29 +04:00
|
|
|
return std::string(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a directory to the search path
|
|
|
|
void cmMakeDepend::AddSearchPath(const char* path)
|
|
|
|
{
|
|
|
|
m_IncludeDirectories.push_back(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a directory to the search path
|
|
|
|
void cmMakeDepend::AddFileToSearchPath(const char* file)
|
|
|
|
{
|
|
|
|
std::string filepath = file;
|
|
|
|
std::string::size_type pos = filepath.rfind('/');
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string path = filepath.substr(0, pos);
|
2001-01-05 19:41:20 +03:00
|
|
|
if(std::find(m_IncludeDirectories.begin(),
|
|
|
|
m_IncludeDirectories.end(), path)
|
2000-08-29 23:26:29 +04:00
|
|
|
== m_IncludeDirectories.end())
|
|
|
|
{
|
|
|
|
m_IncludeDirectories.push_back(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-17 23:09:16 +04:00
|
|
|
const cmDependInformation*
|
|
|
|
cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
|
|
|
|
{
|
|
|
|
for(DependInformationMap::const_iterator i = m_DependInformationMap.begin();
|
|
|
|
i != m_DependInformationMap.end(); ++i)
|
|
|
|
{
|
|
|
|
const cmDependInformation* info = i->second;
|
|
|
|
if(info->m_cmSourceFile == &sf)
|
|
|
|
{
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|