cmGlobalUnixMakefileGenerator3.cxx 28 KB

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