cmGlobalUnixMakefileGenerator3.cxx 34 KB

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