cmGlobalUnixMakefileGenerator3.cxx 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGlobalUnixMakefileGenerator3.h"
  4. #include <algorithm>
  5. #include <functional>
  6. #include <sstream>
  7. #include <utility>
  8. #include "cmAlgorithms.h"
  9. #include "cmDocumentationEntry.h"
  10. #include "cmGeneratedFileStream.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator3.h"
  15. #include "cmMakefile.h"
  16. #include "cmMakefileTargetGenerator.h"
  17. #include "cmOutputConverter.h"
  18. #include "cmState.h"
  19. #include "cmStateDirectory.h"
  20. #include "cmStateTypes.h"
  21. #include "cmSystemTools.h"
  22. #include "cmTargetDepend.h"
  23. #include "cmake.h"
  24. cmGlobalUnixMakefileGenerator3::cmGlobalUnixMakefileGenerator3(cmake* cm)
  25. : cmGlobalCommonGenerator(cm)
  26. {
  27. // This type of makefile always requires unix style paths
  28. this->ForceUnixPaths = true;
  29. this->FindMakeProgramFile = "CMakeUnixFindMake.cmake";
  30. this->ToolSupportsColor = true;
  31. #if defined(_WIN32) || defined(__VMS)
  32. this->UseLinkScript = false;
  33. #else
  34. this->UseLinkScript = true;
  35. #endif
  36. this->CommandDatabase = nullptr;
  37. this->IncludeDirective = "include";
  38. this->DefineWindowsNULL = false;
  39. this->PassMakeflags = false;
  40. this->UnixCD = true;
  41. }
  42. void cmGlobalUnixMakefileGenerator3::EnableLanguage(
  43. std::vector<std::string> const& languages, cmMakefile* mf, bool optional)
  44. {
  45. this->cmGlobalGenerator::EnableLanguage(languages, mf, optional);
  46. for (std::string const& language : languages) {
  47. if (language == "NONE") {
  48. continue;
  49. }
  50. this->ResolveLanguageCompiler(language, mf, optional);
  51. }
  52. }
  53. ///! Create a local generator appropriate to this Global Generator
  54. cmLocalGenerator* cmGlobalUnixMakefileGenerator3::CreateLocalGenerator(
  55. cmMakefile* mf)
  56. {
  57. return new cmLocalUnixMakefileGenerator3(this, mf);
  58. }
  59. void cmGlobalUnixMakefileGenerator3::GetDocumentation(
  60. cmDocumentationEntry& entry)
  61. {
  62. entry.Name = cmGlobalUnixMakefileGenerator3::GetActualName();
  63. entry.Brief = "Generates standard UNIX makefiles.";
  64. }
  65. std::string cmGlobalUnixMakefileGenerator3::GetEditCacheCommand() const
  66. {
  67. // If generating for an extra IDE, the edit_cache target cannot
  68. // launch a terminal-interactive tool, so always use cmake-gui.
  69. if (!this->GetExtraGeneratorName().empty()) {
  70. return cmSystemTools::GetCMakeGUICommand();
  71. }
  72. // Use an internal cache entry to track the latest dialog used
  73. // to edit the cache, and use that for the edit_cache target.
  74. cmake* cm = this->GetCMakeInstance();
  75. std::string editCacheCommand = cm->GetCMakeEditCommand();
  76. if (!cm->GetCacheDefinition("CMAKE_EDIT_COMMAND") ||
  77. !editCacheCommand.empty()) {
  78. if (editCacheCommand.empty()) {
  79. editCacheCommand = cmSystemTools::GetCMakeCursesCommand();
  80. }
  81. if (editCacheCommand.empty()) {
  82. editCacheCommand = cmSystemTools::GetCMakeGUICommand();
  83. }
  84. if (!editCacheCommand.empty()) {
  85. cm->AddCacheEntry("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  86. "Path to cache edit program executable.",
  87. cmStateEnums::INTERNAL);
  88. }
  89. }
  90. const char* edit_cmd = cm->GetCacheDefinition("CMAKE_EDIT_COMMAND");
  91. return edit_cmd ? edit_cmd : "";
  92. }
  93. void cmGlobalUnixMakefileGenerator3::ComputeTargetObjectDirectory(
  94. cmGeneratorTarget* gt) const
  95. {
  96. // Compute full path to object file directory for this target.
  97. std::string dir;
  98. dir += gt->LocalGenerator->GetCurrentBinaryDirectory();
  99. dir += "/";
  100. dir += gt->LocalGenerator->GetTargetDirectory(gt);
  101. dir += "/";
  102. gt->ObjectDirectory = dir;
  103. }
  104. void cmGlobalUnixMakefileGenerator3::Configure()
  105. {
  106. // Initialize CMAKE_EDIT_COMMAND cache entry.
  107. this->GetEditCacheCommand();
  108. this->cmGlobalGenerator::Configure();
  109. }
  110. void cmGlobalUnixMakefileGenerator3::Generate()
  111. {
  112. // first do superclass method
  113. this->cmGlobalGenerator::Generate();
  114. // initialize progress
  115. unsigned long total = 0;
  116. for (auto const& pmi : this->ProgressMap) {
  117. total += pmi.second.NumberOfActions;
  118. }
  119. // write each target's progress.make this loop is done twice. Bascially the
  120. // Generate pass counts all the actions, the first loop below determines
  121. // how many actions have progress updates for each target and writes to
  122. // corrrect variable values for everything except the all targets. The
  123. // second loop actually writes out correct values for the all targets as
  124. // well. This is because the all targets require more information that is
  125. // computed in the first loop.
  126. unsigned long current = 0;
  127. for (auto& pmi : this->ProgressMap) {
  128. pmi.second.WriteProgressVariables(total, current);
  129. }
  130. for (cmLocalGenerator* lg : this->LocalGenerators) {
  131. std::string markFileName = lg->GetCurrentBinaryDirectory();
  132. markFileName += "/";
  133. markFileName += cmake::GetCMakeFilesDirectory();
  134. markFileName += "/progress.marks";
  135. cmGeneratedFileStream markFile(markFileName.c_str());
  136. markFile << this->CountProgressMarksInAll(lg) << "\n";
  137. }
  138. // write the main makefile
  139. this->WriteMainMakefile2();
  140. this->WriteMainCMakefile();
  141. if (this->CommandDatabase != nullptr) {
  142. *this->CommandDatabase << std::endl << "]";
  143. delete this->CommandDatabase;
  144. this->CommandDatabase = nullptr;
  145. }
  146. }
  147. void cmGlobalUnixMakefileGenerator3::AddCXXCompileCommand(
  148. const std::string& sourceFile, const std::string& workingDirectory,
  149. const std::string& compileCommand)
  150. {
  151. if (this->CommandDatabase == nullptr) {
  152. std::string commandDatabaseName =
  153. std::string(this->GetCMakeInstance()->GetHomeOutputDirectory()) +
  154. "/compile_commands.json";
  155. this->CommandDatabase =
  156. new cmGeneratedFileStream(commandDatabaseName.c_str());
  157. *this->CommandDatabase << "[" << std::endl;
  158. } else {
  159. *this->CommandDatabase << "," << std::endl;
  160. }
  161. *this->CommandDatabase << "{" << std::endl
  162. << " \"directory\": \""
  163. << cmGlobalGenerator::EscapeJSON(workingDirectory)
  164. << "\"," << std::endl
  165. << " \"command\": \""
  166. << cmGlobalGenerator::EscapeJSON(compileCommand)
  167. << "\"," << std::endl
  168. << " \"file\": \""
  169. << cmGlobalGenerator::EscapeJSON(sourceFile) << "\""
  170. << std::endl
  171. << "}";
  172. }
  173. void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2()
  174. {
  175. // Open the output file. This should not be copy-if-different
  176. // because the check-build-system step compares the makefile time to
  177. // see if the build system must be regenerated.
  178. std::string makefileName =
  179. this->GetCMakeInstance()->GetHomeOutputDirectory();
  180. makefileName += cmake::GetCMakeFilesDirectory();
  181. makefileName += "/Makefile2";
  182. cmGeneratedFileStream makefileStream(makefileName.c_str(), false,
  183. this->GetMakefileEncoding());
  184. if (!makefileStream) {
  185. return;
  186. }
  187. // get a local generator for some useful methods
  188. cmLocalUnixMakefileGenerator3* lg =
  189. static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  190. // Write the do not edit header.
  191. lg->WriteDisclaimer(makefileStream);
  192. // Write the main entry point target. This must be the VERY first
  193. // target so that make with no arguments will run it.
  194. // Just depend on the all target to drive the build.
  195. std::vector<std::string> depends;
  196. std::vector<std::string> no_commands;
  197. depends.push_back("all");
  198. // Write the rule.
  199. lg->WriteMakeRule(makefileStream,
  200. "Default target executed when no arguments are "
  201. "given to make.",
  202. "default_target", depends, no_commands, true);
  203. depends.clear();
  204. // The all and preinstall rules might never have any dependencies
  205. // added to them.
  206. if (!this->EmptyRuleHackDepends.empty()) {
  207. depends.push_back(this->EmptyRuleHackDepends);
  208. }
  209. // Write and empty all:
  210. lg->WriteMakeRule(makefileStream, "The main recursive all target", "all",
  211. depends, no_commands, true);
  212. // Write an empty preinstall:
  213. lg->WriteMakeRule(makefileStream, "The main recursive preinstall target",
  214. "preinstall", depends, no_commands, true);
  215. // Write out the "special" stuff
  216. lg->WriteSpecialTargetsTop(makefileStream);
  217. // write the target convenience rules
  218. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  219. lg = static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  220. this->WriteConvenienceRules2(makefileStream, lg);
  221. }
  222. lg = static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  223. lg->WriteSpecialTargetsBottom(makefileStream);
  224. }
  225. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile()
  226. {
  227. // Open the output file. This should not be copy-if-different
  228. // because the check-build-system step compares the makefile time to
  229. // see if the build system must be regenerated.
  230. std::string cmakefileName =
  231. this->GetCMakeInstance()->GetHomeOutputDirectory();
  232. cmakefileName += cmake::GetCMakeFilesDirectory();
  233. cmakefileName += "/Makefile.cmake";
  234. cmGeneratedFileStream cmakefileStream(cmakefileName.c_str());
  235. if (!cmakefileStream) {
  236. return;
  237. }
  238. std::string makefileName =
  239. this->GetCMakeInstance()->GetHomeOutputDirectory();
  240. makefileName += "/Makefile";
  241. // get a local generator for some useful methods
  242. cmLocalUnixMakefileGenerator3* lg =
  243. static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  244. // Write the do not edit header.
  245. lg->WriteDisclaimer(cmakefileStream);
  246. // Save the generator name
  247. cmakefileStream << "# The generator used is:\n"
  248. << "set(CMAKE_DEPENDS_GENERATOR \"" << this->GetName()
  249. << "\")\n\n";
  250. // for each cmMakefile get its list of dependencies
  251. std::vector<std::string> lfiles;
  252. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  253. lg = static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  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. #if !defined(__VMS) // The Compaq STL on VMS crashes, so accept duplicates.
  261. std::vector<std::string>::iterator new_end =
  262. std::unique(lfiles.begin(), lfiles.end());
  263. lfiles.erase(new_end, lfiles.end());
  264. #endif
  265. // reset lg to the first makefile
  266. lg = static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  267. std::string currentBinDir = lg->GetCurrentBinaryDirectory();
  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. << " \"CMakeCache.txt\"\n";
  273. for (std::string const& f : lfiles) {
  274. cmakefileStream << " \"" << lg->ConvertToRelativePath(currentBinDir, f)
  275. << "\"\n";
  276. }
  277. cmakefileStream << " )\n\n";
  278. // Build the path to the cache check file.
  279. std::string check = this->GetCMakeInstance()->GetHomeOutputDirectory();
  280. check += cmake::GetCMakeFilesDirectory();
  281. check += "/cmake.check_cache";
  282. // Set the corresponding makefile in the cmake file.
  283. cmakefileStream << "# The corresponding makefile is:\n"
  284. << "set(CMAKE_MAKEFILE_OUTPUTS\n"
  285. << " \""
  286. << lg->ConvertToRelativePath(currentBinDir, makefileName)
  287. << "\"\n"
  288. << " \"" << lg->ConvertToRelativePath(currentBinDir, check)
  289. << "\"\n";
  290. cmakefileStream << " )\n\n";
  291. const std::string binDir = lg->GetBinaryDirectory();
  292. // CMake must rerun if a byproduct is missing.
  293. {
  294. cmakefileStream << "# Byproducts of CMake generate step:\n"
  295. << "set(CMAKE_MAKEFILE_PRODUCTS\n";
  296. const std::vector<std::string>& outfiles =
  297. lg->GetMakefile()->GetOutputFiles();
  298. for (std::string const& outfile : outfiles) {
  299. cmakefileStream << " \"" << lg->ConvertToRelativePath(binDir, outfile)
  300. << "\"\n";
  301. }
  302. // add in all the directory information files
  303. std::string tmpStr;
  304. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  305. lg = static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  306. tmpStr = lg->GetCurrentBinaryDirectory();
  307. tmpStr += cmake::GetCMakeFilesDirectory();
  308. tmpStr += "/CMakeDirectoryInformation.cmake";
  309. cmakefileStream << " \"" << lg->ConvertToRelativePath(binDir, tmpStr)
  310. << "\"\n";
  311. }
  312. cmakefileStream << " )\n\n";
  313. }
  314. this->WriteMainCMakefileLanguageRules(cmakefileStream,
  315. this->LocalGenerators);
  316. }
  317. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefileLanguageRules(
  318. cmGeneratedFileStream& cmakefileStream,
  319. std::vector<cmLocalGenerator*>& lGenerators)
  320. {
  321. cmLocalUnixMakefileGenerator3* lg;
  322. // now list all the target info files
  323. cmakefileStream << "# Dependency information for all targets:\n";
  324. cmakefileStream << "set(CMAKE_DEPEND_INFO_FILES\n";
  325. for (cmLocalGenerator* lGenerator : lGenerators) {
  326. lg = static_cast<cmLocalUnixMakefileGenerator3*>(lGenerator);
  327. // for all of out targets
  328. const std::vector<cmGeneratorTarget*>& tgts = lg->GetGeneratorTargets();
  329. for (cmGeneratorTarget* tgt : tgts) {
  330. if ((tgt->GetType() == cmStateEnums::EXECUTABLE) ||
  331. (tgt->GetType() == cmStateEnums::STATIC_LIBRARY) ||
  332. (tgt->GetType() == cmStateEnums::SHARED_LIBRARY) ||
  333. (tgt->GetType() == cmStateEnums::MODULE_LIBRARY) ||
  334. (tgt->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
  335. (tgt->GetType() == cmStateEnums::UTILITY)) {
  336. cmGeneratorTarget* gt = tgt;
  337. std::string tname = lg->GetRelativeTargetDirectory(gt);
  338. tname += "/DependInfo.cmake";
  339. cmSystemTools::ConvertToUnixSlashes(tname);
  340. cmakefileStream << " \"" << tname << "\"\n";
  341. }
  342. }
  343. }
  344. cmakefileStream << " )\n";
  345. }
  346. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRule2(
  347. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg,
  348. const char* pass, bool check_all, bool check_relink)
  349. {
  350. // Get the relative path to the subdirectory from the top.
  351. std::string makeTarget = lg->GetCurrentBinaryDirectory();
  352. makeTarget += "/";
  353. makeTarget += pass;
  354. // The directory-level rule should depend on the target-level rules
  355. // for all targets in the directory.
  356. std::vector<std::string> depends;
  357. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  358. for (cmGeneratorTarget* gtarget : targets) {
  359. int type = gtarget->GetType();
  360. if ((type == cmStateEnums::EXECUTABLE) ||
  361. (type == cmStateEnums::STATIC_LIBRARY) ||
  362. (type == cmStateEnums::SHARED_LIBRARY) ||
  363. (type == cmStateEnums::MODULE_LIBRARY) ||
  364. (type == cmStateEnums::OBJECT_LIBRARY) ||
  365. (type == cmStateEnums::UTILITY)) {
  366. // Add this to the list of depends rules in this directory.
  367. if ((!check_all || !gtarget->GetPropertyAsBool("EXCLUDE_FROM_ALL")) &&
  368. (!check_relink ||
  369. gtarget->NeedRelinkBeforeInstall(lg->GetConfigName()))) {
  370. std::string tname = lg->GetRelativeTargetDirectory(gtarget);
  371. tname += "/";
  372. tname += pass;
  373. depends.push_back(std::move(tname));
  374. }
  375. }
  376. }
  377. // The directory-level rule should depend on the directory-level
  378. // rules of the subdirectories.
  379. std::vector<cmStateSnapshot> children = lg->GetStateSnapshot().GetChildren();
  380. for (cmStateSnapshot const& c : children) {
  381. std::string subdir = c.GetDirectory().GetCurrentBinary();
  382. subdir += "/";
  383. subdir += pass;
  384. depends.push_back(std::move(subdir));
  385. }
  386. // Work-around for makes that drop rules that have no dependencies
  387. // or commands.
  388. if (depends.empty() && !this->EmptyRuleHackDepends.empty()) {
  389. depends.push_back(this->EmptyRuleHackDepends);
  390. }
  391. // Write the rule.
  392. std::string doc = "Convenience name for \"";
  393. doc += pass;
  394. doc += "\" pass in the directory.";
  395. std::vector<std::string> no_commands;
  396. lg->WriteMakeRule(ruleFileStream, doc.c_str(), makeTarget, depends,
  397. no_commands, true);
  398. }
  399. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRules2(
  400. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  401. {
  402. // Only subdirectories need these rules.
  403. if (lg->IsRootMakefile()) {
  404. return;
  405. }
  406. // Begin the directory-level rules section.
  407. std::string dir = cmSystemTools::ConvertToOutputPath(
  408. lg->ConvertToRelativePath(lg->GetBinaryDirectory(),
  409. lg->GetCurrentBinaryDirectory())
  410. .c_str());
  411. lg->WriteDivider(ruleFileStream);
  412. ruleFileStream << "# Directory level rules for directory " << dir << "\n\n";
  413. // Write directory-level rules for "all".
  414. this->WriteDirectoryRule2(ruleFileStream, lg, "all", true, false);
  415. // Write directory-level rules for "clean".
  416. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false);
  417. // Write directory-level rules for "preinstall".
  418. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", true, true);
  419. }
  420. void cmGlobalUnixMakefileGenerator3::GenerateBuildCommand(
  421. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  422. const std::string& /*projectName*/, const std::string& /*projectDir*/,
  423. const std::string& targetName, const std::string& /*config*/, bool fast,
  424. bool /*verbose*/, std::vector<std::string> const& makeOptions)
  425. {
  426. makeCommand.push_back(this->SelectMakeProgram(makeProgram));
  427. // Since we have full control over the invocation of nmake, let us
  428. // make it quiet.
  429. if (cmHasLiteralPrefix(this->GetName(), "NMake Makefiles")) {
  430. makeCommand.push_back("/NOLOGO");
  431. }
  432. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  433. makeOptions.end());
  434. if (!targetName.empty()) {
  435. cmMakefile* mf;
  436. if (!this->Makefiles.empty()) {
  437. mf = this->Makefiles[0];
  438. } else {
  439. cmStateSnapshot snapshot = this->CMakeInstance->GetCurrentSnapshot();
  440. snapshot.GetDirectory().SetCurrentSource(
  441. this->CMakeInstance->GetHomeDirectory());
  442. snapshot.GetDirectory().SetCurrentBinary(
  443. this->CMakeInstance->GetHomeOutputDirectory());
  444. snapshot.SetDefaultDefinitions();
  445. mf = new cmMakefile(this, snapshot);
  446. }
  447. std::string tname = targetName;
  448. if (fast) {
  449. tname += "/fast";
  450. }
  451. cmOutputConverter conv(mf->GetStateSnapshot());
  452. tname =
  453. conv.ConvertToRelativePath(mf->GetState()->GetBinaryDirectory(), tname);
  454. cmSystemTools::ConvertToOutputSlashes(tname);
  455. makeCommand.push_back(std::move(tname));
  456. if (this->Makefiles.empty()) {
  457. delete mf;
  458. }
  459. }
  460. }
  461. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules(
  462. std::ostream& ruleFileStream, std::set<std::string>& emitted)
  463. {
  464. std::vector<std::string> depends;
  465. std::vector<std::string> commands;
  466. depends.push_back("cmake_check_build_system");
  467. // write the target convenience rules
  468. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  469. cmLocalUnixMakefileGenerator3* lg =
  470. static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  471. // for each target Generate the rule files for each target.
  472. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  473. for (cmGeneratorTarget* gtarget : targets) {
  474. // Don't emit the same rule twice (e.g. two targets with the same
  475. // simple name)
  476. int type = gtarget->GetType();
  477. std::string name = gtarget->GetName();
  478. if (!name.empty() && emitted.insert(name).second &&
  479. // Handle user targets here. Global targets are handled in
  480. // the local generator on a per-directory basis.
  481. ((type == cmStateEnums::EXECUTABLE) ||
  482. (type == cmStateEnums::STATIC_LIBRARY) ||
  483. (type == cmStateEnums::SHARED_LIBRARY) ||
  484. (type == cmStateEnums::MODULE_LIBRARY) ||
  485. (type == cmStateEnums::OBJECT_LIBRARY) ||
  486. (type == cmStateEnums::UTILITY))) {
  487. // Add a rule to build the target by name.
  488. lg->WriteDivider(ruleFileStream);
  489. ruleFileStream << "# Target rules for targets named " << name
  490. << "\n\n";
  491. // Write the rule.
  492. commands.clear();
  493. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  494. tmp += "Makefile2";
  495. commands.push_back(lg->GetRecursiveMakeCall(tmp.c_str(), name));
  496. depends.clear();
  497. depends.push_back("cmake_check_build_system");
  498. lg->WriteMakeRule(ruleFileStream, "Build rule for target.", name,
  499. depends, commands, true);
  500. // Add a fast rule to build the target
  501. std::string localName = lg->GetRelativeTargetDirectory(gtarget);
  502. std::string makefileName;
  503. makefileName = localName;
  504. makefileName += "/build.make";
  505. depends.clear();
  506. commands.clear();
  507. std::string makeTargetName = localName;
  508. makeTargetName += "/build";
  509. localName = name;
  510. localName += "/fast";
  511. commands.push_back(
  512. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  513. lg->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  514. localName, depends, commands, true);
  515. // Add a local name for the rule to relink the target before
  516. // installation.
  517. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  518. makeTargetName = lg->GetRelativeTargetDirectory(gtarget);
  519. makeTargetName += "/preinstall";
  520. localName = name;
  521. localName += "/preinstall";
  522. depends.clear();
  523. commands.clear();
  524. commands.push_back(
  525. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  526. lg->WriteMakeRule(ruleFileStream,
  527. "Manual pre-install relink rule for target.",
  528. localName, depends, commands, true);
  529. }
  530. }
  531. }
  532. }
  533. }
  534. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2(
  535. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  536. {
  537. std::vector<std::string> depends;
  538. std::vector<std::string> commands;
  539. std::string localName;
  540. std::string makeTargetName;
  541. // write the directory level rules for this local gen
  542. this->WriteDirectoryRules2(ruleFileStream, lg);
  543. depends.push_back("cmake_check_build_system");
  544. // for each target Generate the rule files for each target.
  545. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  546. for (cmGeneratorTarget* gtarget : targets) {
  547. int type = gtarget->GetType();
  548. std::string name = gtarget->GetName();
  549. if (!name.empty() && ((type == cmStateEnums::EXECUTABLE) ||
  550. (type == cmStateEnums::STATIC_LIBRARY) ||
  551. (type == cmStateEnums::SHARED_LIBRARY) ||
  552. (type == cmStateEnums::MODULE_LIBRARY) ||
  553. (type == cmStateEnums::OBJECT_LIBRARY) ||
  554. (type == cmStateEnums::UTILITY))) {
  555. std::string makefileName;
  556. // Add a rule to build the target by name.
  557. localName = lg->GetRelativeTargetDirectory(gtarget);
  558. makefileName = localName;
  559. makefileName += "/build.make";
  560. lg->WriteDivider(ruleFileStream);
  561. ruleFileStream << "# Target rules for target " << localName << "\n\n";
  562. commands.clear();
  563. makeTargetName = localName;
  564. makeTargetName += "/depend";
  565. commands.push_back(
  566. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  567. makeTargetName = localName;
  568. makeTargetName += "/build";
  569. commands.push_back(
  570. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  571. // Write the rule.
  572. localName += "/all";
  573. depends.clear();
  574. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  575. progress.Dir = lg->GetBinaryDirectory();
  576. progress.Dir += cmake::GetCMakeFilesDirectory();
  577. {
  578. std::ostringstream progressArg;
  579. const char* sep = "";
  580. std::vector<unsigned long> const& progFiles =
  581. this->ProgressMap[gtarget].Marks;
  582. for (unsigned long progFile : progFiles) {
  583. progressArg << sep << progFile;
  584. sep = ",";
  585. }
  586. progress.Arg = progressArg.str();
  587. }
  588. bool targetMessages = true;
  589. if (const char* tgtMsg =
  590. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  591. "TARGET_MESSAGES")) {
  592. targetMessages = cmSystemTools::IsOn(tgtMsg);
  593. }
  594. if (targetMessages) {
  595. lg->AppendEcho(commands, "Built target " + name,
  596. cmLocalUnixMakefileGenerator3::EchoNormal, &progress);
  597. }
  598. this->AppendGlobalTargetDepends(depends, gtarget);
  599. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  600. localName, depends, commands, true);
  601. // add the all/all dependency
  602. if (!this->IsExcluded(this->LocalGenerators[0], gtarget)) {
  603. depends.clear();
  604. depends.push_back(localName);
  605. commands.clear();
  606. lg->WriteMakeRule(ruleFileStream, "Include target in all.", "all",
  607. depends, commands, true);
  608. }
  609. // Write the rule.
  610. commands.clear();
  611. {
  612. // TODO: Convert the total progress count to a make variable.
  613. std::ostringstream progCmd;
  614. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  615. // # in target
  616. progCmd << lg->ConvertToOutputFormat(
  617. cmSystemTools::CollapseFullPath(progress.Dir),
  618. cmOutputConverter::SHELL);
  619. //
  620. std::set<cmGeneratorTarget const*> emitted;
  621. progCmd << " " << this->CountProgressMarksInTarget(gtarget, emitted);
  622. commands.push_back(progCmd.str());
  623. }
  624. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  625. tmp += "Makefile2";
  626. commands.push_back(lg->GetRecursiveMakeCall(tmp.c_str(), localName));
  627. {
  628. std::ostringstream progCmd;
  629. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  630. progCmd << lg->ConvertToOutputFormat(
  631. cmSystemTools::CollapseFullPath(progress.Dir),
  632. cmOutputConverter::SHELL);
  633. progCmd << " 0";
  634. commands.push_back(progCmd.str());
  635. }
  636. depends.clear();
  637. depends.push_back("cmake_check_build_system");
  638. localName = lg->GetRelativeTargetDirectory(gtarget);
  639. localName += "/rule";
  640. lg->WriteMakeRule(ruleFileStream,
  641. "Build rule for subdir invocation for target.",
  642. localName, depends, commands, true);
  643. // Add a target with the canonical name (no prefix, suffix or path).
  644. commands.clear();
  645. depends.clear();
  646. depends.push_back(localName);
  647. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.", name,
  648. depends, commands, true);
  649. // Add rules to prepare the target for installation.
  650. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  651. localName = lg->GetRelativeTargetDirectory(gtarget);
  652. localName += "/preinstall";
  653. depends.clear();
  654. commands.clear();
  655. commands.push_back(
  656. lg->GetRecursiveMakeCall(makefileName.c_str(), localName));
  657. lg->WriteMakeRule(ruleFileStream,
  658. "Pre-install relink rule for target.", localName,
  659. depends, commands, true);
  660. if (!this->IsExcluded(this->LocalGenerators[0], gtarget)) {
  661. depends.clear();
  662. depends.push_back(localName);
  663. commands.clear();
  664. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  665. "preinstall", depends, commands, true);
  666. }
  667. }
  668. // add the clean rule
  669. localName = lg->GetRelativeTargetDirectory(gtarget);
  670. makeTargetName = localName;
  671. makeTargetName += "/clean";
  672. depends.clear();
  673. commands.clear();
  674. commands.push_back(
  675. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  676. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  677. makeTargetName, depends, commands, true);
  678. commands.clear();
  679. depends.push_back(makeTargetName);
  680. lg->WriteMakeRule(ruleFileStream, "clean rule for target.", "clean",
  681. depends, commands, true);
  682. }
  683. }
  684. }
  685. // Build a map that contains a the set of targets used by each local
  686. // generator directory level.
  687. void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks()
  688. {
  689. this->DirectoryTargetsMap.clear();
  690. // Loop over all targets in all local generators.
  691. for (cmLocalGenerator* lg : this->LocalGenerators) {
  692. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  693. for (cmGeneratorTarget* gt : targets) {
  694. cmLocalGenerator* tlg = gt->GetLocalGenerator();
  695. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  696. gt->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  697. continue;
  698. }
  699. cmStateSnapshot csnp = lg->GetStateSnapshot();
  700. cmStateSnapshot tsnp = tlg->GetStateSnapshot();
  701. // Consider the directory containing the target and all its
  702. // parents until something excludes the target.
  703. for (; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  704. csnp = csnp.GetBuildsystemDirectoryParent()) {
  705. // This local generator includes the target.
  706. std::set<cmGeneratorTarget const*>& targetSet =
  707. this->DirectoryTargetsMap[csnp];
  708. targetSet.insert(gt);
  709. // Add dependencies of the included target. An excluded
  710. // target may still be included if it is a dependency of a
  711. // non-excluded target.
  712. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(gt);
  713. for (cmTargetDepend const& tgtdep : tgtdeps) {
  714. targetSet.insert(tgtdep);
  715. }
  716. }
  717. }
  718. }
  719. }
  720. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget(
  721. cmGeneratorTarget const* target, std::set<cmGeneratorTarget const*>& emitted)
  722. {
  723. size_t count = 0;
  724. if (emitted.insert(target).second) {
  725. count = this->ProgressMap[target].Marks.size();
  726. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  727. for (cmTargetDepend const& depend : depends) {
  728. if (depend->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  729. continue;
  730. }
  731. count += this->CountProgressMarksInTarget(depend, emitted);
  732. }
  733. }
  734. return count;
  735. }
  736. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll(
  737. cmLocalGenerator* lg)
  738. {
  739. size_t count = 0;
  740. std::set<cmGeneratorTarget const*> emitted;
  741. std::set<cmGeneratorTarget const*> const& targets =
  742. this->DirectoryTargetsMap[lg->GetStateSnapshot()];
  743. for (cmGeneratorTarget const* target : targets) {
  744. count += this->CountProgressMarksInTarget(target, emitted);
  745. }
  746. return count;
  747. }
  748. void cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  749. cmMakefileTargetGenerator* tg)
  750. {
  751. TargetProgress& tp = this->ProgressMap[tg->GetGeneratorTarget()];
  752. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  753. tp.VariableFile = tg->GetProgressFileNameFull();
  754. }
  755. void cmGlobalUnixMakefileGenerator3::TargetProgress::WriteProgressVariables(
  756. unsigned long total, unsigned long& current)
  757. {
  758. cmGeneratedFileStream fout(this->VariableFile.c_str());
  759. for (unsigned long i = 1; i <= this->NumberOfActions; ++i) {
  760. fout << "CMAKE_PROGRESS_" << i << " = ";
  761. if (total <= 100) {
  762. unsigned long num = i + current;
  763. fout << num;
  764. this->Marks.push_back(num);
  765. } else if (((i + current) * 100) / total >
  766. ((i - 1 + current) * 100) / total) {
  767. unsigned long num = ((i + current) * 100) / total;
  768. fout << num;
  769. this->Marks.push_back(num);
  770. }
  771. fout << "\n";
  772. }
  773. fout << "\n";
  774. current += this->NumberOfActions;
  775. }
  776. void cmGlobalUnixMakefileGenerator3::AppendGlobalTargetDepends(
  777. std::vector<std::string>& depends, cmGeneratorTarget* target)
  778. {
  779. TargetDependSet const& depends_set = this->GetTargetDirectDepends(target);
  780. for (cmTargetDepend const& i : depends_set) {
  781. // Create the target-level dependency.
  782. cmGeneratorTarget const* dep = i;
  783. if (dep->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  784. continue;
  785. }
  786. cmLocalUnixMakefileGenerator3* lg3 =
  787. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  788. std::string tgtName =
  789. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep));
  790. tgtName += "/all";
  791. depends.push_back(tgtName);
  792. }
  793. }
  794. void cmGlobalUnixMakefileGenerator3::WriteHelpRule(
  795. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  796. {
  797. // add the help target
  798. std::string path;
  799. std::vector<std::string> no_depends;
  800. std::vector<std::string> commands;
  801. lg->AppendEcho(commands, "The following are some of the valid targets "
  802. "for this Makefile:");
  803. lg->AppendEcho(commands, "... all (the default if no target is provided)");
  804. lg->AppendEcho(commands, "... clean");
  805. lg->AppendEcho(commands, "... depend");
  806. // Keep track of targets already listed.
  807. std::set<std::string> emittedTargets;
  808. // for each local generator
  809. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  810. cmLocalUnixMakefileGenerator3* lg2 =
  811. static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  812. // for the passed in makefile or if this is the top Makefile wripte out
  813. // the targets
  814. if (lg2 == lg || lg->IsRootMakefile()) {
  815. // for each target Generate the rule files for each target.
  816. const std::vector<cmGeneratorTarget*>& targets =
  817. lg2->GetGeneratorTargets();
  818. for (cmGeneratorTarget* target : targets) {
  819. cmStateEnums::TargetType type = target->GetType();
  820. if ((type == cmStateEnums::EXECUTABLE) ||
  821. (type == cmStateEnums::STATIC_LIBRARY) ||
  822. (type == cmStateEnums::SHARED_LIBRARY) ||
  823. (type == cmStateEnums::MODULE_LIBRARY) ||
  824. (type == cmStateEnums::OBJECT_LIBRARY) ||
  825. (type == cmStateEnums::GLOBAL_TARGET) ||
  826. (type == cmStateEnums::UTILITY)) {
  827. std::string const& name = target->GetName();
  828. if (emittedTargets.insert(name).second) {
  829. path = "... ";
  830. path += name;
  831. lg->AppendEcho(commands, path);
  832. }
  833. }
  834. }
  835. }
  836. }
  837. for (std::string const& o : lg->GetLocalHelp()) {
  838. path = "... ";
  839. path += o;
  840. lg->AppendEcho(commands, path);
  841. }
  842. lg->WriteMakeRule(ruleFileStream, "Help Target", "help", no_depends,
  843. commands, true);
  844. ruleFileStream << "\n\n";
  845. }