cmDependsFortran.cxx 24 KB

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