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"
11 #include "cmsys/FStream.hxx"
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"
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.
27 static void cmFortranModuleAppendUpperLower(std::string const& mod,
28 std::string& mod_upper,
29 std::string& mod_lower)
31 std::string::size_type ext_len = 0;
32 if (cmHasLiteralSuffix(mod, ".mod") || cmHasLiteralSuffix(mod, ".sub")) {
34 } else if (cmHasLiteralSuffix(mod, ".smod")) {
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;
43 class cmDependsFortranInternals
46 // The set of modules provided by this target.
47 std::set<std::string> TargetProvides;
49 // Map modules required by this target to locations.
50 using TargetRequiresMap = std::map<std::string, std::string>;
51 TargetRequiresMap TargetRequires;
53 // Information about each object file.
54 using ObjectInfoMap = std::map<std::string, cmFortranSourceInfo>;
55 ObjectInfoMap ObjectInfo;
57 cmFortranSourceInfo& CreateObjectInfo(const std::string& obj,
58 const std::string& src)
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;
71 cmDependsFortran::cmDependsFortran() = default;
73 cmDependsFortran::cmDependsFortran(cmLocalUnixMakefileGenerator3* lg)
75 , Internal(new cmDependsFortranInternals)
77 // Configure the include file search path.
78 this->SetIncludePathFromLanguage("Fortran");
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);
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);
92 this->PPDefinitions.insert(def);
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");
100 cmDependsFortran::~cmDependsFortran() = default;
102 bool cmDependsFortran::WriteDependencies(const std::set<std::string>& sources,
103 const std::string& obj,
104 std::ostream& /*makeDepends*/,
105 std::ostream& /*internalDepends*/)
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.");
113 cmSystemTools::Error("Cannot scan dependencies without an object file.");
117 cmFortranCompiler fc;
118 fc.Id = this->CompilerId;
119 fc.SModSep = this->SModSep;
120 fc.SModExt = this->SModExt;
123 for (std::string const& src : sources) {
124 // Get the information object for this source.
125 cmFortranSourceInfo& info = this->Internal->CreateObjectInfo(obj, src);
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);
131 // Push on the starting file.
132 cmFortranParser_FilePush(&parser, src.c_str());
134 // Parse the translation unit.
135 if (cmFortran_yyparse(parser.Scanner) != 0) {
136 // Failed to parse the file. Report failure to write dependencies.
138 /* clang-format off */
140 "warning: failed to parse dependencies from Fortran source "
141 "'" << src << "': " << parser.Error << std::endl
143 /* clang-format on */
149 bool cmDependsFortran::Finalize(std::ostream& makeDepends,
150 std::ostream& internalDepends)
152 // Prepare the module search process.
153 this->LocateModules();
155 // Get the directory in which stamp files will be stored.
156 const std::string& stamp_dir = this->TargetDirectory;
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();
166 bool building_intrinsics =
167 !mf->GetSafeDefinition("CMAKE_Fortran_TARGET_BUILDING_INSTRINSIC_MODULES")
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)) {
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';
191 // Create a script to clean the modules.
192 if (!provides.empty()) {
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");
205 << this->LocalGenerator->MaybeRelativeToCurBinDir(mod_lower)
208 << this->LocalGenerator->MaybeRelativeToCurBinDir(mod_upper)
211 << this->LocalGenerator->MaybeRelativeToCurBinDir(stamp)
219 void cmDependsFortran::LocateModules()
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());
230 for (std::string const& r : info.Requires) {
231 this->Internal->TargetRequires[r].clear();
235 // Short-circuit for simple targets.
236 if (this->Internal->TargetRequires.empty()) {
240 // Match modules provided by this target to those it requires.
241 this->MatchLocalModules();
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());
252 this->MatchRemoteModules(fin, targetDir);
257 void cmDependsFortran::MatchLocalModules()
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);
266 void cmDependsFortran::MatchRemoteModules(std::istream& fin,
267 const std::string& stampDir)
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') {
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.
287 this->ConsiderModule(mod.substr(1), stampDir);
289 } else if (line == "provides") {
290 doing_provides = true;
292 doing_provides = false;
297 void cmDependsFortran::ConsiderModule(const std::string& name,
298 const std::string& stampDir)
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;
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)
318 // Get the source file for this object.
319 std::string const& src = info.Source;
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 << ':';
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;
341 makeDepends << obj_m << ": " << dependee << '\n';
343 internalDepends << ' ' << i << '\n';
348 std::set<std::string> req = info.Requires;
349 if (buildingIntrinsics) {
350 req.insert(info.Intrinsics.begin(), info.Intrinsics.end());
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()) {
360 // The object file should depend on timestamped files for the
362 auto required = this->Internal->TargetRequires.find(i);
363 if (required == this->Internal->TargetRequires.end()) {
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';
372 // This module is not known to CMake. Try to locate it where
373 // the compiler will and depend on that.
375 if (this->FindModule(i, module)) {
376 module = cmSystemTools::ConvertToOutputPath(
377 this->LocalGenerator->MaybeRelativeToTopBinDir(module));
378 makeDepends << obj_m << ": " << module << '\n';
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
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);
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);
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;
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";
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";
441 bool cmDependsFortran::FindModule(std::string const& name, std::string& module)
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);
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)) {
458 // Try the upper-case name.
459 fullName = cmStrCat(ip, '/', mod_upper);
460 if (cmSystemTools::FileExists(fullName, true)) {
468 bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
472 // $(CMAKE_COMMAND) -E cmake_copy_f90_mod input.mod output.mod.stamp
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.
480 std::string mod = args[2];
481 std::string const& stamp = args[3];
482 std::string compilerId;
483 if (args.size() >= 5) {
484 compilerId = args[4];
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.
492 std::string mod_dir = cmSystemTools::GetFilenamePath(mod);
493 if (!mod_dir.empty()) {
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";
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";
521 std::cerr << "Error copying Fortran module \"" << args[2] << "\". Tried \""
522 << mod_upper << "\" and \"" << mod_lower << "\".\n";
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.
529 static bool cmFortranStreamContainsSequence(std::istream& ifs, const char* seq,
536 // Get the next character.
537 int token = ifs.get();
542 // Check the character.
543 if (token == static_cast<int>(seq[cur])) {
546 // Assume the sequence has no repeating subsequence.
551 // The entire sequence was matched.
555 // Helper function to compare the remaining content in two streams.
556 static bool cmFortranStreamsDiffer(std::istream& ifs1, std::istream& ifs2)
558 // Compare the remaining content.
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.
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.
578 bool cmDependsFortran::ModulesDiffer(const std::string& modFile,
579 const std::string& stampFile,
580 const std::string& compilerId)
584 A mod file is an ascii file compressed with gzip.
585 Compiling twice produces identical modules.
588 A mod file is an ascii file.
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.
594 As you can see the first line contains the date.
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
604 A mod file is a binary file. Compiling twice produces identical modules.
610 /* Compilers which do _not_ produce different mod content when the same
611 * source is compiled twice
614 if (compilerId == "SunPro") {
615 return cmSystemTools::FilesDiffer(modFile, stampFile);
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);
623 cmsys::ifstream finModFile(modFile.c_str());
624 cmsys::ifstream finStampFile(stampFile.c_str());
626 if (!finModFile || !finStampFile) {
627 // At least one of the files does not exist. The modules differ.
631 /* Compilers which _do_ produce different mod content when the same
632 * source is compiled twice
636 * Eat the stream content until all recompile only related changes
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();
646 if (!okay || hdr[0] != 0x1f || hdr[1] != 0x8b) {
647 const char seq[1] = { '\n' };
648 const int seqlen = 1;
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;
657 if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
658 // The stamp must differ if the sequence is not contained.
662 } else if (compilerId == "Intel" || compilerId == "IntelLLVM") {
663 const char seq[2] = { '\n', '\0' };
664 const int seqlen = 2;
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.
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;
679 if (!cmFortranStreamContainsSequence(finStampFile, seq, seqlen)) {
680 // The stamp must differ if the sequence is not contained.
685 // Compare the remaining content. If no compiler id matched above,
686 // including the case none was given, this will compare the whole
688 return cmFortranStreamsDiffer(finModFile, finStampFile);