cmDependsFortran.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. // if it provides something then connect the requires rule to the build rule
  158. if(!parser.Provides.empty())
  159. {
  160. os << m_TargetFile.c_str() << ".requires: " << m_TargetFile.c_str()
  161. << ".requires.build" << std::endl;
  162. // provide empty build rule for old gen for now, TODO remove later
  163. os << m_TargetFile.c_str() << ".requires.build:" << std::endl;
  164. }
  165. /*
  166. // TODO:
  167. What about .mod files provided in another directory and found with a
  168. -M search path? The stamp file will not be updated, so things might
  169. not rebuild. Possible solutions (not all thought through):
  170. Solution 1: Have all the .o.requires in a directory depend on a
  171. single .outside.requires that searches for .mod files in another
  172. directory of the build tree and uses copy-if-different to produce
  173. the local directory's stamp files. (won't work because the single
  174. rule cannot know about the modules)
  175. Solution 2: When the dependency is detected search the module
  176. include path for a mark file indicating the module is provided. If
  177. not found just write the dummy stamp file. If found, we need a rule
  178. to copy-if-different the module file. When a module is provided,
  179. write this mark file.
  180. Solution 3: Use a set of make rules like this:
  181. # When required:
  182. foo.mod.proxy: foo.mod.default
  183. foo.mod.default:: foo.mod.hack
  184. @echo foo.mod.default2 # Search for and copy-if-different the mod file.
  185. foo.mod.hack:
  186. # When provided:
  187. foo.mod.proxy: foo.o.requires
  188. @rm -f foo.mod.hack foo.mod.default
  189. foo.o.requires: foo.mod.hack
  190. @echo foo.o.requires
  191. foo.mod.hack:
  192. @touch foo.mod.hack
  193. @touch foo.mod.default
  194. Solution 4:
  195. When scanning dependencies and providing a module:
  196. - Create a .mod.provided.
  197. - Add .mod.proxy rule depending on corresponding .o.requires.
  198. When scanning dependencies and requiring a module:
  199. - Search the module path for a .mod.provided or a .mod.
  200. - If a .mod.provided is found depend on the corresponding .mod.stamp
  201. (it is provided by CMake in another directory)
  202. - Else, if a .mod is found depend on it directly
  203. (it is provided in another directory by a non-CMake project)
  204. - Else:
  205. - Add the empty proxy rule (if it is provided locally this will hook it)
  206. - Depend on a local .mod.stamp (it might be provided locally)
  207. - Create the dummy local .mod.stamp (it might not be provided locally)
  208. */
  209. return true;
  210. }
  211. //----------------------------------------------------------------------------
  212. bool cmDependsFortran::CheckDependencies(std::istream&)
  213. {
  214. // TODO: Parse and check dependencies.
  215. return true;
  216. }
  217. //----------------------------------------------------------------------------
  218. bool cmDependsFortran::CopyModule(const std::vector<std::string>& args)
  219. {
  220. // Implements
  221. //
  222. // $(CMAKE_COMMAND) -E cmake_copy_f90_mod input.mod output.mod.stamp
  223. //
  224. // Note that the case of the .mod file depends on the compiler. In
  225. // the future this copy could also account for the fact that some
  226. // compilers include a timestamp in the .mod file so it changes even
  227. // when the interface described in the module does not.
  228. std::string mod = args[2];
  229. std::string stamp = args[3];
  230. std::string mod_upper = cmSystemTools::UpperCase(mod.c_str());
  231. std::string mod_lower = cmSystemTools::LowerCase(mod.c_str());
  232. mod += ".mod";
  233. mod_upper += ".mod";
  234. mod_lower += ".mod";
  235. if(cmSystemTools::FileExists(mod_upper.c_str()))
  236. {
  237. if(!cmSystemTools::CopyFileIfDifferent(mod_upper.c_str(), stamp.c_str()))
  238. {
  239. std::cerr << "Error copying Fortran module from \""
  240. << mod_upper.c_str() << "\" to \"" << stamp.c_str()
  241. << "\".\n";
  242. return false;
  243. }
  244. return true;
  245. }
  246. else if(cmSystemTools::FileExists(mod_lower.c_str()))
  247. {
  248. if(!cmSystemTools::CopyFileIfDifferent(mod_lower.c_str(), stamp.c_str()))
  249. {
  250. std::cerr << "Error copying Fortran module from \""
  251. << mod_lower.c_str() << "\" to \"" << stamp.c_str()
  252. << "\".\n";
  253. return false;
  254. }
  255. return true;
  256. }
  257. std::cerr << "Error copying Fortran module \"" << args[2].c_str()
  258. << "\". Tried \"" << mod_upper.c_str()
  259. << "\" and \"" << mod_lower.c_str() << "\".\n";
  260. return false;
  261. }
  262. //----------------------------------------------------------------------------
  263. bool cmDependsFortran::FindIncludeFile(const char* dir,
  264. const char* includeName,
  265. std::string& fileName)
  266. {
  267. // If the file is a full path, include it directly.
  268. if(cmSystemTools::FileIsFullPath(includeName))
  269. {
  270. fileName = includeName;
  271. return cmSystemTools::FileExists(fileName.c_str());
  272. }
  273. else
  274. {
  275. // Check for the file in the directory containing the including
  276. // file.
  277. std::string fullName = dir;
  278. fullName += "/";
  279. fullName += includeName;
  280. if(cmSystemTools::FileExists(fullName.c_str()))
  281. {
  282. fileName = fullName;
  283. return true;
  284. }
  285. // Search the include path for the file.
  286. for(std::vector<std::string>::const_iterator i = m_IncludePath->begin();
  287. i != m_IncludePath->end(); ++i)
  288. {
  289. fullName = *i;
  290. fullName += "/";
  291. fullName += includeName;
  292. if(cmSystemTools::FileExists(fullName.c_str()))
  293. {
  294. fileName = fullName;
  295. return true;
  296. }
  297. }
  298. }
  299. return false;
  300. }
  301. //----------------------------------------------------------------------------
  302. cmDependsFortranParser_s::cmDependsFortranParser_s(cmDependsFortran* self):
  303. Self(self)
  304. {
  305. this->InInterface = 0;
  306. // Initialize the lexical scanner.
  307. cmDependsFortran_yylex_init(&this->Scanner);
  308. cmDependsFortran_yyset_extra(this, this->Scanner);
  309. // Create a dummy buffer that is never read but is the fallback
  310. // buffer when the last file is popped off the stack.
  311. YY_BUFFER_STATE buffer =
  312. cmDependsFortran_yy_create_buffer(0, 4, this->Scanner);
  313. cmDependsFortran_yy_switch_to_buffer(buffer, this->Scanner);
  314. }
  315. //----------------------------------------------------------------------------
  316. cmDependsFortranParser_s::~cmDependsFortranParser_s()
  317. {
  318. cmDependsFortran_yylex_destroy(this->Scanner);
  319. }
  320. //----------------------------------------------------------------------------
  321. int cmDependsFortranParser_FilePush(cmDependsFortranParser* parser,
  322. const char* fname)
  323. {
  324. // Open the new file and push it onto the stack. Save the old
  325. // buffer with it on the stack.
  326. if(FILE* file = fopen(fname, "rb"))
  327. {
  328. YY_BUFFER_STATE current =
  329. cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
  330. std::string dir = cmSystemTools::GetParentDirectory(fname);
  331. cmDependsFortranFile f(file, current, dir);
  332. YY_BUFFER_STATE buffer =
  333. cmDependsFortran_yy_create_buffer(0, 16384, parser->Scanner);
  334. cmDependsFortran_yy_switch_to_buffer(buffer, parser->Scanner);
  335. parser->FileStack.push(f);
  336. return 1;
  337. }
  338. else
  339. {
  340. return 0;
  341. }
  342. }
  343. //----------------------------------------------------------------------------
  344. int cmDependsFortranParser_FilePop(cmDependsFortranParser* parser)
  345. {
  346. // Pop one file off the stack and close it. Switch the lexer back
  347. // to the next one on the stack.
  348. if(parser->FileStack.empty())
  349. {
  350. return 0;
  351. }
  352. else
  353. {
  354. cmDependsFortranFile f = parser->FileStack.top(); parser->FileStack.pop();
  355. fclose(f.File);
  356. YY_BUFFER_STATE current =
  357. cmDependsFortranLexer_GetCurrentBuffer(parser->Scanner);
  358. cmDependsFortran_yy_delete_buffer(current, parser->Scanner);
  359. cmDependsFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
  360. return 1;
  361. }
  362. }
  363. //----------------------------------------------------------------------------
  364. int cmDependsFortranParser_Input(cmDependsFortranParser* parser,
  365. char* buffer, size_t bufferSize)
  366. {
  367. // Read from the file on top of the stack. If the stack is empty,
  368. // the end of the translation unit has been reached.
  369. if(!parser->FileStack.empty())
  370. {
  371. FILE* file = parser->FileStack.top().File;
  372. return (int)fread(buffer, 1, bufferSize, file);
  373. }
  374. return 0;
  375. }
  376. //----------------------------------------------------------------------------
  377. void cmDependsFortranParser_StringStart(cmDependsFortranParser* parser)
  378. {
  379. parser->TokenString = "";
  380. }
  381. //----------------------------------------------------------------------------
  382. const char* cmDependsFortranParser_StringEnd(cmDependsFortranParser* parser)
  383. {
  384. return parser->TokenString.c_str();
  385. }
  386. //----------------------------------------------------------------------------
  387. void cmDependsFortranParser_StringAppend(cmDependsFortranParser* parser,
  388. char c)
  389. {
  390. parser->TokenString += c;
  391. }
  392. //----------------------------------------------------------------------------
  393. void cmDependsFortranParser_SetInInterface(cmDependsFortranParser* parser,
  394. int in)
  395. {
  396. parser->InInterface = in;
  397. }
  398. //----------------------------------------------------------------------------
  399. int cmDependsFortranParser_GetInInterface(cmDependsFortranParser* parser)
  400. {
  401. return parser->InInterface;
  402. }
  403. //----------------------------------------------------------------------------
  404. void cmDependsFortranParser_Error(cmDependsFortranParser*, const char*)
  405. {
  406. // If there is a parser error just ignore it. The source will not
  407. // compile and the user will edit it. Then dependencies will have
  408. // to be regenerated anyway.
  409. }
  410. //----------------------------------------------------------------------------
  411. void cmDependsFortranParser_RuleUse(cmDependsFortranParser* parser,
  412. const char* name)
  413. {
  414. parser->Requires.insert(name);
  415. }
  416. //----------------------------------------------------------------------------
  417. void cmDependsFortranParser_RuleInclude(cmDependsFortranParser* parser,
  418. const char* name)
  419. {
  420. // If processing an include statement there must be an open file.
  421. assert(!parser->FileStack.empty());
  422. // Get the directory containing the source in which the include
  423. // statement appears. This is always the first search location for
  424. // Fortran include files.
  425. std::string dir = parser->FileStack.top().Directory;
  426. // Find the included file. If it cannot be found just ignore the
  427. // problem because either the source will not compile or the user
  428. // does not care about depending on this included source.
  429. std::string fullName;
  430. if(parser->Self->FindIncludeFile(dir.c_str(), name, fullName))
  431. {
  432. // Found the included file. Save it in the set of included files.
  433. parser->Includes.insert(fullName);
  434. // Parse it immediately to translate the source inline.
  435. cmDependsFortranParser_FilePush(parser, fullName.c_str());
  436. }
  437. }
  438. //----------------------------------------------------------------------------
  439. void cmDependsFortranParser_RuleModule(cmDependsFortranParser* parser,
  440. const char* name)
  441. {
  442. parser->Provides.insert(name);
  443. }
  444. //----------------------------------------------------------------------------
  445. void cmDependsFortranParser_RuleDefine(cmDependsFortranParser*, const char*)
  446. {
  447. }
  448. //----------------------------------------------------------------------------
  449. void cmDependsFortranParser_RuleUndef(cmDependsFortranParser*, const char*)
  450. {
  451. }
  452. //----------------------------------------------------------------------------
  453. void cmDependsFortranParser_RuleIfdef(cmDependsFortranParser*, const char*)
  454. {
  455. }
  456. //----------------------------------------------------------------------------
  457. void cmDependsFortranParser_RuleIfndef(cmDependsFortranParser*, const char*)
  458. {
  459. }
  460. //----------------------------------------------------------------------------
  461. void cmDependsFortranParser_RuleIf(cmDependsFortranParser*)
  462. {
  463. }
  464. //----------------------------------------------------------------------------
  465. void cmDependsFortranParser_RuleElif(cmDependsFortranParser*)
  466. {
  467. }
  468. //----------------------------------------------------------------------------
  469. void cmDependsFortranParser_RuleElse(cmDependsFortranParser*)
  470. {
  471. }
  472. //----------------------------------------------------------------------------
  473. void cmDependsFortranParser_RuleEndif(cmDependsFortranParser*)
  474. {
  475. }