cmGlobalUnixMakefileGenerator3.cxx 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 (const auto& 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. const std::string& sourceFile, const std::string& workingDirectory,
  128. const std::string& compileCommand, const std::string& 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 (const auto& 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 (const auto& 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. const auto& 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 (const auto& 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 (const auto& lGenerator : lGenerators) {
  301. const auto& lg =
  302. cm::static_reference_cast<cmLocalUnixMakefileGenerator3>(lGenerator);
  303. // for all of out targets
  304. for (const auto& 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, const char* 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. const std::string& makeProgram, const std::string& /*projectName*/,
  469. const std::string& /*projectDir*/,
  470. std::vector<std::string> const& targetNames, const std::string& /*config*/,
  471. int jobs, bool verbose, const cmBuildOptions& 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("-j" + std::to_string(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 (const auto& 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 (const auto& 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 (const auto& 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. std::ostringstream progressArg;
  611. const char* sep = "";
  612. for (unsigned long progFile : this->ProgressMap[gtarget.get()].Marks) {
  613. progressArg << sep << progFile;
  614. sep = ",";
  615. }
  616. progress.Arg = progressArg.str();
  617. }
  618. bool targetMessages = true;
  619. if (cmValue tgtMsg =
  620. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  621. "TARGET_MESSAGES")) {
  622. targetMessages = tgtMsg.IsOn();
  623. }
  624. if (targetMessages) {
  625. lg.AppendEcho(commands, "Built target " + name,
  626. cmLocalUnixMakefileGenerator3::EchoNormal, &progress);
  627. }
  628. this->AppendGlobalTargetDepends(depends, gtarget.get());
  629. rootLG.WriteMakeRule(ruleFileStream, "All Build rule for target.",
  630. localName, depends, commands, true);
  631. // Write the rule.
  632. commands.clear();
  633. {
  634. // TODO: Convert the total progress count to a make variable.
  635. std::ostringstream progCmd;
  636. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  637. // # in target
  638. progCmd << lg.ConvertToOutputFormat(progress.Dir,
  639. cmOutputConverter::SHELL);
  640. //
  641. std::set<cmGeneratorTarget const*> emitted;
  642. progCmd << " "
  643. << this->CountProgressMarksInTarget(gtarget.get(), emitted);
  644. commands.push_back(progCmd.str());
  645. }
  646. std::string tmp = "CMakeFiles/Makefile2";
  647. commands.push_back(lg.GetRecursiveMakeCall(tmp, localName));
  648. {
  649. std::ostringstream progCmd;
  650. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  651. progCmd << lg.ConvertToOutputFormat(progress.Dir,
  652. cmOutputConverter::SHELL);
  653. progCmd << " 0";
  654. commands.push_back(progCmd.str());
  655. }
  656. depends.clear();
  657. if (regenerate) {
  658. depends.emplace_back("cmake_check_build_system");
  659. }
  660. localName =
  661. cmStrCat(lg.GetRelativeTargetDirectory(gtarget.get()), "/rule");
  662. rootLG.WriteMakeRule(ruleFileStream,
  663. "Build rule for subdir invocation for target.",
  664. localName, depends, commands, true);
  665. // Add a target with the canonical name (no prefix, suffix or path).
  666. commands.clear();
  667. depends.clear();
  668. depends.push_back(localName);
  669. rootLG.WriteMakeRule(ruleFileStream, "Convenience name for target.",
  670. name, depends, commands, true);
  671. // Add rules to prepare the target for installation.
  672. if (gtarget->NeedRelinkBeforeInstall(lg.GetConfigName())) {
  673. localName = cmStrCat(lg.GetRelativeTargetDirectory(gtarget.get()),
  674. "/preinstall");
  675. depends.clear();
  676. commands.clear();
  677. commands.push_back(lg.GetRecursiveMakeCall(makefileName, localName));
  678. rootLG.WriteMakeRule(ruleFileStream,
  679. "Pre-install relink rule for target.", localName,
  680. depends, commands, true);
  681. }
  682. // add the codegen rule
  683. localName = lg.GetRelativeTargetDirectory(gtarget.get());
  684. depends.clear();
  685. commands.clear();
  686. makeTargetName = cmStrCat(localName, "/codegen");
  687. commands.push_back(
  688. lg.GetRecursiveMakeCall(makefileName, makeTargetName));
  689. this->AppendCodegenTargetDepends(depends, gtarget.get());
  690. rootLG.WriteMakeRule(ruleFileStream, "codegen rule for target.",
  691. makeTargetName, depends, commands, true);
  692. // add the clean rule
  693. localName = lg.GetRelativeTargetDirectory(gtarget.get());
  694. makeTargetName = cmStrCat(localName, "/clean");
  695. depends.clear();
  696. commands.clear();
  697. commands.push_back(
  698. lg.GetRecursiveMakeCall(makefileName, makeTargetName));
  699. rootLG.WriteMakeRule(ruleFileStream, "clean rule for target.",
  700. makeTargetName, depends, commands, true);
  701. commands.clear();
  702. }
  703. }
  704. }
  705. // Build a map that contains the set of targets used by each local
  706. // generator directory level.
  707. void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks()
  708. {
  709. this->DirectoryTargetsMap.clear();
  710. // Loop over all targets in all local generators.
  711. for (const auto& lg : this->LocalGenerators) {
  712. for (const auto& gt : lg->GetGeneratorTargets()) {
  713. cmLocalGenerator* tlg = gt->GetLocalGenerator();
  714. if (!gt->IsInBuildSystem() || this->IsExcluded(lg.get(), gt.get())) {
  715. continue;
  716. }
  717. cmStateSnapshot csnp = lg->GetStateSnapshot();
  718. cmStateSnapshot tsnp = tlg->GetStateSnapshot();
  719. // Consider the directory containing the target and all its
  720. // parents until something excludes the target.
  721. for (; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  722. csnp = csnp.GetBuildsystemDirectoryParent()) {
  723. // This local generator includes the target.
  724. std::set<cmGeneratorTarget const*>& targetSet =
  725. this->DirectoryTargetsMap[csnp];
  726. targetSet.insert(gt.get());
  727. // Add dependencies of the included target. An excluded
  728. // target may still be included if it is a dependency of a
  729. // non-excluded target.
  730. for (cmTargetDepend const& tgtdep :
  731. this->GetTargetDirectDepends(gt.get())) {
  732. targetSet.insert(tgtdep);
  733. }
  734. }
  735. }
  736. }
  737. }
  738. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget(
  739. cmGeneratorTarget const* target, std::set<cmGeneratorTarget const*>& emitted)
  740. {
  741. size_t count = 0;
  742. if (emitted.insert(target).second) {
  743. count = this->ProgressMap[target].Marks.size();
  744. for (cmTargetDepend const& depend : this->GetTargetDirectDepends(target)) {
  745. if (!depend->IsInBuildSystem()) {
  746. continue;
  747. }
  748. count += this->CountProgressMarksInTarget(depend, emitted);
  749. }
  750. }
  751. return count;
  752. }
  753. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll(
  754. const cmLocalGenerator& lg)
  755. {
  756. size_t count = 0;
  757. std::set<cmGeneratorTarget const*> emitted;
  758. for (cmGeneratorTarget const* target :
  759. this->DirectoryTargetsMap[lg.GetStateSnapshot()]) {
  760. count += this->CountProgressMarksInTarget(target, emitted);
  761. }
  762. return count;
  763. }
  764. void cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  765. cmMakefileTargetGenerator* tg)
  766. {
  767. TargetProgress& tp = this->ProgressMap[tg->GetGeneratorTarget()];
  768. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  769. tp.VariableFile = tg->GetProgressFileNameFull();
  770. }
  771. void cmGlobalUnixMakefileGenerator3::TargetProgress::WriteProgressVariables(
  772. unsigned long total, unsigned long& current)
  773. {
  774. cmGeneratedFileStream fout(this->VariableFile);
  775. for (unsigned long i = 1; i <= this->NumberOfActions; ++i) {
  776. fout << "CMAKE_PROGRESS_" << i << " = ";
  777. if (total <= 100) {
  778. unsigned long num = i + current;
  779. fout << num;
  780. this->Marks.push_back(num);
  781. } else if (((i + current) * 100) / total >
  782. ((i - 1 + current) * 100) / total) {
  783. unsigned long num = ((i + current) * 100) / total;
  784. fout << num;
  785. this->Marks.push_back(num);
  786. }
  787. fout << "\n";
  788. }
  789. fout << "\n";
  790. current += this->NumberOfActions;
  791. }
  792. void cmGlobalUnixMakefileGenerator3::AppendGlobalTargetDepends(
  793. std::vector<std::string>& depends, cmGeneratorTarget* target)
  794. {
  795. for (cmTargetDepend const& i : this->GetTargetDirectDepends(target)) {
  796. // Create the target-level dependency.
  797. cmGeneratorTarget const* dep = i;
  798. if (!dep->IsInBuildSystem()) {
  799. continue;
  800. }
  801. cmLocalUnixMakefileGenerator3* lg3 =
  802. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  803. std::string tgtName = cmStrCat(
  804. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep)),
  805. "/all");
  806. depends.push_back(tgtName);
  807. }
  808. }
  809. void cmGlobalUnixMakefileGenerator3::AppendCodegenTargetDepends(
  810. std::vector<std::string>& depends, cmGeneratorTarget* target)
  811. {
  812. const std::set<std::string>& codegen_depends =
  813. target->Target->GetCodegenDeps();
  814. for (cmTargetDepend const& i : this->GetTargetDirectDepends(target)) {
  815. // Create the target-level dependency.
  816. cmGeneratorTarget const* dep = i;
  817. if (!dep->IsInBuildSystem()) {
  818. continue;
  819. }
  820. if (codegen_depends.find(dep->GetName()) != codegen_depends.end()) {
  821. cmLocalUnixMakefileGenerator3* lg3 =
  822. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  823. std::string tgtName = cmStrCat(
  824. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep)),
  825. "/all");
  826. depends.push_back(tgtName);
  827. }
  828. }
  829. }
  830. void cmGlobalUnixMakefileGenerator3::WriteHelpRule(
  831. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  832. {
  833. // add the help target
  834. std::string path;
  835. std::vector<std::string> no_depends;
  836. std::vector<std::string> commands;
  837. lg->AppendEcho(commands,
  838. "The following are some of the valid targets "
  839. "for this Makefile:");
  840. lg->AppendEcho(commands, "... all (the default if no target is provided)");
  841. lg->AppendEcho(commands, "... clean");
  842. if (!this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  843. lg->AppendEcho(commands, "... depend");
  844. }
  845. // Keep track of targets already listed.
  846. std::set<std::string> emittedTargets;
  847. std::set<std::string> utility_targets;
  848. std::set<std::string> globals_targets;
  849. std::set<std::string> project_targets;
  850. // for each local generator
  851. for (const auto& localGen : this->LocalGenerators) {
  852. const auto& lg2 =
  853. cm::static_reference_cast<cmLocalUnixMakefileGenerator3>(localGen);
  854. // for the passed in makefile or if this is the top Makefile wripte out
  855. // the targets
  856. if (&lg2 == lg || lg->IsRootMakefile()) {
  857. // for each target Generate the rule files for each target.
  858. for (const auto& target : lg2.GetGeneratorTargets()) {
  859. cmStateEnums::TargetType type = target->GetType();
  860. if ((type == cmStateEnums::EXECUTABLE) ||
  861. (type == cmStateEnums::STATIC_LIBRARY) ||
  862. (type == cmStateEnums::SHARED_LIBRARY) ||
  863. (type == cmStateEnums::MODULE_LIBRARY) ||
  864. (type == cmStateEnums::OBJECT_LIBRARY) ||
  865. (type == cmStateEnums::INTERFACE_LIBRARY &&
  866. target->IsInBuildSystem())) {
  867. project_targets.insert(target->GetName());
  868. } else if (type == cmStateEnums::GLOBAL_TARGET) {
  869. globals_targets.insert(target->GetName());
  870. } else if (type == cmStateEnums::UTILITY) {
  871. utility_targets.insert(target->GetName());
  872. }
  873. }
  874. }
  875. }
  876. for (std::string const& name : globals_targets) {
  877. path = cmStrCat("... ", name);
  878. lg->AppendEcho(commands, path);
  879. }
  880. for (std::string const& name : utility_targets) {
  881. path = cmStrCat("... ", name);
  882. lg->AppendEcho(commands, path);
  883. }
  884. for (std::string const& name : project_targets) {
  885. path = cmStrCat("... ", name);
  886. lg->AppendEcho(commands, path);
  887. }
  888. for (std::string const& o : lg->GetLocalHelp()) {
  889. path = cmStrCat("... ", o);
  890. lg->AppendEcho(commands, path);
  891. }
  892. lg->WriteMakeRule(ruleFileStream, "Help Target", "help", no_depends,
  893. commands, true);
  894. ruleFileStream << "\n\n";
  895. }