1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmDependsFortran.h"
14 #include "cmSystemTools.h"
15 #include "cmLocalGenerator.h"
16 #include "cmMakefile.h"
17 #include "cmGeneratedFileStream.h"
19 #include "cmDependsFortranParser.h" /* Interface to parser object. */
24 // TODO: Test compiler for the case of the mod file. Some always
25 // use lower case and some always use upper case. I do not know if any
26 // use the case from the source code.
28 //----------------------------------------------------------------------------
29 // Information about a single source file.
30 class cmDependsFortranSourceInfo
33 // The name of the source file.
36 // Set of provided and required modules.
37 std::set<cmStdString> Provides;
38 std::set<cmStdString> Requires;
40 // Set of files included in the translation unit.
41 std::set<cmStdString> Includes;
44 //----------------------------------------------------------------------------
45 // Parser methods not included in generated interface.
47 // Get the current buffer processed by the lexer.
48 YY_BUFFER_STATE cmDependsFortranLexer_GetCurrentBuffer(yyscan_t yyscanner);
50 // The parser entry point.
51 int cmDependsFortran_yyparse(yyscan_t);
53 //----------------------------------------------------------------------------
54 // Define parser object internal structure.
55 struct cmDependsFortranFile
57 cmDependsFortranFile(FILE* file, YY_BUFFER_STATE buffer,
58 const std::string& dir):
59 File(file), Buffer(buffer), Directory(dir) {}
61 YY_BUFFER_STATE Buffer;
62 std::string Directory;
65 struct cmDependsFortranParser_s
67 cmDependsFortranParser_s(cmDependsFortran* self,
68 std::set<std::string>& ppDefines,
69 cmDependsFortranSourceInfo& info);
70 ~cmDependsFortranParser_s();
72 // Pointer back to the main class.
73 cmDependsFortran* Self;
75 // Lexical scanner instance.
78 // Stack of open files in the translation unit.
79 std::stack<cmDependsFortranFile> FileStack;
81 // Buffer for string literals.
82 std::string TokenString;
84 // Flag for whether lexer is reading from inside an interface.
88 std::set<std::string> PPDefinitions;
89 size_t InPPFalseBranch;
90 std::stack<bool> SkipToEnd;
92 // Information about the parsed source.
93 cmDependsFortranSourceInfo& Info;
96 //----------------------------------------------------------------------------
97 class cmDependsFortranInternals
100 // The set of modules provided by this target.
101 std::set<cmStdString> TargetProvides;
103 // Map modules required by this target to locations.
104 typedef std::map<cmStdString, cmStdString> TargetRequiresMap;
105 TargetRequiresMap TargetRequires;
107 // Information about each object file.
108 typedef std::map<cmStdString, cmDependsFortranSourceInfo> ObjectInfoMap;
109 ObjectInfoMap ObjectInfo;
111 cmDependsFortranSourceInfo& CreateObjectInfo(const char* obj,
114 std::map<cmStdString, cmDependsFortranSourceInfo>::iterator i =
115 this->ObjectInfo.find(obj);
116 if(i == this->ObjectInfo.end())
118 std::map<cmStdString, cmDependsFortranSourceInfo>::value_type
119 entry(obj, cmDependsFortranSourceInfo());
120 i = this->ObjectInfo.insert(entry).first;
121 i->second.Source = src;
127 //----------------------------------------------------------------------------
128 cmDependsFortran::cmDependsFortran():
129 PPDefinitions(0), Internal(0)
133 //----------------------------------------------------------------------------
135 ::cmDependsFortran(cmLocalGenerator* lg):
137 Internal(new cmDependsFortranInternals)
139 // Configure the include file search path.
140 this->SetIncludePathFromLanguage("Fortran");
142 // Get the list of definitions.
143 std::vector<std::string> definitions;
144 cmMakefile* mf = this->LocalGenerator->GetMakefile();
145 if(const char* c_defines =
146 mf->GetDefinition("CMAKE_TARGET_DEFINITIONS"))
148 cmSystemTools::ExpandListArgument(c_defines, definitions);
151 // translate i.e. FOO=BAR to FOO and add it to the list of defined
152 // preprocessor symbols
153 for(std::vector<std::string>::const_iterator
154 it = definitions.begin(); it != definitions.end(); ++it)
156 std::string def = *it;
157 std::string::size_type assignment = def.find("=");
158 if(assignment != std::string::npos)
160 def = it->substr(0, assignment);
162 this->PPDefinitions.push_back(def);
166 //----------------------------------------------------------------------------
167 cmDependsFortran::~cmDependsFortran()
169 delete this->Internal;
172 //----------------------------------------------------------------------------
173 bool cmDependsFortran::WriteDependencies(
174 const std::set<std::string>& sources, const std::string& obj,
175 std::ostream&, std::ostream&)
177 // Make sure this is a scanning instance.
178 if(sources.empty() || sources.begin()->empty())
180 cmSystemTools::Error("Cannot scan dependencies without a source file.");
185 cmSystemTools::Error("Cannot scan dependencies without an object file.");
190 for(std::set<std::string>::const_iterator it = sources.begin();
191 it != sources.end(); ++it)
193 const std::string& src = *it;
194 // Get the information object for this source.
195 cmDependsFortranSourceInfo& info =
196 this->Internal->CreateObjectInfo(obj.c_str(), src.c_str());
198 // Make a copy of the macros defined via ADD_DEFINITIONS
199 std::set<std::string> ppDefines(this->PPDefinitions.begin(),
200 this->PPDefinitions.end());
202 // Create the parser object. The constructor takes ppMacro and info per
203 // reference, so we may look into the resulting objects later.
204 cmDependsFortranParser parser(this, ppDefines, info);
206 // Push on the starting file.
207 cmDependsFortranParser_FilePush(&parser, src.c_str());
209 // Parse the translation unit.
210 if(cmDependsFortran_yyparse(parser.Scanner) != 0)
212 // Failed to parse the file. Report failure to write dependencies.
219 //----------------------------------------------------------------------------
220 bool cmDependsFortran::Finalize(std::ostream& makeDepends,
221 std::ostream& internalDepends)
223 // Prepare the module search process.
224 this->LocateModules();
226 // Get the directory in which stamp files will be stored.
227 const char* stamp_dir = this->TargetDirectory.c_str();
229 // Get the directory in which module files will be created.
231 cmMakefile* mf = this->LocalGenerator->GetMakefile();
232 if(const char* target_mod_dir =
233 mf->GetDefinition("CMAKE_Fortran_TARGET_MODULE_DIR"))
235 mod_dir = target_mod_dir;
240 this->LocalGenerator->GetMakefile()->GetCurrentOutputDirectory();
243 // Actually write dependencies to the streams.
244 typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
245 ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
246 for(ObjectInfoMap::const_iterator i = objInfo.begin();
247 i != objInfo.end(); ++i)
249 if(!this->WriteDependenciesReal(i->first.c_str(), i->second,
251 makeDepends, internalDepends))
257 // Store the list of modules provided by this target.
258 std::string fiName = this->TargetDirectory;
259 fiName += "/fortran.internal";
260 cmGeneratedFileStream fiStream(fiName.c_str());
261 fiStream << "# The fortran modules provided by this target.\n";
262 fiStream << "provides\n";
263 std::set<cmStdString> const& provides = this->Internal->TargetProvides;
264 for(std::set<cmStdString>::const_iterator i = provides.begin();
265 i != provides.end(); ++i)
267 fiStream << " " << *i << "\n";
270 // Create a script to clean the modules.
271 if(!provides.empty())
273 std::string fcName = this->TargetDirectory;
274 fcName += "/cmake_clean_Fortran.cmake";
275 cmGeneratedFileStream fcStream(fcName.c_str());
276 fcStream << "# Remove fortran modules provided by this target.\n";
277 fcStream << "FILE(REMOVE";
278 for(std::set<cmStdString>::const_iterator i = provides.begin();
279 i != provides.end(); ++i)
281 std::string mod_upper = mod_dir;
283 mod_upper += cmSystemTools::UpperCase(*i);
285 std::string mod_lower = mod_dir;
289 std::string stamp = stamp_dir;
292 stamp += ".mod.stamp";
295 this->LocalGenerator->Convert(mod_lower.c_str(),
296 cmLocalGenerator::START_OUTPUT)
299 this->LocalGenerator->Convert(mod_upper.c_str(),
300 cmLocalGenerator::START_OUTPUT)
303 this->LocalGenerator->Convert(stamp.c_str(),
304 cmLocalGenerator::START_OUTPUT)
312 //----------------------------------------------------------------------------
313 void cmDependsFortran::LocateModules()
315 // Collect the set of modules provided and required by all sources.
316 typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
317 ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
318 for(ObjectInfoMap::const_iterator infoI = objInfo.begin();
319 infoI != objInfo.end(); ++infoI)
321 cmDependsFortranSourceInfo const& info = infoI->second;
322 for(std::set<cmStdString>::const_iterator i = info.Provides.begin();
323 i != info.Provides.end(); ++i)
325 // Include this module in the set provided by this target.
326 this->Internal->TargetProvides.insert(*i);
329 for(std::set<cmStdString>::const_iterator i = info.Requires.begin();
330 i != info.Requires.end(); ++i)
332 // Include this module in the set required by this target.
333 this->Internal->TargetRequires[*i] = "";
337 // Short-circuit for simple targets.
338 if(this->Internal->TargetRequires.empty())
343 // Match modules provided by this target to those it requires.
344 this->MatchLocalModules();
346 // Load information about other targets.
347 cmMakefile* mf = this->LocalGenerator->GetMakefile();
348 std::vector<std::string> infoFiles;
349 if(const char* infoFilesValue =
350 mf->GetDefinition("CMAKE_TARGET_LINKED_INFO_FILES"))
352 cmSystemTools::ExpandListArgument(infoFilesValue, infoFiles);
354 for(std::vector<std::string>::const_iterator i = infoFiles.begin();
355 i != infoFiles.end(); ++i)
357 std::string targetDir = cmSystemTools::GetFilenamePath(*i);
358 std::string fname = targetDir + "/fortran.internal";
359 std::ifstream fin(fname.c_str());
362 this->MatchRemoteModules(fin, targetDir.c_str());
367 //----------------------------------------------------------------------------
368 void cmDependsFortran::MatchLocalModules()
370 const char* stampDir = this->TargetDirectory.c_str();
371 std::set<cmStdString> const& provides = this->Internal->TargetProvides;
372 for(std::set<cmStdString>::const_iterator i = provides.begin();
373 i != provides.end(); ++i)
375 this->ConsiderModule(i->c_str(), stampDir);
379 //----------------------------------------------------------------------------
380 void cmDependsFortran::MatchRemoteModules(std::istream& fin,
381 const char* stampDir)
384 bool doing_provides = false;
385 while(cmSystemTools::GetLineFromStream(fin, line))
387 // Ignore comments and empty lines.
388 if(line.empty() || line[0] == '#' || line[0] == '\r')
397 this->ConsiderModule(line.c_str()+1, stampDir);
400 else if(line == "provides")
402 doing_provides = true;
406 doing_provides = false;
411 //----------------------------------------------------------------------------
412 void cmDependsFortran::ConsiderModule(const char* name,
413 const char* stampDir)
415 // Locate each required module.
416 typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
417 TargetRequiresMap::iterator required =
418 this->Internal->TargetRequires.find(name);
419 if(required != this->Internal->TargetRequires.end() &&
420 required->second.empty())
422 // The module is provided by a CMake target. It will have a stamp file.
423 std::string stampFile = stampDir;
426 stampFile += ".mod.stamp";
427 required->second = stampFile;
431 //----------------------------------------------------------------------------
434 ::WriteDependenciesReal(const char *obj,
435 cmDependsFortranSourceInfo const& info,
436 const char* mod_dir, const char* stamp_dir,
437 std::ostream& makeDepends,
438 std::ostream& internalDepends)
440 typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
442 // Get the source file for this object.
443 const char* src = info.Source.c_str();
445 // Write the include dependencies to the output stream.
446 internalDepends << obj << std::endl;
447 internalDepends << " " << src << std::endl;
448 for(std::set<cmStdString>::const_iterator i = info.Includes.begin();
449 i != info.Includes.end(); ++i)
451 makeDepends << obj << ": " <<
452 this->LocalGenerator->Convert(i->c_str(),
453 cmLocalGenerator::HOME_OUTPUT,
454 cmLocalGenerator::MAKEFILE)
456 internalDepends << " " << i->c_str() << std::endl;
458 makeDepends << std::endl;
460 // Write module requirements to the output stream.
461 for(std::set<cmStdString>::const_iterator i = info.Requires.begin();
462 i != info.Requires.end(); ++i)
464 // Require only modules not provided in the same source.
465 if(std::set<cmStdString>::const_iterator(info.Provides.find(*i)) !=
471 // If the module is provided in this target special handling is
473 if(this->Internal->TargetProvides.find(*i) !=
474 this->Internal->TargetProvides.end())
476 // The module is provided by a different source in the same
477 // target. Add the proxy dependency to make sure the other
478 // source builds first.
479 std::string proxy = stamp_dir;
482 proxy += ".mod.proxy";
483 proxy = this->LocalGenerator->Convert(proxy.c_str(),
484 cmLocalGenerator::HOME_OUTPUT,
485 cmLocalGenerator::MAKEFILE);
487 // since we require some things add them to our list of requirements
488 makeDepends << obj << ".requires: " << proxy << std::endl;
491 // The object file should depend on timestamped files for the
493 TargetRequiresMap::const_iterator required =
494 this->Internal->TargetRequires.find(*i);
495 if(required == this->Internal->TargetRequires.end()) { abort(); }
496 if(!required->second.empty())
498 // This module is known. Depend on its timestamp file.
499 std::string stampFile =
500 this->LocalGenerator->Convert(required->second.c_str(),
501 cmLocalGenerator::HOME_OUTPUT,
502 cmLocalGenerator::MAKEFILE);
503 makeDepends << obj << ": " << stampFile << "\n";
507 // This module is not known to CMake. Try to locate it where
508 // the compiler will and depend on that.
510 if(this->FindModule(*i, module))
513 this->LocalGenerator->Convert(module.c_str(),
514 cmLocalGenerator::HOME_OUTPUT,
515 cmLocalGenerator::MAKEFILE);
516 makeDepends << obj << ": " << module << "\n";
521 // Write provided modules to the output stream.
522 for(std::set<cmStdString>::const_iterator i = info.Provides.begin();
523 i != info.Provides.end(); ++i)
525 std::string proxy = stamp_dir;
528 proxy += ".mod.proxy";
529 proxy = this->LocalGenerator->Convert(proxy.c_str(),
530 cmLocalGenerator::HOME_OUTPUT,
531 cmLocalGenerator::MAKEFILE);
532 makeDepends << proxy << ": " << obj << ".provides" << std::endl;
535 // If any modules are provided then they must be converted to stamp files.
536 if(!info.Provides.empty())
538 // Create a target to copy the module after the object file
540 makeDepends << obj << ".provides.build:\n";
541 for(std::set<cmStdString>::const_iterator i = info.Provides.begin();
542 i != info.Provides.end(); ++i)
544 // Include this module in the set provided by this target.
545 this->Internal->TargetProvides.insert(*i);
547 // Always use lower case for the mod stamp file name. The
548 // cmake_copy_f90_mod will call back to this class, which will
549 // try various cases for the real mod file name.
550 std::string m = cmSystemTools::LowerCase(*i);
551 std::string modFile = mod_dir;
555 this->LocalGenerator->Convert(modFile.c_str(),
556 cmLocalGenerator::HOME_OUTPUT,
557 cmLocalGenerator::SHELL);
558 std::string stampFile = stamp_dir;
561 stampFile += ".mod.stamp";
563 this->LocalGenerator->Convert(stampFile.c_str(),
564 cmLocalGenerator::HOME_OUTPUT,
565 cmLocalGenerator::SHELL);
566 makeDepends << "\t$(CMAKE_COMMAND) -E cmake_copy_f90_mod "
567 << modFile << " " << stampFile;
568 cmMakefile* mf = this->LocalGenerator->GetMakefile();
569 const char* cid = mf->GetDefinition("CMAKE_Fortran_COMPILER_ID");
572 makeDepends << " " << cid;
576 // After copying the modules update the timestamp file so that
577 // copying will not be done again until the source rebuilds.
578 makeDepends << "\t$(CMAKE_COMMAND) -E touch " << obj
579 << ".provides.build\n";
581 // Make sure the module timestamp rule is evaluated by the time
582 // the target finishes building.
583 std::string driver = this->TargetDirectory;
585 driver = this->LocalGenerator->Convert(driver.c_str(),
586 cmLocalGenerator::HOME_OUTPUT,
587 cmLocalGenerator::MAKEFILE);
588 makeDepends << driver << ": " << obj << ".provides.build\n";
594 //----------------------------------------------------------------------------
595 bool cmDependsFortran::FindModule(std::string const& name,
598 // Construct possible names for the module file.
599 std::string mod_upper = cmSystemTools::UpperCase(name);
600 std::string mod_lower = name;
604 // Search the include path for the module.
605 std::string fullName;
606 for(std::vector<std::string>::const_iterator i =
607 this->IncludePath.begin(); i != this->IncludePath.end(); ++i)
609 // Try the lower-case name.
612 fullName += mod_lower;
613 if(cmSystemTools::FileExists(fullName.c_str(), true))
619 // Try the upper-case name.
622 fullName += mod_upper;
623 if(cmSystemTools::FileExists(fullName.c_str(), true))
632 //----------------------------------------------------------------------------
633 bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
637 // $(CMAKE_COMMAND) -E cmake_copy_f90_mod input.mod output.mod.stamp
640 // Note that the case of the .mod file depends on the compiler. In
641 // the future this copy could also account for the fact that some
642 // compilers include a timestamp in the .mod file so it changes even
643 // when the interface described in the module does not.
645 std::string mod = args[2];
646 std::string stamp = args[3];
647 std::string compilerId;
650 compilerId = args[4];
652 std::string mod_dir = cmSystemTools::GetFilenamePath(mod);
653 if(!mod_dir.empty()) { mod_dir += "/"; }
654 std::string mod_upper = mod_dir;
655 mod_upper += cmSystemTools::UpperCase(cmSystemTools::GetFilenameName(mod));
656 std::string mod_lower = mod_dir;
657 mod_lower += cmSystemTools::LowerCase(cmSystemTools::GetFilenameName(mod));
661 if(cmSystemTools::FileExists(mod_upper.c_str(), true))
663 if(cmDependsFortran::ModulesDiffer(mod_upper.c_str(), stamp.c_str(),
666 if(!cmSystemTools::CopyFileAlways(mod_upper.c_str(), stamp.c_str()))
668 std::cerr << "Error copying Fortran module from \""
669 << mod_upper.c_str() << "\" to \"" << stamp.c_str()
676 else if(cmSystemTools::FileExists(mod_lower.c_str(), true))
678 if(cmDependsFortran::ModulesDiffer(mod_lower.c_str(), stamp.c_str(),
681 if(!cmSystemTools::CopyFileAlways(mod_lower.c_str(), stamp.c_str()))
683 std::cerr << "Error copying Fortran module from \""
684 << mod_lower.c_str() << "\" to \"" << stamp.c_str()
692 std::cerr << "Error copying Fortran module \"" << args[2].c_str()
693 << "\". Tried \"" << mod_upper.c_str()
694 << "\" and \"" << mod_lower.c_str() << "\".\n";
698 //----------------------------------------------------------------------------
699 // Helper function to look for a short sequence in a stream. If this
700 // is later used for longer sequences it should be re-written using an
701 // efficient string search algorithm such as Boyer-Moore.
703 bool cmDependsFortranStreamContainsSequence(std::ifstream& ifs,
704 const char* seq, int len)
711 // Get the next character.
712 int token = ifs.get();
718 // Check the character.
719 if(token == static_cast<int>(seq[cur]))
725 // Assume the sequence has no repeating subsequence.
730 // The entire sequence was matched.
734 //----------------------------------------------------------------------------
735 // Helper function to compare the remaining content in two streams.
736 static bool cmDependsFortranStreamsDiffer(std::ifstream& ifs1,
739 // Compare the remaining content.
742 int ifs1_c = ifs1.get();
743 int ifs2_c = ifs2.get();
746 // We have reached the end of both streams simultaneously.
747 // The streams are identical.
751 if(!ifs1 || !ifs2 || ifs1_c != ifs2_c)
753 // We have reached the end of one stream before the other or
754 // found differing content. The streams are different.
762 //----------------------------------------------------------------------------
763 bool cmDependsFortran::ModulesDiffer(const char* modFile,
764 const char* stampFile,
765 const char* compilerId)
769 A mod file is an ascii file.
771 FORTRAN module created from /path/to/foo.f90 on Sun Dec 30 22:47:58 2007
772 If you edit this, you'll get what you deserve.
775 As you can see the first line contains the date.
778 A mod file is a binary file.
779 However, looking into both generated bar.mod files with a hex editor
780 shows that they differ only before a sequence linefeed-zero (0x0A 0x00)
781 which is located some bytes in front of the absoulte path to the source
785 A mod file is a binary file. Compiling twice produces identical modules.
792 /* Compilers which do _not_ produce different mod content when the same
793 * source is compiled twice
796 if(strcmp(compilerId, "SunPro") == 0)
798 return cmSystemTools::FilesDiffer(modFile, stampFile);
801 #if defined(_WIN32) || defined(__CYGWIN__)
802 std::ifstream finModFile(modFile, std::ios::in | std::ios::binary);
803 std::ifstream finStampFile(stampFile, std::ios::in | std::ios::binary);
805 std::ifstream finModFile(modFile, std::ios::in);
806 std::ifstream finStampFile(stampFile, std::ios::in);
808 if(!finModFile || !finStampFile)
810 // At least one of the files does not exist. The modules differ.
814 /* Compilers which _do_ produce different mod content when the same
815 * source is compiled twice
819 * Eat the stream content until all recompile only related changes
822 if (strcmp(compilerId, "GNU") == 0 )
824 const char seq[1] = {'\n'};
825 const int seqlen = 1;
827 if(!cmDependsFortranStreamContainsSequence(finModFile, seq, seqlen))
829 // The module is of unexpected format. Assume it is different.
830 std::cerr << compilerId << " fortran module " << modFile
831 << " has unexpected format." << std::endl;
835 if(!cmDependsFortranStreamContainsSequence(finStampFile, seq, seqlen))
837 // The stamp must differ if the sequence is not contained.
841 else if(strcmp(compilerId, "Intel") == 0)
843 const char seq[2] = {'\n', '\0'};
844 const int seqlen = 2;
846 if(!cmDependsFortranStreamContainsSequence(finModFile, seq, seqlen))
848 // The module is of unexpected format. Assume it is different.
849 std::cerr << compilerId << " fortran module " << modFile
850 << " has unexpected format." << std::endl;
854 if(!cmDependsFortranStreamContainsSequence(finStampFile, seq, seqlen))
856 // The stamp must differ if the sequence is not contained.
861 // Compare the remaining content. If no compiler id matched above,
862 // including the case none was given, this will compare the whole
864 if(!cmDependsFortranStreamsDiffer(finModFile, finStampFile))
869 // The modules are different.
873 //----------------------------------------------------------------------------
874 bool cmDependsFortran::FindIncludeFile(const char* dir,
875 const char* includeName,
876 std::string& fileName)
878 // If the file is a full path, include it directly.
879 if(cmSystemTools::FileIsFullPath(includeName))
881 fileName = includeName;
882 return cmSystemTools::FileExists(fileName.c_str(), true);
886 // Check for the file in the directory containing the including
888 std::string fullName = dir;
890 fullName += includeName;
891 if(cmSystemTools::FileExists(fullName.c_str(), true))
897 // Search the include path for the file.
898 for(std::vector<std::string>::const_iterator i =
899 this->IncludePath.begin(); i != this->IncludePath.end(); ++i)
903 fullName += includeName;
904 if(cmSystemTools::FileExists(fullName.c_str(), true))
914 //----------------------------------------------------------------------------
915 cmDependsFortranParser_s
916 ::cmDependsFortranParser_s(cmDependsFortran* self,
917 std::set<std::string>& ppDefines,
918 cmDependsFortranSourceInfo& info):
919 Self(self), PPDefinitions(ppDefines), Info(info)
921 this->InInterface = 0;
922 this->InPPFalseBranch = 0;
924 // Initialize the lexical scanner.
925 cmDependsFortran_yylex_init(&this->Scanner);
926 cmDependsFortran_yyset_extra(this, this->Scanner);
928 // Create a dummy buffer that is never read but is the fallback
929 // buffer when the last file is popped off the stack.
930 YY_BUFFER_STATE buffer =
931 cmDependsFortran_yy_create_buffer(0, 4, this->Scanner);
932 cmDependsFortran_yy_switch_to_buffer(buffer, this->Scanner);
935 //----------------------------------------------------------------------------
936 cmDependsFortranParser_s::~cmDependsFortranParser_s()
938 cmDependsFortran_yylex_destroy(this->Scanner);
941 //----------------------------------------------------------------------------
942 bool cmDependsFortranParser_FilePush(cmDependsFortranParser* parser,
945 // Open the new file and push it onto the stack. Save the old
946 // buffer with it on the stack.
947 if(FILE* file = fopen(fname, "rb"))
949 YY_BUFFER_STATE current =
950 cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
951 std::string dir = cmSystemTools::GetParentDirectory(fname);
952 cmDependsFortranFile f(file, current, dir);
953 YY_BUFFER_STATE buffer =
954 cmDependsFortran_yy_create_buffer(0, 16384, parser->Scanner);
955 cmDependsFortran_yy_switch_to_buffer(buffer, parser->Scanner);
956 parser->FileStack.push(f);
965 //----------------------------------------------------------------------------
966 bool cmDependsFortranParser_FilePop(cmDependsFortranParser* parser)
968 // Pop one file off the stack and close it. Switch the lexer back
969 // to the next one on the stack.
970 if(parser->FileStack.empty())
976 cmDependsFortranFile f = parser->FileStack.top(); parser->FileStack.pop();
978 YY_BUFFER_STATE current =
979 cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
980 cmDependsFortran_yy_delete_buffer(current, parser->Scanner);
981 cmDependsFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
986 //----------------------------------------------------------------------------
987 int cmDependsFortranParser_Input(cmDependsFortranParser* parser,
988 char* buffer, size_t bufferSize)
990 // Read from the file on top of the stack. If the stack is empty,
991 // the end of the translation unit has been reached.
992 if(!parser->FileStack.empty())
994 FILE* file = parser->FileStack.top().File;
995 return (int)fread(buffer, 1, bufferSize, file);
1000 //----------------------------------------------------------------------------
1001 void cmDependsFortranParser_StringStart(cmDependsFortranParser* parser)
1003 parser->TokenString = "";
1006 //----------------------------------------------------------------------------
1007 const char* cmDependsFortranParser_StringEnd(cmDependsFortranParser* parser)
1009 return parser->TokenString.c_str();
1012 //----------------------------------------------------------------------------
1013 void cmDependsFortranParser_StringAppend(cmDependsFortranParser* parser,
1016 parser->TokenString += c;
1019 //----------------------------------------------------------------------------
1020 void cmDependsFortranParser_SetInInterface(cmDependsFortranParser* parser,
1023 if(parser->InPPFalseBranch)
1028 parser->InInterface = in;
1031 //----------------------------------------------------------------------------
1032 bool cmDependsFortranParser_GetInInterface(cmDependsFortranParser* parser)
1034 return parser->InInterface;
1037 //----------------------------------------------------------------------------
1038 void cmDependsFortranParser_SetOldStartcond(cmDependsFortranParser* parser,
1041 parser->OldStartcond = arg;
1044 //----------------------------------------------------------------------------
1045 int cmDependsFortranParser_GetOldStartcond(cmDependsFortranParser* parser)
1047 return parser->OldStartcond;
1050 //----------------------------------------------------------------------------
1051 void cmDependsFortranParser_Error(cmDependsFortranParser*, const char*)
1053 // If there is a parser error just ignore it. The source will not
1054 // compile and the user will edit it. Then dependencies will have
1055 // to be regenerated anyway.
1058 //----------------------------------------------------------------------------
1059 void cmDependsFortranParser_RuleUse(cmDependsFortranParser* parser,
1062 if(!parser->InPPFalseBranch)
1064 parser->Info.Requires.insert(cmSystemTools::LowerCase(name) );
1068 //----------------------------------------------------------------------------
1069 void cmDependsFortranParser_RuleInclude(cmDependsFortranParser* parser,
1072 if(parser->InPPFalseBranch)
1077 // If processing an include statement there must be an open file.
1078 assert(!parser->FileStack.empty());
1080 // Get the directory containing the source in which the include
1081 // statement appears. This is always the first search location for
1082 // Fortran include files.
1083 std::string dir = parser->FileStack.top().Directory;
1085 // Find the included file. If it cannot be found just ignore the
1086 // problem because either the source will not compile or the user
1087 // does not care about depending on this included source.
1088 std::string fullName;
1089 if(parser->Self->FindIncludeFile(dir.c_str(), name, fullName))
1091 // Found the included file. Save it in the set of included files.
1092 parser->Info.Includes.insert(fullName);
1094 // Parse it immediately to translate the source inline.
1095 cmDependsFortranParser_FilePush(parser, fullName.c_str());
1099 //----------------------------------------------------------------------------
1100 void cmDependsFortranParser_RuleModule(cmDependsFortranParser* parser,
1103 if(!parser->InPPFalseBranch && !parser->InInterface)
1105 parser->Info.Provides.insert(cmSystemTools::LowerCase(name));
1109 //----------------------------------------------------------------------------
1110 void cmDependsFortranParser_RuleDefine(cmDependsFortranParser* parser,
1113 if(!parser->InPPFalseBranch)
1115 parser->PPDefinitions.insert(macro);
1119 //----------------------------------------------------------------------------
1120 void cmDependsFortranParser_RuleUndef(cmDependsFortranParser* parser,
1123 if(!parser->InPPFalseBranch)
1125 std::set<std::string>::iterator match;
1126 match = parser->PPDefinitions.find(macro);
1127 if(match != parser->PPDefinitions.end())
1129 parser->PPDefinitions.erase(match);
1134 //----------------------------------------------------------------------------
1135 void cmDependsFortranParser_RuleIfdef(cmDependsFortranParser* parser,
1138 // A new PP branch has been opened
1139 parser->SkipToEnd.push(false);
1141 if (parser->InPPFalseBranch)
1143 parser->InPPFalseBranch++;
1145 else if(parser->PPDefinitions.find(macro) == parser->PPDefinitions.end())
1147 parser->InPPFalseBranch=1;
1151 parser->SkipToEnd.top() = true;
1155 //----------------------------------------------------------------------------
1156 void cmDependsFortranParser_RuleIfndef(cmDependsFortranParser* parser,
1159 // A new PP branch has been opened
1160 parser->SkipToEnd.push(false);
1162 if (parser->InPPFalseBranch)
1164 parser->InPPFalseBranch++;
1166 else if(parser->PPDefinitions.find(macro) != parser->PPDefinitions.end())
1168 parser->InPPFalseBranch = 1;
1172 // ignore other branches
1173 parser->SkipToEnd.top() = true;
1177 //----------------------------------------------------------------------------
1178 void cmDependsFortranParser_RuleIf(cmDependsFortranParser* parser)
1180 /* Note: The current parser is _not_ able to get statements like
1184 * #if defined(MYSYMBOL)
1185 * #if defined(MYSYMBOL) && ...
1186 * right. The same for #elif. Thus in
1197 * _all_ N+1 branches are considered. If you got something like this
1198 * #if defined(MYSYMBOL)
1199 * #if !defined(MYSYMBOL)
1206 // A new PP branch has been opened
1207 // Never skip! See note above.
1208 parser->SkipToEnd.push(false);
1211 //----------------------------------------------------------------------------
1212 void cmDependsFortranParser_RuleElif(cmDependsFortranParser* parser)
1214 /* Note: There are parser limitations. See the note at
1215 * cmDependsFortranParser_RuleIf(..)
1218 // Always taken unless an #ifdef or #ifndef-branch has been taken
1219 // already. If the second condition isn't meet already
1220 // (parser->InPPFalseBranch == 0) correct it.
1221 if(!parser->SkipToEnd.empty() &&
1222 parser->SkipToEnd.top() && !parser->InPPFalseBranch)
1224 parser->InPPFalseBranch = 1;
1228 //----------------------------------------------------------------------------
1229 void cmDependsFortranParser_RuleElse(cmDependsFortranParser* parser)
1231 // if the parent branch is false do nothing!
1232 if(parser->InPPFalseBranch > 1)
1237 // parser->InPPFalseBranch is either 0 or 1. We change it depending on
1238 // parser->SkipToEnd.top()
1239 if(!parser->SkipToEnd.empty() &&
1240 parser->SkipToEnd.top())
1242 parser->InPPFalseBranch = 1;
1246 parser->InPPFalseBranch = 0;
1250 //----------------------------------------------------------------------------
1251 void cmDependsFortranParser_RuleEndif(cmDependsFortranParser* parser)
1253 if(!parser->SkipToEnd.empty())
1255 parser->SkipToEnd.pop();
1258 // #endif doesn't know if there was a "#else" in before, so it
1259 // always decreases InPPFalseBranch
1260 if(parser->InPPFalseBranch)
1262 parser->InPPFalseBranch--;