cmDependsFortran.cxx 24 KB

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