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
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmDepends::cmDepends(cmLocalGenerator* lg, const char* targetDir)
|
|
|
|
: CompileDirectory()
|
|
|
|
, LocalGenerator(lg)
|
|
|
|
, Verbose(false)
|
2016-06-27 23:44:16 +03:00
|
|
|
, FileComparison(CM_NULLPTR)
|
2016-05-16 17:34:04 +03:00
|
|
|
, TargetDirectory(targetDir)
|
|
|
|
, MaxPath(16384)
|
|
|
|
, 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()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
delete[] this->Dependee;
|
|
|
|
delete[] this->Depender;
|
2005-05-11 21:16:45 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +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;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::iterator si = pairs.begin();
|
|
|
|
si != pairs.end();) {
|
2007-12-23 06:41:42 +03:00
|
|
|
// Get the source and object file.
|
|
|
|
std::string const& src = *si++;
|
2016-05-16 17:34:04 +03:00
|
|
|
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);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
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.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->WriteDependencies(it->second, it->first, makeDepends,
|
|
|
|
internalDepends)) {
|
2007-12-23 06:41:42 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-12-23 06:41:42 +03:00
|
|
|
|
2007-12-28 19:49:59 +03:00
|
|
|
return this->Finalize(makeDepends, internalDepends);
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
bool cmDepends::Finalize(std::ostream& /*unused*/, std::ostream& /*unused*/)
|
2007-12-28 19:49:59 +03:00
|
|
|
{
|
2007-12-23 06:41:42 +03:00
|
|
|
return true;
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmDepends::Check(const char* makeFile, const char* internalFile,
|
2009-09-23 22:02:05 +04:00
|
|
|
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 = ".";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->CompileDirectory != ".") {
|
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);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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);
|
2016-05-16 17:34:04 +03: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;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-03-08 17:24:24 +03:00
|
|
|
|
|
|
|
// Restore working directory.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (oldcwd != ".") {
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::ChangeDirectory(oldcwd);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-12-20 00:36:30 +03:00
|
|
|
|
|
|
|
return okay;
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03: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.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Verbose) {
|
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());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-05-06 17:58:58 +04:00
|
|
|
|
2005-01-19 01:09:05 +03:00
|
|
|
// Write an empty dependency file.
|
2005-07-27 17:49:37 +04:00
|
|
|
cmGeneratedFileStream depFileStream(file);
|
2016-05-16 17:34:04 +03:00
|
|
|
depFileStream << "# Empty dependencies file\n"
|
|
|
|
<< "# This may be replaced when dependencies are built."
|
|
|
|
<< std::endl;
|
2005-01-19 01:09:05 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
bool cmDepends::WriteDependencies(const std::set<std::string>& /*unused*/,
|
|
|
|
const std::string& /*unused*/,
|
|
|
|
std::ostream& /*unused*/,
|
|
|
|
std::ostream& /*unused*/)
|
2007-12-20 00:36:30 +03:00
|
|
|
{
|
|
|
|
// This should be implemented by the subclass.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmDepends::CheckDependencies(
|
|
|
|
std::istream& internalDepends, const char* internalDependsFileName,
|
|
|
|
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;
|
2016-06-27 23:44:16 +03:00
|
|
|
DependencyVector* currentDependencies = CM_NULLPTR;
|
2009-09-23 22:02:05 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
while (internalDepends.getline(this->Dependee, this->MaxPath)) {
|
|
|
|
if (this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
|
|
|
|
this->Dependee[0] == '\r') {
|
2005-10-12 21:52:29 +04:00
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
size_t len = internalDepends.gcount() - 1;
|
|
|
|
if (this->Dependee[len - 1] == '\r') {
|
|
|
|
len--;
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Dependee[len] = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->Dependee[0] != ' ') {
|
|
|
|
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;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-12 21:52:29 +04:00
|
|
|
/*
|
|
|
|
// 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;
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* dependee = this->Dependee + 1;
|
2006-03-15 19:02:08 +03:00
|
|
|
const char* depender = this->Depender;
|
2016-06-27 23:44:16 +03:00
|
|
|
if (currentDependencies != CM_NULLPTR) {
|
2009-09-23 22:02:05 +04:00
|
|
|
currentDependencies->push_back(dependee);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-09-23 22:02:05 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::FileExists(dependee)) {
|
2005-10-12 21:52:29 +04:00
|
|
|
// The dependee does not exist.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Verbose) {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream msg;
|
2016-05-16 17:34:04 +03:00
|
|
|
msg << "Dependee \"" << dependee << "\" does not exist for depender \""
|
2005-10-12 21:52:29 +04:00
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
if (dependerExists) {
|
2012-08-17 00:46:33 +04:00
|
|
|
// The dependee and depender both exist. Compare file times.
|
|
|
|
int result = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((!this->FileComparison->FileTimeCompare(depender, dependee,
|
|
|
|
&result) ||
|
|
|
|
result < 0)) {
|
2012-08-17 00:46:33 +04:00
|
|
|
// 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.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Verbose) {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream msg;
|
2016-05-16 17:34:04 +03:00
|
|
|
msg << "Dependee \"" << dependee << "\" is newer than depender \""
|
2012-08-17 00:46:33 +04:00
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2012-08-17 00:46:33 +04:00
|
|
|
// The dependee exists, but the depender doesn't. Regenerate if the
|
|
|
|
// internalDepends file is older than the dependee.
|
|
|
|
int result = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((!this->FileComparison->FileTimeCompare(internalDependsFileName,
|
|
|
|
dependee, &result) ||
|
|
|
|
result < 0)) {
|
2012-08-17 00:46:33 +04:00
|
|
|
// The depends-file is older than the dependee.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
2016-05-16 17:34:04 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (regenerate) {
|
2005-10-12 21:52:29 +04:00
|
|
|
// 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
|
2016-06-27 23:44:16 +03:00
|
|
|
if (currentDependencies != CM_NULLPTR) {
|
2009-09-23 22:02:05 +04:00
|
|
|
validDeps.erase(this->Depender);
|
2016-06-27 23:44:16 +03:00
|
|
|
currentDependencies = CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-09-23 22:02:05 +04:00
|
|
|
|
2005-10-12 21:52:29 +04:00
|
|
|
// Remove the depender to be sure it is rebuilt.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (dependerExists) {
|
2009-09-19 21:02:12 +04:00
|
|
|
cmSystemTools::RemoveFile(depender);
|
|
|
|
dependerExists = false;
|
2005-10-12 21:52:29 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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:
|
2016-06-27 23:44:16 +03:00
|
|
|
const char* includePath = CM_NULLPTR;
|
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);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (includePath) {
|
2008-05-08 18:09:14 +04:00
|
|
|
cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2011-11-05 19:17:49 +04:00
|
|
|
// 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);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (includePath) {
|
2011-11-05 19:17:49 +04:00
|
|
|
cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-05-08 18:09:14 +04:00
|
|
|
}
|