cmDependsFortran.cxx 24 KB

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