cmGlobalUnixMakefileGenerator3.cxx 32 KB

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