2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2005-01-19 01:09:05 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2005-01-19 01:09:05 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2005-01-19 01:09:05 +03:00
|
|
|
#include "cmDepends.h"
|
|
|
|
|
2007-12-23 06:41:42 +03:00
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmMakefile.h"
|
2005-01-19 01:09:05 +03:00
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
#include "cmSystemTools.h"
|
2005-10-12 21:52:29 +04:00
|
|
|
#include "cmFileTimeComparison.h"
|
2006-01-23 20:31:08 +03:00
|
|
|
#include <string.h>
|
2005-03-08 17:24:24 +03:00
|
|
|
|
2005-01-19 01:09:05 +03:00
|
|
|
//----------------------------------------------------------------------------
|
2008-05-08 18:09:14 +04:00
|
|
|
cmDepends::cmDepends(cmLocalGenerator* lg, const char* targetDir):
|
2006-05-25 17:47:30 +04:00
|
|
|
CompileDirectory(),
|
2008-05-08 18:09:14 +04:00
|
|
|
LocalGenerator(lg),
|
2006-05-25 17:47:30 +04:00
|
|
|
Verbose(false),
|
|
|
|
FileComparison(0),
|
2008-05-08 18:09:14 +04:00
|
|
|
TargetDirectory(targetDir),
|
2006-03-15 19:02:08 +03:00
|
|
|
MaxPath(cmSystemTools::GetMaximumFilePathLength()),
|
|
|
|
Dependee(new char[MaxPath]),
|
|
|
|
Depender(new char[MaxPath])
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
2005-05-11 21:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmDepends::~cmDepends()
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
delete [] this->Dependee;
|
|
|
|
delete [] this->Depender;
|
2005-05-11 21:16:45 +04:00
|
|
|
}
|
|
|
|
|
2005-01-19 01:09:05 +03:00
|
|
|
//----------------------------------------------------------------------------
|
2007-12-23 06:41:42 +03:00
|
|
|
bool cmDepends::Write(std::ostream &makeDepends,
|
|
|
|
std::ostream &internalDepends)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
2007-12-23 06:41:42 +03:00
|
|
|
// Lookup the set of sources to scan.
|
|
|
|
std::string srcLang = "CMAKE_DEPENDS_CHECK_";
|
|
|
|
srcLang += this->Language;
|
|
|
|
cmMakefile* mf = this->LocalGenerator->GetMakefile();
|
|
|
|
const char* srcStr = mf->GetSafeDefinition(srcLang.c_str());
|
|
|
|
std::vector<std::string> pairs;
|
|
|
|
cmSystemTools::ExpandListArgument(srcStr, pairs);
|
|
|
|
|
|
|
|
for(std::vector<std::string>::iterator si = pairs.begin();
|
|
|
|
si != pairs.end();)
|
|
|
|
{
|
|
|
|
// Get the source and object file.
|
|
|
|
std::string const& src = *si++;
|
|
|
|
if(si == pairs.end()) { break; }
|
|
|
|
std::string obj = *si++;
|
|
|
|
|
|
|
|
// Make sure the object file is relative to the top of the build tree.
|
|
|
|
obj = this->LocalGenerator->Convert(obj.c_str(),
|
|
|
|
cmLocalGenerator::HOME_OUTPUT,
|
|
|
|
cmLocalGenerator::MAKEFILE);
|
|
|
|
|
|
|
|
// Write the dependencies for this pair.
|
|
|
|
if(!this->WriteDependencies(src.c_str(), obj.c_str(),
|
|
|
|
makeDepends, internalDepends))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-28 19:49:59 +03:00
|
|
|
return this->Finalize(makeDepends, internalDepends);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDepends::Finalize(std::ostream&,
|
|
|
|
std::ostream&)
|
|
|
|
{
|
2007-12-23 06:41:42 +03:00
|
|
|
return true;
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2009-09-23 22:02:05 +04:00
|
|
|
bool cmDepends::Check(const char *makeFile, const char *internalFile,
|
|
|
|
std::map<std::string, DependencyVector>& validDeps)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
2005-03-08 17:24:24 +03:00
|
|
|
// Dependency checks must be done in proper working directory.
|
|
|
|
std::string oldcwd = ".";
|
2006-03-15 19:02:08 +03:00
|
|
|
if(this->CompileDirectory != ".")
|
2005-03-08 17:24:24 +03:00
|
|
|
{
|
2005-04-12 21:26:35 +04:00
|
|
|
// Get the CWD but do not call CollapseFullPath because
|
|
|
|
// we only need it to cd back, and the form does not matter
|
|
|
|
oldcwd = cmSystemTools::GetCurrentWorkingDirectory(false);
|
2006-03-15 19:02:08 +03:00
|
|
|
cmSystemTools::ChangeDirectory(this->CompileDirectory.c_str());
|
2005-03-08 17:24:24 +03:00
|
|
|
}
|
|
|
|
|
2005-01-19 01:09:05 +03:00
|
|
|
// Check whether dependencies must be regenerated.
|
2007-12-20 00:36:30 +03:00
|
|
|
bool okay = true;
|
2005-10-12 21:52:29 +04:00
|
|
|
std::ifstream fin(internalFile);
|
2009-09-23 22:02:05 +04:00
|
|
|
if(!(fin && this->CheckDependencies(fin, validDeps)))
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
|
|
|
// Clear all dependencies so they will be regenerated.
|
2005-10-12 21:52:29 +04:00
|
|
|
this->Clear(makeFile);
|
2007-12-20 00:36:30 +03:00
|
|
|
cmSystemTools::RemoveFile(internalFile);
|
|
|
|
okay = false;
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
2005-03-08 17:24:24 +03:00
|
|
|
|
|
|
|
// Restore working directory.
|
|
|
|
if(oldcwd != ".")
|
|
|
|
{
|
|
|
|
cmSystemTools::ChangeDirectory(oldcwd.c_str());
|
|
|
|
}
|
2007-12-20 00:36:30 +03:00
|
|
|
|
|
|
|
return okay;
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-07-27 17:49:37 +04:00
|
|
|
void cmDepends::Clear(const char *file)
|
2005-01-19 01:09:05 +03:00
|
|
|
{
|
2005-05-06 17:58:58 +04:00
|
|
|
// Print verbose output.
|
2006-03-15 19:02:08 +03:00
|
|
|
if(this->Verbose)
|
2005-05-06 17:58:58 +04:00
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
2005-07-27 17:49:37 +04:00
|
|
|
msg << "Clearing dependencies in \"" << file << "\"." << std::endl;
|
2005-05-06 17:58:58 +04:00
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
|
2005-01-19 01:09:05 +03:00
|
|
|
// Write an empty dependency file.
|
2005-07-27 17:49:37 +04:00
|
|
|
cmGeneratedFileStream depFileStream(file);
|
2005-01-19 01:09:05 +03:00
|
|
|
depFileStream
|
2005-07-27 17:49:37 +04:00
|
|
|
<< "# Empty dependencies file\n"
|
2005-01-19 01:09:05 +03:00
|
|
|
<< "# This may be replaced when dependencies are built." << std::endl;
|
|
|
|
}
|
|
|
|
|
2007-12-20 00:36:30 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDepends::WriteDependencies(const char*, const char*,
|
|
|
|
std::ostream&, std::ostream&)
|
|
|
|
{
|
|
|
|
// This should be implemented by the subclass.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-12 21:52:29 +04:00
|
|
|
//----------------------------------------------------------------------------
|
2009-09-23 22:02:05 +04:00
|
|
|
bool cmDepends::CheckDependencies(std::istream& internalDepends,
|
|
|
|
std::map<std::string, DependencyVector>& validDeps)
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
|
|
|
// Parse dependencies from the stream. If any dependee is missing
|
|
|
|
// or newer than the depender then dependencies should be
|
|
|
|
// regenerated.
|
|
|
|
bool okay = true;
|
2009-09-19 21:02:12 +04:00
|
|
|
bool dependerExists = false;
|
2009-09-23 22:02:05 +04:00
|
|
|
DependencyVector* currentDependencies = 0;
|
|
|
|
|
2006-03-15 19:02:08 +03:00
|
|
|
while(internalDepends.getline(this->Dependee, this->MaxPath))
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
2006-05-10 22:15:15 +04:00
|
|
|
if ( this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
|
|
|
|
this->Dependee[0] == '\r' )
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t len = internalDepends.gcount()-1;
|
2006-03-15 19:02:08 +03:00
|
|
|
if ( this->Dependee[len-1] == '\r' )
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
|
|
|
len --;
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Dependee[len] = 0;
|
2005-10-12 21:52:29 +04:00
|
|
|
}
|
2006-03-15 19:02:08 +03:00
|
|
|
if ( this->Dependee[0] != ' ' )
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
memcpy(this->Depender, this->Dependee, len+1);
|
2009-09-19 21:02:12 +04:00
|
|
|
// Calling FileExists() for the depender here saves in many cases 50%
|
|
|
|
// of the calls to FileExists() further down in the loop. E.g. for
|
|
|
|
// kdelibs/khtml this reduces the number of calls from 184k down to 92k,
|
|
|
|
// or the time for cmake -E cmake_depends from 0.3 s down to 0.21 s.
|
|
|
|
dependerExists = cmSystemTools::FileExists(this->Depender);
|
2009-09-23 22:02:05 +04:00
|
|
|
DependencyVector tmp;
|
|
|
|
validDeps[this->Depender] = tmp;
|
|
|
|
currentDependencies = &validDeps[this->Depender];
|
2005-10-12 21:52:29 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
// Parse the dependency line.
|
|
|
|
if(!this->ParseDependency(line.c_str()))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Dependencies must be regenerated if the dependee does not exist
|
|
|
|
// or if the depender exists and is older than the dependee.
|
|
|
|
bool regenerate = false;
|
2006-03-15 19:02:08 +03:00
|
|
|
const char* dependee = this->Dependee+1;
|
|
|
|
const char* depender = this->Depender;
|
2009-09-23 22:02:05 +04:00
|
|
|
if (currentDependencies != 0)
|
|
|
|
{
|
|
|
|
currentDependencies->push_back(dependee);
|
|
|
|
}
|
|
|
|
|
2005-10-12 21:52:29 +04:00
|
|
|
if(!cmSystemTools::FileExists(dependee))
|
|
|
|
{
|
|
|
|
// The dependee does not exist.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
2006-03-15 19:02:08 +03:00
|
|
|
if(this->Verbose)
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" does not exist for depender \""
|
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
2009-09-19 21:02:12 +04:00
|
|
|
else if(dependerExists)
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
|
|
|
// The dependee and depender both exist. Compare file times.
|
|
|
|
int result = 0;
|
2006-03-15 19:02:08 +03:00
|
|
|
if((!this->FileComparison->FileTimeCompare(depender, dependee,
|
2005-10-12 21:52:29 +04:00
|
|
|
&result) || result < 0))
|
|
|
|
{
|
|
|
|
// The depender is older than the dependee.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
2006-03-15 19:02:08 +03:00
|
|
|
if(this->Verbose)
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" is newer than depender \""
|
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(regenerate)
|
|
|
|
{
|
|
|
|
// Dependencies must be regenerated.
|
|
|
|
okay = false;
|
|
|
|
|
2009-09-23 22:02:05 +04:00
|
|
|
// Remove the information of this depender from the map, it needs
|
|
|
|
// to be rescanned
|
|
|
|
if (currentDependencies != 0)
|
|
|
|
{
|
|
|
|
validDeps.erase(this->Depender);
|
|
|
|
currentDependencies = 0;
|
|
|
|
}
|
|
|
|
|
2005-10-12 21:52:29 +04:00
|
|
|
// Remove the depender to be sure it is rebuilt.
|
2009-09-19 21:02:12 +04:00
|
|
|
if (dependerExists)
|
|
|
|
{
|
|
|
|
cmSystemTools::RemoveFile(depender);
|
|
|
|
dependerExists = false;
|
|
|
|
}
|
2005-10-12 21:52:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return okay;
|
|
|
|
}
|
|
|
|
|
2008-05-08 18:09:14 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmDepends::SetIncludePathFromLanguage(const char* lang)
|
|
|
|
{
|
|
|
|
std::string includePathVar = "CMAKE_";
|
|
|
|
includePathVar += lang;
|
|
|
|
includePathVar += "_INCLUDE_PATH";
|
|
|
|
cmMakefile* mf = this->LocalGenerator->GetMakefile();
|
|
|
|
if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
|
|
|
|
{
|
|
|
|
cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
|
|
|
|
}
|
|
|
|
}
|