cmGlobalUnixMakefileGenerator3.cxx 34 KB

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