cmGlobalUnixMakefileGenerator3.cxx 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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->WriteMainMakefile2();
  90. this->WriteMainCMakefile();
  91. }
  92. void cmGlobalUnixMakefileGenerator3::WriteMainMakefile()
  93. {
  94. // Open the output file. This should not be copy-if-different
  95. // because the check-build-system step compares the makefile time to
  96. // see if the build system must be regenerated.
  97. std::string makefileName =
  98. this->GetCMakeInstance()->GetHomeOutputDirectory();
  99. makefileName += "/Makefile";
  100. cmGeneratedFileStream makefileStream(makefileName.c_str());
  101. if(!makefileStream)
  102. {
  103. return;
  104. }
  105. // get a local generator for some useful methods
  106. cmLocalUnixMakefileGenerator3 *lg =
  107. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  108. // Write the do not edit header.
  109. lg->WriteDisclaimer(makefileStream);
  110. // Write the main entry point target. This must be the VERY first
  111. // target so that make with no arguments will run it.
  112. // Just depend on the all target to drive the build.
  113. std::vector<std::string> depends;
  114. std::vector<std::string> no_commands;
  115. depends.push_back("all");
  116. // Write the rule.
  117. lg->WriteMakeRule(makefileStream,
  118. "Default target executed when no arguments are "
  119. "given to make.",
  120. "default_target",
  121. depends,
  122. no_commands);
  123. lg->WriteMakeVariables(makefileStream, cmLocalGenerator::HOME_OUTPUT);
  124. lg->WriteSpecialTargetsTop(makefileStream);
  125. this->WriteAllRules(lg,makefileStream);
  126. // Keep track of targets already listed.
  127. std::set<cmStdString> emittedTargets;
  128. // write the target convenience rules
  129. unsigned int i;
  130. for (i = 0; i < m_LocalGenerators.size(); ++i)
  131. {
  132. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  133. this->WriteConvenienceRules(makefileStream,lg,emittedTargets);
  134. }
  135. // add a help target as long as there isn;t a real target named help
  136. if(emittedTargets.insert("help").second)
  137. {
  138. this->WriteHelpRule(makefileStream);
  139. }
  140. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  141. lg->WriteSpecialTargetsBottom(makefileStream);
  142. }
  143. void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2()
  144. {
  145. // Open the output file. This should not be copy-if-different
  146. // because the check-build-system step compares the makefile time to
  147. // see if the build system must be regenerated.
  148. std::string makefileName =
  149. this->GetCMakeInstance()->GetHomeOutputDirectory();
  150. makefileName += "/CMakeFiles/Makefile2";
  151. cmGeneratedFileStream makefileStream(makefileName.c_str());
  152. if(!makefileStream)
  153. {
  154. return;
  155. }
  156. // get a local generator for some useful methods
  157. cmLocalUnixMakefileGenerator3 *lg =
  158. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  159. // Write the do not edit header.
  160. lg->WriteDisclaimer(makefileStream);
  161. // Write the main entry point target. This must be the VERY first
  162. // target so that make with no arguments will run it.
  163. // Just depend on the all target to drive the build.
  164. std::vector<std::string> depends;
  165. std::vector<std::string> no_commands;
  166. depends.push_back("all");
  167. // Write the rule.
  168. lg->WriteMakeRule(makefileStream,
  169. "Default target executed when no arguments are "
  170. "given to make.",
  171. "default_target",
  172. depends,
  173. no_commands);
  174. // Write and empty all:
  175. lg->WriteMakeRule(makefileStream,
  176. "The main recursive all target", "all",
  177. no_commands, no_commands);
  178. lg->WriteMakeVariables(makefileStream,cmLocalGenerator::HOME_OUTPUT);
  179. // write the target convenience rules
  180. unsigned int i;
  181. for (i = 0; i < m_LocalGenerators.size(); ++i)
  182. {
  183. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  184. // are any parents excluded
  185. bool exclude = false;
  186. cmLocalGenerator *lg3 = lg;
  187. while (lg3)
  188. {
  189. if (lg3->GetExcludeAll())
  190. {
  191. exclude = true;
  192. break;
  193. }
  194. lg3 = lg3->GetParent();
  195. }
  196. this->WriteConvenienceRules2(makefileStream,lg,exclude);
  197. }
  198. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  199. lg->WriteSpecialTargetsBottom(makefileStream);
  200. }
  201. //----------------------------------------------------------------------------
  202. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile()
  203. {
  204. // Open the output file. This should not be copy-if-different
  205. // because the check-build-system step compares the makefile time to
  206. // see if the build system must be regenerated.
  207. std::string cmakefileName =
  208. this->GetCMakeInstance()->GetHomeOutputDirectory();
  209. cmakefileName += "/CMakeFiles/Makefile.cmake";
  210. cmGeneratedFileStream cmakefileStream(cmakefileName.c_str());
  211. if(!cmakefileStream)
  212. {
  213. return;
  214. }
  215. std::string makefileName =
  216. this->GetCMakeInstance()->GetHomeOutputDirectory();
  217. makefileName += "/Makefile";
  218. // get a local generator for some useful methods
  219. cmLocalUnixMakefileGenerator3 *lg =
  220. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  221. // Write the do not edit header.
  222. lg->WriteDisclaimer(cmakefileStream);
  223. // Save the generator name
  224. cmakefileStream
  225. << "# The generator used is:\n"
  226. << "SET(CMAKE_DEPENDS_GENERATOR \"" << this->GetName() << "\")\n\n";
  227. // for each cmMakefile get its list of dependencies
  228. std::vector<std::string> lfiles;
  229. for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
  230. {
  231. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  232. // Get the list of files contributing to this generation step.
  233. lfiles.insert(lfiles.end(),lg->GetMakefile()->GetListFiles().begin(),
  234. lg->GetMakefile()->GetListFiles().end());
  235. }
  236. // Sort the list and remove duplicates.
  237. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  238. std::vector<std::string>::iterator new_end =
  239. std::unique(lfiles.begin(),lfiles.end());
  240. lfiles.erase(new_end, lfiles.end());
  241. // reset lg to the first makefile
  242. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  243. // Build the path to the cache file.
  244. std::string cache = this->GetCMakeInstance()->GetHomeOutputDirectory();
  245. cache += "/CMakeCache.txt";
  246. // Save the list to the cmake file.
  247. cmakefileStream
  248. << "# The top level Makefile was generated from the following files:\n"
  249. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  250. << " \"" << lg->Convert(cache.c_str(),
  251. cmLocalGenerator::START_OUTPUT).c_str() << "\"\n";
  252. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  253. i != lfiles.end(); ++i)
  254. {
  255. cmakefileStream
  256. << " \"" << lg->Convert(i->c_str(),
  257. cmLocalGenerator::START_OUTPUT).c_str()
  258. << "\"\n";
  259. }
  260. cmakefileStream
  261. << " )\n\n";
  262. // Build the path to the cache check file.
  263. std::string check = this->GetCMakeInstance()->GetHomeOutputDirectory();
  264. check += "/CMakeFiles/cmake.check_cache";
  265. // Set the corresponding makefile in the cmake file.
  266. cmakefileStream
  267. << "# The corresponding makefile is:\n"
  268. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  269. << " \"" << lg->Convert(makefileName.c_str(),
  270. cmLocalGenerator::START_OUTPUT).c_str() << "\"\n"
  271. << " \"" << lg->Convert(check.c_str(),
  272. cmLocalGenerator::START_OUTPUT).c_str() << "\"\n";
  273. // add in all the directory information files
  274. std::string tmpStr;
  275. for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
  276. {
  277. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  278. tmpStr = lg->GetMakefile()->GetStartOutputDirectory();
  279. tmpStr += "/CMakeFiles/CMakeDirectoryInformation.cmake";
  280. cmakefileStream << " \"" <<
  281. lg->Convert(tmpStr.c_str(),cmLocalGenerator::HOME_OUTPUT).c_str() << "\"\n";
  282. }
  283. cmakefileStream << " )\n\n";
  284. this->WriteMainCMakefileLanguageRules(cmakefileStream, m_LocalGenerators);
  285. }
  286. void cmGlobalUnixMakefileGenerator3
  287. ::WriteMainCMakefileLanguageRules(cmGeneratedFileStream& cmakefileStream,
  288. std::vector<cmLocalGenerator *> &lGenerators)
  289. {
  290. cmLocalUnixMakefileGenerator3 *lg;
  291. // now list all the target info files
  292. cmakefileStream
  293. << "# The set of files whose dependency integrity should be checked:\n";
  294. cmakefileStream
  295. << "SET(CMAKE_DEPEND_INFO_FILES\n";
  296. for (unsigned int i = 0; i < lGenerators.size(); ++i)
  297. {
  298. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(lGenerators[i]);
  299. // for all of out targets
  300. for (cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  301. l != lg->GetMakefile()->GetTargets().end(); l++)
  302. {
  303. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  304. tname += "/DependInfo.cmake";
  305. cmSystemTools::ConvertToUnixSlashes(tname);
  306. cmakefileStream << " \"" << tname.c_str() << "\"\n";
  307. }
  308. }
  309. cmakefileStream << " )\n";
  310. }
  311. //----------------------------------------------------------------------------
  312. void cmGlobalUnixMakefileGenerator3
  313. ::WriteAllRules(cmLocalUnixMakefileGenerator3 *lg,
  314. std::ostream& makefileStream)
  315. {
  316. // Write section header.
  317. lg->WriteDivider(makefileStream);
  318. makefileStream
  319. << "# Rules to build dependencies and targets.\n"
  320. << "\n";
  321. std::vector<std::string> depends;
  322. std::vector<std::string> commands;
  323. // Check the build system in this directory.
  324. depends.push_back("cmake_check_build_system");
  325. commands.push_back(lg->GetRecursiveMakeCall("CMakeFiles/Makefile2","all"));
  326. // Write the rule.
  327. lg->WriteMakeRule(makefileStream, "The main all target", "all", depends, commands);
  328. // write the clean
  329. depends.clear();
  330. commands.clear();
  331. commands.push_back(lg->GetRecursiveMakeCall("CMakeFiles/Makefile2","clean"));
  332. lg->WriteMakeRule(makefileStream, "The main clean target", "clean",
  333. depends, commands);
  334. // write the depend rule, really a recompute depends rule
  335. depends.clear();
  336. commands.clear();
  337. std::string cmakefileName = "CMakeFiles/Makefile.cmake";
  338. std::string runRule =
  339. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  340. runRule += " --check-build-system ";
  341. runRule += lg->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
  342. cmLocalGenerator::SHELL);
  343. runRule += " 1";
  344. commands.push_back(runRule);
  345. lg->WriteMakeRule(makefileStream, "clear depends",
  346. "depend",
  347. depends, commands);
  348. }
  349. //----------------------------------------------------------------------------
  350. void
  351. cmGlobalUnixMakefileGenerator3
  352. ::WriteDirectoryRules(std::ostream& ruleFileStream,
  353. cmLocalUnixMakefileGenerator3 *lg)
  354. {
  355. std::vector<std::string> depends;
  356. std::vector<std::string> commands;
  357. std::string localName;
  358. std::string makeTargetName;
  359. depends.push_back("cmake_check_build_system");
  360. if (lg->GetParent())
  361. {
  362. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  363. dir = lg->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  364. lg->WriteDivider(ruleFileStream);
  365. ruleFileStream
  366. << "# Directory level rules for directory "
  367. << dir << "\n\n";
  368. localName = dir;
  369. localName += "/directorystart";
  370. makeTargetName = dir;
  371. makeTargetName += "/directory";
  372. std::vector<std::string> all_tgts;
  373. // for all of out targets
  374. for (cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  375. l != lg->GetMakefile()->GetTargets().end(); l++)
  376. {
  377. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  378. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  379. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  380. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  381. (l->second.GetType() == cmTarget::UTILITY))
  382. {
  383. // Add this to the list of depends rules in this directory.
  384. if(l->second.IsInAll())
  385. {
  386. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  387. tname += "/all";
  388. all_tgts.push_back(tname);
  389. }
  390. }
  391. }
  392. // write the directory rule add in the subdirs
  393. std::vector<cmLocalGenerator *> subdirs = lg->GetChildren();
  394. // for each subdir add the directory depend
  395. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  396. for (; sdi != subdirs.end(); ++sdi)
  397. {
  398. cmLocalUnixMakefileGenerator3 * lg2 =
  399. static_cast<cmLocalUnixMakefileGenerator3 *>(*sdi);
  400. dir = lg2->GetMakefile()->GetStartOutputDirectory();
  401. dir += "/directory";
  402. dir = lg2->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,
  403. cmLocalGenerator::MAKEFILE);
  404. all_tgts.push_back(dir);
  405. }
  406. // write the directory rule
  407. commands.clear();
  408. commands.push_back
  409. (lg->GetRecursiveMakeCall("CMakeFiles/Makefile2",
  410. makeTargetName.c_str()));
  411. // Write the rule.
  412. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory.",
  413. localName.c_str(), depends, commands);
  414. // Write the rule.
  415. commands.clear();
  416. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory.",
  417. makeTargetName.c_str(), all_tgts, commands);
  418. }
  419. // now do the clean targets
  420. if (lg->GetParent())
  421. {
  422. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  423. dir = lg->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  424. makeTargetName = dir;
  425. makeTargetName += "/clean";
  426. std::vector<std::string> all_tgts;
  427. // for all of out targets
  428. for (cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  429. l != lg->GetMakefile()->GetTargets().end(); l++)
  430. {
  431. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  432. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  433. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  434. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  435. (l->second.GetType() == cmTarget::UTILITY))
  436. {
  437. // Add this to the list of depends rules in this directory.
  438. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  439. tname += "/clean";
  440. all_tgts.push_back(tname);
  441. }
  442. }
  443. // write the directory rule add in the subdirs
  444. std::vector<cmLocalGenerator *> subdirs = lg->GetChildren();
  445. // for each subdir add the directory depend
  446. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  447. for (; sdi != subdirs.end(); ++sdi)
  448. {
  449. cmLocalUnixMakefileGenerator3 * lg2 =
  450. static_cast<cmLocalUnixMakefileGenerator3 *>(*sdi);
  451. dir = lg2->GetMakefile()->GetStartOutputDirectory();
  452. dir += "/clean";
  453. dir = lg2->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,
  454. cmLocalGenerator::MAKEFILE);
  455. all_tgts.push_back(dir);
  456. }
  457. // write the directory clean rule
  458. commands.clear();
  459. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory clean.",
  460. makeTargetName.c_str(), all_tgts, commands);
  461. }
  462. }
  463. //----------------------------------------------------------------------------
  464. void
  465. cmGlobalUnixMakefileGenerator3
  466. ::WriteDirectoryRules2(std::ostream& ruleFileStream,
  467. cmLocalUnixMakefileGenerator3 *lg)
  468. {
  469. std::vector<std::string> depends;
  470. std::vector<std::string> commands;
  471. std::string localName;
  472. std::string makeTargetName;
  473. depends.push_back("cmake_check_build_system");
  474. if (lg->GetParent())
  475. {
  476. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  477. dir = lg->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  478. lg->WriteDivider(ruleFileStream);
  479. ruleFileStream
  480. << "# Directory level rules for directory "
  481. << dir << "\n\n";
  482. localName = dir;
  483. localName += "/directorystart";
  484. makeTargetName = dir;
  485. makeTargetName += "/directory";
  486. std::vector<std::string> all_tgts;
  487. // for all of out targets
  488. for (cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  489. l != lg->GetMakefile()->GetTargets().end(); l++)
  490. {
  491. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  492. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  493. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  494. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  495. (l->second.GetType() == cmTarget::UTILITY))
  496. {
  497. // Add this to the list of depends rules in this directory.
  498. if(l->second.IsInAll())
  499. {
  500. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  501. tname += "/all";
  502. all_tgts.push_back(tname);
  503. }
  504. }
  505. }
  506. // write the directory rule add in the subdirs
  507. std::vector<cmLocalGenerator *> subdirs = lg->GetChildren();
  508. // for each subdir add the directory depend
  509. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  510. for (; sdi != subdirs.end(); ++sdi)
  511. {
  512. cmLocalUnixMakefileGenerator3 * lg2 =
  513. static_cast<cmLocalUnixMakefileGenerator3 *>(*sdi);
  514. dir = lg2->GetMakefile()->GetStartOutputDirectory();
  515. dir += "/directory";
  516. dir = lg2->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,
  517. cmLocalGenerator::MAKEFILE);
  518. all_tgts.push_back(dir);
  519. }
  520. // write the directory rule
  521. commands.clear();
  522. commands.push_back
  523. (lg->GetRecursiveMakeCall("CMakeFiles/Makefile2",
  524. makeTargetName.c_str()));
  525. // Write the rule.
  526. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory.",
  527. localName.c_str(), depends, commands);
  528. // Write the rule.
  529. commands.clear();
  530. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory.",
  531. makeTargetName.c_str(), all_tgts, commands);
  532. }
  533. // now do the clean targets
  534. if (lg->GetParent())
  535. {
  536. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  537. dir = lg->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  538. makeTargetName = dir;
  539. makeTargetName += "/clean";
  540. std::vector<std::string> all_tgts;
  541. // for all of out targets
  542. for (cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  543. l != lg->GetMakefile()->GetTargets().end(); l++)
  544. {
  545. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  546. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  547. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  548. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  549. (l->second.GetType() == cmTarget::UTILITY))
  550. {
  551. // Add this to the list of depends rules in this directory.
  552. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  553. tname += "/clean";
  554. all_tgts.push_back(tname);
  555. }
  556. }
  557. // write the directory rule add in the subdirs
  558. std::vector<cmLocalGenerator *> subdirs = lg->GetChildren();
  559. // for each subdir add the directory depend
  560. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  561. for (; sdi != subdirs.end(); ++sdi)
  562. {
  563. cmLocalUnixMakefileGenerator3 * lg2 =
  564. static_cast<cmLocalUnixMakefileGenerator3 *>(*sdi);
  565. dir = lg2->GetMakefile()->GetStartOutputDirectory();
  566. dir += "/clean";
  567. dir = lg2->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,
  568. cmLocalGenerator::MAKEFILE);
  569. all_tgts.push_back(dir);
  570. }
  571. // write the directory clean rule
  572. commands.clear();
  573. lg->WriteMakeRule(ruleFileStream, "Convenience name for directory clean.",
  574. makeTargetName.c_str(), all_tgts, commands);
  575. }
  576. }
  577. //----------------------------------------------------------------------------
  578. void
  579. cmGlobalUnixMakefileGenerator3
  580. ::WriteConvenienceRules(std::ostream& ruleFileStream,
  581. cmLocalUnixMakefileGenerator3 *lg,
  582. std::set<cmStdString> &emitted)
  583. {
  584. std::vector<std::string> depends;
  585. std::vector<std::string> commands;
  586. // write the directory level rules for this local gen
  587. //this->WriteDirectoryRules(ruleFileStream,lg);
  588. depends.push_back("cmake_check_build_system");
  589. // for each target Generate the rule files for each target.
  590. cmTargets& targets = lg->GetMakefile()->GetTargets();
  591. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  592. {
  593. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  594. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  595. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  596. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  597. (t->second.GetType() == cmTarget::UTILITY))
  598. {
  599. // Don't emit the same rule twice (e.g. two targets with the same
  600. // simple name)
  601. if(t->second.GetName() &&
  602. strlen(t->second.GetName()) &&
  603. emitted.insert(t->second.GetName()).second)
  604. {
  605. // Add a rule to build the target by name.
  606. lg->WriteDivider(ruleFileStream);
  607. ruleFileStream
  608. << "# Target rules for targets named "
  609. << t->second.GetName() << "\n\n";
  610. // Write the rule.
  611. commands.clear();
  612. commands.push_back(lg->GetRecursiveMakeCall("CMakeFiles/Makefile2",
  613. t->second.GetName()));
  614. depends.clear();
  615. depends.push_back("cmake_check_build_system");
  616. lg->WriteMakeRule(ruleFileStream,
  617. "Build rule for target.",
  618. t->second.GetName(), depends, commands);
  619. }
  620. }
  621. }
  622. }
  623. //----------------------------------------------------------------------------
  624. void
  625. cmGlobalUnixMakefileGenerator3
  626. ::WriteConvenienceRules2(std::ostream& ruleFileStream,
  627. cmLocalUnixMakefileGenerator3 *lg,
  628. bool exclude)
  629. {
  630. std::vector<std::string> depends;
  631. std::vector<std::string> commands;
  632. std::string localName;
  633. std::string makeTargetName;
  634. // write the directory level rules for this local gen
  635. this->WriteDirectoryRules2(ruleFileStream,lg);
  636. depends.push_back("cmake_check_build_system");
  637. // for each target Generate the rule files for each target.
  638. cmTargets& targets = lg->GetMakefile()->GetTargets();
  639. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  640. {
  641. if (((t->second.GetType() == cmTarget::EXECUTABLE) ||
  642. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  643. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  644. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  645. (t->second.GetType() == cmTarget::UTILITY)) &&
  646. t->second.GetName() &&
  647. strlen(t->second.GetName()))
  648. {
  649. bool needRequiresStep =
  650. this->NeedRequiresStep(lg,t->second.GetName());
  651. // Add a rule to build the target by name.
  652. localName = lg->GetRelativeTargetDirectory(t->second);
  653. std::string makefileName = localName;
  654. makefileName += "/build.make";
  655. lg->WriteDivider(ruleFileStream);
  656. ruleFileStream
  657. << "# Target rules for target "
  658. << localName << "\n\n";
  659. commands.clear();
  660. if (t->second.GetType() != cmTarget::UTILITY)
  661. {
  662. makeTargetName = localName;
  663. makeTargetName += "/depend";
  664. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  665. makeTargetName.c_str()));
  666. // add requires if we need it for this generator
  667. if (needRequiresStep)
  668. {
  669. makeTargetName = localName;
  670. makeTargetName += "/requires";
  671. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  672. makeTargetName.c_str()));
  673. }
  674. }
  675. makeTargetName = localName;
  676. makeTargetName += "/build";
  677. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  678. makeTargetName.c_str()));
  679. // Write the rule.
  680. localName += "/all";
  681. depends.clear();
  682. this->AppendGlobalTargetDepends(depends,t->second);
  683. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  684. localName.c_str(), depends, commands);
  685. // add the all/all dependency
  686. if (!exclude && t->second.IsInAll())
  687. {
  688. depends.clear();
  689. depends.push_back(localName);
  690. commands.clear();
  691. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  692. "all", depends, commands);
  693. }
  694. // Write the rule.
  695. commands.clear();
  696. commands.push_back(lg->GetRecursiveMakeCall("CMakeFiles/Makefile2",
  697. localName.c_str()));
  698. depends.clear();
  699. depends.push_back("cmake_check_build_system");
  700. localName = lg->GetRelativeTargetDirectory(t->second);
  701. localName += "/rule";
  702. lg->WriteMakeRule(ruleFileStream,
  703. "Build rule for subdir invocation for target.",
  704. localName.c_str(), depends, commands);
  705. // Add a target with the canonical name (no prefix, suffix or path).
  706. commands.clear();
  707. depends.clear();
  708. depends.push_back(localName);
  709. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  710. t->second.GetName(), depends, commands);
  711. // add the clean rule
  712. localName = lg->GetRelativeTargetDirectory(t->second);
  713. makeTargetName = localName;
  714. makeTargetName += "/clean";
  715. depends.clear();
  716. commands.clear();
  717. commands.push_back(lg->GetRecursiveMakeCall(makefileName.c_str(),
  718. makeTargetName.c_str()));
  719. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  720. makeTargetName.c_str(), depends, commands);
  721. commands.clear();
  722. depends.push_back(makeTargetName);
  723. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  724. "clean", depends, commands);
  725. }
  726. }
  727. }
  728. //----------------------------------------------------------------------------
  729. void
  730. cmGlobalUnixMakefileGenerator3
  731. ::AppendGlobalTargetDepends(std::vector<std::string>& depends,
  732. cmTarget& target)
  733. {
  734. // Keep track of dependencies already listed.
  735. std::set<cmStdString> emitted;
  736. // A target should not depend on itself.
  737. emitted.insert(target.GetName());
  738. // Loop over all library dependencies but not for static libs
  739. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  740. {
  741. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  742. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  743. lib != tlibs.end(); ++lib)
  744. {
  745. // Don't emit the same library twice for this target.
  746. if(emitted.insert(lib->first).second)
  747. {
  748. // Add this dependency.
  749. this->AppendAnyGlobalDepend(depends, lib->first.c_str(), emitted);
  750. }
  751. }
  752. }
  753. // Loop over all utility dependencies.
  754. const std::set<cmStdString>& tutils = target.GetUtilities();
  755. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  756. util != tutils.end(); ++util)
  757. {
  758. // Don't emit the same utility twice for this target.
  759. if(emitted.insert(*util).second)
  760. {
  761. // Add this dependency.
  762. this->AppendAnyGlobalDepend(depends, util->c_str(), emitted);
  763. }
  764. }
  765. }
  766. //----------------------------------------------------------------------------
  767. void
  768. cmGlobalUnixMakefileGenerator3
  769. ::AppendAnyGlobalDepend(std::vector<std::string>& depends, const char* name,
  770. std::set<cmStdString>& emitted)
  771. {
  772. cmTarget *result;
  773. // search each local generator until a match is found
  774. unsigned int i;
  775. for (i = 0; i < m_LocalGenerators.size(); ++i)
  776. {
  777. // search all targets
  778. result = m_LocalGenerators[i]->GetMakefile()->FindTarget(name);
  779. // if a match was found then ...
  780. if (result)
  781. {
  782. cmLocalUnixMakefileGenerator3 *lg3 =
  783. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  784. std::string tgtName = lg3->GetRelativeTargetDirectory(*result);
  785. tgtName += "/all";
  786. depends.push_back(tgtName);
  787. if(result->GetType() == cmTarget::STATIC_LIBRARY)
  788. {
  789. const cmTarget::LinkLibraries& tlibs = result->GetLinkLibraries();
  790. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  791. lib != tlibs.end(); ++lib)
  792. {
  793. // Don't emit the same library twice for this target.
  794. if(emitted.insert(lib->first).second)
  795. {
  796. // Add this dependency.
  797. this->AppendAnyGlobalDepend(depends, lib->first.c_str(),
  798. emitted);
  799. }
  800. }
  801. }
  802. return;
  803. }
  804. }
  805. }
  806. //----------------------------------------------------------------------------
  807. void
  808. cmGlobalUnixMakefileGenerator3::WriteHelpRule(std::ostream& ruleFileStream)
  809. {
  810. cmLocalUnixMakefileGenerator3 *lg =
  811. static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  812. // add the help target
  813. std::string path;
  814. std::vector<std::string> no_depends;
  815. std::vector<std::string> commands;
  816. lg->AppendEcho(commands,
  817. "The following are some of the valid targets for this Makefile:");
  818. lg->AppendEcho(commands,"... all (the default if no target is provided)");
  819. lg->AppendEcho(commands,"... clean");
  820. lg->AppendEcho(commands,"... depend");
  821. lg->AppendEcho(commands,"... install");
  822. lg->AppendEcho(commands,"... rebuild_cache");
  823. // Keep track of targets already listed.
  824. std::set<cmStdString> emittedTargets;
  825. // for each local generator
  826. unsigned int i;
  827. for (i = 0; i < m_LocalGenerators.size(); ++i)
  828. {
  829. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  830. // for each target Generate the rule files for each target.
  831. cmTargets& targets = lg->GetMakefile()->GetTargets();
  832. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  833. {
  834. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  835. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  836. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  837. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  838. (t->second.GetType() == cmTarget::UTILITY))
  839. {
  840. if(emittedTargets.insert(t->second.GetName()).second)
  841. {
  842. path = "... ";
  843. path += t->second.GetName();
  844. lg->AppendEcho(commands,path.c_str());
  845. }
  846. }
  847. }
  848. }
  849. lg->WriteMakeRule(ruleFileStream, "Help Target",
  850. "help:",
  851. no_depends, commands);
  852. ruleFileStream << "\n\n";
  853. }
  854. bool cmGlobalUnixMakefileGenerator3
  855. ::NeedRequiresStep(cmLocalUnixMakefileGenerator3 *lg,const char *name)
  856. {
  857. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  858. checkSet = lg->GetIntegrityCheckSet()[name];
  859. for(std::map<cmStdString,
  860. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  861. l = checkSet.begin(); l != checkSet.end(); ++l)
  862. {
  863. std::string name2 = "CMAKE_NEEDS_REQUIRES_STEP_";
  864. name2 += l->first;
  865. name2 += "_FLAG";
  866. if(lg->GetMakefile()->GetDefinition(name2.c_str()))
  867. {
  868. return true;
  869. }
  870. }
  871. return false;
  872. }