cmGlobalUnixMakefileGenerator3.cxx 33 KB

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