cmGlobalUnixMakefileGenerator3.cxx 34 KB

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