cmGlobalUnixMakefileGenerator3.cxx 33 KB

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