cmDependsFortran.cxx 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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 "cmSystemTools.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmDependsFortranParser.h" /* Interface to parser object. */
  16. #include <assert.h>
  17. #include <stack>
  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. //----------------------------------------------------------------------------
  22. // Information about a single source file.
  23. class cmDependsFortranSourceInfo
  24. {
  25. public:
  26. // The name of the soruce file.
  27. std::string Source;
  28. // Set of provided and required modules.
  29. std::set<cmStdString> Provides;
  30. std::set<cmStdString> Requires;
  31. // Set of files included in the translation unit.
  32. std::set<cmStdString> Includes;
  33. };
  34. //----------------------------------------------------------------------------
  35. // Parser methods not included in generated interface.
  36. // Get the current buffer processed by the lexer.
  37. YY_BUFFER_STATE cmDependsFortranLexer_GetCurrentBuffer(yyscan_t yyscanner);
  38. // The parser entry point.
  39. int cmDependsFortran_yyparse(yyscan_t);
  40. //----------------------------------------------------------------------------
  41. // Define parser object internal structure.
  42. struct cmDependsFortranFile
  43. {
  44. cmDependsFortranFile(FILE* file, YY_BUFFER_STATE buffer,
  45. const std::string& dir):
  46. File(file), Buffer(buffer), Directory(dir) {}
  47. FILE* File;
  48. YY_BUFFER_STATE Buffer;
  49. std::string Directory;
  50. };
  51. struct cmDependsFortranParser_s
  52. {
  53. cmDependsFortranParser_s(cmDependsFortran* self,
  54. std::set<std::string>& ppDefines,
  55. cmDependsFortranSourceInfo& info);
  56. ~cmDependsFortranParser_s();
  57. // Pointer back to the main class.
  58. cmDependsFortran* Self;
  59. // Lexical scanner instance.
  60. yyscan_t Scanner;
  61. // Stack of open files in the translation unit.
  62. std::stack<cmDependsFortranFile> FileStack;
  63. // Buffer for string literals.
  64. std::string TokenString;
  65. // Flag for whether lexer is reading from inside an interface.
  66. bool InInterface;
  67. int OldStartcond;
  68. std::set<std::string> PPDefinitions;
  69. size_t InPPFalseBranch;
  70. std::stack<bool> SkipToEnd;
  71. // Information about the parsed source.
  72. cmDependsFortranSourceInfo& Info;
  73. };
  74. //----------------------------------------------------------------------------
  75. class cmDependsFortranInternals
  76. {
  77. public:
  78. // The set of modules provided by this target.
  79. std::set<cmStdString> TargetProvides;
  80. // Map modules required by this target to locations.
  81. typedef std::map<cmStdString, cmStdString> TargetRequiresMap;
  82. TargetRequiresMap TargetRequires;
  83. // Information about each object file.
  84. typedef std::map<cmStdString, cmDependsFortranSourceInfo> ObjectInfoMap;
  85. ObjectInfoMap ObjectInfo;
  86. cmDependsFortranSourceInfo& CreateObjectInfo(const char* obj,
  87. const char* src)
  88. {
  89. std::map<cmStdString, cmDependsFortranSourceInfo>::iterator i =
  90. this->ObjectInfo.find(obj);
  91. if(i == this->ObjectInfo.end())
  92. {
  93. std::map<cmStdString, cmDependsFortranSourceInfo>::value_type
  94. entry(obj, cmDependsFortranSourceInfo());
  95. i = this->ObjectInfo.insert(entry).first;
  96. i->second.Source = src;
  97. }
  98. return i->second;
  99. }
  100. };
  101. //----------------------------------------------------------------------------
  102. cmDependsFortran::cmDependsFortran():
  103. PPDefinitions(0), Internal(0)
  104. {
  105. }
  106. //----------------------------------------------------------------------------
  107. cmDependsFortran
  108. ::cmDependsFortran(cmLocalGenerator* lg):
  109. cmDepends(lg),
  110. Internal(new cmDependsFortranInternals)
  111. {
  112. // Configure the include file search path.
  113. this->SetIncludePathFromLanguage("Fortran");
  114. // Get the list of definitions.
  115. std::vector<std::string> definitions;
  116. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  117. if(const char* c_defines =
  118. mf->GetDefinition("CMAKE_TARGET_DEFINITIONS"))
  119. {
  120. cmSystemTools::ExpandListArgument(c_defines, definitions);
  121. }
  122. // translate i.e. FOO=BAR to FOO and add it to the list of defined
  123. // preprocessor symbols
  124. for(std::vector<std::string>::const_iterator
  125. it = definitions.begin(); it != definitions.end(); ++it)
  126. {
  127. std::string def = *it;
  128. std::string::size_type assignment = def.find("=");
  129. if(assignment != std::string::npos)
  130. {
  131. def = it->substr(0, assignment);
  132. }
  133. this->PPDefinitions.push_back(def);
  134. }
  135. }
  136. //----------------------------------------------------------------------------
  137. cmDependsFortran::~cmDependsFortran()
  138. {
  139. delete this->Internal;
  140. }
  141. //----------------------------------------------------------------------------
  142. bool cmDependsFortran::WriteDependencies(const char *src, const char *obj,
  143. std::ostream&, std::ostream&)
  144. {
  145. // Make sure this is a scanning instance.
  146. if(!src || src[0] == '\0')
  147. {
  148. cmSystemTools::Error("Cannot scan dependencies without an source file.");
  149. return false;
  150. }
  151. if(!obj || obj[0] == '\0')
  152. {
  153. cmSystemTools::Error("Cannot scan dependencies without an object file.");
  154. return false;
  155. }
  156. // Get the information object for this source.
  157. cmDependsFortranSourceInfo& info =
  158. this->Internal->CreateObjectInfo(obj, src);
  159. // Make a copy of the macros defined via ADD_DEFINITIONS
  160. std::set<std::string> ppDefines(this->PPDefinitions.begin(),
  161. this->PPDefinitions.end());
  162. // Create the parser object. The constructor takes ppMacro and info per
  163. // reference, so we may look into the resulting objects later.
  164. cmDependsFortranParser parser(this, ppDefines, info);
  165. // Push on the starting file.
  166. cmDependsFortranParser_FilePush(&parser, src);
  167. // Parse the translation unit.
  168. if(cmDependsFortran_yyparse(parser.Scanner) != 0)
  169. {
  170. // Failed to parse the file. Report failure to write dependencies.
  171. return false;
  172. }
  173. return true;
  174. }
  175. //----------------------------------------------------------------------------
  176. bool cmDependsFortran::Finalize(std::ostream& makeDepends,
  177. std::ostream& internalDepends)
  178. {
  179. // Prepare the module search process.
  180. this->LocateModules();
  181. // Get the directory in which stamp files will be stored.
  182. const char* stamp_dir = this->TargetDirectory.c_str();
  183. // Get the directory in which module files will be created.
  184. const char* mod_dir;
  185. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  186. if(const char* target_mod_dir =
  187. mf->GetDefinition("CMAKE_Fortran_TARGET_MODULE_DIR"))
  188. {
  189. mod_dir = target_mod_dir;
  190. }
  191. else
  192. {
  193. mod_dir =
  194. this->LocalGenerator->GetMakefile()->GetCurrentOutputDirectory();
  195. }
  196. // Actually write dependencies to the streams.
  197. typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
  198. ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
  199. for(ObjectInfoMap::const_iterator i = objInfo.begin();
  200. i != objInfo.end(); ++i)
  201. {
  202. if(!this->WriteDependenciesReal(i->first.c_str(), i->second,
  203. mod_dir, stamp_dir,
  204. makeDepends, internalDepends))
  205. {
  206. return false;
  207. }
  208. }
  209. // Store the list of modules provided by this target.
  210. std::string fiName = this->TargetDirectory;
  211. fiName += "/fortran.internal";
  212. cmGeneratedFileStream fiStream(fiName.c_str());
  213. fiStream << "# The fortran modules provided by this target.\n";
  214. fiStream << "provides\n";
  215. std::set<cmStdString> const& provides = this->Internal->TargetProvides;
  216. for(std::set<cmStdString>::const_iterator i = provides.begin();
  217. i != provides.end(); ++i)
  218. {
  219. fiStream << " " << *i << "\n";
  220. }
  221. // Create a script to clean the modules.
  222. if(!provides.empty())
  223. {
  224. std::string fcName = this->TargetDirectory;
  225. fcName += "/cmake_clean_Fortran.cmake";
  226. cmGeneratedFileStream fcStream(fcName.c_str());
  227. fcStream << "# Remove fortran modules provided by this target.\n";
  228. fcStream << "FILE(REMOVE";
  229. for(std::set<cmStdString>::const_iterator i = provides.begin();
  230. i != provides.end(); ++i)
  231. {
  232. std::string mod_upper = mod_dir;
  233. mod_upper += "/";
  234. mod_upper += cmSystemTools::UpperCase(*i);
  235. mod_upper += ".mod";
  236. std::string mod_lower = mod_dir;
  237. mod_lower += "/";
  238. mod_lower += *i;
  239. mod_lower += ".mod";
  240. std::string stamp = stamp_dir;
  241. stamp += "/";
  242. stamp += *i;
  243. stamp += ".mod.stamp";
  244. fcStream << "\n";
  245. fcStream << " \"" <<
  246. this->LocalGenerator->Convert(mod_lower.c_str(),
  247. cmLocalGenerator::START_OUTPUT)
  248. << "\"\n";
  249. fcStream << " \"" <<
  250. this->LocalGenerator->Convert(mod_upper.c_str(),
  251. cmLocalGenerator::START_OUTPUT)
  252. << "\"\n";
  253. fcStream << " \"" <<
  254. this->LocalGenerator->Convert(stamp.c_str(),
  255. cmLocalGenerator::START_OUTPUT)
  256. << "\"\n";
  257. }
  258. fcStream << " )\n";
  259. }
  260. return true;
  261. }
  262. //----------------------------------------------------------------------------
  263. void cmDependsFortran::LocateModules()
  264. {
  265. // Collect the set of modules provided and required by all sources.
  266. typedef cmDependsFortranInternals::ObjectInfoMap ObjectInfoMap;
  267. ObjectInfoMap const& objInfo = this->Internal->ObjectInfo;
  268. for(ObjectInfoMap::const_iterator infoI = objInfo.begin();
  269. infoI != objInfo.end(); ++infoI)
  270. {
  271. cmDependsFortranSourceInfo const& info = infoI->second;
  272. for(std::set<cmStdString>::const_iterator i = info.Provides.begin();
  273. i != info.Provides.end(); ++i)
  274. {
  275. // Include this module in the set provided by this target.
  276. this->Internal->TargetProvides.insert(*i);
  277. }
  278. for(std::set<cmStdString>::const_iterator i = info.Requires.begin();
  279. i != info.Requires.end(); ++i)
  280. {
  281. // Include this module in the set required by this target.
  282. this->Internal->TargetRequires[*i] = "";
  283. }
  284. }
  285. // Short-circuit for simple targets.
  286. if(this->Internal->TargetRequires.empty())
  287. {
  288. return;
  289. }
  290. // Match modules provided by this target to those it requires.
  291. this->MatchLocalModules();
  292. // Load information about other targets.
  293. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  294. std::vector<std::string> infoFiles;
  295. if(const char* infoFilesValue =
  296. mf->GetDefinition("CMAKE_TARGET_LINKED_INFO_FILES"))
  297. {
  298. cmSystemTools::ExpandListArgument(infoFilesValue, infoFiles);
  299. }
  300. for(std::vector<std::string>::const_iterator i = infoFiles.begin();
  301. i != infoFiles.end(); ++i)
  302. {
  303. std::string targetDir = cmSystemTools::GetFilenamePath(*i);
  304. std::string fname = targetDir + "/fortran.internal";
  305. std::ifstream fin(fname.c_str());
  306. if(fin)
  307. {
  308. this->MatchRemoteModules(fin, targetDir.c_str());
  309. }
  310. }
  311. }
  312. //----------------------------------------------------------------------------
  313. void cmDependsFortran::MatchLocalModules()
  314. {
  315. const char* stampDir = this->TargetDirectory.c_str();
  316. std::set<cmStdString> const& provides = this->Internal->TargetProvides;
  317. for(std::set<cmStdString>::const_iterator i = provides.begin();
  318. i != provides.end(); ++i)
  319. {
  320. this->ConsiderModule(i->c_str(), stampDir);
  321. }
  322. }
  323. //----------------------------------------------------------------------------
  324. void cmDependsFortran::MatchRemoteModules(std::istream& fin,
  325. const char* stampDir)
  326. {
  327. std::string line;
  328. bool doing_provides = false;
  329. while(cmSystemTools::GetLineFromStream(fin, line))
  330. {
  331. // Ignore comments and empty lines.
  332. if(line.empty() || line[0] == '#' || line[0] == '\r')
  333. {
  334. continue;
  335. }
  336. if(line[0] == ' ')
  337. {
  338. if(doing_provides)
  339. {
  340. this->ConsiderModule(line.c_str()+1, stampDir);
  341. }
  342. }
  343. else if(line == "provides")
  344. {
  345. doing_provides = true;
  346. }
  347. else
  348. {
  349. doing_provides = false;
  350. }
  351. }
  352. }
  353. //----------------------------------------------------------------------------
  354. void cmDependsFortran::ConsiderModule(const char* name,
  355. const char* stampDir)
  356. {
  357. // Locate each required module.
  358. typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
  359. TargetRequiresMap::iterator required =
  360. this->Internal->TargetRequires.find(name);
  361. if(required != this->Internal->TargetRequires.end() &&
  362. required->second.empty())
  363. {
  364. // The module is provided by a CMake target. It will have a stamp file.
  365. std::string stampFile = stampDir;
  366. stampFile += "/";
  367. stampFile += name;
  368. stampFile += ".mod.stamp";
  369. required->second = stampFile;
  370. }
  371. }
  372. //----------------------------------------------------------------------------
  373. bool
  374. cmDependsFortran
  375. ::WriteDependenciesReal(const char *obj,
  376. cmDependsFortranSourceInfo const& info,
  377. const char* mod_dir, const char* stamp_dir,
  378. std::ostream& makeDepends,
  379. std::ostream& internalDepends)
  380. {
  381. typedef cmDependsFortranInternals::TargetRequiresMap TargetRequiresMap;
  382. // Get the source file for this object.
  383. const char* src = info.Source.c_str();
  384. // Write the include dependencies to the output stream.
  385. internalDepends << obj << std::endl;
  386. internalDepends << " " << src << std::endl;
  387. for(std::set<cmStdString>::const_iterator i = info.Includes.begin();
  388. i != info.Includes.end(); ++i)
  389. {
  390. makeDepends << obj << ": "
  391. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  392. << std::endl;
  393. internalDepends << " " << i->c_str() << std::endl;
  394. }
  395. makeDepends << std::endl;
  396. // Write module requirements to the output stream.
  397. for(std::set<cmStdString>::const_iterator i = info.Requires.begin();
  398. i != info.Requires.end(); ++i)
  399. {
  400. // Require only modules not provided in the same source.
  401. if(std::set<cmStdString>::const_iterator(info.Provides.find(*i)) !=
  402. info.Provides.end())
  403. {
  404. continue;
  405. }
  406. // If the module is provided in this target special handling is
  407. // needed.
  408. if(this->Internal->TargetProvides.find(*i) !=
  409. this->Internal->TargetProvides.end())
  410. {
  411. // The module is provided by a different source in the same
  412. // target. Add the proxy dependency to make sure the other
  413. // source builds first.
  414. std::string proxy = stamp_dir;
  415. proxy += "/";
  416. proxy += *i;
  417. proxy += ".mod.proxy";
  418. proxy = this->LocalGenerator->Convert(proxy.c_str(),
  419. cmLocalGenerator::HOME_OUTPUT,
  420. cmLocalGenerator::MAKEFILE);
  421. // since we require some things add them to our list of requirements
  422. makeDepends << obj << ".requires: " << proxy << std::endl;
  423. }
  424. // The object file should depend on timestamped files for the
  425. // modules it uses.
  426. TargetRequiresMap::const_iterator required =
  427. this->Internal->TargetRequires.find(*i);
  428. if(required == this->Internal->TargetRequires.end()) { abort(); }
  429. if(!required->second.empty())
  430. {
  431. // This module is known. Depend on its timestamp file.
  432. std::string stampFile =
  433. this->LocalGenerator->Convert(required->second.c_str(),
  434. cmLocalGenerator::HOME_OUTPUT,
  435. cmLocalGenerator::SHELL);
  436. makeDepends << obj << ": " << stampFile << "\n";
  437. }
  438. else
  439. {
  440. // This module is not known to CMake. Try to locate it where
  441. // the compiler will and depend on that.
  442. std::string module;
  443. if(this->FindModule(*i, module))
  444. {
  445. module =
  446. this->LocalGenerator->Convert(module.c_str(),
  447. cmLocalGenerator::HOME_OUTPUT,
  448. cmLocalGenerator::SHELL);
  449. makeDepends << obj << ": " << module << "\n";
  450. }
  451. }
  452. }
  453. // Write provided modules to the output stream.
  454. for(std::set<cmStdString>::const_iterator i = info.Provides.begin();
  455. i != info.Provides.end(); ++i)
  456. {
  457. std::string proxy = stamp_dir;
  458. proxy += "/";
  459. proxy += *i;
  460. proxy += ".mod.proxy";
  461. proxy = this->LocalGenerator->Convert(proxy.c_str(),
  462. cmLocalGenerator::HOME_OUTPUT,
  463. cmLocalGenerator::MAKEFILE);
  464. makeDepends << proxy << ": " << obj << ".provides" << std::endl;
  465. }
  466. // If any modules are provided then they must be converted to stamp files.
  467. if(!info.Provides.empty())
  468. {
  469. // Create a target to copy the module after the object file
  470. // changes.
  471. makeDepends << obj << ".provides.build:\n";
  472. for(std::set<cmStdString>::const_iterator i = info.Provides.begin();
  473. i != info.Provides.end(); ++i)
  474. {
  475. // Include this module in the set provided by this target.
  476. this->Internal->TargetProvides.insert(*i);
  477. // Always use lower case for the mod stamp file name. The
  478. // cmake_copy_f90_mod will call back to this class, which will
  479. // try various cases for the real mod file name.
  480. std::string m = cmSystemTools::LowerCase(*i);
  481. std::string modFile = mod_dir;
  482. modFile += "/";
  483. modFile += *i;
  484. modFile =
  485. this->LocalGenerator->Convert(modFile.c_str(),
  486. cmLocalGenerator::HOME_OUTPUT,
  487. cmLocalGenerator::SHELL);
  488. std::string stampFile = stamp_dir;
  489. stampFile += "/";
  490. stampFile += m;
  491. stampFile += ".mod.stamp";
  492. stampFile =
  493. this->LocalGenerator->Convert(stampFile.c_str(),
  494. cmLocalGenerator::HOME_OUTPUT,
  495. cmLocalGenerator::SHELL);
  496. makeDepends << "\t$(CMAKE_COMMAND) -E cmake_copy_f90_mod "
  497. << modFile << " " << stampFile;
  498. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  499. const char* cid = mf->GetDefinition("CMAKE_Fortran_COMPILER_ID");
  500. if(cid && *cid)
  501. {
  502. makeDepends << " " << cid;
  503. }
  504. makeDepends << "\n";
  505. }
  506. // After copying the modules update the timestamp file so that
  507. // copying will not be done again until the source rebuilds.
  508. makeDepends << "\t$(CMAKE_COMMAND) -E touch " << obj
  509. << ".provides.build\n";
  510. // Make sure the module timestamp rule is evaluated by the time
  511. // the target finishes building.
  512. std::string driver = this->TargetDirectory;
  513. driver += "/build";
  514. driver = this->LocalGenerator->Convert(driver.c_str(),
  515. cmLocalGenerator::HOME_OUTPUT,
  516. cmLocalGenerator::MAKEFILE);
  517. makeDepends << driver << ": " << obj << ".provides.build\n";
  518. }
  519. return true;
  520. }
  521. //----------------------------------------------------------------------------
  522. bool cmDependsFortran::FindModule(std::string const& name,
  523. std::string& module)
  524. {
  525. // Construct possible names for the module file.
  526. std::string mod_upper = cmSystemTools::UpperCase(name);
  527. std::string mod_lower = name;
  528. mod_upper += ".mod";
  529. mod_lower += ".mod";
  530. // Search the include path for the module.
  531. std::string fullName;
  532. for(std::vector<std::string>::const_iterator i =
  533. this->IncludePath.begin(); i != this->IncludePath.end(); ++i)
  534. {
  535. // Try the lower-case name.
  536. fullName = *i;
  537. fullName += "/";
  538. fullName += mod_lower;
  539. if(cmSystemTools::FileExists(fullName.c_str(), true))
  540. {
  541. module = fullName;
  542. return true;
  543. }
  544. // Try the upper-case name.
  545. fullName = *i;
  546. fullName += "/";
  547. fullName += mod_upper;
  548. if(cmSystemTools::FileExists(fullName.c_str(), true))
  549. {
  550. module = fullName;
  551. return true;
  552. }
  553. }
  554. return false;
  555. }
  556. //----------------------------------------------------------------------------
  557. bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
  558. {
  559. // Implements
  560. //
  561. // $(CMAKE_COMMAND) -E cmake_copy_f90_mod input.mod output.mod.stamp
  562. // [compiler-id]
  563. //
  564. // Note that the case of the .mod file depends on the compiler. In
  565. // the future this copy could also account for the fact that some
  566. // compilers include a timestamp in the .mod file so it changes even
  567. // when the interface described in the module does not.
  568. std::string mod = args[2];
  569. std::string stamp = args[3];
  570. std::string compilerId;
  571. if(args.size() >= 5)
  572. {
  573. compilerId = args[4];
  574. }
  575. std::string mod_dir = cmSystemTools::GetFilenamePath(mod);
  576. if(!mod_dir.empty()) { mod_dir += "/"; }
  577. std::string mod_upper = mod_dir;
  578. mod_upper += cmSystemTools::UpperCase(cmSystemTools::GetFilenameName(mod));
  579. std::string mod_lower = mod_dir;
  580. mod_lower += cmSystemTools::LowerCase(cmSystemTools::GetFilenameName(mod));
  581. mod += ".mod";
  582. mod_upper += ".mod";
  583. mod_lower += ".mod";
  584. if(cmSystemTools::FileExists(mod_upper.c_str(), true))
  585. {
  586. if(cmDependsFortran::ModulesDiffer(mod_upper.c_str(), stamp.c_str(),
  587. compilerId.c_str()))
  588. {
  589. if(!cmSystemTools::CopyFileAlways(mod_upper.c_str(), stamp.c_str()))
  590. {
  591. std::cerr << "Error copying Fortran module from \""
  592. << mod_upper.c_str() << "\" to \"" << stamp.c_str()
  593. << "\".\n";
  594. return false;
  595. }
  596. }
  597. return true;
  598. }
  599. else if(cmSystemTools::FileExists(mod_lower.c_str(), true))
  600. {
  601. if(cmDependsFortran::ModulesDiffer(mod_lower.c_str(), stamp.c_str(),
  602. compilerId.c_str()))
  603. {
  604. if(!cmSystemTools::CopyFileAlways(mod_lower.c_str(), stamp.c_str()))
  605. {
  606. std::cerr << "Error copying Fortran module from \""
  607. << mod_lower.c_str() << "\" to \"" << stamp.c_str()
  608. << "\".\n";
  609. return false;
  610. }
  611. }
  612. return true;
  613. }
  614. std::cerr << "Error copying Fortran module \"" << args[2].c_str()
  615. << "\". Tried \"" << mod_upper.c_str()
  616. << "\" and \"" << mod_lower.c_str() << "\".\n";
  617. return false;
  618. }
  619. //----------------------------------------------------------------------------
  620. // Helper function to look for a short sequence in a stream. If this
  621. // is later used for longer sequences it should be re-written using an
  622. // efficient string search algorithm such as Boyer-Moore.
  623. static
  624. bool cmDependsFortranStreamContainsSequence(std::ifstream& ifs,
  625. const char* seq, int len)
  626. {
  627. assert(len > 0);
  628. int cur = 0;
  629. while(cur < len)
  630. {
  631. // Get the next character.
  632. int token = ifs.get();
  633. if(!ifs)
  634. {
  635. return false;
  636. }
  637. // Check the character.
  638. if(token == static_cast<int>(seq[cur]))
  639. {
  640. ++cur;
  641. }
  642. else
  643. {
  644. // Assume the sequence has no repeating subsequence.
  645. cur = 0;
  646. }
  647. }
  648. // The entire sequence was matched.
  649. return true;
  650. }
  651. //----------------------------------------------------------------------------
  652. // Helper function to compare the remaining content in two streams.
  653. static bool cmDependsFortranStreamsDiffer(std::ifstream& ifs1,
  654. std::ifstream& ifs2)
  655. {
  656. // Compare the remaining content.
  657. for(;;)
  658. {
  659. int ifs1_c = ifs1.get();
  660. int ifs2_c = ifs2.get();
  661. if(!ifs1 && !ifs2)
  662. {
  663. // We have reached the end of both streams simultaneously.
  664. // The streams are identical.
  665. return false;
  666. }
  667. if(!ifs1 || !ifs2 || ifs1_c != ifs2_c)
  668. {
  669. // We have reached the end of one stream before the other or
  670. // found differing content. The streams are different.
  671. break;
  672. }
  673. }
  674. return true;
  675. }
  676. //----------------------------------------------------------------------------
  677. bool cmDependsFortran::ModulesDiffer(const char* modFile,
  678. const char* stampFile,
  679. const char* compilerId)
  680. {
  681. /*
  682. gnu:
  683. A mod file is an ascii file.
  684. <bar.mod>
  685. FORTRAN module created from /path/to/foo.f90 on Sun Dec 30 22:47:58 2007
  686. If you edit this, you'll get what you deserve.
  687. ...
  688. </bar.mod>
  689. As you can see the first line contains the date.
  690. intel:
  691. A mod file is a binary file.
  692. However, looking into both generated bar.mod files with a hex editor
  693. shows that they differ only before a sequence linefeed-zero (0x0A 0x00)
  694. which is located some bytes in front of the absoulte path to the source
  695. file.
  696. sun:
  697. A mod file is a binary file. Compiling twice produces identical modules.
  698. others:
  699. TODO ...
  700. */
  701. /* Compilers which do _not_ produce different mod content when the same
  702. * source is compiled twice
  703. * -SunPro
  704. */
  705. if(strcmp(compilerId, "SunPro") == 0)
  706. {
  707. return cmSystemTools::FilesDiffer(modFile, stampFile);
  708. }
  709. #if defined(_WIN32) || defined(__CYGWIN__)
  710. std::ifstream finModFile(modFile, std::ios::in | std::ios::binary);
  711. std::ifstream finStampFile(stampFile, std::ios::in | std::ios::binary);
  712. #else
  713. std::ifstream finModFile(modFile, std::ios::in);
  714. std::ifstream finStampFile(stampFile, std::ios::in);
  715. #endif
  716. if(!finModFile || !finStampFile)
  717. {
  718. // At least one of the files does not exist. The modules differ.
  719. return true;
  720. }
  721. /* Compilers which _do_ produce different mod content when the same
  722. * source is compiled twice
  723. * -GNU
  724. * -Intel
  725. *
  726. * Eat the stream content until all recompile only realated changes
  727. * are left bedind.
  728. */
  729. if (strcmp(compilerId, "GNU") == 0 )
  730. {
  731. const char seq[1] = {'\n'};
  732. const int seqlen = 1;
  733. if(!cmDependsFortranStreamContainsSequence(finModFile, seq, seqlen))
  734. {
  735. // The module is of unexpected format. Assume it is different.
  736. std::cerr << compilerId << " fortran module " << modFile
  737. << " has unexpected format." << std::endl;
  738. return true;
  739. }
  740. if(!cmDependsFortranStreamContainsSequence(finStampFile, seq, seqlen))
  741. {
  742. // The stamp must differ if the sequence is not contained.
  743. return true;
  744. }
  745. }
  746. else if(strcmp(compilerId, "Intel") == 0)
  747. {
  748. const char seq[2] = {'\n', '\0'};
  749. const int seqlen = 2;
  750. if(!cmDependsFortranStreamContainsSequence(finModFile, seq, seqlen))
  751. {
  752. // The module is of unexpected format. Assume it is different.
  753. std::cerr << compilerId << " fortran module " << modFile
  754. << " has unexpected format." << std::endl;
  755. return true;
  756. }
  757. if(!cmDependsFortranStreamContainsSequence(finStampFile, seq, seqlen))
  758. {
  759. // The stamp must differ if the sequence is not contained.
  760. return true;
  761. }
  762. }
  763. // Compare the remainng content. If no compiler id matched above,
  764. // including the case none was given, this will compare the whole
  765. // content.
  766. if(!cmDependsFortranStreamsDiffer(finModFile, finStampFile))
  767. {
  768. return false;
  769. }
  770. // The modules are different.
  771. return true;
  772. }
  773. //----------------------------------------------------------------------------
  774. bool cmDependsFortran::FindIncludeFile(const char* dir,
  775. const char* includeName,
  776. std::string& fileName)
  777. {
  778. // If the file is a full path, include it directly.
  779. if(cmSystemTools::FileIsFullPath(includeName))
  780. {
  781. fileName = includeName;
  782. return cmSystemTools::FileExists(fileName.c_str(), true);
  783. }
  784. else
  785. {
  786. // Check for the file in the directory containing the including
  787. // file.
  788. std::string fullName = dir;
  789. fullName += "/";
  790. fullName += includeName;
  791. if(cmSystemTools::FileExists(fullName.c_str(), true))
  792. {
  793. fileName = fullName;
  794. return true;
  795. }
  796. // Search the include path for the file.
  797. for(std::vector<std::string>::const_iterator i =
  798. this->IncludePath.begin(); i != this->IncludePath.end(); ++i)
  799. {
  800. fullName = *i;
  801. fullName += "/";
  802. fullName += includeName;
  803. if(cmSystemTools::FileExists(fullName.c_str(), true))
  804. {
  805. fileName = fullName;
  806. return true;
  807. }
  808. }
  809. }
  810. return false;
  811. }
  812. //----------------------------------------------------------------------------
  813. cmDependsFortranParser_s
  814. ::cmDependsFortranParser_s(cmDependsFortran* self,
  815. std::set<std::string>& ppDefines,
  816. cmDependsFortranSourceInfo& info):
  817. Self(self), PPDefinitions(ppDefines), Info(info)
  818. {
  819. this->InInterface = 0;
  820. this->InPPFalseBranch = 0;
  821. // Initialize the lexical scanner.
  822. cmDependsFortran_yylex_init(&this->Scanner);
  823. cmDependsFortran_yyset_extra(this, this->Scanner);
  824. // Create a dummy buffer that is never read but is the fallback
  825. // buffer when the last file is popped off the stack.
  826. YY_BUFFER_STATE buffer =
  827. cmDependsFortran_yy_create_buffer(0, 4, this->Scanner);
  828. cmDependsFortran_yy_switch_to_buffer(buffer, this->Scanner);
  829. }
  830. //----------------------------------------------------------------------------
  831. cmDependsFortranParser_s::~cmDependsFortranParser_s()
  832. {
  833. cmDependsFortran_yylex_destroy(this->Scanner);
  834. }
  835. //----------------------------------------------------------------------------
  836. bool cmDependsFortranParser_FilePush(cmDependsFortranParser* parser,
  837. const char* fname)
  838. {
  839. // Open the new file and push it onto the stack. Save the old
  840. // buffer with it on the stack.
  841. if(FILE* file = fopen(fname, "rb"))
  842. {
  843. YY_BUFFER_STATE current =
  844. cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
  845. std::string dir = cmSystemTools::GetParentDirectory(fname);
  846. cmDependsFortranFile f(file, current, dir);
  847. YY_BUFFER_STATE buffer =
  848. cmDependsFortran_yy_create_buffer(0, 16384, parser->Scanner);
  849. cmDependsFortran_yy_switch_to_buffer(buffer, parser->Scanner);
  850. parser->FileStack.push(f);
  851. return 1;
  852. }
  853. else
  854. {
  855. return 0;
  856. }
  857. }
  858. //----------------------------------------------------------------------------
  859. bool cmDependsFortranParser_FilePop(cmDependsFortranParser* parser)
  860. {
  861. // Pop one file off the stack and close it. Switch the lexer back
  862. // to the next one on the stack.
  863. if(parser->FileStack.empty())
  864. {
  865. return 0;
  866. }
  867. else
  868. {
  869. cmDependsFortranFile f = parser->FileStack.top(); parser->FileStack.pop();
  870. fclose(f.File);
  871. YY_BUFFER_STATE current =
  872. cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
  873. cmDependsFortran_yy_delete_buffer(current, parser->Scanner);
  874. cmDependsFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
  875. return 1;
  876. }
  877. }
  878. //----------------------------------------------------------------------------
  879. int cmDependsFortranParser_Input(cmDependsFortranParser* parser,
  880. char* buffer, size_t bufferSize)
  881. {
  882. // Read from the file on top of the stack. If the stack is empty,
  883. // the end of the translation unit has been reached.
  884. if(!parser->FileStack.empty())
  885. {
  886. FILE* file = parser->FileStack.top().File;
  887. return (int)fread(buffer, 1, bufferSize, file);
  888. }
  889. return 0;
  890. }
  891. //----------------------------------------------------------------------------
  892. void cmDependsFortranParser_StringStart(cmDependsFortranParser* parser)
  893. {
  894. parser->TokenString = "";
  895. }
  896. //----------------------------------------------------------------------------
  897. const char* cmDependsFortranParser_StringEnd(cmDependsFortranParser* parser)
  898. {
  899. return parser->TokenString.c_str();
  900. }
  901. //----------------------------------------------------------------------------
  902. void cmDependsFortranParser_StringAppend(cmDependsFortranParser* parser,
  903. char c)
  904. {
  905. parser->TokenString += c;
  906. }
  907. //----------------------------------------------------------------------------
  908. void cmDependsFortranParser_SetInInterface(cmDependsFortranParser* parser,
  909. bool in)
  910. {
  911. if(parser->InPPFalseBranch)
  912. {
  913. return;
  914. }
  915. parser->InInterface = in;
  916. }
  917. //----------------------------------------------------------------------------
  918. bool cmDependsFortranParser_GetInInterface(cmDependsFortranParser* parser)
  919. {
  920. return parser->InInterface;
  921. }
  922. //----------------------------------------------------------------------------
  923. void cmDependsFortranParser_SetOldStartcond(cmDependsFortranParser* parser,
  924. int arg)
  925. {
  926. parser->OldStartcond = arg;
  927. }
  928. //----------------------------------------------------------------------------
  929. int cmDependsFortranParser_GetOldStartcond(cmDependsFortranParser* parser)
  930. {
  931. return parser->OldStartcond;
  932. }
  933. //----------------------------------------------------------------------------
  934. void cmDependsFortranParser_Error(cmDependsFortranParser*, const char*)
  935. {
  936. // If there is a parser error just ignore it. The source will not
  937. // compile and the user will edit it. Then dependencies will have
  938. // to be regenerated anyway.
  939. }
  940. //----------------------------------------------------------------------------
  941. void cmDependsFortranParser_RuleUse(cmDependsFortranParser* parser,
  942. const char* name)
  943. {
  944. if(!parser->InPPFalseBranch)
  945. {
  946. parser->Info.Requires.insert(cmSystemTools::LowerCase(name) );
  947. }
  948. }
  949. //----------------------------------------------------------------------------
  950. void cmDependsFortranParser_RuleInclude(cmDependsFortranParser* parser,
  951. const char* name)
  952. {
  953. if(parser->InPPFalseBranch)
  954. {
  955. return;
  956. }
  957. // If processing an include statement there must be an open file.
  958. assert(!parser->FileStack.empty());
  959. // Get the directory containing the source in which the include
  960. // statement appears. This is always the first search location for
  961. // Fortran include files.
  962. std::string dir = parser->FileStack.top().Directory;
  963. // Find the included file. If it cannot be found just ignore the
  964. // problem because either the source will not compile or the user
  965. // does not care about depending on this included source.
  966. std::string fullName;
  967. if(parser->Self->FindIncludeFile(dir.c_str(), name, fullName))
  968. {
  969. // Found the included file. Save it in the set of included files.
  970. parser->Info.Includes.insert(fullName);
  971. // Parse it immediately to translate the source inline.
  972. cmDependsFortranParser_FilePush(parser, fullName.c_str());
  973. }
  974. }
  975. //----------------------------------------------------------------------------
  976. void cmDependsFortranParser_RuleModule(cmDependsFortranParser* parser,
  977. const char* name)
  978. {
  979. if(!parser->InPPFalseBranch && !parser->InInterface)
  980. {
  981. parser->Info.Provides.insert(cmSystemTools::LowerCase(name));
  982. }
  983. }
  984. //----------------------------------------------------------------------------
  985. void cmDependsFortranParser_RuleDefine(cmDependsFortranParser* parser,
  986. const char* macro)
  987. {
  988. if(!parser->InPPFalseBranch)
  989. {
  990. parser->PPDefinitions.insert(macro);
  991. }
  992. }
  993. //----------------------------------------------------------------------------
  994. void cmDependsFortranParser_RuleUndef(cmDependsFortranParser* parser,
  995. const char* macro)
  996. {
  997. if(!parser->InPPFalseBranch)
  998. {
  999. std::set<std::string>::iterator match;
  1000. match = parser->PPDefinitions.find(macro);
  1001. if(match != parser->PPDefinitions.end())
  1002. {
  1003. parser->PPDefinitions.erase(match);
  1004. }
  1005. }
  1006. }
  1007. //----------------------------------------------------------------------------
  1008. void cmDependsFortranParser_RuleIfdef(cmDependsFortranParser* parser,
  1009. const char* macro)
  1010. {
  1011. // A new PP branch has been opened
  1012. parser->SkipToEnd.push(false);
  1013. if (parser->InPPFalseBranch)
  1014. {
  1015. parser->InPPFalseBranch++;
  1016. }
  1017. else if(parser->PPDefinitions.find(macro) == parser->PPDefinitions.end())
  1018. {
  1019. parser->InPPFalseBranch=1;
  1020. }
  1021. else
  1022. {
  1023. parser->SkipToEnd.top() = true;
  1024. }
  1025. }
  1026. //----------------------------------------------------------------------------
  1027. void cmDependsFortranParser_RuleIfndef(cmDependsFortranParser* parser,
  1028. const char* macro)
  1029. {
  1030. // A new PP branch has been opened
  1031. parser->SkipToEnd.push(false);
  1032. if (parser->InPPFalseBranch)
  1033. {
  1034. parser->InPPFalseBranch++;
  1035. }
  1036. else if(parser->PPDefinitions.find(macro) != parser->PPDefinitions.end())
  1037. {
  1038. parser->InPPFalseBranch = 1;
  1039. }
  1040. else
  1041. {
  1042. // ignore other branches
  1043. parser->SkipToEnd.top() = true;
  1044. }
  1045. }
  1046. //----------------------------------------------------------------------------
  1047. void cmDependsFortranParser_RuleIf(cmDependsFortranParser* parser)
  1048. {
  1049. /* Note: The current parser is _not_ able to get statements like
  1050. * #if 0
  1051. * #if 1
  1052. * #if MYSMBOL
  1053. * #if defined(MYSYMBOL)
  1054. * #if defined(MYSYMBOL) && ...
  1055. * right. The same for #elif. Thus in
  1056. * #if SYMBOL_1
  1057. * ..
  1058. * #elif SYMBOL_2
  1059. * ...
  1060. * ...
  1061. * #elif SYMBOL_N
  1062. * ..
  1063. * #else
  1064. * ..
  1065. * #endif
  1066. * _all_ N+1 branches are considered. If you got something like this
  1067. * #if defined(MYSYMBOL)
  1068. * #if !defined(MYSYMBOL)
  1069. * use
  1070. * #ifdef MYSYMBOL
  1071. * #ifndef MYSYMBOL
  1072. * instead.
  1073. */
  1074. // A new PP branch has been opened
  1075. // Never skip! See note above.
  1076. parser->SkipToEnd.push(false);
  1077. }
  1078. //----------------------------------------------------------------------------
  1079. void cmDependsFortranParser_RuleElif(cmDependsFortranParser* parser)
  1080. {
  1081. /* Note: There are parser limitations. See the note at
  1082. * cmDependsFortranParser_RuleIf(..)
  1083. */
  1084. // Allways taken unless an #ifdef or #ifndef-branch has been taken
  1085. // already. If the second condition isn't meet already
  1086. // (parser->InPPFalseBranch == 0) correct it.
  1087. if(!parser->SkipToEnd.empty() &&
  1088. parser->SkipToEnd.top() && !parser->InPPFalseBranch)
  1089. {
  1090. parser->InPPFalseBranch = 1;
  1091. }
  1092. }
  1093. //----------------------------------------------------------------------------
  1094. void cmDependsFortranParser_RuleElse(cmDependsFortranParser* parser)
  1095. {
  1096. // if the parent branch is false do nothing!
  1097. if(parser->InPPFalseBranch > 1)
  1098. {
  1099. return;
  1100. }
  1101. // parser->InPPFalseBranch is either 0 or 1. We change it denpending on
  1102. // parser->SkipToEnd.top()
  1103. if(!parser->SkipToEnd.empty() &&
  1104. parser->SkipToEnd.top())
  1105. {
  1106. parser->InPPFalseBranch = 1;
  1107. }
  1108. else
  1109. {
  1110. parser->InPPFalseBranch = 0;
  1111. }
  1112. }
  1113. //----------------------------------------------------------------------------
  1114. void cmDependsFortranParser_RuleEndif(cmDependsFortranParser* parser)
  1115. {
  1116. if(!parser->SkipToEnd.empty())
  1117. {
  1118. parser->SkipToEnd.pop();
  1119. }
  1120. // #endif doesn't know if there was a "#else" in before, so it
  1121. // always decreases InPPFalseBranch
  1122. if(parser->InPPFalseBranch)
  1123. {
  1124. parser->InPPFalseBranch--;
  1125. }
  1126. }