cmGlobalUnixMakefileGenerator3.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator3
  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 "cmGlobalUnixMakefileGenerator3.h"
  14. #include "cmLocalUnixMakefileGenerator3.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. #include "cmGeneratedFileStream.h"
  18. cmGlobalUnixMakefileGenerator3::cmGlobalUnixMakefileGenerator3()
  19. {
  20. // This type of makefile always requires unix style paths
  21. m_ForceUnixPaths = true;
  22. m_FindMakeProgramFile = "CMakeUnixFindMake.cmake";
  23. }
  24. void cmGlobalUnixMakefileGenerator3
  25. ::EnableLanguage(std::vector<std::string>const& languages, cmMakefile *mf)
  26. {
  27. mf->AddDefinition("CMAKE_CFG_INTDIR",".");
  28. this->cmGlobalGenerator::EnableLanguage(languages, mf);
  29. std::string path;
  30. for(std::vector<std::string>::const_iterator l = languages.begin();
  31. l != languages.end(); ++l)
  32. {
  33. const char* lang = l->c_str();
  34. std::string langComp = "CMAKE_";
  35. langComp += lang;
  36. langComp += "_COMPILER";
  37. if(!mf->GetDefinition(langComp.c_str()))
  38. {
  39. cmSystemTools::Error(langComp.c_str(), " not set, after EnableLanguage");
  40. continue;
  41. }
  42. const char* cc = mf->GetRequiredDefinition(langComp.c_str());
  43. path = cmSystemTools::FindProgram(cc);
  44. if(path.size() == 0)
  45. {
  46. std::string message = "your ";
  47. message += lang;
  48. message += " compiler: ";
  49. if(cc)
  50. {
  51. message += cc;
  52. }
  53. else
  54. {
  55. message += "(NULL)";
  56. }
  57. message += " was not found in your path. "
  58. "For CMake to correctly use try compile commands, the compiler must "
  59. "be in your path. Please add the compiler to your PATH environment,"
  60. " and re-run CMake.";
  61. cmSystemTools::Error(message.c_str());
  62. }
  63. }
  64. }
  65. ///! Create a local generator appropriate to this Global Generator
  66. cmLocalGenerator *cmGlobalUnixMakefileGenerator3::CreateLocalGenerator()
  67. {
  68. cmLocalGenerator* lg = new cmLocalUnixMakefileGenerator3;
  69. lg->SetGlobalGenerator(this);
  70. return lg;
  71. }
  72. //----------------------------------------------------------------------------
  73. void cmGlobalUnixMakefileGenerator3::GetDocumentation(cmDocumentationEntry& entry) const
  74. {
  75. entry.name = this->GetName();
  76. entry.brief = "Generates standard UNIX makefiles.";
  77. entry.full =
  78. "A hierarchy of UNIX makefiles is generated into the build tree. Any "
  79. "standard UNIX-style make program can build the project through the "
  80. "default make target. A \"make install\" target is also provided.";
  81. }
  82. //----------------------------------------------------------------------------
  83. void cmGlobalUnixMakefileGenerator3::Generate()
  84. {
  85. // first do superclass method
  86. this->cmGlobalGenerator::Generate();
  87. // write the main makefile
  88. this->WriteMainMakefile();
  89. this->WriteMainCMakefile();
  90. // now write the support Makefiles
  91. //this->WriteBuildMakefile();
  92. }
  93. void cmGlobalUnixMakefileGenerator3::WriteMainMakefile()
  94. {
  95. // Open the output file. This should not be copy-if-different
  96. // because the check-build-system step compares the makefile time to
  97. // see if the build system must be regenerated.
  98. std::string makefileName =
  99. this->GetCMakeInstance()->GetHomeOutputDirectory();
  100. makefileName += "/Makefile";
  101. cmGeneratedFileStream makefileStream(makefileName.c_str());
  102. if(!makefileStream)
  103. {
  104. return;
  105. }
  106. // get a local generator for some useful methods
  107. cmLocalUnixMakefileGenerator3 *lg =
  108. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  109. // Write the do not edit header.
  110. lg->WriteDisclaimer(makefileStream);
  111. // Write the main entry point target. This must be the VERY first
  112. // target so that make with no arguments will run it.
  113. // Just depend on the all target to drive the build.
  114. std::vector<std::string> depends;
  115. std::vector<std::string> no_commands;
  116. depends.push_back("all");
  117. // Write the rule.
  118. lg->WriteMakeRule(makefileStream,
  119. "Default target executed when no arguments are "
  120. "given to make.",
  121. "default_target",
  122. depends,
  123. no_commands);
  124. lg->WriteMakeVariables(makefileStream);
  125. lg->WriteSpecialTargetsTop(makefileStream);
  126. this->WriteAllRules(lg,makefileStream);
  127. // write the target convenience rules
  128. unsigned int i;
  129. for (i = 0; i < m_LocalGenerators.size(); ++i)
  130. {
  131. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  132. // are any parents excluded
  133. bool exclude = false;
  134. cmLocalGenerator *lg3 = lg;
  135. while (lg3)
  136. {
  137. if (lg3->GetExcludeAll())
  138. {
  139. exclude = true;
  140. break;
  141. }
  142. lg3 = lg3->GetParent();
  143. }
  144. this->WriteConvenienceRules(makefileStream,lg,exclude);
  145. }
  146. this->WriteHelpRule(makefileStream);
  147. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  148. lg->WriteSpecialTargetsBottom(makefileStream);
  149. }
  150. //----------------------------------------------------------------------------
  151. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile()
  152. {
  153. // Open the output file. This should not be copy-if-different
  154. // because the check-build-system step compares the makefile time to
  155. // see if the build system must be regenerated.
  156. std::string cmakefileName =
  157. this->GetCMakeInstance()->GetHomeOutputDirectory();
  158. cmakefileName += "/Makefile.cmake";
  159. cmGeneratedFileStream cmakefileStream(cmakefileName.c_str());
  160. if(!cmakefileStream)
  161. {
  162. return;
  163. }
  164. std::string makefileName =
  165. this->GetCMakeInstance()->GetHomeOutputDirectory();
  166. makefileName += "/Makefile";
  167. // get a local generator for some useful methods
  168. cmLocalUnixMakefileGenerator3 *lg =
  169. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  170. // Write the do not edit header.
  171. lg->WriteDisclaimer(cmakefileStream);
  172. // Save the generator name
  173. cmakefileStream
  174. << "# The generator used is:\n"
  175. << "SET(CMAKE_DEPENDS_GENERATOR \"" << this->GetName() << "\")\n\n";
  176. // for each cmMakefile get its list of dependencies
  177. std::vector<std::string> lfiles;
  178. for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
  179. {
  180. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  181. // Get the list of files contributing to this generation step.
  182. lfiles.insert(lfiles.end(),lg->GetMakefile()->GetListFiles().begin(),
  183. lg->GetMakefile()->GetListFiles().end());
  184. }
  185. // Sort the list and remove duplicates.
  186. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  187. std::vector<std::string>::iterator new_end =
  188. std::unique(lfiles.begin(),lfiles.end());
  189. lfiles.erase(new_end, lfiles.end());
  190. // reset lg to the first makefile
  191. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  192. // Build the path to the cache file.
  193. std::string cache = this->GetCMakeInstance()->GetHomeOutputDirectory();
  194. cache += "/CMakeCache.txt";
  195. // Save the list to the cmake file.
  196. cmakefileStream
  197. << "# The top level Makefile was generated from the following files:\n"
  198. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  199. << " \"" << lg->ConvertToRelativePath(cache.c_str()).c_str() << "\"\n";
  200. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  201. i != lfiles.end(); ++i)
  202. {
  203. cmakefileStream
  204. << " \"" << lg->ConvertToRelativePath(i->c_str()).c_str()
  205. << "\"\n";
  206. }
  207. cmakefileStream
  208. << " )\n\n";
  209. // Build the path to the cache check file.
  210. std::string check = this->GetCMakeInstance()->GetHomeOutputDirectory();
  211. check += "/cmake.check_cache";
  212. // Set the corresponding makefile in the cmake file.
  213. cmakefileStream
  214. << "# The corresponding makefile is:\n"
  215. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  216. << " \"" << lg->ConvertToRelativePath(makefileName.c_str()).c_str() << "\"\n"
  217. << " \"" << lg->ConvertToRelativePath(check.c_str()).c_str() << "\"\n";
  218. // add in all the directory information files
  219. std::string tmpStr;
  220. for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
  221. {
  222. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  223. tmpStr = lg->GetMakefile()->GetStartOutputDirectory();
  224. tmpStr += "/CMakeDirectoryInformation.cmake";
  225. cmakefileStream << " \"" <<
  226. lg->Convert(tmpStr.c_str(),cmLocalGenerator::HOME_OUTPUT).c_str() << "\"\n";
  227. }
  228. cmakefileStream << " )\n\n";
  229. this->WriteMainCMakefileLanguageRules(cmakefileStream);
  230. }
  231. void cmGlobalUnixMakefileGenerator3
  232. ::WriteMainCMakefileLanguageRules(cmGeneratedFileStream& cmakefileStream)
  233. {
  234. cmLocalUnixMakefileGenerator3 *lg;
  235. // now write all the language stuff
  236. // Set the set of files to check for dependency integrity.
  237. // loop over all of the local generators to collect this
  238. std::set<cmStdString> checkSetLangs;
  239. for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
  240. {
  241. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  242. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>& checkSet =
  243. lg->GetIntegrityCheckSet();
  244. for(std::map<cmStdString,
  245. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  246. l = checkSet.begin(); l != checkSet.end(); ++l)
  247. {
  248. checkSetLangs.insert(l->first);
  249. }
  250. }
  251. // list the languages
  252. cmakefileStream
  253. << "# The set of files whose dependency integrity should be checked:\n";
  254. cmakefileStream
  255. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  256. for(std::set<cmStdString>::iterator
  257. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  258. {
  259. cmakefileStream << " \"" << l->c_str() << "\"\n";
  260. }
  261. cmakefileStream << " )\n";
  262. // now list the files for each language
  263. for(std::set<cmStdString>::iterator
  264. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  265. {
  266. cmakefileStream
  267. << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
  268. // now for each local gen get the checkset
  269. for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
  270. {
  271. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  272. // get the check set for this local gen and language
  273. cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet =
  274. lg->GetIntegrityCheckSet()[*l];
  275. // for each file
  276. for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator csIter =
  277. iCheckSet.begin();
  278. csIter != iCheckSet.end(); ++csIter)
  279. {
  280. cmakefileStream << " \"" <<
  281. lg->Convert(csIter->c_str(),cmLocalGenerator::HOME_OUTPUT).c_str() << "\"\n";
  282. }
  283. }
  284. cmakefileStream << " )\n";
  285. }
  286. }
  287. void cmGlobalUnixMakefileGenerator3::WriteBuildMakefile()
  288. {
  289. unsigned int i;
  290. // Open the output file. This should not be copy-if-different
  291. // because the check-build-system step compares the makefile time to
  292. // see if the build system must be regenerated.
  293. std::string makefileName = this->GetCMakeInstance()->GetHomeOutputDirectory();
  294. makefileName += "/build.make";
  295. cmGeneratedFileStream makefileStream(makefileName.c_str());
  296. if(!makefileStream)
  297. {
  298. return;
  299. }
  300. // get a local generator for some useful methods
  301. cmLocalUnixMakefileGenerator3 *lg =
  302. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  303. // Write the do not edit header.
  304. lg->WriteDisclaimer(makefileStream);
  305. lg->WriteMakeVariables(makefileStream);
  306. // add the generic dependency
  307. std::vector<std::string> depends;
  308. std::vector<std::string> no_commands;
  309. lg->WriteMakeRule(makefileStream, 0, "build", depends, no_commands);
  310. lg->WriteMakeRule(makefileStream, 0, "depend", depends, no_commands);
  311. lg->WriteMakeRule(makefileStream, 0, "requires", depends, no_commands);
  312. // include all the target depends
  313. for (i = 0; i < m_LocalGenerators.size(); ++i)
  314. {
  315. cmLocalUnixMakefileGenerator3 *lg2 =
  316. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  317. // are any parents excluded
  318. bool exclude = false;
  319. cmLocalGenerator *lg3 = lg2;
  320. while (lg3)
  321. {
  322. if (lg3->GetExcludeAll())
  323. {
  324. exclude = true;
  325. break;
  326. }
  327. lg3 = lg3->GetParent();
  328. }
  329. lg2->WriteMainTargetIncludes(makefileStream,"build.make","build");
  330. lg2->WriteMainTargetRules(makefileStream,"build.make","depend",!exclude);
  331. lg2->WriteMainTargetRules(makefileStream,"build.make","requires",!exclude);
  332. lg2->WriteMainTargetRules(makefileStream,"build.make","build",!exclude);
  333. }
  334. }
  335. //----------------------------------------------------------------------------
  336. void cmGlobalUnixMakefileGenerator3
  337. ::WriteAllRules(cmLocalUnixMakefileGenerator3 *lg,
  338. std::ostream& makefileStream)
  339. {
  340. // Write section header.
  341. lg->WriteDivider(makefileStream);
  342. makefileStream
  343. << "# Rules to build dependencies and targets.\n"
  344. << "\n";
  345. std::vector<std::string> depends;
  346. std::vector<std::string> commands;
  347. // Check the build system in this directory.
  348. depends.push_back("cmake_check_build_system");
  349. commands.push_back(lg->GetRecursiveMakeCall("Makefile","all/all"));
  350. // Write the rule.
  351. lg->WriteMakeRule(makefileStream, "The main all target", "all", depends, commands);
  352. // Write and empty all/all:
  353. commands.clear();
  354. lg->WriteMakeRule(makefileStream, "The main recursive all target", "all/all",
  355. commands, commands);
  356. // write the clean
  357. lg->WriteMakeRule(makefileStream, "The main clean target", "clean",
  358. commands, commands);
  359. }
  360. //----------------------------------------------------------------------------
  361. void
  362. cmGlobalUnixMakefileGenerator3
  363. ::WriteConvenienceRules(std::ostream& ruleFileStream,
  364. cmLocalUnixMakefileGenerator3 *lg,
  365. bool exclude)
  366. {
  367. std::vector<std::string> depends;
  368. std::vector<std::string> commands;
  369. std::string localName;
  370. std::string makeTargetName;
  371. depends.push_back("cmake_check_build_system");
  372. if (lg->GetParent())
  373. {
  374. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  375. dir = lg->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  376. localName = dir;
  377. localName += "/directorystart";
  378. makeTargetName = dir;
  379. makeTargetName += "/directory";
  380. std::vector<std::string> all_tgts;
  381. // for all of out targets
  382. for (cmTargets::const_iterator l = lg->GetMakefile()->GetTargets().begin();
  383. l != lg->GetMakefile()->GetTargets().end(); l++)
  384. {
  385. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  386. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  387. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  388. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  389. (l->second.GetType() == cmTarget::UTILITY))
  390. {
  391. // Add this to the list of depends rules in this directory.
  392. if(l->second.IsInAll())
  393. {
  394. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  395. tname += "/all";
  396. all_tgts.push_back(tname);
  397. }
  398. }
  399. }
  400. // write the directory rule add in the subdirs
  401. std::vector<cmLocalGenerator *> subdirs = lg->GetChildren();
  402. // for each subdir add the directory depend
  403. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  404. for (; sdi != subdirs.end(); ++sdi)
  405. {
  406. cmLocalUnixMakefileGenerator3 * lg2 =
  407. static_cast<cmLocalUnixMakefileGenerator3 *>(*sdi);
  408. dir = lg2->GetMakefile()->GetStartOutputDirectory();
  409. dir += "/directory";
  410. dir = lg2->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,
  411. cmLocalGenerator::MAKEFILE);
  412. all_tgts.push_back(dir);
  413. }
  414. // write the directory rule
  415. commands.clear();
  416. commands.push_back
  417. (lg->GetRecursiveMakeCall("Makefile",makeTargetName.c_str()));
  418. // Write the rule.
  419. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory.",
  420. localName.c_str(), depends, commands);
  421. // Write the rule.
  422. commands.clear();
  423. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory.",
  424. makeTargetName.c_str(), all_tgts, commands);
  425. }
  426. // now do the clean targets
  427. if (lg->GetParent())
  428. {
  429. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  430. dir = lg->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  431. makeTargetName = dir;
  432. makeTargetName += "/clean";
  433. std::vector<std::string> all_tgts;
  434. // for all of out targets
  435. for (cmTargets::const_iterator l = lg->GetMakefile()->GetTargets().begin();
  436. l != lg->GetMakefile()->GetTargets().end(); l++)
  437. {
  438. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  439. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  440. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  441. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  442. (l->second.GetType() == cmTarget::UTILITY))
  443. {
  444. // Add this to the list of depends rules in this directory.
  445. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  446. tname += "/clean";
  447. all_tgts.push_back(tname);
  448. }
  449. }
  450. // write the directory rule add in the subdirs
  451. std::vector<cmLocalGenerator *> subdirs = lg->GetChildren();
  452. // for each subdir add the directory depend
  453. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  454. for (; sdi != subdirs.end(); ++sdi)
  455. {
  456. cmLocalUnixMakefileGenerator3 * lg2 =
  457. static_cast<cmLocalUnixMakefileGenerator3 *>(*sdi);
  458. dir = lg2->GetMakefile()->GetStartOutputDirectory();
  459. dir += "/clean";
  460. dir = lg2->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,
  461. cmLocalGenerator::MAKEFILE);
  462. all_tgts.push_back(dir);
  463. }
  464. // write the directory clean rule
  465. commands.clear();
  466. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory clean.",
  467. makeTargetName.c_str(), all_tgts, commands);
  468. }
  469. // for each target Generate the rule files for each target.
  470. const cmTargets& targets = lg->GetMakefile()->GetTargets();
  471. bool needRequiresStep = this->NeedRequiresStep(lg);
  472. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  473. {
  474. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  475. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  476. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  477. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  478. (t->second.GetType() == cmTarget::UTILITY))
  479. {
  480. // Add a rule to build the target by name.
  481. localName = lg->GetRelativeTargetDirectory(t->second);
  482. std::string makefileName = localName;
  483. makefileName += "/build.make";
  484. commands.clear();
  485. if (t->second.GetType() != cmTarget::UTILITY)
  486. {
  487. makeTargetName = localName;
  488. makeTargetName += "/depend";
  489. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  490. makeTargetName.c_str()));
  491. // add requires if we need it for this generator
  492. if (needRequiresStep)
  493. {
  494. makeTargetName = localName;
  495. makeTargetName += "/requires";
  496. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  497. makeTargetName.c_str()));
  498. }
  499. }
  500. makeTargetName = localName;
  501. makeTargetName += "/build";
  502. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  503. makeTargetName.c_str()));
  504. // Write the rule.
  505. localName += "/all";
  506. depends.clear();
  507. this->AppendGlobalTargetDepends(depends,t->second);
  508. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  509. localName.c_str(), depends, commands);
  510. // add the all/all dependency
  511. if (!exclude && t->second.IsInAll())
  512. {
  513. depends.clear();
  514. depends.push_back(localName);
  515. commands.clear();
  516. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  517. "all/all", depends, commands);
  518. }
  519. // Write the rule.
  520. commands.clear();
  521. commands.push_back(lg->GetRecursiveMakeCall("Makefile",localName.c_str()));
  522. depends.clear();
  523. depends.push_back("cmake_check_build_system");
  524. localName = lg->GetRelativeTargetDirectory(t->second);
  525. lg->WriteMakeRule(ruleFileStream, "Build rule for subir invocation for target.",
  526. localName.c_str(), depends, commands);
  527. // Add a target with the canonical name (no prefix, suffix or path).
  528. commands.clear();
  529. depends.clear();
  530. depends.push_back(localName);
  531. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  532. t->second.GetName(), depends, commands);
  533. // add the clean rule
  534. makeTargetName = localName;
  535. makeTargetName += "/clean";
  536. depends.clear();
  537. commands.clear();
  538. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  539. makeTargetName.c_str()));
  540. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  541. makeTargetName.c_str(), depends, commands);
  542. commands.clear();
  543. depends.push_back(makeTargetName);
  544. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  545. "clean", depends, commands);
  546. }
  547. }
  548. }
  549. //----------------------------------------------------------------------------
  550. void
  551. cmGlobalUnixMakefileGenerator3
  552. ::AppendGlobalTargetDepends(std::vector<std::string>& depends,
  553. const cmTarget& target)
  554. {
  555. // Keep track of dependencies already listed.
  556. std::set<cmStdString> emitted;
  557. // A target should not depend on itself.
  558. emitted.insert(target.GetName());
  559. // Loop over all library dependencies but not for static libs
  560. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  561. {
  562. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  563. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  564. lib != tlibs.end(); ++lib)
  565. {
  566. // Don't emit the same library twice for this target.
  567. if(emitted.insert(lib->first).second)
  568. {
  569. // Add this dependency.
  570. this->AppendAnyGlobalDepend(depends, lib->first.c_str());
  571. }
  572. }
  573. }
  574. // Loop over all utility dependencies.
  575. const std::set<cmStdString>& tutils = target.GetUtilities();
  576. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  577. util != tutils.end(); ++util)
  578. {
  579. // Don't emit the same utility twice for this target.
  580. if(emitted.insert(*util).second)
  581. {
  582. // Add this dependency.
  583. this->AppendAnyGlobalDepend(depends, util->c_str());
  584. }
  585. }
  586. }
  587. //----------------------------------------------------------------------------
  588. void
  589. cmGlobalUnixMakefileGenerator3
  590. ::AppendAnyGlobalDepend(std::vector<std::string>& depends, const char* name)
  591. {
  592. cmTarget *result;
  593. // search each local generator until a match is found
  594. unsigned int i;
  595. for (i = 0; i < m_LocalGenerators.size(); ++i)
  596. {
  597. // search all targets
  598. result = m_LocalGenerators[i]->GetMakefile()->FindTarget(name);
  599. // if a match was found then ...
  600. if (result)
  601. {
  602. cmLocalUnixMakefileGenerator3 *lg3 =
  603. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  604. std::string tgtName = lg3->GetRelativeTargetDirectory(*result);
  605. tgtName += "/all";
  606. depends.push_back(tgtName);
  607. return;
  608. }
  609. }
  610. }
  611. //----------------------------------------------------------------------------
  612. void
  613. cmGlobalUnixMakefileGenerator3::WriteHelpRule(std::ostream& ruleFileStream)
  614. {
  615. cmLocalUnixMakefileGenerator3 *lg =
  616. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  617. // add the help target
  618. std::string path;
  619. std::vector<std::string> no_depends;
  620. std::vector<std::string> commands;
  621. lg->AppendEcho(commands,
  622. "The following are some of the valid targets for this Makefile:");
  623. lg->AppendEcho(commands,"... all (the default if no target is provided)");
  624. lg->AppendEcho(commands,"... clean");
  625. lg->AppendEcho(commands,"... depend");
  626. lg->AppendEcho(commands,"... install");
  627. lg->AppendEcho(commands,"... rebuild_cache");
  628. // for each local generator
  629. unsigned int i;
  630. for (i = 0; i < m_LocalGenerators.size(); ++i)
  631. {
  632. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  633. // for each target Generate the rule files for each target.
  634. const cmTargets& targets = lg->GetMakefile()->GetTargets();
  635. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  636. {
  637. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  638. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  639. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  640. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  641. (t->second.GetType() == cmTarget::UTILITY))
  642. {
  643. path = "... ";
  644. path += t->second.GetName();
  645. lg->AppendEcho(commands,path.c_str());
  646. }
  647. }
  648. }
  649. lg->WriteMakeRule(ruleFileStream, "Help Target",
  650. "help:",
  651. no_depends, commands);
  652. ruleFileStream << "\n\n";
  653. }
  654. bool cmGlobalUnixMakefileGenerator3
  655. ::NeedRequiresStep(cmLocalUnixMakefileGenerator3 *lg)
  656. {
  657. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  658. checkSet = lg->GetIntegrityCheckSet();
  659. for(std::map<cmStdString,
  660. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  661. l = checkSet.begin(); l != checkSet.end(); ++l)
  662. {
  663. std::string name = "CMAKE_NEEDS_REQUIRES_STEP_";
  664. name += l->first;
  665. name += "_FLAG";
  666. if(lg->GetMakefile()->GetDefinition(name.c_str()))
  667. {
  668. return true;
  669. }
  670. }
  671. return false;
  672. }