cmGlobalUnixMakefileGenerator3.cxx 34 KB

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