cmGlobalUnixMakefileGenerator3.cxx 34 KB

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