cmDependsFortran.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDependsFortran.h"
  14. #include "cmSystemTools.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. // Parser methods not included in generated interface.
  23. // Get the current buffer processed by the lexer.
  24. YY_BUFFER_STATE cmDependsFortranLexer_GetCurrentBuffer(yyscan_t yyscanner);
  25. // The parser entry point.
  26. int cmDependsFortran_yyparse(yyscan_t);
  27. //----------------------------------------------------------------------------
  28. // Define parser object internal structure.
  29. struct cmDependsFortranFile
  30. {
  31. cmDependsFortranFile(FILE* file, YY_BUFFER_STATE buffer,
  32. const std::string& dir):
  33. File(file), Buffer(buffer), Directory(dir) {}
  34. FILE* File;
  35. YY_BUFFER_STATE Buffer;
  36. std::string Directory;
  37. };
  38. struct cmDependsFortranParser_s
  39. {
  40. cmDependsFortranParser_s(cmDependsFortran* self);
  41. ~cmDependsFortranParser_s();
  42. // Pointer back to the main class.
  43. cmDependsFortran* Self;
  44. // Lexical scanner instance.
  45. yyscan_t Scanner;
  46. // Stack of open files in the translation unit.
  47. std::stack<cmDependsFortranFile> FileStack;
  48. // Buffer for string literals.
  49. std::string TokenString;
  50. // Flag for whether lexer is reading from inside an interface.
  51. int InInterface;
  52. // Set of provided and required modules.
  53. std::set<cmStdString> Provides;
  54. std::set<cmStdString> Requires;
  55. // Set of files included in the translation unit.
  56. std::set<cmStdString> Includes;
  57. };
  58. //----------------------------------------------------------------------------
  59. cmDependsFortran::cmDependsFortran():
  60. m_SourceFile(),
  61. m_IncludePath(0)
  62. {
  63. }
  64. //----------------------------------------------------------------------------
  65. cmDependsFortran::cmDependsFortran(const char* sourceFile,
  66. std::vector<std::string> const& includes):
  67. m_SourceFile(sourceFile),
  68. m_IncludePath(&includes)
  69. {
  70. }
  71. //----------------------------------------------------------------------------
  72. cmDependsFortran::~cmDependsFortran()
  73. {
  74. }
  75. //----------------------------------------------------------------------------
  76. bool cmDependsFortran::WriteDependencies(std::ostream& os)
  77. {
  78. // Make sure this is a scanning instance.
  79. if(m_SourceFile == "")
  80. {
  81. cmSystemTools::Error("Cannot scan dependencies without an source file.");
  82. return false;
  83. }
  84. if(!m_IncludePath)
  85. {
  86. cmSystemTools::Error("Cannot scan dependencies without an include path.");
  87. return false;
  88. }
  89. // Create the parser object.
  90. cmDependsFortranParser parser(this);
  91. // Push on the starting file.
  92. cmDependsFortranParser_FilePush(&parser, m_SourceFile.c_str());
  93. // Parse the translation unit.
  94. if(cmDependsFortran_yyparse(parser.Scanner) != 0)
  95. {
  96. // Failed to parse the file. Report failure to write dependencies.
  97. return false;
  98. }
  99. // Write the include dependencies to the output stream.
  100. for(std::set<cmStdString>::const_iterator i = parser.Includes.begin();
  101. i != parser.Includes.end(); ++i)
  102. {
  103. os << m_TargetFile.c_str() << ": "
  104. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  105. << std::endl;
  106. }
  107. os << std::endl;
  108. // Write module requirements to the output stream.
  109. for(std::set<cmStdString>::const_iterator i = parser.Requires.begin();
  110. i != parser.Requires.end(); ++i)
  111. {
  112. // Require only modules not provided in the same source.
  113. if(parser.Provides.find(*i) == parser.Provides.end())
  114. {
  115. // Always use lower case for the mod stamp file name.
  116. std::string m = cmSystemTools::LowerCase(*i);
  117. os << m_TargetFile.c_str() << ": " << m.c_str() << ".mod.stamp"
  118. << std::endl;
  119. os << m_TargetFile.c_str() << ".requires: " << i->c_str() << ".mod.proxy"
  120. << std::endl;
  121. os << i->c_str() << ".mod.proxy:" << std::endl;
  122. std::string stampName = m_Directory;
  123. stampName += "/";
  124. stampName += m;
  125. stampName += ".mod.stamp";
  126. if(!cmSystemTools::FileExists(stampName.c_str()))
  127. {
  128. std::ofstream stamp(stampName.c_str());
  129. stamp << "# Dummy stamp file in case nothing provides it."
  130. << std::endl;
  131. }
  132. }
  133. }
  134. // Write provided modules to the output stream.
  135. for(std::set<cmStdString>::const_iterator i = parser.Provides.begin();
  136. i != parser.Provides.end(); ++i)
  137. {
  138. os << i->c_str() << ".mod.proxy: " << m_TargetFile.c_str()
  139. << ".requires" << std::endl;
  140. }
  141. // If any modules are provided then they must be converted to stamp files.
  142. if(!parser.Provides.empty())
  143. {
  144. os << m_TargetFile.c_str() << ".provides:\n";
  145. for(std::set<cmStdString>::const_iterator i = parser.Provides.begin();
  146. i != parser.Provides.end(); ++i)
  147. {
  148. // Always use lower case for the mod stamp file name. The
  149. // cmake_copy_f90_mod will call back to this class, which will
  150. // try various cases for the real mod file name.
  151. std::string m = cmSystemTools::LowerCase(*i);
  152. os << "\t$(CMAKE_COMMAND) -E cmake_copy_f90_mod "
  153. << i->c_str() << " " << m.c_str() << ".mod.stamp\n";
  154. }
  155. os << "\t@touch " << m_TargetFile.c_str() << ".provides\n";
  156. }
  157. /*
  158. // TODO:
  159. What about .mod files provided in another directory and found with a
  160. -M search path? The stamp file will not be updated, so things might
  161. not rebuild. Possible solutions (not all thought through):
  162. Solution 1: Have all the .o.requires in a directory depend on a
  163. single .outside.requires that searches for .mod files in another
  164. directory of the build tree and uses copy-if-different to produce
  165. the local directory's stamp files. (won't work because the single
  166. rule cannot know about the modules)
  167. Solution 2: When the dependency is detected search the module
  168. include path for a mark file indicating the module is provided. If
  169. not found just write the dummy stamp file. If found, we need a rule
  170. to copy-if-different the module file. When a module is provided,
  171. write this mark file.
  172. Solution 3: Use a set of make rules like this:
  173. # When required:
  174. foo.mod.proxy: foo.mod.default
  175. foo.mod.default:: foo.mod.hack
  176. @echo foo.mod.default2 # Search for and copy-if-different the mod file.
  177. foo.mod.hack:
  178. # When provided:
  179. foo.mod.proxy: foo.o.requires
  180. @rm -f foo.mod.hack foo.mod.default
  181. foo.o.requires: foo.mod.hack
  182. @echo foo.o.requires
  183. foo.mod.hack:
  184. @touch foo.mod.hack
  185. @touch foo.mod.default
  186. Solution 4:
  187. When scanning dependencies and providing a module:
  188. - Create a .mod.provided.
  189. - Add .mod.proxy rule depending on corresponding .o.requires.
  190. When scanning dependencies and requiring a module:
  191. - Search the module path for a .mod.provided or a .mod.
  192. - If a .mod.provided is found depend on the corresponding .mod.stamp
  193. (it is provided by CMake in another directory)
  194. - Else, if a .mod is found depend on it directly
  195. (it is provided in another directory by a non-CMake project)
  196. - Else:
  197. - Add the empty proxy rule (if it is provided locally this will hook it)
  198. - Depend on a local .mod.stamp (it might be provided locally)
  199. - Create the dummy local .mod.stamp (it might not be provided locally)
  200. */
  201. return true;
  202. }
  203. //----------------------------------------------------------------------------
  204. bool cmDependsFortran::CheckDependencies(std::istream&)
  205. {
  206. // TODO: Parse and check dependencies.
  207. return true;
  208. }
  209. //----------------------------------------------------------------------------
  210. bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
  211. {
  212. // Implements
  213. //
  214. // $(CMAKE_COMMAND) -E cmake_copy_f90_mod input.mod output.mod.stamp
  215. //
  216. // Note that the case of the .mod file depends on the compiler. In
  217. // the future this copy could also account for the fact that some
  218. // compilers include a timestamp in the .mod file so it changes even
  219. // when the interface described in the module does not.
  220. std::string mod = args[2];
  221. std::string stamp = args[3];
  222. std::string mod_upper = cmSystemTools::UpperCase(mod.c_str());
  223. std::string mod_lower = cmSystemTools::LowerCase(mod.c_str());
  224. mod += ".mod";
  225. mod_upper += ".mod";
  226. mod_lower += ".mod";
  227. if(cmSystemTools::FileExists(mod_upper.c_str()))
  228. {
  229. if(!cmSystemTools::CopyFileIfDifferent(mod_upper.c_str(), stamp.c_str()))
  230. {
  231. std::cerr << "Error copying Fortran module from \""
  232. << mod_upper.c_str() << "\" to \"" << stamp.c_str()
  233. << "\".\n";
  234. return false;
  235. }
  236. return true;
  237. }
  238. else if(cmSystemTools::FileExists(mod_lower.c_str()))
  239. {
  240. if(!cmSystemTools::CopyFileIfDifferent(mod_lower.c_str(), stamp.c_str()))
  241. {
  242. std::cerr << "Error copying Fortran module from \""
  243. << mod_lower.c_str() << "\" to \"" << stamp.c_str()
  244. << "\".\n";
  245. return false;
  246. }
  247. return true;
  248. }
  249. std::cerr << "Error copying Fortran module \"" << args[2].c_str()
  250. << "\". Tried \"" << mod_upper.c_str()
  251. << "\" and \"" << mod_lower.c_str() << "\".\n";
  252. return false;
  253. }
  254. //----------------------------------------------------------------------------
  255. bool cmDependsFortran::FindIncludeFile(const char* dir,
  256. const char* includeName,
  257. std::string& fileName)
  258. {
  259. // If the file is a full path, include it directly.
  260. if(cmSystemTools::FileIsFullPath(includeName))
  261. {
  262. fileName = includeName;
  263. return cmSystemTools::FileExists(fileName.c_str());
  264. }
  265. else
  266. {
  267. // Check for the file in the directory containing the including
  268. // file.
  269. std::string fullName = dir;
  270. fullName += "/";
  271. fullName += includeName;
  272. if(cmSystemTools::FileExists(fullName.c_str()))
  273. {
  274. fileName = fullName;
  275. return true;
  276. }
  277. // Search the include path for the file.
  278. for(std::vector<std::string>::const_iterator i = m_IncludePath->begin();
  279. i != m_IncludePath->end(); ++i)
  280. {
  281. fullName = *i;
  282. fullName += "/";
  283. fullName += includeName;
  284. if(cmSystemTools::FileExists(fullName.c_str()))
  285. {
  286. fileName = fullName;
  287. return true;
  288. }
  289. }
  290. }
  291. return false;
  292. }
  293. //----------------------------------------------------------------------------
  294. cmDependsFortranParser_s::cmDependsFortranParser_s(cmDependsFortran* self):
  295. Self(self)
  296. {
  297. this->InInterface = 0;
  298. // Initialize the lexical scanner.
  299. cmDependsFortran_yylex_init(&this->Scanner);
  300. cmDependsFortran_yyset_extra(this, this->Scanner);
  301. // Create a dummy buffer that is never read but is the fallback
  302. // buffer when the last file is popped off the stack.
  303. YY_BUFFER_STATE buffer =
  304. cmDependsFortran_yy_create_buffer(0, 4, this->Scanner);
  305. cmDependsFortran_yy_switch_to_buffer(buffer, this->Scanner);
  306. }
  307. //----------------------------------------------------------------------------
  308. cmDependsFortranParser_s::~cmDependsFortranParser_s()
  309. {
  310. cmDependsFortran_yylex_destroy(this->Scanner);
  311. }
  312. //----------------------------------------------------------------------------
  313. int cmDependsFortranParser_FilePush(cmDependsFortranParser* parser,
  314. const char* fname)
  315. {
  316. // Open the new file and push it onto the stack. Save the old
  317. // buffer with it on the stack.
  318. if(FILE* file = fopen(fname, "rb"))
  319. {
  320. YY_BUFFER_STATE current =
  321. cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
  322. std::string dir = cmSystemTools::GetParentDirectory(fname);
  323. cmDependsFortranFile f(file, current, dir);
  324. YY_BUFFER_STATE buffer =
  325. cmDependsFortran_yy_create_buffer(0, 16384, parser->Scanner);
  326. cmDependsFortran_yy_switch_to_buffer(buffer, parser->Scanner);
  327. parser->FileStack.push(f);
  328. return 1;
  329. }
  330. else
  331. {
  332. return 0;
  333. }
  334. }
  335. //----------------------------------------------------------------------------
  336. int cmDependsFortranParser_FilePop(cmDependsFortranParser* parser)
  337. {
  338. // Pop one file off the stack and close it. Switch the lexer back
  339. // to the next one on the stack.
  340. if(parser->FileStack.empty())
  341. {
  342. return 0;
  343. }
  344. else
  345. {
  346. cmDependsFortranFile f = parser->FileStack.top(); parser->FileStack.pop();
  347. fclose(f.File);
  348. YY_BUFFER_STATE current =
  349. cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
  350. cmDependsFortran_yy_delete_buffer(current, parser->Scanner);
  351. cmDependsFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
  352. return 1;
  353. }
  354. }
  355. //----------------------------------------------------------------------------
  356. int cmDependsFortranParser_Input(cmDependsFortranParser* parser,
  357. char* buffer, size_t bufferSize)
  358. {
  359. // Read from the file on top of the stack. If the stack is empty,
  360. // the end of the translation unit has been reached.
  361. if(!parser->FileStack.empty())
  362. {
  363. FILE* file = parser->FileStack.top().File;
  364. return (int)fread(buffer, 1, bufferSize, file);
  365. }
  366. return 0;
  367. }
  368. //----------------------------------------------------------------------------
  369. void cmDependsFortranParser_StringStart(cmDependsFortranParser* parser)
  370. {
  371. parser->TokenString = "";
  372. }
  373. //----------------------------------------------------------------------------
  374. const char* cmDependsFortranParser_StringEnd(cmDependsFortranParser* parser)
  375. {
  376. return parser->TokenString.c_str();
  377. }
  378. //----------------------------------------------------------------------------
  379. void cmDependsFortranParser_StringAppend(cmDependsFortranParser* parser,
  380. char c)
  381. {
  382. parser->TokenString += c;
  383. }
  384. //----------------------------------------------------------------------------
  385. void cmDependsFortranParser_SetInInterface(cmDependsFortranParser* parser,
  386. int in)
  387. {
  388. parser->InInterface = in;
  389. }
  390. //----------------------------------------------------------------------------
  391. int cmDependsFortranParser_GetInInterface(cmDependsFortranParser* parser)
  392. {
  393. return parser->InInterface;
  394. }
  395. //----------------------------------------------------------------------------
  396. void cmDependsFortranParser_Error(cmDependsFortranParser*, const char*)
  397. {
  398. // If there is a parser error just ignore it. The source will not
  399. // compile and the user will edit it. Then dependencies will have
  400. // to be regenerated anyway.
  401. }
  402. //----------------------------------------------------------------------------
  403. void cmDependsFortranParser_RuleUse(cmDependsFortranParser* parser,
  404. const char* name)
  405. {
  406. parser->Requires.insert(name);
  407. }
  408. //----------------------------------------------------------------------------
  409. void cmDependsFortranParser_RuleInclude(cmDependsFortranParser* parser,
  410. const char* name)
  411. {
  412. // If processing an include statement there must be an open file.
  413. assert(!parser->FileStack.empty());
  414. // Get the directory containing the source in which the include
  415. // statement appears. This is always the first search location for
  416. // Fortran include files.
  417. std::string dir = parser->FileStack.top().Directory;
  418. // Find the included file. If it cannot be found just ignore the
  419. // problem because either the source will not compile or the user
  420. // does not care about depending on this included source.
  421. std::string fullName;
  422. if(parser->Self->FindIncludeFile(dir.c_str(), name, fullName))
  423. {
  424. // Found the included file. Save it in the set of included files.
  425. parser->Includes.insert(fullName);
  426. // Parse it immediately to translate the source inline.
  427. cmDependsFortranParser_FilePush(parser, fullName.c_str());
  428. }
  429. }
  430. //----------------------------------------------------------------------------
  431. void cmDependsFortranParser_RuleModule(cmDependsFortranParser* parser,
  432. const char* name)
  433. {
  434. parser->Provides.insert(name);
  435. }
  436. //----------------------------------------------------------------------------
  437. void cmDependsFortranParser_RuleDefine(cmDependsFortranParser*, const char*)
  438. {
  439. }
  440. //----------------------------------------------------------------------------
  441. void cmDependsFortranParser_RuleUndef(cmDependsFortranParser*, const char*)
  442. {
  443. }
  444. //----------------------------------------------------------------------------
  445. void cmDependsFortranParser_RuleIfdef(cmDependsFortranParser*, const char*)
  446. {
  447. }
  448. //----------------------------------------------------------------------------
  449. void cmDependsFortranParser_RuleIfndef(cmDependsFortranParser*, const char*)
  450. {
  451. }
  452. //----------------------------------------------------------------------------
  453. void cmDependsFortranParser_RuleIf(cmDependsFortranParser*)
  454. {
  455. }
  456. //----------------------------------------------------------------------------
  457. void cmDependsFortranParser_RuleElif(cmDependsFortranParser*)
  458. {
  459. }
  460. //----------------------------------------------------------------------------
  461. void cmDependsFortranParser_RuleElse(cmDependsFortranParser*)
  462. {
  463. }
  464. //----------------------------------------------------------------------------
  465. void cmDependsFortranParser_RuleEndif(cmDependsFortranParser*)
  466. {
  467. }