cmGlobalUnixMakefileGenerator3.cxx 34 KB

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