cmDependsFortran.cxx 24 KB

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