cmDependsFortran.cxx 24 KB

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