cmGlobalUnixMakefileGenerator3.cxx 33 KB

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