cmGlobalUnixMakefileGenerator3.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. }
  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 *>(this->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(this->EmptyRuleHackDepends != "")
  133. {
  134. depends.push_back(this->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 < this->LocalGenerators.size(); ++i)
  150. {
  151. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(this->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 *>(this->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 *>(this->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 < this->LocalGenerators.size(); ++i)
  198. {
  199. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(this->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 *>(this->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 < this->LocalGenerators.size(); ++i)
  244. {
  245. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(this->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, this->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. ::WriteDirectoryRule2(std::ostream& ruleFileStream,
  289. cmLocalUnixMakefileGenerator3* lg,
  290. const char* pass, bool check_all,
  291. bool check_relink)
  292. {
  293. // Get the relative path to the subdirectory from the top.
  294. std::string makeTarget = lg->GetMakefile()->GetStartOutputDirectory();
  295. makeTarget += "/";
  296. makeTarget += pass;
  297. makeTarget = lg->Convert(makeTarget.c_str(),
  298. cmLocalGenerator::HOME_OUTPUT,
  299. cmLocalGenerator::MAKEFILE);
  300. // The directory-level rule should depend on the target-level rules
  301. // for all targets in the directory.
  302. std::vector<std::string> depends;
  303. for(cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  304. l != lg->GetMakefile()->GetTargets().end(); ++l)
  305. {
  306. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  307. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  308. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  309. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  310. (l->second.GetType() == cmTarget::UTILITY))
  311. {
  312. // Add this to the list of depends rules in this directory.
  313. if((!check_all || l->second.IsInAll()) &&
  314. (!check_relink || l->second.NeedRelinkBeforeInstall()))
  315. {
  316. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  317. tname += "/";
  318. tname += pass;
  319. depends.push_back(tname);
  320. }
  321. }
  322. }
  323. // The directory-level rule should depend on the directory-level
  324. // rules of the subdirectories.
  325. for(std::vector<cmLocalGenerator*>::iterator sdi = lg->GetChildren().begin();
  326. sdi != lg->GetChildren().end(); ++sdi)
  327. {
  328. cmLocalUnixMakefileGenerator3* slg =
  329. static_cast<cmLocalUnixMakefileGenerator3*>(*sdi);
  330. std::string subdir = slg->GetMakefile()->GetStartOutputDirectory();
  331. subdir += "/";
  332. subdir += pass;
  333. subdir = slg->Convert(subdir.c_str(),
  334. cmLocalGenerator::HOME_OUTPUT,
  335. cmLocalGenerator::MAKEFILE);
  336. depends.push_back(subdir);
  337. }
  338. // Work-around for makes that drop rules that have no dependencies
  339. // or commands.
  340. if(depends.empty() && this->EmptyRuleHackDepends != "")
  341. {
  342. depends.push_back(this->EmptyRuleHackDepends);
  343. }
  344. // Write the rule.
  345. std::string doc = "Convenience name for \"";
  346. doc += pass;
  347. doc += "\" pass in the directory.";
  348. std::vector<std::string> no_commands;
  349. lg->WriteMakeRule(ruleFileStream, doc.c_str(),
  350. makeTarget.c_str(), depends, no_commands, true);
  351. }
  352. //----------------------------------------------------------------------------
  353. void
  354. cmGlobalUnixMakefileGenerator3
  355. ::WriteDirectoryRules2(std::ostream& ruleFileStream,
  356. cmLocalUnixMakefileGenerator3* lg)
  357. {
  358. // Only subdirectories need these rules.
  359. if(!lg->GetParent())
  360. {
  361. return;
  362. }
  363. // Begin the directory-level rules section.
  364. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  365. dir = lg->Convert(dir.c_str(), cmLocalGenerator::HOME_OUTPUT,
  366. cmLocalGenerator::MAKEFILE);
  367. lg->WriteDivider(ruleFileStream);
  368. ruleFileStream
  369. << "# Directory level rules for directory "
  370. << dir << "\n\n";
  371. // Write directory-level rules for "all".
  372. this->WriteDirectoryRule2(ruleFileStream, lg, "all", true, false);
  373. // Write directory-level rules for "clean".
  374. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false);
  375. // Write directory-level rules for "preinstall".
  376. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", false, true);
  377. }
  378. //----------------------------------------------------------------------------
  379. void
  380. cmGlobalUnixMakefileGenerator3
  381. ::WriteConvenienceRules(std::ostream& ruleFileStream,
  382. std::set<cmStdString> &emitted)
  383. {
  384. std::vector<std::string> depends;
  385. std::vector<std::string> commands;
  386. depends.push_back("cmake_check_build_system");
  387. // write the target convenience rules
  388. unsigned int i;
  389. cmLocalUnixMakefileGenerator3 *lg;
  390. for (i = 0; i < this->LocalGenerators.size(); ++i)
  391. {
  392. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  393. // for each target Generate the rule files for each target.
  394. cmTargets& targets = lg->GetMakefile()->GetTargets();
  395. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  396. {
  397. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  398. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  399. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  400. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  401. (t->second.GetType() == cmTarget::UTILITY))
  402. {
  403. // Don't emit the same rule twice (e.g. two targets with the same
  404. // simple name)
  405. if(t->second.GetName() &&
  406. strlen(t->second.GetName()) &&
  407. emitted.insert(t->second.GetName()).second)
  408. {
  409. // Add a rule to build the target by name.
  410. lg->WriteDivider(ruleFileStream);
  411. ruleFileStream
  412. << "# Target rules for targets named "
  413. << t->second.GetName() << "\n\n";
  414. // Write the rule.
  415. commands.clear();
  416. commands.push_back(lg->GetRecursiveMakeCall
  417. ("CMakeFiles/Makefile2",t->second.GetName()));
  418. depends.clear();
  419. depends.push_back("cmake_check_build_system");
  420. lg->WriteMakeRule(ruleFileStream,
  421. "Build rule for target.",
  422. t->second.GetName(), depends, commands,
  423. true);
  424. }
  425. }
  426. }
  427. }
  428. }
  429. //----------------------------------------------------------------------------
  430. void
  431. cmGlobalUnixMakefileGenerator3
  432. ::WriteConvenienceRules2(std::ostream& ruleFileStream,
  433. cmLocalUnixMakefileGenerator3 *lg,
  434. bool exclude)
  435. {
  436. std::vector<std::string> depends;
  437. std::vector<std::string> commands;
  438. std::string localName;
  439. std::string makeTargetName;
  440. // write the directory level rules for this local gen
  441. this->WriteDirectoryRules2(ruleFileStream,lg);
  442. depends.push_back("cmake_check_build_system");
  443. // for each target Generate the rule files for each target.
  444. cmTargets& targets = lg->GetMakefile()->GetTargets();
  445. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  446. {
  447. if (((t->second.GetType() == cmTarget::EXECUTABLE) ||
  448. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  449. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  450. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  451. (t->second.GetType() == cmTarget::UTILITY)) &&
  452. t->second.GetName() &&
  453. strlen(t->second.GetName()))
  454. {
  455. bool needRequiresStep =
  456. this->NeedRequiresStep(lg,t->second.GetName());
  457. // Add a rule to build the target by name.
  458. localName = lg->GetRelativeTargetDirectory(t->second);
  459. std::string makefileName = localName;
  460. makefileName += "/build.make";
  461. lg->WriteDivider(ruleFileStream);
  462. ruleFileStream
  463. << "# Target rules for target "
  464. << localName << "\n\n";
  465. commands.clear();
  466. if (t->second.GetType() != cmTarget::UTILITY)
  467. {
  468. makeTargetName = localName;
  469. makeTargetName += "/depend";
  470. commands.push_back(lg->GetRecursiveMakeCall
  471. (makefileName.c_str(),makeTargetName.c_str()));
  472. // add requires if we need it for this generator
  473. if (needRequiresStep)
  474. {
  475. makeTargetName = localName;
  476. makeTargetName += "/requires";
  477. commands.push_back(lg->GetRecursiveMakeCall
  478. (makefileName.c_str(),makeTargetName.c_str()));
  479. }
  480. }
  481. makeTargetName = localName;
  482. makeTargetName += "/build";
  483. commands.push_back(lg->GetRecursiveMakeCall
  484. (makefileName.c_str(),makeTargetName.c_str()));
  485. // Write the rule.
  486. localName += "/all";
  487. depends.clear();
  488. this->AppendGlobalTargetDepends(depends,t->second);
  489. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  490. localName.c_str(), depends, commands, true);
  491. // add the all/all dependency
  492. if (!exclude && t->second.IsInAll())
  493. {
  494. depends.clear();
  495. depends.push_back(localName);
  496. commands.clear();
  497. lg->WriteMakeRule(ruleFileStream, "Include target in all.",
  498. "all", depends, commands, true);
  499. }
  500. // Write the rule.
  501. commands.clear();
  502. commands.push_back(lg->GetRecursiveMakeCall
  503. ("CMakeFiles/Makefile2",localName.c_str()));
  504. depends.clear();
  505. depends.push_back("cmake_check_build_system");
  506. localName = lg->GetRelativeTargetDirectory(t->second);
  507. localName += "/rule";
  508. lg->WriteMakeRule(ruleFileStream,
  509. "Build rule for subdir invocation for target.",
  510. localName.c_str(), depends, commands, true);
  511. // Add a target with the canonical name (no prefix, suffix or path).
  512. commands.clear();
  513. depends.clear();
  514. depends.push_back(localName);
  515. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  516. t->second.GetName(), depends, commands, true);
  517. // Add rules to prepare the target for installation.
  518. if(t->second.NeedRelinkBeforeInstall())
  519. {
  520. localName = lg->GetRelativeTargetDirectory(t->second);
  521. localName += "/preinstall";
  522. depends.clear();
  523. commands.clear();
  524. commands.push_back(lg->GetRecursiveMakeCall
  525. (makefileName.c_str(), localName.c_str()));
  526. this->AppendGlobalTargetDepends(depends,t->second);
  527. lg->WriteMakeRule(ruleFileStream, "Pre-install relink rule for target.",
  528. localName.c_str(), depends, commands, true);
  529. depends.clear();
  530. depends.push_back(localName);
  531. commands.clear();
  532. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  533. "preinstall", depends, commands, true);
  534. }
  535. // add the clean rule
  536. localName = lg->GetRelativeTargetDirectory(t->second);
  537. makeTargetName = localName;
  538. makeTargetName += "/clean";
  539. depends.clear();
  540. commands.clear();
  541. commands.push_back(lg->GetRecursiveMakeCall
  542. (makefileName.c_str(), makeTargetName.c_str()));
  543. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  544. makeTargetName.c_str(), depends, commands, true);
  545. commands.clear();
  546. depends.push_back(makeTargetName);
  547. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  548. "clean", depends, commands, true);
  549. }
  550. }
  551. }
  552. //----------------------------------------------------------------------------
  553. void
  554. cmGlobalUnixMakefileGenerator3
  555. ::AppendGlobalTargetDepends(std::vector<std::string>& depends,
  556. cmTarget& target)
  557. {
  558. // Keep track of dependencies already listed.
  559. std::set<cmStdString> emitted;
  560. // A target should not depend on itself.
  561. emitted.insert(target.GetName());
  562. // Loop over all library dependencies but not for static libs
  563. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  564. {
  565. const cmTarget::LinkLibraryVectorType& tlibs = target.GetLinkLibraries();
  566. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  567. lib != tlibs.end(); ++lib)
  568. {
  569. // Don't emit the same library twice for this target.
  570. if(emitted.insert(lib->first).second)
  571. {
  572. // Add this dependency.
  573. this->AppendAnyGlobalDepend(depends, lib->first.c_str(),
  574. emitted, target);
  575. }
  576. }
  577. }
  578. // Loop over all utility dependencies.
  579. const std::set<cmStdString>& tutils = target.GetUtilities();
  580. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  581. util != tutils.end(); ++util)
  582. {
  583. // Don't emit the same utility twice for this target.
  584. if(emitted.insert(*util).second)
  585. {
  586. // Add this dependency.
  587. this->AppendAnyGlobalDepend(depends, util->c_str(), emitted, target);
  588. }
  589. }
  590. }
  591. //----------------------------------------------------------------------------
  592. void
  593. cmGlobalUnixMakefileGenerator3
  594. ::AppendAnyGlobalDepend(std::vector<std::string>& depends, const char* name,
  595. std::set<cmStdString>& emitted, cmTarget &target)
  596. {
  597. cmTarget *result;
  598. cmLocalUnixMakefileGenerator3 *lg3;
  599. // first check the same dir as the current target
  600. lg3 = static_cast<cmLocalUnixMakefileGenerator3 *>
  601. (target.GetMakefile()->GetLocalGenerator());
  602. result = target.GetMakefile()->FindTarget(name);
  603. // search each local generator until a match is found
  604. if (!result)
  605. {
  606. unsigned int i;
  607. for (i = 0; i < this->LocalGenerators.size(); ++i)
  608. {
  609. // search all targets
  610. result = this->LocalGenerators[i]->GetMakefile()->FindTarget(name);
  611. if (result)
  612. {
  613. lg3 = static_cast<cmLocalUnixMakefileGenerator3 *>
  614. (this->LocalGenerators[i]);
  615. break;
  616. }
  617. }
  618. }
  619. // if a match was found then ...
  620. if (result)
  621. {
  622. std::string tgtName = lg3->GetRelativeTargetDirectory(*result);
  623. tgtName += "/all";
  624. depends.push_back(tgtName);
  625. if(result->GetType() == cmTarget::STATIC_LIBRARY)
  626. {
  627. const cmTarget::LinkLibraryVectorType& tlibs
  628. = result->GetLinkLibraries();
  629. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  630. lib != tlibs.end(); ++lib)
  631. {
  632. // Don't emit the same library twice for this target.
  633. if(emitted.insert(lib->first).second)
  634. {
  635. // Add this dependency.
  636. this->AppendAnyGlobalDepend(depends, lib->first.c_str(),
  637. emitted, *result);
  638. }
  639. }
  640. }
  641. return;
  642. }
  643. }
  644. //----------------------------------------------------------------------------
  645. void cmGlobalUnixMakefileGenerator3::WriteHelpRule
  646. (std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3 *lg)
  647. {
  648. // add the help target
  649. std::string path;
  650. std::vector<std::string> no_depends;
  651. std::vector<std::string> commands;
  652. lg->AppendEcho(commands,
  653. "The following are some of the valid targets for this Makefile:");
  654. lg->AppendEcho(commands,"... all (the default if no target is provided)");
  655. lg->AppendEcho(commands,"... clean");
  656. lg->AppendEcho(commands,"... depend");
  657. lg->AppendEcho(commands,"... install");
  658. lg->AppendEcho(commands,"... rebuild_cache");
  659. // Keep track of targets already listed.
  660. std::set<cmStdString> emittedTargets;
  661. // for each local generator
  662. unsigned int i;
  663. cmLocalUnixMakefileGenerator3 *lg2;
  664. for (i = 0; i < this->LocalGenerators.size(); ++i)
  665. {
  666. lg2 = static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  667. // for the passed in makefile or if this is the top Makefile wripte out
  668. // the targets
  669. if (lg2 == lg || !lg->GetParent())
  670. {
  671. // for each target Generate the rule files for each target.
  672. cmTargets& targets = lg->GetMakefile()->GetTargets();
  673. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  674. {
  675. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  676. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  677. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  678. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  679. (t->second.GetType() == cmTarget::UTILITY))
  680. {
  681. if(emittedTargets.insert(t->second.GetName()).second)
  682. {
  683. path = "... ";
  684. path += t->second.GetName();
  685. lg->AppendEcho(commands,path.c_str());
  686. }
  687. }
  688. }
  689. std::map<cmStdString,std::vector<cmTarget *> > const& objs =
  690. lg->GetLocalObjectFiles();
  691. for(std::map<cmStdString,std::vector<cmTarget *> >::const_iterator o =
  692. objs.begin(); o != objs.end(); ++o)
  693. {
  694. path = "... ";
  695. path += o->first;
  696. lg->AppendEcho(commands, path.c_str());
  697. }
  698. }
  699. }
  700. lg->WriteMakeRule(ruleFileStream, "Help Target",
  701. "help:",
  702. no_depends, commands, true);
  703. ruleFileStream << "\n\n";
  704. }
  705. bool cmGlobalUnixMakefileGenerator3
  706. ::NeedRequiresStep(cmLocalUnixMakefileGenerator3 *lg,const char *name)
  707. {
  708. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  709. checkSet = lg->GetIntegrityCheckSet()[name];
  710. for(std::map<cmStdString,
  711. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  712. l = checkSet.begin(); l != checkSet.end(); ++l)
  713. {
  714. std::string name2 = "CMAKE_NEEDS_REQUIRES_STEP_";
  715. name2 += l->first;
  716. name2 += "_FLAG";
  717. if(lg->GetMakefile()->GetDefinition(name2.c_str()))
  718. {
  719. return true;
  720. }
  721. }
  722. return false;
  723. }