cmDependsFortran.cxx 24 KB

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