cmGlobalUnixMakefileGenerator3.cxx 30 KB

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