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"
|
|
|
|
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmFileTimeComparison.h"
|
|
|
|
#include "cmGeneratedFileStream.h"
|
2007-12-23 06:41:42 +03:00
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmMakefile.h"
|
2005-01-19 01:09:05 +03:00
|
|
|
#include "cmSystemTools.h"
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <string.h>
|
2005-03-08 17:24:24 +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),
|
2010-12-17 01:41:27 +03:00
|
|
|
MaxPath(16384),
|
2006-03-15 19:02:08 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
2014-03-11 03:04:11 +04:00
|
|
|
const char* srcStr = mf->GetSafeDefinition(srcLang);
|
2007-12-23 06:41:42 +03:00
|
|
|
std::vector<std::string> pairs;
|
|
|
|
cmSystemTools::ExpandListArgument(srcStr, pairs);
|
|
|
|
|
2012-09-30 19:53:01 +04:00
|
|
|
std::map<std::string, std::set<std::string> > dependencies;
|
2007-12-23 06:41:42 +03:00
|
|
|
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; }
|
2014-07-22 20:50:37 +04:00
|
|
|
std::string const& obj = *si++;
|
2012-09-30 19:53:01 +04:00
|
|
|
dependencies[obj].insert(src);
|
|
|
|
}
|
|
|
|
for(std::map<std::string, std::set<std::string> >::const_iterator
|
|
|
|
it = dependencies.begin(); it != dependencies.end(); ++it)
|
|
|
|
{
|
2007-12-23 06:41:42 +03:00
|
|
|
|
|
|
|
// Write the dependencies for this pair.
|
2012-09-30 19:53:01 +04:00
|
|
|
if(!this->WriteDependencies(it->second, it->first,
|
2007-12-23 06:41:42 +03:00
|
|
|
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);
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::ChangeDirectory(this->CompileDirectory);
|
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;
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ifstream fin(internalFile);
|
2012-08-17 00:46:33 +04:00
|
|
|
if(!(fin && this->CheckDependencies(fin, internalFile, 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 != ".")
|
|
|
|
{
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::ChangeDirectory(oldcwd);
|
2005-03-08 17:24:24 +03:00
|
|
|
}
|
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
|
|
|
{
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream 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;
|
|
|
|
}
|
|
|
|
|
2012-09-30 19:53:01 +04:00
|
|
|
bool cmDepends::WriteDependencies(
|
|
|
|
const std::set<std::string>&, const std::string&,
|
|
|
|
std::ostream&, std::ostream&)
|
2007-12-20 00:36:30 +03:00
|
|
|
{
|
|
|
|
// This should be implemented by the subclass.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-23 22:02:05 +04:00
|
|
|
bool cmDepends::CheckDependencies(std::istream& internalDepends,
|
2012-08-17 00:46:33 +04:00
|
|
|
const char* internalDependsFileName,
|
2009-09-23 22:02:05 +04:00
|
|
|
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
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
if ( this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
|
2006-05-10 22:15:15 +04:00
|
|
|
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-12-14 20:06:51 +03:00
|
|
|
// If we erase validDeps[this->Depender] by overwriting it with an empty
|
|
|
|
// vector, we lose dependencies for dependers that have multiple
|
|
|
|
// entries. No need to initialize the entry, std::map will do so on first
|
|
|
|
// access.
|
2009-09-23 22:02:05 +04:00
|
|
|
currentDependencies = &validDeps[this->Depender];
|
2005-10-12 21:52:29 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
// Parse the dependency line.
|
|
|
|
if(!this->ParseDependency(line.c_str()))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2012-08-17 00:46:33 +04:00
|
|
|
// Dependencies must be regenerated
|
|
|
|
// * if the dependee does not exist
|
|
|
|
// * if the depender exists and is older than the dependee.
|
|
|
|
// * if the depender does not exist, but the dependee is newer than the
|
|
|
|
// depends file
|
2005-10-12 21:52:29 +04:00
|
|
|
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
|
|
|
{
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream msg;
|
2005-10-12 21:52:29 +04:00
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" does not exist for depender \""
|
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
2012-08-17 00:46:33 +04:00
|
|
|
else
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
2012-08-17 00:46:33 +04:00
|
|
|
if(dependerExists)
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
2012-08-17 00:46:33 +04:00
|
|
|
// The dependee and depender both exist. Compare file times.
|
|
|
|
int result = 0;
|
|
|
|
if((!this->FileComparison->FileTimeCompare(depender, dependee,
|
|
|
|
&result) || result < 0))
|
|
|
|
{
|
|
|
|
// The depender is older than the dependee.
|
|
|
|
regenerate = true;
|
2005-10-12 21:52:29 +04:00
|
|
|
|
2012-08-17 00:46:33 +04:00
|
|
|
// Print verbose output.
|
|
|
|
if(this->Verbose)
|
|
|
|
{
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream msg;
|
2012-08-17 00:46:33 +04:00
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" is newer than depender \""
|
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The dependee exists, but the depender doesn't. Regenerate if the
|
|
|
|
// internalDepends file is older than the dependee.
|
|
|
|
int result = 0;
|
2012-08-20 23:23:20 +04:00
|
|
|
if((!this->FileComparison->FileTimeCompare(internalDependsFileName,
|
|
|
|
dependee, &result) || result < 0))
|
2005-10-12 21:52:29 +04:00
|
|
|
{
|
2012-08-17 00:46:33 +04:00
|
|
|
// The depends-file is older than the dependee.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
|
|
|
if(this->Verbose)
|
|
|
|
{
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream msg;
|
2012-08-17 00:46:33 +04:00
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" is newer than depends file \""
|
|
|
|
<< internalDependsFileName << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
2005-10-12 21:52:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-04 06:20:56 +04:00
|
|
|
void cmDepends::SetIncludePathFromLanguage(const std::string& lang)
|
2008-05-08 18:09:14 +04:00
|
|
|
{
|
2011-11-05 19:17:49 +04:00
|
|
|
// Look for the new per "TARGET_" variant first:
|
2012-02-23 01:38:17 +04:00
|
|
|
const char * includePath = 0;
|
2008-05-08 18:09:14 +04:00
|
|
|
std::string includePathVar = "CMAKE_";
|
|
|
|
includePathVar += lang;
|
2011-11-05 19:17:49 +04:00
|
|
|
includePathVar += "_TARGET_INCLUDE_PATH";
|
2008-05-08 18:09:14 +04:00
|
|
|
cmMakefile* mf = this->LocalGenerator->GetMakefile();
|
2014-03-11 03:04:11 +04:00
|
|
|
includePath = mf->GetDefinition(includePathVar);
|
2012-02-23 02:12:11 +04:00
|
|
|
if(includePath)
|
2008-05-08 18:09:14 +04:00
|
|
|
{
|
|
|
|
cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
|
|
|
|
}
|
2011-11-05 19:17:49 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Fallback to the old directory level variable if no per-target var:
|
|
|
|
includePathVar = "CMAKE_";
|
|
|
|
includePathVar += lang;
|
|
|
|
includePathVar += "_INCLUDE_PATH";
|
2014-03-11 03:04:11 +04:00
|
|
|
includePath = mf->GetDefinition(includePathVar);
|
2012-02-23 02:12:11 +04:00
|
|
|
if(includePath)
|
2011-11-05 19:17:49 +04:00
|
|
|
{
|
|
|
|
cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
|
|
|
|
}
|
|
|
|
}
|
2008-05-08 18:09:14 +04:00
|
|
|
}
|