123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- #include "cmDependsFortran.h"
- #include "cmsys/FStream.hxx"
- #include <assert.h>
- #include <iostream>
- #include <map>
- #include <stdlib.h>
- #include <string.h>
- #include <utility>
- #include "cmFortranParser.h"
- #include "cmGeneratedFileStream.h"
- #include "cmLocalGenerator.h"
- #include "cmMakefile.h"
- #include "cmOutputConverter.h"
- #include "cmStateDirectory.h"
- #include "cmStateSnapshot.h"
- #include "cmSystemTools.h"
- class cmDependsFortranInternals
- {
- public:
-
- std::set<std::string> TargetProvides;
-
- typedef std::map<std::string, std::string> TargetRequiresMap;
- TargetRequiresMap TargetRequires;
-
- typedef std::map<std::string, cmFortranSourceInfo> ObjectInfoMap;
- ObjectInfoMap ObjectInfo;
- cmFortranSourceInfo& CreateObjectInfo(const char* obj, const char* src)
- {
- std::map<std::string, cmFortranSourceInfo>::iterator i =
- this->ObjectInfo.find(obj);
- if (i == this->ObjectInfo.end()) {
- std::map<std::string, cmFortranSourceInfo>::value_type entry(
- obj, cmFortranSourceInfo());
- i = this->ObjectInfo.insert(entry).first;
- i->second.Source = src;
- }
- return i->second;
- }
- };
- cmDependsFortran::cmDependsFortran()
- : Internal(nullptr)
- {
- }
- cmDependsFortran::cmDependsFortran(cmLocalGenerator* lg)
- : cmDepends(lg)
- , Internal(new cmDependsFortranInternals)
- {
-
- this->SetIncludePathFromLanguage("Fortran");
-
- std::vector<std::string> definitions;
- cmMakefile* mf = this->LocalGenerator->GetMakefile();
- if (const char* c_defines =
- mf->GetDefinition("CMAKE_TARGET_DEFINITIONS_Fortran")) {
- cmSystemTools::ExpandListArgument(c_defines, definitions);
- }
-
-
- for (std::string def : definitions) {
- std::string::size_type assignment = def.find('=');
- if (assignment != std::string::npos) {
- def = def.substr(0, assignment);
- }
- this->PPDefinitions.insert(def);
- }
- }
- cmDependsFortran::~cmDependsFortran()
- {
- delete this->Internal;
- }
- bool cmDependsFortran::WriteDependencies(const std::set<std::string>& sources,
- const std::string& obj,
- std::ostream& ,
- std::ostream& )
- {
-
- if (sources.empty() || sources.begin()->empty()) {
- cmSystemTools::Error("Cannot scan dependencies without a source file.");
- return false;
- }
- if (obj.empty()) {
- cmSystemTools::Error("Cannot scan dependencies without an object file.");
- return false;
- }
- bool okay = true;
- for (std::string const& src : sources) {
-
- cmFortranSourceInfo& info =
- this->Internal->CreateObjectInfo(obj.c_str(), src.c_str());
-
-
- cmFortranParser parser(this->IncludePath, this->PPDefinitions, info);
-
- cmFortranParser_FilePush(&parser, src.c_str());
-
- if (cmFortran_yyparse(parser.Scanner) != 0) {
-
- okay = false;
-
- std::cerr <<
- "warning: failed to parse dependencies from Fortran source "
- "'" << src << "': " << parser.Error << std::endl
- ;
-
- }
- }
- return okay;
- }
- bool cmDependsFortran::Finalize(std::ostream& makeDepends,
- std::ostream& internalDepends)
- {
-
- this->LocateModules();
-
- const char* stamp_dir = this->TargetDirectory.c_str();
-
- cmMakefile* mf = this->LocalGenerator->GetMakefile();
- std::string mod_dir =
- mf->GetSafeDefinition("CMAKE_Fortran_TARGET_MODULE_DIR");
- if (mod_dir.empty()) {
- mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
- }
-
- typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
- ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
- for (auto const& i : objInfo) {
- if (!this->WriteDependenciesReal(i.first.c_str(), i.second, mod_dir,
- stamp_dir, makeDepends,
- internalDepends)) {
- return false;
- }
- }
-
- std::string fiName = this->TargetDirectory;
- fiName += "/fortran.internal";
- cmGeneratedFileStream fiStream(fiName.c_str());
- fiStream << "# The fortran modules provided by this target.\n";
- fiStream << "provides\n";
- std::set<std::string> const& provides = this->Internal->TargetProvides;
- for (std::string const& i : provides) {
- fiStream << " " << i << "\n";
- }
-
- if (!provides.empty()) {
- std::string fcName = this->TargetDirectory;
- fcName += "/cmake_clean_Fortran.cmake";
- cmGeneratedFileStream fcStream(fcName.c_str());
- fcStream << "# Remove fortran modules provided by this target.\n";
- fcStream << "FILE(REMOVE";
- std::string currentBinDir =
- this->LocalGenerator->GetCurrentBinaryDirectory();
- for (std::string const& i : provides) {
- std::string mod_upper = mod_dir;
- mod_upper += "/";
- mod_upper += cmSystemTools::UpperCase(i);
- mod_upper += ".mod";
- std::string mod_lower = mod_dir;
- mod_lower += "/";
- mod_lower += i;
- mod_lower += ".mod";
- std::string stamp = stamp_dir;
- stamp += "/";
- stamp += i;
- stamp += ".mod.stamp";
- fcStream << "\n";
- fcStream << " \""
- << this->MaybeConvertToRelativePath(currentBinDir, mod_lower)
- << "\"\n";
- fcStream << " \""
- << this->MaybeConvertToRelativePath(currentBinDir, mod_upper)
- << "\"\n";
- fcStream << " \""
- << this->MaybeConvertToRelativePath(currentBinDir, stamp)
- << "\"\n";
- }
- fcStream << " )\n";
- }
- return true;
- }
- void cmDependsFortran::LocateModules()
- {
-
- typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
- ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
- for (auto const& infoI : objInfo) {
- cmFortranSourceInfo const& info = infoI.second;
-
- this->Internal->TargetProvides.insert(info.Provides.begin(),
- info.Provides.end());
- for (std::string const& r : info.Requires) {
- this->Internal->TargetRequires[r].clear();
- }
- }
-
- if (this->Internal->TargetRequires.empty()) {
- return;
- }
-
- this->MatchLocalModules();
-
- cmMakefile* mf = this->LocalGenerator->GetMakefile();
- std::vector<std::string> infoFiles;
- if (const char* infoFilesValue =
- mf->GetDefinition("CMAKE_TARGET_LINKED_INFO_FILES")) {
- cmSystemTools::ExpandListArgument(infoFilesValue, infoFiles);
- }
- for (std::string const& i : infoFiles) {
- std::string targetDir = cmSystemTools::GetFilenamePath(i);
- std::string fname = targetDir + "/fortran.internal";
- cmsys::ifstream fin(fname.c_str());
- if (fin) {
- this->MatchRemoteModules(fin, targetDir.c_str());
- }
- }
- }
- void cmDependsFortran::MatchLocalModules()
- {
- const char* stampDir = this->TargetDirectory.c_str();
- std::set<std::string> const& provides = this->Internal->TargetProvides;
- for (std::string const& i : provides) {
- this->ConsiderModule(i.c_str(), stampDir);
- }
- }
- void cmDependsFortran::MatchRemoteModules(std::istream& fin,
- const char* stampDir)
- {
- std::string line;
- bool doing_provides = false;
- while (cmSystemTools::GetLineFromStream(fin, line)) {
-
- if (line.empty() || line[0] == '#' || line[0] == '\r') {
- continue;
- }
- if (line[0] == ' ') {
- if (doing_provides) {
- this->ConsiderModule(line.c_str() + 1, stampDir);
- }
- } else if (line == "provides") {
- doing_provides = true;
- } else {
- doing_provides = false;
- }
- }
- }
- void cmDependsFortran::ConsiderModule(const char* name, const char* stampDir)
- {
-
- typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
- TargetRequiresMap::iterator required =
- this->Internal->TargetRequires.find(name);
- if (required != this->Internal->TargetRequires.end() &&
- required->second.empty()) {
-
- std::string stampFile = stampDir;
- stampFile += "/";
- stampFile += name;
- stampFile += ".mod.stamp";
- required->second = stampFile;
- }
- }
- bool cmDependsFortran::WriteDependenciesReal(const char* obj,
- cmFortranSourceInfo const& info,
- std::string const& mod_dir,
- const char* stamp_dir,
- std::ostream& makeDepends,
- std::ostream& internalDepends)
- {
- typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
-
- const char* src = info.Source.c_str();
-
- std::string binDir = this->LocalGenerator->GetBinaryDirectory();
- std::string obj_i = this->MaybeConvertToRelativePath(binDir, obj);
- std::string obj_m = cmSystemTools::ConvertToOutputPath(obj_i);
- internalDepends << obj_i << std::endl;
- internalDepends << " " << src << std::endl;
- for (std::string const& i : info.Includes) {
- makeDepends << obj_m << ": "
- << cmSystemTools::ConvertToOutputPath(
- this->MaybeConvertToRelativePath(binDir, i))
- << std::endl;
- internalDepends << " " << i << std::endl;
- }
- makeDepends << std::endl;
-
- for (std::string const& i : info.Requires) {
-
- if (info.Provides.find(i) != info.Provides.cend()) {
- continue;
- }
-
-
- TargetRequiresMap::const_iterator required =
- this->Internal->TargetRequires.find(i);
- if (required == this->Internal->TargetRequires.end()) {
- abort();
- }
- if (!required->second.empty()) {
-
- std::string stampFile = cmSystemTools::ConvertToOutputPath(
- this->MaybeConvertToRelativePath(binDir, required->second));
- makeDepends << obj_m << ": " << stampFile << "\n";
- } else {
-
-
- std::string module;
- if (this->FindModule(i, module)) {
- module = cmSystemTools::ConvertToOutputPath(
- this->MaybeConvertToRelativePath(binDir, module));
- makeDepends << obj_m << ": " << module << "\n";
- }
- }
- }
-
- if (!info.Provides.empty()) {
-
-
- for (std::string const& i : info.Provides) {
-
- this->Internal->TargetProvides.insert(i);
-
-
-
- std::string m = cmSystemTools::LowerCase(i);
- std::string modFile = mod_dir;
- modFile += "/";
- modFile += i;
- modFile = this->LocalGenerator->ConvertToOutputFormat(
- this->MaybeConvertToRelativePath(binDir, modFile),
- cmOutputConverter::SHELL);
- std::string stampFile = stamp_dir;
- stampFile += "/";
- stampFile += m;
- stampFile += ".mod.stamp";
- stampFile = this->MaybeConvertToRelativePath(binDir, stampFile);
- std::string const stampFileForShell =
- this->LocalGenerator->ConvertToOutputFormat(stampFile,
- cmOutputConverter::SHELL);
- std::string const stampFileForMake =
- cmSystemTools::ConvertToOutputPath(stampFile);
- makeDepends << obj_m << ".provides.build"
- << ": " << stampFileForMake << "\n";
-
-
-
-
-
-
-
- makeDepends << stampFileForMake << ": " << obj_m << "\n";
- makeDepends << "\t$(CMAKE_COMMAND) -E cmake_copy_f90_mod " << modFile
- << " " << stampFileForShell;
- cmMakefile* mf = this->LocalGenerator->GetMakefile();
- const char* cid = mf->GetDefinition("CMAKE_Fortran_COMPILER_ID");
- if (cid && *cid) {
- makeDepends << " " << cid;
- }
- makeDepends << "\n";
- }
- makeDepends << obj_m << ".provides.build:\n";
-
- makeDepends << "\t$(CMAKE_COMMAND) -E touch " << obj_m
- << ".provides.build\n";
-
-
- std::string driver = this->TargetDirectory;
- driver += "/build";
- driver = cmSystemTools::ConvertToOutputPath(
- this->MaybeConvertToRelativePath(binDir, driver));
- makeDepends << driver << ": " << obj_m << ".provides.build\n";
- }
- return true;
- }
- bool cmDependsFortran::FindModule(std::string const& name, std::string& module)
- {
-
- std::string mod_upper = cmSystemTools::UpperCase(name);
- std::string mod_lower = name;
- mod_upper += ".mod";
- mod_lower += ".mod";
-
- std::string fullName;
- for (std::string const& ip : this->IncludePath) {
-
- fullName = ip;
- fullName += "/";
- fullName += mod_lower;
- if (cmSystemTools::FileExists(fullName, true)) {
- module = fullName;
- return true;
- }
-
- fullName = ip;
- fullName += "/";
- fullName += mod_upper;
- if (cmSystemTools::FileExists(fullName, true)) {
- module = fullName;
- return true;
- }
- }
- return false;
- }
- bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
- {
-
-
-
-
-
-
-
-
-
- std::string mod = args[2];
- std::string stamp = args[3];
- std::string compilerId;
- if (args.size() >= 5) {
- compilerId = args[4];
- }
- std::string mod_dir = cmSystemTools::GetFilenamePath(mod);
- if (!mod_dir.empty()) {
- mod_dir += "/";
- }
- std::string mod_upper = mod_dir;
- mod_upper += cmSystemTools::UpperCase(cmSystemTools::GetFilenameName(mod));
- std::string mod_lower = mod_dir;
- mod_lower += cmSystemTools::LowerCase(cmSystemTools::GetFilenameName(mod));
- mod += ".mod";
- mod_upper += ".mod";
- mod_lower += ".mod";
- if (cmSystemTools::FileExists(mod_upper, true)) {
- if (cmDependsFortran::ModulesDiffer(mod_upper.c_str(), stamp.c_str(),
- compilerId.c_str())) {
- if (!cmSystemTools::CopyFileAlways(mod_upper, stamp)) {
- std::cerr << "Error copying Fortran module from \"" << mod_upper
- << "\" to \"" << stamp << "\".\n";
- return false;
- }
- }
- return true;
- }
- if (cmSystemTools::FileExists(mod_lower, true)) {
- if (cmDependsFortran::ModulesDiffer(mod_lower.c_str(), stamp.c_str(),
- compilerId.c_str())) {
- if (!cmSystemTools::CopyFileAlways(mod_lower, stamp)) {
- std::cerr << "Error copying Fortran module from \"" << mod_lower
- << "\" to \"" << stamp << "\".\n";
- return false;
- }
- }
- return true;
- }
- std::cerr << "Error copying Fortran module \"" << args[2] << "\". Tried \""
- << mod_upper << "\" and \"" << mod_lower << "\".\n";
- return false;
- }
- static bool cmFortranStreamContainsSequence(std::istream& ifs, const char* seq,
- int len)
- {
- assert(len > 0);
- int cur = 0;
- while (cur < len) {
-
- int token = ifs.get();
- if (!ifs) {
- return false;
- }
-
- if (token == static_cast<int>(seq[cur])) {
- ++cur;
- } else {
-
- cur = 0;
- }
- }
-
- return true;
- }
- static bool cmFortranStreamsDiffer(std::istream& ifs1, std::istream& ifs2)
- {
-
- for (;;) {
- int ifs1_c = ifs1.get();
- int ifs2_c = ifs2.get();
- if (!ifs1 && !ifs2) {
-
-
- return false;
- }
- if (!ifs1 || !ifs2 || ifs1_c != ifs2_c) {
-
-
- break;
- }
- }
- return true;
- }
- bool cmDependsFortran::ModulesDiffer(const char* modFile,
- const char* stampFile,
- const char* compilerId)
- {
-
-
- if (strcmp(compilerId, "SunPro") == 0) {
- return cmSystemTools::FilesDiffer(modFile, stampFile);
- }
- #if defined(_WIN32) || defined(__CYGWIN__)
- cmsys::ifstream finModFile(modFile, std::ios::in | std::ios::binary);
- cmsys::ifstream finStampFile(stampFile, std::ios::in | std::ios::binary);
- #else
- cmsys::ifstream finModFile(modFile);
- cmsys::ifstream finStampFile(stampFile);
- #endif
- if (!finModFile || !finStampFile) {
-
- return true;
- }
-
- if (strcmp(compilerId, "GNU") == 0) {
-
-
-
- unsigned char hdr[2];
- bool okay = !finModFile.read(reinterpret_cast<char*>(hdr), 2).fail();
- finModFile.seekg(0);
- if (!okay || hdr[0] != 0x1f || hdr[1] != 0x8b) {
- const char seq[1] = { '\n' };
- const int seqlen = 1;
- if (!cmFortranStreamContainsSequence(finModFile, seq, seqlen)) {
-
- std::cerr << compilerId << " fortran module " << modFile
- << " has unexpected format." << std::endl;
- return true;
- }
- if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
-
- return true;
- }
- }
- } else if (strcmp(compilerId, "Intel") == 0) {
- const char seq[2] = { '\n', '\0' };
- const int seqlen = 2;
-
-
-
- finModFile.get();
- finStampFile.get();
- if (!cmFortranStreamContainsSequence(finModFile, seq, seqlen)) {
-
- std::cerr << compilerId << " fortran module " << modFile
- << " has unexpected format." << std::endl;
- return true;
- }
- if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
-
- return true;
- }
- }
-
-
-
- return cmFortranStreamsDiffer(finModFile, finStampFile);
- }
- std::string cmDependsFortran::MaybeConvertToRelativePath(
- std::string const& base, std::string const& path)
- {
- if (!cmOutputConverter::ContainedInDirectory(
- base, path, this->LocalGenerator->GetStateSnapshot().GetDirectory())) {
- return path;
- }
- return cmOutputConverter::ForceToRelativePath(base, path);
- }
|