cmGlobalUnixMakefileGenerator3.cxx 33 KB

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