cmGlobalUnixMakefileGenerator3.cxx 33 KB

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