cmDependsFortran.cxx 24 KB

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