2005-01-18 22:09:05 +00:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#include "cmDepends.h"
|
|
|
|
|
2007-12-23 03:41:42 +00:00
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmMakefile.h"
|
2005-01-18 22:09:05 +00:00
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
#include "cmSystemTools.h"
|
2005-10-12 17:52:29 +00:00
|
|
|
#include "cmFileTimeComparison.h"
|
2006-01-23 17:31:08 +00:00
|
|
|
#include <string.h>
|
2005-03-08 14:24:24 +00:00
|
|
|
|
2005-01-18 22:09:05 +00:00
|
|
|
//----------------------------------------------------------------------------
|
2008-05-08 14:09:14 +00:00
|
|
|
cmDepends::cmDepends(cmLocalGenerator* lg, const char* targetDir):
|
2006-05-25 13:47:30 +00:00
|
|
|
CompileDirectory(),
|
2008-05-08 14:09:14 +00:00
|
|
|
LocalGenerator(lg),
|
2006-05-25 13:47:30 +00:00
|
|
|
Verbose(false),
|
|
|
|
FileComparison(0),
|
2008-05-08 14:09:14 +00:00
|
|
|
TargetDirectory(targetDir),
|
2006-03-15 16:02:08 +00:00
|
|
|
MaxPath(cmSystemTools::GetMaximumFilePathLength()),
|
|
|
|
Dependee(new char[MaxPath]),
|
|
|
|
Depender(new char[MaxPath])
|
2005-01-18 22:09:05 +00:00
|
|
|
{
|
2005-05-11 17:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmDepends::~cmDepends()
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
delete [] this->Dependee;
|
|
|
|
delete [] this->Depender;
|
2005-05-11 17:16:45 +00:00
|
|
|
}
|
|
|
|
|
2005-01-18 22:09:05 +00:00
|
|
|
//----------------------------------------------------------------------------
|
2007-12-23 03:41:42 +00:00
|
|
|
bool cmDepends::Write(std::ostream &makeDepends,
|
|
|
|
std::ostream &internalDepends)
|
2005-01-18 22:09:05 +00:00
|
|
|
{
|
2007-12-23 03:41:42 +00: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 16:49:59 +00:00
|
|
|
return this->Finalize(makeDepends, internalDepends);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDepends::Finalize(std::ostream&,
|
|
|
|
std::ostream&)
|
|
|
|
{
|
2007-12-23 03:41:42 +00:00
|
|
|
return true;
|
2005-01-18 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2007-12-19 21:36:30 +00:00
|
|
|
bool cmDepends::Check(const char *makeFile, const char *internalFile)
|
2005-01-18 22:09:05 +00:00
|
|
|
{
|
2005-03-08 14:24:24 +00:00
|
|
|
// Dependency checks must be done in proper working directory.
|
|
|
|
std::string oldcwd = ".";
|
2006-03-15 16:02:08 +00:00
|
|
|
if(this->CompileDirectory != ".")
|
2005-03-08 14:24:24 +00:00
|
|
|
{
|
2005-04-12 17:26:35 +00: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 16:02:08 +00:00
|
|
|
cmSystemTools::ChangeDirectory(this->CompileDirectory.c_str());
|
2005-03-08 14:24:24 +00:00
|
|
|
}
|
|
|
|
|
2005-01-18 22:09:05 +00:00
|
|
|
// Check whether dependencies must be regenerated.
|
2007-12-19 21:36:30 +00:00
|
|
|
bool okay = true;
|
2005-10-12 17:52:29 +00:00
|
|
|
std::ifstream fin(internalFile);
|
2005-01-18 22:09:05 +00:00
|
|
|
if(!(fin && this->CheckDependencies(fin)))
|
|
|
|
{
|
|
|
|
// Clear all dependencies so they will be regenerated.
|
2005-10-12 17:52:29 +00:00
|
|
|
this->Clear(makeFile);
|
2007-12-19 21:36:30 +00:00
|
|
|
cmSystemTools::RemoveFile(internalFile);
|
|
|
|
okay = false;
|
2005-01-18 22:09:05 +00:00
|
|
|
}
|
2005-03-08 14:24:24 +00:00
|
|
|
|
|
|
|
// Restore working directory.
|
|
|
|
if(oldcwd != ".")
|
|
|
|
{
|
|
|
|
cmSystemTools::ChangeDirectory(oldcwd.c_str());
|
|
|
|
}
|
2007-12-19 21:36:30 +00:00
|
|
|
|
|
|
|
return okay;
|
2005-01-18 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-07-27 13:49:37 +00:00
|
|
|
void cmDepends::Clear(const char *file)
|
2005-01-18 22:09:05 +00:00
|
|
|
{
|
2005-05-06 13:58:58 +00:00
|
|
|
// Print verbose output.
|
2006-03-15 16:02:08 +00:00
|
|
|
if(this->Verbose)
|
2005-05-06 13:58:58 +00:00
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
2005-07-27 13:49:37 +00:00
|
|
|
msg << "Clearing dependencies in \"" << file << "\"." << std::endl;
|
2005-05-06 13:58:58 +00:00
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
|
2005-01-18 22:09:05 +00:00
|
|
|
// Write an empty dependency file.
|
2005-07-27 13:49:37 +00:00
|
|
|
cmGeneratedFileStream depFileStream(file);
|
2005-01-18 22:09:05 +00:00
|
|
|
depFileStream
|
2005-07-27 13:49:37 +00:00
|
|
|
<< "# Empty dependencies file\n"
|
2005-01-18 22:09:05 +00:00
|
|
|
<< "# This may be replaced when dependencies are built." << std::endl;
|
|
|
|
}
|
|
|
|
|
2007-12-19 21:36:30 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDepends::WriteDependencies(const char*, const char*,
|
|
|
|
std::ostream&, std::ostream&)
|
|
|
|
{
|
|
|
|
// This should be implemented by the subclass.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-12 17:52:29 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmDepends::CheckDependencies(std::istream& internalDepends)
|
|
|
|
{
|
|
|
|
// Parse dependencies from the stream. If any dependee is missing
|
|
|
|
// or newer than the depender then dependencies should be
|
|
|
|
// regenerated.
|
|
|
|
bool okay = true;
|
2006-03-15 16:02:08 +00:00
|
|
|
while(internalDepends.getline(this->Dependee, this->MaxPath))
|
2005-10-12 17:52:29 +00:00
|
|
|
{
|
2006-05-10 18:15:15 +00:00
|
|
|
if ( this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
|
|
|
|
this->Dependee[0] == '\r' )
|
2005-10-12 17:52:29 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t len = internalDepends.gcount()-1;
|
2006-03-15 16:02:08 +00:00
|
|
|
if ( this->Dependee[len-1] == '\r' )
|
2005-10-12 17:52:29 +00:00
|
|
|
{
|
|
|
|
len --;
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Dependee[len] = 0;
|
2005-10-12 17:52:29 +00:00
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
if ( this->Dependee[0] != ' ' )
|
2005-10-12 17:52:29 +00:00
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
memcpy(this->Depender, this->Dependee, len+1);
|
2005-10-12 17:52:29 +00: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 16:02:08 +00:00
|
|
|
const char* dependee = this->Dependee+1;
|
|
|
|
const char* depender = this->Depender;
|
2005-10-12 17:52:29 +00:00
|
|
|
if(!cmSystemTools::FileExists(dependee))
|
|
|
|
{
|
|
|
|
// The dependee does not exist.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
2006-03-15 16:02:08 +00:00
|
|
|
if(this->Verbose)
|
2005-10-12 17:52:29 +00:00
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
|
|
|
msg << "Dependee \"" << dependee
|
|
|
|
<< "\" does not exist for depender \""
|
|
|
|
<< depender << "\"." << std::endl;
|
|
|
|
cmSystemTools::Stdout(msg.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(cmSystemTools::FileExists(depender))
|
|
|
|
{
|
|
|
|
// The dependee and depender both exist. Compare file times.
|
|
|
|
int result = 0;
|
2006-03-15 16:02:08 +00:00
|
|
|
if((!this->FileComparison->FileTimeCompare(depender, dependee,
|
2005-10-12 17:52:29 +00:00
|
|
|
&result) || result < 0))
|
|
|
|
{
|
|
|
|
// The depender is older than the dependee.
|
|
|
|
regenerate = true;
|
|
|
|
|
|
|
|
// Print verbose output.
|
2006-03-15 16:02:08 +00:00
|
|
|
if(this->Verbose)
|
2005-10-12 17:52:29 +00: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;
|
|
|
|
|
|
|
|
// Remove the depender to be sure it is rebuilt.
|
|
|
|
cmSystemTools::RemoveFile(depender);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return okay;
|
|
|
|
}
|
|
|
|
|
2008-05-08 14:09:14 +00: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);
|
|
|
|
}
|
|
|
|
}
|