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