cmDependsFortran.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmDependsFortran.h"
  4. #include "cmsys/FStream.hxx"
  5. #include <assert.h>
  6. #include <iostream>
  7. #include <map>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <utility>
  11. #include "cmFortranParser.h" /* Interface to parser object. */
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmOutputConverter.h"
  16. #include "cmStateDirectory.h"
  17. #include "cmStateSnapshot.h"
  18. #include "cmSystemTools.h"
  19. // TODO: Test compiler for the case of the mod file. Some always
  20. // use lower case and some always use upper case. I do not know if any
  21. // use the case from the source code.
  22. class cmDependsFortranInternals
  23. {
  24. public:
  25. // The set of modules provided by this target.
  26. std::set<std::string> TargetProvides;
  27. // Map modules required by this target to locations.
  28. typedef std::map<std::string, std::string> TargetRequiresMap;
  29. TargetRequiresMap TargetRequires;
  30. // Information about each object file.
  31. typedef std::map<std::string, cmFortranSourceInfo> ObjectInfoMap;
  32. ObjectInfoMap ObjectInfo;
  33. cmFortranSourceInfo& CreateObjectInfo(const char* obj, const char* src)
  34. {
  35. std::map<std::string, cmFortranSourceInfo>::iterator i =
  36. this->ObjectInfo.find(obj);
  37. if (i == this->ObjectInfo.end()) {
  38. std::map<std::string, cmFortranSourceInfo>::value_type entry(
  39. obj, cmFortranSourceInfo());
  40. i = this->ObjectInfo.insert(entry).first;
  41. i->second.Source = src;
  42. }
  43. return i->second;
  44. }
  45. };
  46. cmDependsFortran::cmDependsFortran()
  47. : Internal(nullptr)
  48. {
  49. }
  50. cmDependsFortran::cmDependsFortran(cmLocalGenerator* lg)
  51. : cmDepends(lg)
  52. , Internal(new cmDependsFortranInternals)
  53. {
  54. // Configure the include file search path.
  55. this->SetIncludePathFromLanguage("Fortran");
  56. // Get the list of definitions.
  57. std::vector<std::string> definitions;
  58. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  59. if (const char* c_defines =
  60. mf->GetDefinition("CMAKE_TARGET_DEFINITIONS_Fortran")) {
  61. cmSystemTools::ExpandListArgument(c_defines, definitions);
  62. }
  63. // translate i.e. FOO=BAR to FOO and add it to the list of defined
  64. // preprocessor symbols
  65. for (std::string def : definitions) {
  66. std::string::size_type assignment = def.find('=');
  67. if (assignment != std::string::npos) {
  68. def = def.substr(0, assignment);
  69. }
  70. this->PPDefinitions.insert(def);
  71. }
  72. }
  73. cmDependsFortran::~cmDependsFortran()
  74. {
  75. delete this->Internal;
  76. }
  77. bool cmDependsFortran::WriteDependencies(const std::set<std::string>& sources,
  78. const std::string& obj,
  79. std::ostream& /*makeDepends*/,
  80. std::ostream& /*internalDepends*/)
  81. {
  82. // Make sure this is a scanning instance.
  83. if (sources.empty() || sources.begin()->empty()) {
  84. cmSystemTools::Error("Cannot scan dependencies without a source file.");
  85. return false;
  86. }
  87. if (obj.empty()) {
  88. cmSystemTools::Error("Cannot scan dependencies without an object file.");
  89. return false;
  90. }
  91. bool okay = true;
  92. for (std::string const& src : sources) {
  93. // Get the information object for this source.
  94. cmFortranSourceInfo& info =
  95. this->Internal->CreateObjectInfo(obj.c_str(), src.c_str());
  96. // Create the parser object. The constructor takes info by reference,
  97. // so we may look into the resulting objects later.
  98. cmFortranParser parser(this->IncludePath, this->PPDefinitions, info);
  99. // Push on the starting file.
  100. cmFortranParser_FilePush(&parser, src.c_str());
  101. // Parse the translation unit.
  102. if (cmFortran_yyparse(parser.Scanner) != 0) {
  103. // Failed to parse the file. Report failure to write dependencies.
  104. okay = false;
  105. /* clang-format off */
  106. std::cerr <<
  107. "warning: failed to parse dependencies from Fortran source "
  108. "'" << src << "': " << parser.Error << std::endl
  109. ;
  110. /* clang-format on */
  111. }
  112. }
  113. return okay;
  114. }
  115. bool cmDependsFortran::Finalize(std::ostream& makeDepends,
  116. std::ostream& internalDepends)
  117. {
  118. // Prepare the module search process.
  119. this->LocateModules();
  120. // Get the directory in which stamp files will be stored.
  121. const char* stamp_dir = this->TargetDirectory.c_str();
  122. // Get the directory in which module files will be created.
  123. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  124. std::string mod_dir =
  125. mf->GetSafeDefinition("CMAKE_Fortran_TARGET_MODULE_DIR");
  126. if (mod_dir.empty()) {
  127. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  128. }
  129. // Actually write dependencies to the streams.
  130. typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
  131. ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
  132. for (auto const& i : objInfo) {
  133. if (!this->WriteDependenciesReal(i.first.c_str(), i.second, mod_dir,
  134. stamp_dir, makeDepends,
  135. internalDepends)) {
  136. return false;
  137. }
  138. }
  139. // Store the list of modules provided by this target.
  140. std::string fiName = this->TargetDirectory;
  141. fiName += "/fortran.internal";
  142. cmGeneratedFileStream fiStream(fiName.c_str());
  143. fiStream << "# The fortran modules provided by this target.\n";
  144. fiStream << "provides\n";
  145. std::set<std::string> const& provides = this->Internal->TargetProvides;
  146. for (std::string const& i : provides) {
  147. fiStream << " " << i << "\n";
  148. }
  149. // Create a script to clean the modules.
  150. if (!provides.empty()) {
  151. std::string fcName = this->TargetDirectory;
  152. fcName += "/cmake_clean_Fortran.cmake";
  153. cmGeneratedFileStream fcStream(fcName.c_str());
  154. fcStream << "# Remove fortran modules provided by this target.\n";
  155. fcStream << "FILE(REMOVE";
  156. std::string currentBinDir =
  157. this->LocalGenerator->GetCurrentBinaryDirectory();
  158. for (std::string const& i : provides) {
  159. std::string mod_upper = mod_dir;
  160. mod_upper += "/";
  161. mod_upper += cmSystemTools::UpperCase(i);
  162. mod_upper += ".mod";
  163. std::string mod_lower = mod_dir;
  164. mod_lower += "/";
  165. mod_lower += i;
  166. mod_lower += ".mod";
  167. std::string stamp = stamp_dir;
  168. stamp += "/";
  169. stamp += i;
  170. stamp += ".mod.stamp";
  171. fcStream << "\n";
  172. fcStream << " \""
  173. << this->MaybeConvertToRelativePath(currentBinDir, mod_lower)
  174. << "\"\n";
  175. fcStream << " \""
  176. << this->MaybeConvertToRelativePath(currentBinDir, mod_upper)
  177. << "\"\n";
  178. fcStream << " \""
  179. << this->MaybeConvertToRelativePath(currentBinDir, stamp)
  180. << "\"\n";
  181. }
  182. fcStream << " )\n";
  183. }
  184. return true;
  185. }
  186. void cmDependsFortran::LocateModules()
  187. {
  188. // Collect the set of modules provided and required by all sources.
  189. typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
  190. ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
  191. for (auto const& infoI : objInfo) {
  192. cmFortranSourceInfo const& info = infoI.second;
  193. // Include this module in the set provided by this target.
  194. this->Internal->TargetProvides.insert(info.Provides.begin(),
  195. info.Provides.end());
  196. for (std::string const& r : info.Requires) {
  197. this->Internal->TargetRequires[r].clear();
  198. }
  199. }
  200. // Short-circuit for simple targets.
  201. if (this->Internal->TargetRequires.empty()) {
  202. return;
  203. }
  204. // Match modules provided by this target to those it requires.
  205. this->MatchLocalModules();
  206. // Load information about other targets.
  207. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  208. std::vector<std::string> infoFiles;
  209. if (const char* infoFilesValue =
  210. mf->GetDefinition("CMAKE_TARGET_LINKED_INFO_FILES")) {
  211. cmSystemTools::ExpandListArgument(infoFilesValue, infoFiles);
  212. }
  213. for (std::string const& i : infoFiles) {
  214. std::string targetDir = cmSystemTools::GetFilenamePath(i);
  215. std::string fname = targetDir + "/fortran.internal";
  216. cmsys::ifstream fin(fname.c_str());
  217. if (fin) {
  218. this->MatchRemoteModules(fin, targetDir.c_str());
  219. }
  220. }
  221. }
  222. void cmDependsFortran::MatchLocalModules()
  223. {
  224. const char* stampDir = this->TargetDirectory.c_str();
  225. std::set<std::string> const& provides = this->Internal->TargetProvides;
  226. for (std::string const& i : provides) {
  227. this->ConsiderModule(i.c_str(), stampDir);
  228. }
  229. }
  230. void cmDependsFortran::MatchRemoteModules(std::istream& fin,
  231. const char* stampDir)
  232. {
  233. std::string line;
  234. bool doing_provides = false;
  235. while (cmSystemTools::GetLineFromStream(fin, line)) {
  236. // Ignore comments and empty lines.
  237. if (line.empty() || line[0] == '#' || line[0] == '\r') {
  238. continue;
  239. }
  240. if (line[0] == ' ') {
  241. if (doing_provides) {
  242. this->ConsiderModule(line.c_str() + 1, stampDir);
  243. }
  244. } else if (line == "provides") {
  245. doing_provides = true;
  246. } else {
  247. doing_provides = false;
  248. }
  249. }
  250. }
  251. void cmDependsFortran::ConsiderModule(const char* name, const char* stampDir)
  252. {
  253. // Locate each required module.
  254. typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
  255. TargetRequiresMap::iterator required =
  256. this->Internal->TargetRequires.find(name);
  257. if (required != this->Internal->TargetRequires.end() &&
  258. required->second.empty()) {
  259. // The module is provided by a CMake target. It will have a stamp file.
  260. std::string stampFile = stampDir;
  261. stampFile += "/";
  262. stampFile += name;
  263. stampFile += ".mod.stamp";
  264. required->second = stampFile;
  265. }
  266. }
  267. bool cmDependsFortran::WriteDependenciesReal(const char* obj,
  268. cmFortranSourceInfo const& info,
  269. std::string const& mod_dir,
  270. const char* stamp_dir,
  271. std::ostream& makeDepends,
  272. std::ostream& internalDepends)
  273. {
  274. typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
  275. // Get the source file for this object.
  276. const char* src = info.Source.c_str();
  277. // Write the include dependencies to the output stream.
  278. std::string binDir = this->LocalGenerator->GetBinaryDirectory();
  279. std::string obj_i = this->MaybeConvertToRelativePath(binDir, obj);
  280. std::string obj_m = cmSystemTools::ConvertToOutputPath(obj_i);
  281. internalDepends << obj_i << std::endl;
  282. internalDepends << " " << src << std::endl;
  283. for (std::string const& i : info.Includes) {
  284. makeDepends << obj_m << ": "
  285. << cmSystemTools::ConvertToOutputPath(
  286. this->MaybeConvertToRelativePath(binDir, i))
  287. << std::endl;
  288. internalDepends << " " << i << std::endl;
  289. }
  290. makeDepends << std::endl;
  291. // Write module requirements to the output stream.
  292. for (std::string const& i : info.Requires) {
  293. // Require only modules not provided in the same source.
  294. if (info.Provides.find(i) != info.Provides.cend()) {
  295. continue;
  296. }
  297. // The object file should depend on timestamped files for the
  298. // modules it uses.
  299. TargetRequiresMap::const_iterator required =
  300. this->Internal->TargetRequires.find(i);
  301. if (required == this->Internal->TargetRequires.end()) {
  302. abort();
  303. }
  304. if (!required->second.empty()) {
  305. // This module is known. Depend on its timestamp file.
  306. std::string stampFile = cmSystemTools::ConvertToOutputPath(
  307. this->MaybeConvertToRelativePath(binDir, required->second));
  308. makeDepends << obj_m << ": " << stampFile << "\n";
  309. } else {
  310. // This module is not known to CMake. Try to locate it where
  311. // the compiler will and depend on that.
  312. std::string module;
  313. if (this->FindModule(i, module)) {
  314. module = cmSystemTools::ConvertToOutputPath(
  315. this->MaybeConvertToRelativePath(binDir, module));
  316. makeDepends << obj_m << ": " << module << "\n";
  317. }
  318. }
  319. }
  320. // If any modules are provided then they must be converted to stamp files.
  321. if (!info.Provides.empty()) {
  322. // Create a target to copy the module after the object file
  323. // changes.
  324. for (std::string const& i : info.Provides) {
  325. // Include this module in the set provided by this target.
  326. this->Internal->TargetProvides.insert(i);
  327. // Always use lower case for the mod stamp file name. The
  328. // cmake_copy_f90_mod will call back to this class, which will
  329. // try various cases for the real mod file name.
  330. std::string m = cmSystemTools::LowerCase(i);
  331. std::string modFile = mod_dir;
  332. modFile += "/";
  333. modFile += i;
  334. modFile = this->LocalGenerator->ConvertToOutputFormat(
  335. this->MaybeConvertToRelativePath(binDir, modFile),
  336. cmOutputConverter::SHELL);
  337. std::string stampFile = stamp_dir;
  338. stampFile += "/";
  339. stampFile += m;
  340. stampFile += ".mod.stamp";
  341. stampFile = this->MaybeConvertToRelativePath(binDir, stampFile);
  342. std::string const stampFileForShell =
  343. this->LocalGenerator->ConvertToOutputFormat(stampFile,
  344. cmOutputConverter::SHELL);
  345. std::string const stampFileForMake =
  346. cmSystemTools::ConvertToOutputPath(stampFile);
  347. makeDepends << obj_m << ".provides.build"
  348. << ": " << stampFileForMake << "\n";
  349. // Note that when cmake_copy_f90_mod finds that a module file
  350. // and the corresponding stamp file have no differences, the stamp
  351. // file is not updated. In such case the stamp file will be always
  352. // older than its prerequisite and trigger cmake_copy_f90_mod
  353. // on each new build. This is expected behavior for incremental
  354. // builds and can not be changed without preforming recursive make
  355. // calls that would considerably slow down the building process.
  356. makeDepends << stampFileForMake << ": " << obj_m << "\n";
  357. makeDepends << "\t$(CMAKE_COMMAND) -E cmake_copy_f90_mod " << modFile
  358. << " " << stampFileForShell;
  359. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  360. const char* cid = mf->GetDefinition("CMAKE_Fortran_COMPILER_ID");
  361. if (cid && *cid) {
  362. makeDepends << " " << cid;
  363. }
  364. makeDepends << "\n";
  365. }
  366. makeDepends << obj_m << ".provides.build:\n";
  367. // After copying the modules update the timestamp file.
  368. makeDepends << "\t$(CMAKE_COMMAND) -E touch " << obj_m
  369. << ".provides.build\n";
  370. // Make sure the module timestamp rule is evaluated by the time
  371. // the target finishes building.
  372. std::string driver = this->TargetDirectory;
  373. driver += "/build";
  374. driver = cmSystemTools::ConvertToOutputPath(
  375. this->MaybeConvertToRelativePath(binDir, driver));
  376. makeDepends << driver << ": " << obj_m << ".provides.build\n";
  377. }
  378. return true;
  379. }
  380. bool cmDependsFortran::FindModule(std::string const& name, std::string& module)
  381. {
  382. // Construct possible names for the module file.
  383. std::string mod_upper = cmSystemTools::UpperCase(name);
  384. std::string mod_lower = name;
  385. mod_upper += ".mod";
  386. mod_lower += ".mod";
  387. // Search the include path for the module.
  388. std::string fullName;
  389. for (std::string const& ip : this->IncludePath) {
  390. // Try the lower-case name.
  391. fullName = ip;
  392. fullName += "/";
  393. fullName += mod_lower;
  394. if (cmSystemTools::FileExists(fullName, true)) {
  395. module = fullName;
  396. return true;
  397. }
  398. // Try the upper-case name.
  399. fullName = ip;
  400. fullName += "/";
  401. fullName += mod_upper;
  402. if (cmSystemTools::FileExists(fullName, true)) {
  403. module = fullName;
  404. return true;
  405. }
  406. }
  407. return false;
  408. }
  409. bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
  410. {
  411. // Implements
  412. //
  413. // $(CMAKE_COMMAND) -E cmake_copy_f90_mod input.mod output.mod.stamp
  414. // [compiler-id]
  415. //
  416. // Note that the case of the .mod file depends on the compiler. In
  417. // the future this copy could also account for the fact that some
  418. // compilers include a timestamp in the .mod file so it changes even
  419. // when the interface described in the module does not.
  420. std::string mod = args[2];
  421. std::string stamp = args[3];
  422. std::string compilerId;
  423. if (args.size() >= 5) {
  424. compilerId = args[4];
  425. }
  426. std::string mod_dir = cmSystemTools::GetFilenamePath(mod);
  427. if (!mod_dir.empty()) {
  428. mod_dir += "/";
  429. }
  430. std::string mod_upper = mod_dir;
  431. mod_upper += cmSystemTools::UpperCase(cmSystemTools::GetFilenameName(mod));
  432. std::string mod_lower = mod_dir;
  433. mod_lower += cmSystemTools::LowerCase(cmSystemTools::GetFilenameName(mod));
  434. mod += ".mod";
  435. mod_upper += ".mod";
  436. mod_lower += ".mod";
  437. if (cmSystemTools::FileExists(mod_upper, true)) {
  438. if (cmDependsFortran::ModulesDiffer(mod_upper.c_str(), stamp.c_str(),
  439. compilerId.c_str())) {
  440. if (!cmSystemTools::CopyFileAlways(mod_upper, stamp)) {
  441. std::cerr << "Error copying Fortran module from \"" << mod_upper
  442. << "\" to \"" << stamp << "\".\n";
  443. return false;
  444. }
  445. }
  446. return true;
  447. }
  448. if (cmSystemTools::FileExists(mod_lower, true)) {
  449. if (cmDependsFortran::ModulesDiffer(mod_lower.c_str(), stamp.c_str(),
  450. compilerId.c_str())) {
  451. if (!cmSystemTools::CopyFileAlways(mod_lower, stamp)) {
  452. std::cerr << "Error copying Fortran module from \"" << mod_lower
  453. << "\" to \"" << stamp << "\".\n";
  454. return false;
  455. }
  456. }
  457. return true;
  458. }
  459. std::cerr << "Error copying Fortran module \"" << args[2] << "\". Tried \""
  460. << mod_upper << "\" and \"" << mod_lower << "\".\n";
  461. return false;
  462. }
  463. // Helper function to look for a short sequence in a stream. If this
  464. // is later used for longer sequences it should be re-written using an
  465. // efficient string search algorithm such as Boyer-Moore.
  466. static bool cmFortranStreamContainsSequence(std::istream& ifs, const char* seq,
  467. int len)
  468. {
  469. assert(len > 0);
  470. int cur = 0;
  471. while (cur < len) {
  472. // Get the next character.
  473. int token = ifs.get();
  474. if (!ifs) {
  475. return false;
  476. }
  477. // Check the character.
  478. if (token == static_cast<int>(seq[cur])) {
  479. ++cur;
  480. } else {
  481. // Assume the sequence has no repeating subsequence.
  482. cur = 0;
  483. }
  484. }
  485. // The entire sequence was matched.
  486. return true;
  487. }
  488. // Helper function to compare the remaining content in two streams.
  489. static bool cmFortranStreamsDiffer(std::istream& ifs1, std::istream& ifs2)
  490. {
  491. // Compare the remaining content.
  492. for (;;) {
  493. int ifs1_c = ifs1.get();
  494. int ifs2_c = ifs2.get();
  495. if (!ifs1 && !ifs2) {
  496. // We have reached the end of both streams simultaneously.
  497. // The streams are identical.
  498. return false;
  499. }
  500. if (!ifs1 || !ifs2 || ifs1_c != ifs2_c) {
  501. // We have reached the end of one stream before the other or
  502. // found differing content. The streams are different.
  503. break;
  504. }
  505. }
  506. return true;
  507. }
  508. bool cmDependsFortran::ModulesDiffer(const char* modFile,
  509. const char* stampFile,
  510. const char* compilerId)
  511. {
  512. /*
  513. gnu >= 4.9:
  514. A mod file is an ascii file compressed with gzip.
  515. Compiling twice produces identical modules.
  516. gnu < 4.9:
  517. A mod file is an ascii file.
  518. <bar.mod>
  519. FORTRAN module created from /path/to/foo.f90 on Sun Dec 30 22:47:58 2007
  520. If you edit this, you'll get what you deserve.
  521. ...
  522. </bar.mod>
  523. As you can see the first line contains the date.
  524. intel:
  525. A mod file is a binary file.
  526. However, looking into both generated bar.mod files with a hex editor
  527. shows that they differ only before a sequence linefeed-zero (0x0A 0x00)
  528. which is located some bytes in front of the absolute path to the source
  529. file.
  530. sun:
  531. A mod file is a binary file. Compiling twice produces identical modules.
  532. others:
  533. TODO ...
  534. */
  535. /* Compilers which do _not_ produce different mod content when the same
  536. * source is compiled twice
  537. * -SunPro
  538. */
  539. if (strcmp(compilerId, "SunPro") == 0) {
  540. return cmSystemTools::FilesDiffer(modFile, stampFile);
  541. }
  542. #if defined(_WIN32) || defined(__CYGWIN__)
  543. cmsys::ifstream finModFile(modFile, std::ios::in | std::ios::binary);
  544. cmsys::ifstream finStampFile(stampFile, std::ios::in | std::ios::binary);
  545. #else
  546. cmsys::ifstream finModFile(modFile);
  547. cmsys::ifstream finStampFile(stampFile);
  548. #endif
  549. if (!finModFile || !finStampFile) {
  550. // At least one of the files does not exist. The modules differ.
  551. return true;
  552. }
  553. /* Compilers which _do_ produce different mod content when the same
  554. * source is compiled twice
  555. * -GNU
  556. * -Intel
  557. *
  558. * Eat the stream content until all recompile only related changes
  559. * are left behind.
  560. */
  561. if (strcmp(compilerId, "GNU") == 0) {
  562. // GNU Fortran 4.9 and later compress .mod files with gzip
  563. // but also do not include a date so we can fall through to
  564. // compare them without skipping any prefix.
  565. unsigned char hdr[2];
  566. bool okay = !finModFile.read(reinterpret_cast<char*>(hdr), 2).fail();
  567. finModFile.seekg(0);
  568. if (!okay || hdr[0] != 0x1f || hdr[1] != 0x8b) {
  569. const char seq[1] = { '\n' };
  570. const int seqlen = 1;
  571. if (!cmFortranStreamContainsSequence(finModFile, seq, seqlen)) {
  572. // The module is of unexpected format. Assume it is different.
  573. std::cerr << compilerId << " fortran module " << modFile
  574. << " has unexpected format." << std::endl;
  575. return true;
  576. }
  577. if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
  578. // The stamp must differ if the sequence is not contained.
  579. return true;
  580. }
  581. }
  582. } else if (strcmp(compilerId, "Intel") == 0) {
  583. const char seq[2] = { '\n', '\0' };
  584. const int seqlen = 2;
  585. // Skip the leading byte which appears to be a version number.
  586. // We do not need to check for an error because the sequence search
  587. // below will fail in that case.
  588. finModFile.get();
  589. finStampFile.get();
  590. if (!cmFortranStreamContainsSequence(finModFile, seq, seqlen)) {
  591. // The module is of unexpected format. Assume it is different.
  592. std::cerr << compilerId << " fortran module " << modFile
  593. << " has unexpected format." << std::endl;
  594. return true;
  595. }
  596. if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
  597. // The stamp must differ if the sequence is not contained.
  598. return true;
  599. }
  600. }
  601. // Compare the remaining content. If no compiler id matched above,
  602. // including the case none was given, this will compare the whole
  603. // content.
  604. return cmFortranStreamsDiffer(finModFile, finStampFile);
  605. }
  606. std::string cmDependsFortran::MaybeConvertToRelativePath(
  607. std::string const& base, std::string const& path)
  608. {
  609. if (!cmOutputConverter::ContainedInDirectory(
  610. base, path, this->LocalGenerator->GetStateSnapshot().GetDirectory())) {
  611. return path;
  612. }
  613. return cmOutputConverter::ForceToRelativePath(base, path);
  614. }