cmDependsFortran.cxx 24 KB

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