cmDependsFortran.cxx 24 KB

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