123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #include "cmDepends.h"
- #include "cmFileTimeComparison.h"
- #include "cmGeneratedFileStream.h"
- #include "cmLocalGenerator.h"
- #include "cmMakefile.h"
- #include "cmSystemTools.h"
- #include "cmWorkingDirectory.h"
- #include "cmsys/FStream.hxx"
- #include <sstream>
- #include <string.h>
- #include <utility>
- cmDepends::cmDepends(cmLocalGenerator* lg, const char* targetDir)
- : CompileDirectory()
- , LocalGenerator(lg)
- , Verbose(false)
- , FileComparison(nullptr)
- , TargetDirectory(targetDir)
- , MaxPath(16384)
- , Dependee(new char[MaxPath])
- , Depender(new char[MaxPath])
- {
- }
- cmDepends::~cmDepends()
- {
- delete[] this->Dependee;
- delete[] this->Depender;
- }
- bool cmDepends::Write(std::ostream& makeDepends, std::ostream& internalDepends)
- {
-
- std::string srcLang = "CMAKE_DEPENDS_CHECK_";
- srcLang += this->Language;
- cmMakefile* mf = this->LocalGenerator->GetMakefile();
- const char* srcStr = mf->GetSafeDefinition(srcLang);
- std::vector<std::string> pairs;
- cmSystemTools::ExpandListArgument(srcStr, pairs);
- std::map<std::string, std::set<std::string>> dependencies;
- for (std::vector<std::string>::iterator si = pairs.begin();
- si != pairs.end();) {
-
- std::string const& src = *si++;
- if (si == pairs.end()) {
- break;
- }
- std::string const& obj = *si++;
- dependencies[obj].insert(src);
- }
- for (auto const& d : dependencies) {
-
- if (!this->WriteDependencies(d.second, d.first, makeDepends,
- internalDepends)) {
- return false;
- }
- }
- return this->Finalize(makeDepends, internalDepends);
- }
- bool cmDepends::Finalize(std::ostream& , std::ostream& )
- {
- return true;
- }
- bool cmDepends::Check(const char* makeFile, const char* internalFile,
- std::map<std::string, DependencyVector>& validDeps)
- {
-
- cmWorkingDirectory workdir(this->CompileDirectory);
-
- bool okay = true;
- cmsys::ifstream fin(internalFile);
- if (!(fin && this->CheckDependencies(fin, internalFile, validDeps))) {
-
- this->Clear(makeFile);
- cmSystemTools::RemoveFile(internalFile);
- okay = false;
- }
- return okay;
- }
- void cmDepends::Clear(const char* file)
- {
-
- if (this->Verbose) {
- std::ostringstream msg;
- msg << "Clearing dependencies in \"" << file << "\"." << std::endl;
- cmSystemTools::Stdout(msg.str().c_str());
- }
-
- cmGeneratedFileStream depFileStream(file);
- depFileStream << "# Empty dependencies file\n"
- << "# This may be replaced when dependencies are built."
- << std::endl;
- }
- bool cmDepends::WriteDependencies(const std::set<std::string>& ,
- const std::string& ,
- std::ostream& ,
- std::ostream& )
- {
-
- return false;
- }
- bool cmDepends::CheckDependencies(
- std::istream& internalDepends, const char* internalDependsFileName,
- std::map<std::string, DependencyVector>& validDeps)
- {
-
-
-
- bool okay = true;
- bool dependerExists = false;
- DependencyVector* currentDependencies = nullptr;
- while (internalDepends.getline(this->Dependee, this->MaxPath)) {
- if (this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
- this->Dependee[0] == '\r') {
- continue;
- }
- size_t len = internalDepends.gcount() - 1;
- if (this->Dependee[len - 1] == '\r') {
- len--;
- this->Dependee[len] = 0;
- }
- if (this->Dependee[0] != ' ') {
- memcpy(this->Depender, this->Dependee, len + 1);
-
-
-
-
- dependerExists = cmSystemTools::FileExists(this->Depender);
-
-
-
-
- currentDependencies = &validDeps[this->Depender];
- continue;
- }
-
-
-
-
-
-
- bool regenerate = false;
- const char* dependee = this->Dependee + 1;
- const char* depender = this->Depender;
- if (currentDependencies != nullptr) {
- currentDependencies->push_back(dependee);
- }
- if (!cmSystemTools::FileExists(dependee)) {
-
- regenerate = true;
-
- if (this->Verbose) {
- std::ostringstream msg;
- msg << "Dependee \"" << dependee << "\" does not exist for depender \""
- << depender << "\"." << std::endl;
- cmSystemTools::Stdout(msg.str().c_str());
- }
- } else {
- if (dependerExists) {
-
- int result = 0;
- if ((!this->FileComparison->FileTimeCompare(depender, dependee,
- &result) ||
- result < 0)) {
-
- regenerate = true;
-
- if (this->Verbose) {
- std::ostringstream msg;
- msg << "Dependee \"" << dependee << "\" is newer than depender \""
- << depender << "\"." << std::endl;
- cmSystemTools::Stdout(msg.str().c_str());
- }
- }
- } else {
-
-
- int result = 0;
- if ((!this->FileComparison->FileTimeCompare(internalDependsFileName,
- dependee, &result) ||
- result < 0)) {
-
- regenerate = true;
-
- if (this->Verbose) {
- std::ostringstream msg;
- msg << "Dependee \"" << dependee
- << "\" is newer than depends file \""
- << internalDependsFileName << "\"." << std::endl;
- cmSystemTools::Stdout(msg.str().c_str());
- }
- }
- }
- }
- if (regenerate) {
-
- okay = false;
-
-
- if (currentDependencies != nullptr) {
- validDeps.erase(this->Depender);
- currentDependencies = nullptr;
- }
-
- if (dependerExists) {
- cmSystemTools::RemoveFile(depender);
- dependerExists = false;
- }
- }
- }
- return okay;
- }
- void cmDepends::SetIncludePathFromLanguage(const std::string& lang)
- {
-
- const char* includePath = nullptr;
- std::string includePathVar = "CMAKE_";
- includePathVar += lang;
- includePathVar += "_TARGET_INCLUDE_PATH";
- cmMakefile* mf = this->LocalGenerator->GetMakefile();
- includePath = mf->GetDefinition(includePathVar);
- if (includePath) {
- cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
- } else {
-
- includePathVar = "CMAKE_";
- includePathVar += lang;
- includePathVar += "_INCLUDE_PATH";
- includePath = mf->GetDefinition(includePathVar);
- if (includePath) {
- cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
- }
- }
- }
|