cmDependsFortran.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 <cassert>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <map>
  8. #include <utility>
  9. #include "cmsys/FStream.hxx"
  10. #include "cmFortranParser.h" /* Interface to parser object. */
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGlobalUnixMakefileGenerator3.h"
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmMakefile.h"
  15. #include "cmOutputConverter.h"
  16. #include "cmProperty.h"
  17. #include "cmStringAlgorithms.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. static void cmFortranModuleAppendUpperLower(std::string const& mod,
  23. std::string& mod_upper,
  24. std::string& mod_lower)
  25. {
  26. std::string::size_type ext_len = 0;
  27. if (cmHasLiteralSuffix(mod, ".mod") || cmHasLiteralSuffix(mod, ".sub")) {
  28. ext_len = 4;
  29. } else if (cmHasLiteralSuffix(mod, ".smod")) {
  30. ext_len = 5;
  31. }
  32. std::string const& name = mod.substr(0, mod.size() - ext_len);
  33. std::string const& ext = mod.substr(mod.size() - ext_len);
  34. mod_upper += cmSystemTools::UpperCase(name) + ext;
  35. mod_lower += mod;
  36. }
  37. class cmDependsFortranInternals
  38. {
  39. public:
  40. // The set of modules provided by this target.
  41. std::set<std::string> TargetProvides;
  42. // Map modules required by this target to locations.
  43. using TargetRequiresMap = std::map<std::string, std::string>;
  44. TargetRequiresMap TargetRequires;
  45. // Information about each object file.
  46. using ObjectInfoMap = std::map<std::string, cmFortranSourceInfo>;
  47. ObjectInfoMap ObjectInfo;
  48. cmFortranSourceInfo& CreateObjectInfo(const std::string& obj,
  49. const std::string& src)
  50. {
  51. auto i = this->ObjectInfo.find(obj);
  52. if (i == this->ObjectInfo.end()) {
  53. std::map<std::string, cmFortranSourceInfo>::value_type entry(
  54. obj, cmFortranSourceInfo());
  55. i = this->ObjectInfo.insert(entry).first;
  56. i->second.Source = src;
  57. }
  58. return i->second;
  59. }
  60. };
  61. cmDependsFortran::cmDependsFortran() = default;
  62. cmDependsFortran::cmDependsFortran(cmLocalUnixMakefileGenerator3* lg)
  63. : cmDepends(lg)
  64. , Internal(new cmDependsFortranInternals)
  65. {
  66. // Configure the include file search path.
  67. this->SetIncludePathFromLanguage("Fortran");
  68. // Get the list of definitions.
  69. std::vector<std::string> definitions;
  70. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  71. mf->GetDefExpandList("CMAKE_TARGET_DEFINITIONS_Fortran", definitions);
  72. // translate i.e. FOO=BAR to FOO and add it to the list of defined
  73. // preprocessor symbols
  74. for (std::string def : definitions) {
  75. std::string::size_type assignment = def.find('=');
  76. if (assignment != std::string::npos) {
  77. def = def.substr(0, assignment);
  78. }
  79. this->PPDefinitions.insert(def);
  80. }
  81. this->CompilerId = mf->GetSafeDefinition("CMAKE_Fortran_COMPILER_ID");
  82. this->SModSep = mf->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_SEP");
  83. this->SModExt = mf->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
  84. }
  85. cmDependsFortran::~cmDependsFortran() = default;
  86. bool cmDependsFortran::WriteDependencies(const std::set<std::string>& sources,
  87. const std::string& obj,
  88. std::ostream& /*makeDepends*/,
  89. std::ostream& /*internalDepends*/)
  90. {
  91. // Make sure this is a scanning instance.
  92. if (sources.empty() || sources.begin()->empty()) {
  93. cmSystemTools::Error("Cannot scan dependencies without a source file.");
  94. return false;
  95. }
  96. if (obj.empty()) {
  97. cmSystemTools::Error("Cannot scan dependencies without an object file.");
  98. return false;
  99. }
  100. cmFortranCompiler fc;
  101. fc.Id = this->CompilerId;
  102. fc.SModSep = this->SModSep;
  103. fc.SModExt = this->SModExt;
  104. bool okay = true;
  105. for (std::string const& src : sources) {
  106. // Get the information object for this source.
  107. cmFortranSourceInfo& info = this->Internal->CreateObjectInfo(obj, src);
  108. // Create the parser object. The constructor takes info by reference,
  109. // so we may look into the resulting objects later.
  110. cmFortranParser parser(fc, this->IncludePath, this->PPDefinitions, info);
  111. // Push on the starting file.
  112. cmFortranParser_FilePush(&parser, src.c_str());
  113. // Parse the translation unit.
  114. if (cmFortran_yyparse(parser.Scanner) != 0) {
  115. // Failed to parse the file. Report failure to write dependencies.
  116. okay = false;
  117. /* clang-format off */
  118. std::cerr <<
  119. "warning: failed to parse dependencies from Fortran source "
  120. "'" << src << "': " << parser.Error << std::endl
  121. ;
  122. /* clang-format on */
  123. }
  124. }
  125. return okay;
  126. }
  127. bool cmDependsFortran::Finalize(std::ostream& makeDepends,
  128. std::ostream& internalDepends)
  129. {
  130. // Prepare the module search process.
  131. this->LocateModules();
  132. // Get the directory in which stamp files will be stored.
  133. const std::string& stamp_dir = this->TargetDirectory;
  134. // Get the directory in which module files will be created.
  135. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  136. std::string mod_dir =
  137. mf->GetSafeDefinition("CMAKE_Fortran_TARGET_MODULE_DIR");
  138. if (mod_dir.empty()) {
  139. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  140. }
  141. // Actually write dependencies to the streams.
  142. using ObjectInfoMap = cmDependsFortranInternals::ObjectInfoMap;
  143. ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
  144. for (auto const& i : objInfo) {
  145. if (!this->WriteDependenciesReal(i.first, i.second, mod_dir, stamp_dir,
  146. makeDepends, internalDepends)) {
  147. return false;
  148. }
  149. }
  150. // Store the list of modules provided by this target.
  151. std::string fiName = cmStrCat(this->TargetDirectory, "/fortran.internal");
  152. cmGeneratedFileStream fiStream(fiName);
  153. fiStream << "# The fortran modules provided by this target.\n";
  154. fiStream << "provides\n";
  155. std::set<std::string> const& provides = this->Internal->TargetProvides;
  156. for (std::string const& i : provides) {
  157. fiStream << ' ' << i << '\n';
  158. }
  159. // Create a script to clean the modules.
  160. if (!provides.empty()) {
  161. std::string fcName =
  162. cmStrCat(this->TargetDirectory, "/cmake_clean_Fortran.cmake");
  163. cmGeneratedFileStream fcStream(fcName);
  164. fcStream << "# Remove fortran modules provided by this target.\n";
  165. fcStream << "FILE(REMOVE";
  166. for (std::string const& i : provides) {
  167. std::string mod_upper = cmStrCat(mod_dir, '/');
  168. std::string mod_lower = cmStrCat(mod_dir, '/');
  169. cmFortranModuleAppendUpperLower(i, mod_upper, mod_lower);
  170. std::string stamp = cmStrCat(stamp_dir, '/', i, ".stamp");
  171. fcStream << "\n"
  172. " \""
  173. << this->LocalGenerator->MaybeRelativeToCurBinDir(mod_lower)
  174. << "\"\n"
  175. " \""
  176. << this->LocalGenerator->MaybeRelativeToCurBinDir(mod_upper)
  177. << "\"\n"
  178. " \""
  179. << this->LocalGenerator->MaybeRelativeToCurBinDir(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. using ObjectInfoMap = cmDependsFortranInternals::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. mf->GetDefExpandList("CMAKE_TARGET_LINKED_INFO_FILES", infoFiles);
  210. for (std::string const& i : infoFiles) {
  211. std::string targetDir = cmSystemTools::GetFilenamePath(i);
  212. std::string fname = targetDir + "/fortran.internal";
  213. cmsys::ifstream fin(fname.c_str());
  214. if (fin) {
  215. this->MatchRemoteModules(fin, targetDir);
  216. }
  217. }
  218. }
  219. void cmDependsFortran::MatchLocalModules()
  220. {
  221. std::string const& stampDir = this->TargetDirectory;
  222. std::set<std::string> const& provides = this->Internal->TargetProvides;
  223. for (std::string const& i : provides) {
  224. this->ConsiderModule(i, stampDir);
  225. }
  226. }
  227. void cmDependsFortran::MatchRemoteModules(std::istream& fin,
  228. const std::string& stampDir)
  229. {
  230. std::string line;
  231. bool doing_provides = false;
  232. while (cmSystemTools::GetLineFromStream(fin, line)) {
  233. // Ignore comments and empty lines.
  234. if (line.empty() || line[0] == '#' || line[0] == '\r') {
  235. continue;
  236. }
  237. if (line[0] == ' ') {
  238. if (doing_provides) {
  239. std::string mod = line;
  240. if (!cmHasLiteralSuffix(mod, ".mod") &&
  241. !cmHasLiteralSuffix(mod, ".smod") &&
  242. !cmHasLiteralSuffix(mod, ".sub")) {
  243. // Support fortran.internal files left by older versions of CMake.
  244. // They do not include the ".mod" extension.
  245. mod += ".mod";
  246. }
  247. this->ConsiderModule(mod.substr(1), stampDir);
  248. }
  249. } else if (line == "provides") {
  250. doing_provides = true;
  251. } else {
  252. doing_provides = false;
  253. }
  254. }
  255. }
  256. void cmDependsFortran::ConsiderModule(const std::string& name,
  257. const std::string& stampDir)
  258. {
  259. // Locate each required module.
  260. auto required = this->Internal->TargetRequires.find(name);
  261. if (required != this->Internal->TargetRequires.end() &&
  262. required->second.empty()) {
  263. // The module is provided by a CMake target. It will have a stamp file.
  264. std::string stampFile = cmStrCat(stampDir, '/', name, ".stamp");
  265. required->second = stampFile;
  266. }
  267. }
  268. bool cmDependsFortran::WriteDependenciesReal(std::string const& obj,
  269. cmFortranSourceInfo const& info,
  270. std::string const& mod_dir,
  271. std::string const& stamp_dir,
  272. std::ostream& makeDepends,
  273. std::ostream& internalDepends)
  274. {
  275. // Get the source file for this object.
  276. std::string const& src = info.Source;
  277. // Write the include dependencies to the output stream.
  278. std::string obj_i = this->LocalGenerator->MaybeRelativeToTopBinDir(obj);
  279. std::string obj_m = cmSystemTools::ConvertToOutputPath(obj_i);
  280. internalDepends << obj_i << "\n " << src << '\n';
  281. if (!info.Includes.empty()) {
  282. const auto& lineContinue = static_cast<cmGlobalUnixMakefileGenerator3*>(
  283. this->LocalGenerator->GetGlobalGenerator())
  284. ->LineContinueDirective;
  285. bool supportLongLineDepend = static_cast<cmGlobalUnixMakefileGenerator3*>(
  286. this->LocalGenerator->GetGlobalGenerator())
  287. ->SupportsLongLineDependencies();
  288. if (supportLongLineDepend) {
  289. makeDepends << obj_m << ':';
  290. }
  291. for (std::string const& i : info.Includes) {
  292. std::string dependee = cmSystemTools::ConvertToOutputPath(
  293. this->LocalGenerator->MaybeRelativeToTopBinDir(i));
  294. if (supportLongLineDepend) {
  295. makeDepends << ' ' << lineContinue << ' ' << dependee;
  296. } else {
  297. makeDepends << obj_m << ": " << dependee << '\n';
  298. }
  299. internalDepends << ' ' << i << '\n';
  300. }
  301. makeDepends << '\n';
  302. }
  303. // Write module requirements to the output stream.
  304. for (std::string const& i : info.Requires) {
  305. // Require only modules not provided in the same source.
  306. if (info.Provides.find(i) != info.Provides.cend()) {
  307. continue;
  308. }
  309. // The object file should depend on timestamped files for the
  310. // modules it uses.
  311. auto required = this->Internal->TargetRequires.find(i);
  312. if (required == this->Internal->TargetRequires.end()) {
  313. abort();
  314. }
  315. if (!required->second.empty()) {
  316. // This module is known. Depend on its timestamp file.
  317. std::string stampFile = cmSystemTools::ConvertToOutputPath(
  318. this->LocalGenerator->MaybeRelativeToTopBinDir(required->second));
  319. makeDepends << obj_m << ": " << stampFile << '\n';
  320. } else {
  321. // This module is not known to CMake. Try to locate it where
  322. // the compiler will and depend on that.
  323. std::string module;
  324. if (this->FindModule(i, module)) {
  325. module = cmSystemTools::ConvertToOutputPath(
  326. this->LocalGenerator->MaybeRelativeToTopBinDir(module));
  327. makeDepends << obj_m << ": " << module << '\n';
  328. }
  329. }
  330. }
  331. // If any modules are provided then they must be converted to stamp files.
  332. if (!info.Provides.empty()) {
  333. // Create a target to copy the module after the object file
  334. // changes.
  335. for (std::string const& i : info.Provides) {
  336. // Include this module in the set provided by this target.
  337. this->Internal->TargetProvides.insert(i);
  338. // Always use lower case for the mod stamp file name. The
  339. // cmake_copy_f90_mod will call back to this class, which will
  340. // try various cases for the real mod file name.
  341. std::string modFile = cmStrCat(mod_dir, '/', i);
  342. modFile = this->LocalGenerator->ConvertToOutputFormat(
  343. this->LocalGenerator->MaybeRelativeToTopBinDir(modFile),
  344. cmOutputConverter::SHELL);
  345. std::string stampFile = cmStrCat(stamp_dir, '/', i, ".stamp");
  346. stampFile = this->LocalGenerator->MaybeRelativeToTopBinDir(stampFile);
  347. std::string const stampFileForShell =
  348. this->LocalGenerator->ConvertToOutputFormat(stampFile,
  349. cmOutputConverter::SHELL);
  350. std::string const stampFileForMake =
  351. cmSystemTools::ConvertToOutputPath(stampFile);
  352. makeDepends << obj_m << ".provides.build"
  353. << ": " << stampFileForMake << '\n';
  354. // Note that when cmake_copy_f90_mod finds that a module file
  355. // and the corresponding stamp file have no differences, the stamp
  356. // file is not updated. In such case the stamp file will be always
  357. // older than its prerequisite and trigger cmake_copy_f90_mod
  358. // on each new build. This is expected behavior for incremental
  359. // builds and can not be changed without preforming recursive make
  360. // calls that would considerably slow down the building process.
  361. makeDepends << stampFileForMake << ": " << obj_m << '\n';
  362. makeDepends << "\t$(CMAKE_COMMAND) -E cmake_copy_f90_mod " << modFile
  363. << ' ' << stampFileForShell;
  364. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  365. cmProp cid = mf->GetDefinition("CMAKE_Fortran_COMPILER_ID");
  366. if (cmNonempty(cid)) {
  367. makeDepends << ' ' << *cid;
  368. }
  369. makeDepends << '\n';
  370. }
  371. makeDepends << obj_m << ".provides.build:\n";
  372. // After copying the modules update the timestamp file.
  373. makeDepends << "\t$(CMAKE_COMMAND) -E touch " << obj_m
  374. << ".provides.build\n";
  375. // Make sure the module timestamp rule is evaluated by the time
  376. // the target finishes building.
  377. std::string driver = cmStrCat(this->TargetDirectory, "/build");
  378. driver = cmSystemTools::ConvertToOutputPath(
  379. this->LocalGenerator->MaybeRelativeToTopBinDir(driver));
  380. makeDepends << driver << ": " << obj_m << ".provides.build\n";
  381. }
  382. return true;
  383. }
  384. bool cmDependsFortran::FindModule(std::string const& name, std::string& module)
  385. {
  386. // Construct possible names for the module file.
  387. std::string mod_upper;
  388. std::string mod_lower;
  389. cmFortranModuleAppendUpperLower(name, mod_upper, mod_lower);
  390. // Search the include path for the module.
  391. std::string fullName;
  392. for (std::string const& ip : this->IncludePath) {
  393. // Try the lower-case name.
  394. fullName = cmStrCat(ip, '/', mod_lower);
  395. if (cmSystemTools::FileExists(fullName, true)) {
  396. module = fullName;
  397. return true;
  398. }
  399. // Try the upper-case name.
  400. fullName = cmStrCat(ip, '/', mod_upper);
  401. if (cmSystemTools::FileExists(fullName, true)) {
  402. module = fullName;
  403. return true;
  404. }
  405. }
  406. return false;
  407. }
  408. bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
  409. {
  410. // Implements
  411. //
  412. // $(CMAKE_COMMAND) -E cmake_copy_f90_mod input.mod output.mod.stamp
  413. // [compiler-id]
  414. //
  415. // Note that the case of the .mod file depends on the compiler. In
  416. // the future this copy could also account for the fact that some
  417. // compilers include a timestamp in the .mod file so it changes even
  418. // when the interface described in the module does not.
  419. std::string mod = args[2];
  420. std::string stamp = args[3];
  421. std::string compilerId;
  422. if (args.size() >= 5) {
  423. compilerId = args[4];
  424. }
  425. if (!cmHasLiteralSuffix(mod, ".mod") && !cmHasLiteralSuffix(mod, ".smod") &&
  426. !cmHasLiteralSuffix(mod, ".sub")) {
  427. // Support depend.make files left by older versions of CMake.
  428. // They do not include the ".mod" extension.
  429. mod += ".mod";
  430. }
  431. std::string mod_dir = cmSystemTools::GetFilenamePath(mod);
  432. if (!mod_dir.empty()) {
  433. mod_dir += "/";
  434. }
  435. std::string mod_upper = mod_dir;
  436. std::string mod_lower = mod_dir;
  437. cmFortranModuleAppendUpperLower(cmSystemTools::GetFilenameName(mod),
  438. mod_upper, mod_lower);
  439. if (cmSystemTools::FileExists(mod_upper, true)) {
  440. if (cmDependsFortran::ModulesDiffer(mod_upper, stamp, compilerId)) {
  441. if (!cmSystemTools::CopyFileAlways(mod_upper, stamp)) {
  442. std::cerr << "Error copying Fortran module from \"" << mod_upper
  443. << "\" to \"" << stamp << "\".\n";
  444. return false;
  445. }
  446. }
  447. return true;
  448. }
  449. if (cmSystemTools::FileExists(mod_lower, true)) {
  450. if (cmDependsFortran::ModulesDiffer(mod_lower, stamp, compilerId)) {
  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 std::string& modFile,
  509. const std::string& stampFile,
  510. const std::string& 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 (compilerId == "SunPro") {
  540. return cmSystemTools::FilesDiffer(modFile, stampFile);
  541. }
  542. #if defined(_WIN32) || defined(__CYGWIN__)
  543. cmsys::ifstream finModFile(modFile.c_str(), std::ios::in | std::ios::binary);
  544. cmsys::ifstream finStampFile(stampFile.c_str(),
  545. std::ios::in | std::ios::binary);
  546. #else
  547. cmsys::ifstream finModFile(modFile.c_str());
  548. cmsys::ifstream finStampFile(stampFile.c_str());
  549. #endif
  550. if (!finModFile || !finStampFile) {
  551. // At least one of the files does not exist. The modules differ.
  552. return true;
  553. }
  554. /* Compilers which _do_ produce different mod content when the same
  555. * source is compiled twice
  556. * -GNU
  557. * -Intel
  558. *
  559. * Eat the stream content until all recompile only related changes
  560. * are left behind.
  561. */
  562. if (compilerId == "GNU") {
  563. // GNU Fortran 4.9 and later compress .mod files with gzip
  564. // but also do not include a date so we can fall through to
  565. // compare them without skipping any prefix.
  566. unsigned char hdr[2];
  567. bool okay = !finModFile.read(reinterpret_cast<char*>(hdr), 2).fail();
  568. finModFile.seekg(0);
  569. if (!okay || hdr[0] != 0x1f || hdr[1] != 0x8b) {
  570. const char seq[1] = { '\n' };
  571. const int seqlen = 1;
  572. if (!cmFortranStreamContainsSequence(finModFile, seq, seqlen)) {
  573. // The module is of unexpected format. Assume it is different.
  574. std::cerr << compilerId << " fortran module " << modFile
  575. << " has unexpected format." << std::endl;
  576. return true;
  577. }
  578. if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
  579. // The stamp must differ if the sequence is not contained.
  580. return true;
  581. }
  582. }
  583. } else if (compilerId == "Intel" || compilerId == "IntelLLVM") {
  584. const char seq[2] = { '\n', '\0' };
  585. const int seqlen = 2;
  586. // Skip the leading byte which appears to be a version number.
  587. // We do not need to check for an error because the sequence search
  588. // below will fail in that case.
  589. finModFile.get();
  590. finStampFile.get();
  591. if (!cmFortranStreamContainsSequence(finModFile, seq, seqlen)) {
  592. // The module is of unexpected format. Assume it is different.
  593. std::cerr << compilerId << " fortran module " << modFile
  594. << " has unexpected format." << std::endl;
  595. return true;
  596. }
  597. if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
  598. // The stamp must differ if the sequence is not contained.
  599. return true;
  600. }
  601. }
  602. // Compare the remaining content. If no compiler id matched above,
  603. // including the case none was given, this will compare the whole
  604. // content.
  605. return cmFortranStreamsDiffer(finModFile, finStampFile);
  606. }