cmLocalUnixMakefileGenerator2.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 "cmLocalUnixMakefileGenerator2.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include <queue>
  19. //----------------------------------------------------------------------------
  20. cmLocalUnixMakefileGenerator2::cmLocalUnixMakefileGenerator2()
  21. {
  22. }
  23. //----------------------------------------------------------------------------
  24. cmLocalUnixMakefileGenerator2::~cmLocalUnixMakefileGenerator2()
  25. {
  26. }
  27. //----------------------------------------------------------------------------
  28. void cmLocalUnixMakefileGenerator2::Generate(bool fromTheTop)
  29. {
  30. // TODO: Account for control-c during Makefile generation.
  31. // Generate old style for now.
  32. this->cmLocalUnixMakefileGenerator::Generate(fromTheTop);
  33. // Generate the rule files for each target.
  34. const cmTargets& targets = m_Makefile->GetTargets();
  35. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  36. {
  37. this->GenerateTargetRuleFile(t->second);
  38. }
  39. // Generate the main makefile.
  40. this->GenerateMakefile();
  41. // Generate the cmake file that keeps the makefile up to date.
  42. this->GenerateCMakefile();
  43. }
  44. //----------------------------------------------------------------------------
  45. void cmLocalUnixMakefileGenerator2::GenerateMakefile()
  46. {
  47. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  48. makefileName += "/Makefile2";
  49. std::string cmakefileName = makefileName;
  50. cmakefileName += ".cmake";
  51. // Open the output files.
  52. std::ofstream makefileStream(makefileName.c_str());
  53. if(!makefileStream)
  54. {
  55. cmSystemTools::Error("Error can not open for write: ",
  56. makefileName.c_str());
  57. cmSystemTools::ReportLastSystemError("");
  58. return;
  59. }
  60. // Write the do not edit header.
  61. this->WriteDisclaimer(makefileStream);
  62. // Write some rules to make things look nice.
  63. makefileStream
  64. << "# Disable some common implicit rules to speed things up.\n"
  65. << ".SUFFIXES:\n"
  66. << ".SUFFIXES:.hpuxmakemusthaverule\n\n";
  67. // Write standard variables to the makefile.
  68. this->OutputMakeVariables(makefileStream);
  69. // Build command to run CMake to check if anything needs regenerating.
  70. std::string runRule =
  71. "@$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  72. runRule += " --check-rerun ";
  73. runRule += this->ConvertToRelativeOutputPath(cmakefileName.c_str());
  74. // Most unix makes will pass the command line flags to make down to
  75. // sub-invoked makes via an environment variable. However, some
  76. // makes do not support that, so you have to pass the flags
  77. // explicitly.
  78. const char* depRule = "$(MAKE) -f Makefile2 $(MAKESILENT) all.depends";
  79. const char* allRule = "$(MAKE) -f Makefile2 $(MAKESILENT) all";
  80. if(m_PassMakeflags)
  81. {
  82. depRule = "$(MAKE) -f Makefile2 $(MAKESILENT) -$(MAKEFLAGS) all.depends";
  83. allRule = "$(MAKE) -f Makefile2 $(MAKESILENT) -$(MAKEFLAGS) all";
  84. }
  85. // Write the main entry point target. This must be the VERY first
  86. // target so that make with no arguments will run it.
  87. {
  88. std::vector<std::string> depends;
  89. std::vector<std::string> commands;
  90. commands.push_back(runRule);
  91. commands.push_back(depRule);
  92. commands.push_back(allRule);
  93. this->OutputMakeRule(
  94. makefileStream,
  95. "Default target executed when no arguments are given to make.",
  96. "default_target",
  97. depends,
  98. commands);
  99. }
  100. // Write special target to silence make output. This must be after
  101. // the default target in case VERBOSE is set (which changes the name).
  102. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  103. {
  104. makefileStream
  105. << "# Suppress display of executed commands.\n"
  106. << "$(VERBOSE).SILENT:\n\n";
  107. }
  108. // Get the set of targets.
  109. const cmTargets& targets = m_Makefile->GetTargets();
  110. // Output top level dependency rule.
  111. {
  112. std::vector<std::string> depends;
  113. std::vector<std::string> commands;
  114. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  115. {
  116. if(t->second.IsInAll())
  117. {
  118. std::string dep = this->GetTargetDirectory(t->second);
  119. dep += "/";
  120. dep += t->first;
  121. dep += ".depends";
  122. depends.push_back(dep);
  123. }
  124. }
  125. this->OutputMakeRule(makefileStream, "all dependencies", "all.depends",
  126. depends, commands);
  127. }
  128. // Output top level build rule.
  129. {
  130. std::vector<std::string> depends;
  131. std::vector<std::string> commands;
  132. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  133. {
  134. if(t->second.IsInAll())
  135. {
  136. depends.push_back(t->first+".requires");
  137. }
  138. }
  139. this->OutputMakeRule(makefileStream, "all", "all",
  140. depends, commands);
  141. }
  142. // Write include statements to get rules for each target.
  143. makefileStream
  144. << "# Include target rule files.\n";
  145. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  146. {
  147. std::string ruleFileName = this->GetTargetDirectory(t->second);
  148. ruleFileName += "/";
  149. ruleFileName += t->first;
  150. ruleFileName += ".make";
  151. makefileStream
  152. << m_IncludeDirective << " "
  153. << this->ConvertToOutputForExisting(ruleFileName.c_str()).c_str()
  154. << "\n";
  155. }
  156. }
  157. //----------------------------------------------------------------------------
  158. void cmLocalUnixMakefileGenerator2::GenerateCMakefile()
  159. {
  160. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  161. makefileName += "/Makefile2";
  162. std::string cmakefileName = makefileName;
  163. cmakefileName += ".cmake";
  164. // Open the output file.
  165. std::ofstream cmakefileStream(cmakefileName.c_str());
  166. if(!cmakefileStream)
  167. {
  168. cmSystemTools::Error("Error can not open for write: ",
  169. cmakefileName.c_str());
  170. cmSystemTools::ReportLastSystemError("");
  171. return;
  172. }
  173. // Write the do not edit header.
  174. this->WriteDisclaimer(cmakefileStream);
  175. // Get the list of files contributing to this generation step.
  176. // Sort the list and remove duplicates.
  177. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  178. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  179. std::vector<std::string>::iterator new_end = std::unique(lfiles.begin(),
  180. lfiles.end());
  181. lfiles.erase(new_end, lfiles.end());
  182. // Save the list to the cmake file.
  183. cmakefileStream
  184. << "# The corresponding makefile\n"
  185. << "# \"" << makefileName << "\"\n"
  186. << "# was generated from the following files:\n"
  187. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  188. << " \"" << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\"\n";
  189. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  190. i != lfiles.end(); ++i)
  191. {
  192. cmakefileStream
  193. << " \"" << i->c_str() << "\"\n";
  194. }
  195. cmakefileStream
  196. << " )\n\n";
  197. // Set the corresponding makefile in the cmake file.
  198. cmakefileStream
  199. << "# The corresponding makefile is:\n"
  200. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  201. << " \"" << makefileName.c_str() << "\"\n"
  202. << " )\n";
  203. }
  204. //----------------------------------------------------------------------------
  205. void
  206. cmLocalUnixMakefileGenerator2
  207. ::GenerateTargetRuleFile(const cmTarget& target)
  208. {
  209. // Create a directory for this target.
  210. std::string dir = this->GetTargetDirectory(target);
  211. cmSystemTools::MakeDirectory(dir.c_str());
  212. // First generate the object rule files. Save a list of all object
  213. // files for this target.
  214. std::vector<std::string> objects;
  215. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  216. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  217. source != sources.end(); ++source)
  218. {
  219. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  220. !(*source)->GetCustomCommand())
  221. {
  222. // Generate this object file's rule file.
  223. this->GenerateObjectRuleFile(target, *(*source));
  224. // Save the object file full path.
  225. std::string obj = dir;
  226. obj += "/";
  227. obj += this->GetObjectFileName(*(*source));
  228. objects.push_back(obj);
  229. }
  230. }
  231. // If there is no dependencies file, create an empty one.
  232. std::string depFileName = dir;
  233. depFileName += "/";
  234. depFileName += target.GetName();
  235. depFileName += ".depends.make";
  236. if(!cmSystemTools::FileExists(depFileName.c_str()))
  237. {
  238. std::ofstream depFileStream(depFileName.c_str());
  239. this->WriteDisclaimer(depFileStream);
  240. depFileStream
  241. << "# Empty dependencies file for target " << target.GetName() << ".\n"
  242. << "# This may be replaced when dependencies are built.\n";
  243. }
  244. // Open the rule file. This should be copy-if-different because the
  245. // rules may depend on this file itself.
  246. std::string ruleFileName = dir;
  247. ruleFileName += "/";
  248. ruleFileName += target.GetName();
  249. ruleFileName += ".make";
  250. cmGeneratedFileStream ruleFile(ruleFileName.c_str());
  251. std::ostream& ruleFileStream = ruleFile.GetStream();
  252. if(!ruleFileStream)
  253. {
  254. // TODO: Produce error message that accounts for generated stream
  255. // .tmp.
  256. return;
  257. }
  258. this->WriteDisclaimer(ruleFileStream);
  259. ruleFileStream
  260. << "# Rule file for target " << target.GetName() << ".\n\n";
  261. // Include the dependencies for the target.
  262. ruleFileStream
  263. << "# Include any dependencies generated for this rule.\n"
  264. << m_IncludeDirective << " "
  265. << this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
  266. << "\n\n";
  267. // Include the rule file for each object.
  268. if(!objects.empty())
  269. {
  270. ruleFileStream
  271. << "# Include rules for object files.\n";
  272. for(std::vector<std::string>::const_iterator obj = objects.begin();
  273. obj != objects.end(); ++obj)
  274. {
  275. std::string objRuleFileName = *obj;
  276. objRuleFileName += ".make";
  277. ruleFileStream
  278. << m_IncludeDirective << " "
  279. << this->ConvertToOutputForExisting(objRuleFileName.c_str()).c_str()
  280. << "\n";
  281. }
  282. ruleFileStream
  283. << "\n";
  284. }
  285. // Write the dependency generation rule.
  286. {
  287. std::vector<std::string> depends;
  288. std::vector<std::string> commands;
  289. std::string depComment = "dependencies for ";
  290. depComment += target.GetName();
  291. std::string depTarget = dir;
  292. depTarget += "/";
  293. depTarget += target.GetName();
  294. depTarget += ".depends";
  295. for(std::vector<std::string>::const_iterator obj = objects.begin();
  296. obj != objects.end(); ++obj)
  297. {
  298. depends.push_back((*obj)+".depends");
  299. }
  300. depends.push_back(ruleFileName);
  301. std::string touchCmd = "@touch ";
  302. touchCmd += this->ConvertToRelativeOutputPath(depTarget.c_str());
  303. // TODO: Construct dependency generation rule and append command.
  304. commands.push_back(touchCmd);
  305. this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
  306. depends, commands);
  307. }
  308. // Write the requires rule.
  309. {
  310. std::vector<std::string> depends;
  311. std::vector<std::string> commands;
  312. std::string reqComment = "requirements for ";
  313. reqComment += target.GetName();
  314. std::string reqTarget = target.GetName();
  315. reqTarget += ".requires";
  316. for(std::vector<std::string>::const_iterator obj = objects.begin();
  317. obj != objects.end(); ++obj)
  318. {
  319. depends.push_back(*obj);
  320. }
  321. depends.push_back(ruleFileName);
  322. this->OutputMakeRule(ruleFileStream, reqComment.c_str(), reqTarget.c_str(),
  323. depends, commands);
  324. }
  325. #if 0
  326. // Write the build rule.
  327. {
  328. std::vector<std::string> depends;
  329. std::vector<std::string> commands;
  330. std::string buildComment = " target ";
  331. buildComment += objName;
  332. for(std::vector<std::string>::const_iterator obj = objects.begin();
  333. obj != objects.end(); ++obj)
  334. {
  335. depends.push_back(*obj);
  336. }
  337. depends.push_back(ruleFileName);
  338. std::string touchCmd = "@touch ";
  339. touchCmd += this->ConvertToRelativeOutputPath(obj.c_str());
  340. // TODO: Construct build rule and append command.
  341. commands.push_back(touchCmd);
  342. this->OutputMakeRule(ruleFileStream, buildComment.c_str(), obj.c_str(),
  343. depends, commands);
  344. }
  345. #endif
  346. }
  347. //----------------------------------------------------------------------------
  348. void
  349. cmLocalUnixMakefileGenerator2
  350. ::GenerateObjectRuleFile(const cmTarget& target, const cmSourceFile& source)
  351. {
  352. // Get the full path name of the object file.
  353. std::string objName = this->GetObjectFileName(source);
  354. std::string obj = this->GetTargetDirectory(target);
  355. obj += "/";
  356. obj += objName;
  357. // Create the directory containing the object file. This may be a
  358. // subdirectory under the target's directory.
  359. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  360. cmSystemTools::MakeDirectory(dir.c_str());
  361. // If there is no dependencies file, create an empty one.
  362. std::string depFileName = obj;
  363. depFileName += ".depends.make";
  364. if(!cmSystemTools::FileExists(depFileName.c_str()))
  365. {
  366. std::ofstream depFileStream(depFileName.c_str());
  367. this->WriteDisclaimer(depFileStream);
  368. depFileStream
  369. << "# Empty dependencies file for object file "
  370. << objName.c_str() << ".\n"
  371. << "# This may be replaced when dependencies are built.\n";
  372. }
  373. // Open the rule file for writing. This should be copy-if-different
  374. // because the rules may depend on this file itself.
  375. std::string ruleFileName = obj;
  376. ruleFileName += ".make";
  377. cmGeneratedFileStream ruleFile(ruleFileName.c_str());
  378. std::ostream& ruleFileStream = ruleFile.GetStream();
  379. if(!ruleFileStream)
  380. {
  381. // TODO: Produce error message that accounts for generated stream
  382. // .tmp.
  383. return;
  384. }
  385. this->WriteDisclaimer(ruleFileStream);
  386. ruleFileStream
  387. << "# Rule file for object file " << objName.c_str() << ".\n\n";
  388. // Include the dependencies for the target.
  389. ruleFileStream
  390. << "# Include any dependencies generated for this rule.\n"
  391. << m_IncludeDirective << " "
  392. << this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
  393. << "\n\n";
  394. // Identify the language of the source file.
  395. const char* lang =
  396. m_GlobalGenerator->GetLanguageFromExtension(source.GetSourceExtension().c_str());
  397. // Write the dependency generation rule.
  398. std::string depTarget = obj;
  399. depTarget += ".depends";
  400. {
  401. std::vector<std::string> depends;
  402. std::vector<std::string> commands;
  403. std::string depComment = "dependencies for ";
  404. depComment += objName;
  405. depends.push_back(source.GetFullPath());
  406. depends.push_back(ruleFileName);
  407. cmOStringStream depCmd;
  408. // TODO: Account for source file properties and directory-level
  409. // definitions.
  410. depCmd << "$(CMAKE_COMMAND) -E cmake_depends " << lang << " "
  411. << this->ConvertToRelativeOutputPath(obj.c_str()) << " "
  412. << this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  413. std::vector<std::string> includeDirs;
  414. this->GetIncludeDirectories(includeDirs);
  415. for(std::vector<std::string>::iterator i = includeDirs.begin();
  416. i != includeDirs.end(); ++i)
  417. {
  418. depCmd << " -I" << this->ConvertToRelativeOutputPath(i->c_str());
  419. }
  420. commands.push_back(depCmd.str());
  421. std::string touchCmd = "@touch ";
  422. touchCmd += this->ConvertToRelativeOutputPath(depTarget.c_str());
  423. commands.push_back(touchCmd);
  424. this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
  425. depends, commands);
  426. }
  427. // Write the build rule.
  428. {
  429. std::vector<std::string> depends;
  430. std::vector<std::string> commands;
  431. std::string buildComment = "object ";
  432. buildComment += objName;
  433. depends.push_back(source.GetFullPath());
  434. depends.push_back(ruleFileName);
  435. std::string touchCmd = "@touch ";
  436. touchCmd += this->ConvertToRelativeOutputPath(obj.c_str());
  437. // TODO: Construct build rule and append command.
  438. commands.push_back(touchCmd);
  439. this->OutputMakeRule(ruleFileStream, buildComment.c_str(), obj.c_str(),
  440. depends, commands);
  441. }
  442. }
  443. //----------------------------------------------------------------------------
  444. void cmLocalUnixMakefileGenerator2::WriteDisclaimer(std::ostream& os)
  445. {
  446. os
  447. << "# CMAKE generated file: DO NOT EDIT!\n"
  448. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  449. << " Generator, CMake Version "
  450. << cmMakefile::GetMajorVersion() << "."
  451. << cmMakefile::GetMinorVersion() << "\n\n";
  452. }
  453. //----------------------------------------------------------------------------
  454. std::string
  455. cmLocalUnixMakefileGenerator2
  456. ::GetTargetDirectory(const cmTarget& target)
  457. {
  458. std::string dir = m_Makefile->GetStartOutputDirectory();
  459. dir += "/";
  460. dir += target.GetName();
  461. dir += ".dir";
  462. return dir;
  463. }
  464. //----------------------------------------------------------------------------
  465. std::string
  466. cmLocalUnixMakefileGenerator2
  467. ::GetObjectFileName(const cmSourceFile& source)
  468. {
  469. // If the full path to the source file includes this directory,
  470. // we want to use the relative path for the filename of the
  471. // object file. Otherwise, we will use just the filename
  472. // portion.
  473. std::string objectName;
  474. if((cmSystemTools::GetFilenamePath(
  475. source.GetFullPath()).find(
  476. m_Makefile->GetCurrentDirectory()) == 0)
  477. || (cmSystemTools::GetFilenamePath(
  478. source.GetFullPath()).find(
  479. m_Makefile->GetCurrentOutputDirectory()) == 0))
  480. {
  481. objectName = source.GetSourceName();
  482. }
  483. else
  484. {
  485. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  486. }
  487. // Append the object file extension.
  488. objectName +=
  489. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  490. source.GetSourceExtension().c_str());
  491. return objectName;
  492. }
  493. //----------------------------------------------------------------------------
  494. bool
  495. cmLocalUnixMakefileGenerator2
  496. ::ScanDependencies(std::vector<std::string> const& args)
  497. {
  498. // Format of arguments is:
  499. // $(CMAKE_COMMAND), cmake_depends, <lang>, <obj>, <src>, [include-flags]
  500. // The caller has ensured that all required arguments exist.
  501. // The file to which to write dependencies.
  502. const char* objFile = args[3].c_str();
  503. // The source file at which to start the scan.
  504. const char* srcFile = args[4].c_str();
  505. // Convert the include flags to full paths.
  506. std::vector<std::string> includes;
  507. for(unsigned int i=5; i < args.size(); ++i)
  508. {
  509. if(args[i].substr(0, 2) == "-I")
  510. {
  511. // Get the include path without the -I flag.
  512. std::string inc = args[i].substr(2);
  513. includes.push_back(cmSystemTools::CollapseFullPath(inc.c_str()));
  514. }
  515. }
  516. // Dispatch the scan for each language.
  517. std::string const& lang = args[2];
  518. if(lang == "C" || lang == "CXX")
  519. {
  520. return cmLocalUnixMakefileGenerator2::ScanDependenciesC(objFile, srcFile,
  521. includes);
  522. }
  523. return false;
  524. }
  525. //----------------------------------------------------------------------------
  526. void
  527. cmLocalUnixMakefileGenerator2ScanDependenciesC(
  528. std::ifstream& fin,
  529. std::set<cmStdString>& encountered,
  530. std::queue<cmStdString>& unscanned)
  531. {
  532. // Regular expression to identify C preprocessor include directives.
  533. cmsys::RegularExpression
  534. includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  535. // Read one line at a time.
  536. std::string line;
  537. while(cmSystemTools::GetLineFromStream(fin, line))
  538. {
  539. // Match include directives.
  540. if(includeLine.find(line.c_str()))
  541. {
  542. // Get the file being included.
  543. std::string includeFile = includeLine.match(1);
  544. // Queue the file if it has not yet been encountered.
  545. if(encountered.find(includeFile) == encountered.end())
  546. {
  547. encountered.insert(includeFile);
  548. unscanned.push(includeFile);
  549. }
  550. }
  551. }
  552. }
  553. //----------------------------------------------------------------------------
  554. bool
  555. cmLocalUnixMakefileGenerator2
  556. ::ScanDependenciesC(const char* objFile, const char* srcFile,
  557. std::vector<std::string> const& includes)
  558. {
  559. // Walk the dependency graph starting with the source file.
  560. std::set<cmStdString> dependencies;
  561. std::set<cmStdString> encountered;
  562. std::set<cmStdString> scanned;
  563. std::queue<cmStdString> unscanned;
  564. unscanned.push(srcFile);
  565. encountered.insert(srcFile);
  566. while(!unscanned.empty())
  567. {
  568. // Get the next file to scan.
  569. std::string fname = unscanned.front();
  570. unscanned.pop();
  571. // If not a full path, find the file in the include path.
  572. std::string fullName;
  573. if(cmSystemTools::FileIsFullPath(fname.c_str()))
  574. {
  575. fullName = fname;
  576. }
  577. else
  578. {
  579. for(std::vector<std::string>::const_iterator i = includes.begin();
  580. i != includes.end(); ++i)
  581. {
  582. std::string temp = *i;
  583. temp += "/";
  584. temp += fname;
  585. if(cmSystemTools::FileExists(temp.c_str()))
  586. {
  587. fullName = temp;
  588. break;
  589. }
  590. }
  591. }
  592. // Scan the file if it has not been scanned already.
  593. if(scanned.find(fullName) == scanned.end())
  594. {
  595. // Record scanned files.
  596. scanned.insert(fullName);
  597. // Try to scan the file. Just leave it out if we cannot find
  598. // it.
  599. std::ifstream fin(fullName.c_str());
  600. if(fin)
  601. {
  602. // Add this file as a dependency.
  603. dependencies.insert(fullName);
  604. // Scan this file for new dependencies.
  605. cmLocalUnixMakefileGenerator2ScanDependenciesC(fin, encountered,
  606. unscanned);
  607. }
  608. }
  609. }
  610. // Write the dependencies to the output file.
  611. std::string depMakeFile = objFile;
  612. depMakeFile += ".depends.make";
  613. std::ofstream fout(depMakeFile.c_str());
  614. fout << "# Dependencies for " << objFile << std::endl;
  615. for(std::set<cmStdString>::iterator i=dependencies.begin();
  616. i != dependencies.end(); ++i)
  617. {
  618. fout << objFile << " : " << i->c_str() << std::endl;
  619. }
  620. fout << std::endl;
  621. fout << "# Dependencies for " << objFile << ".depends" << std::endl;
  622. for(std::set<cmStdString>::iterator i=dependencies.begin();
  623. i != dependencies.end(); ++i)
  624. {
  625. fout << objFile << ".depends : " << i->c_str() << std::endl;
  626. }
  627. return true;
  628. }