cmGlobalUnixMakefileGenerator3.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 (cmGeneratorTarget* 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. cmGeneratorTarget* gt = tgt;
  335. std::string tname =
  336. cmStrCat(lg->GetRelativeTargetDirectory(gt), "/DependInfo.cmake");
  337. cmSystemTools::ConvertToUnixSlashes(tname);
  338. cmakefileStream << " \"" << tname << "\"\n";
  339. }
  340. }
  341. }
  342. cmakefileStream << " )\n";
  343. }
  344. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRule2(
  345. std::ostream& ruleFileStream, DirectoryTarget const& dt, const char* pass,
  346. bool check_all, bool check_relink, std::vector<std::string> const& commands)
  347. {
  348. auto* lg = static_cast<cmLocalUnixMakefileGenerator3*>(dt.LG);
  349. std::string makeTarget =
  350. cmStrCat(lg->GetCurrentBinaryDirectory(), '/', pass);
  351. // The directory-level rule should depend on the target-level rules
  352. // for all targets in the directory.
  353. std::vector<std::string> depends;
  354. for (DirectoryTarget::Target const& t : dt.Targets) {
  355. // Add this to the list of depends rules in this directory.
  356. if ((!check_all || !t.ExcludeFromAll) &&
  357. (!check_relink ||
  358. t.GT->NeedRelinkBeforeInstall(lg->GetConfigName()))) {
  359. // The target may be from a different directory; use its local gen.
  360. auto const* tlg = static_cast<cmLocalUnixMakefileGenerator3 const*>(
  361. t.GT->GetLocalGenerator());
  362. std::string tname =
  363. cmStrCat(tlg->GetRelativeTargetDirectory(t.GT), '/', pass);
  364. depends.push_back(std::move(tname));
  365. }
  366. }
  367. // The directory-level rule should depend on the directory-level
  368. // rules of the subdirectories.
  369. for (DirectoryTarget::Dir const& d : dt.Children) {
  370. if (check_all && d.ExcludeFromAll) {
  371. continue;
  372. }
  373. std::string subdir = cmStrCat(d.Path, '/', pass);
  374. depends.push_back(std::move(subdir));
  375. }
  376. // Work-around for makes that drop rules that have no dependencies
  377. // or commands.
  378. if (depends.empty() && !this->EmptyRuleHackDepends.empty()) {
  379. depends.push_back(this->EmptyRuleHackDepends);
  380. }
  381. // Write the rule.
  382. std::string doc;
  383. if (lg->IsRootMakefile()) {
  384. doc = cmStrCat("The main recursive \"", pass, "\" target.");
  385. } else {
  386. doc = cmStrCat("Recursive \"", pass, "\" directory target.");
  387. }
  388. lg->WriteMakeRule(ruleFileStream, doc.c_str(), makeTarget, depends, commands,
  389. true);
  390. }
  391. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRules2(
  392. std::ostream& ruleFileStream, DirectoryTarget const& dt)
  393. {
  394. auto* lg = static_cast<cmLocalUnixMakefileGenerator3*>(dt.LG);
  395. // Begin the directory-level rules section.
  396. {
  397. std::string dir =
  398. cmSystemTools::ConvertToOutputPath(lg->MaybeConvertToRelativePath(
  399. lg->GetBinaryDirectory(), lg->GetCurrentBinaryDirectory()));
  400. lg->WriteDivider(ruleFileStream);
  401. if (lg->IsRootMakefile()) {
  402. ruleFileStream << "# Directory level rules for the build root directory";
  403. } else {
  404. ruleFileStream << "# Directory level rules for directory " << dir;
  405. }
  406. ruleFileStream << "\n\n";
  407. }
  408. // Write directory-level rules for "all".
  409. this->WriteDirectoryRule2(ruleFileStream, dt, "all", true, false);
  410. // Write directory-level rules for "preinstall".
  411. this->WriteDirectoryRule2(ruleFileStream, dt, "preinstall", true, true);
  412. // Write directory-level rules for "clean".
  413. {
  414. std::vector<std::string> cmds;
  415. lg->AppendDirectoryCleanCommand(cmds);
  416. this->WriteDirectoryRule2(ruleFileStream, dt, "clean", false, false, cmds);
  417. }
  418. }
  419. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  420. cmGlobalUnixMakefileGenerator3::GenerateBuildCommand(
  421. const std::string& makeProgram, const std::string& /*projectName*/,
  422. const std::string& /*projectDir*/,
  423. std::vector<std::string> const& targetNames, const std::string& /*config*/,
  424. bool fast, int jobs, bool verbose,
  425. std::vector<std::string> const& makeOptions)
  426. {
  427. std::unique_ptr<cmMakefile> mfu;
  428. cmMakefile* mf;
  429. if (!this->Makefiles.empty()) {
  430. mf = this->Makefiles[0];
  431. } else {
  432. cmStateSnapshot snapshot = this->CMakeInstance->GetCurrentSnapshot();
  433. snapshot.GetDirectory().SetCurrentSource(
  434. this->CMakeInstance->GetHomeDirectory());
  435. snapshot.GetDirectory().SetCurrentBinary(
  436. this->CMakeInstance->GetHomeOutputDirectory());
  437. snapshot.SetDefaultDefinitions();
  438. mfu = cm::make_unique<cmMakefile>(this, snapshot);
  439. mf = mfu.get();
  440. }
  441. GeneratedMakeCommand makeCommand;
  442. // Make it possible to set verbosity also from command line
  443. if (verbose) {
  444. makeCommand.Add(cmSystemTools::GetCMakeCommand());
  445. makeCommand.Add("-E");
  446. makeCommand.Add("env");
  447. makeCommand.Add("VERBOSE=1");
  448. }
  449. makeCommand.Add(this->SelectMakeProgram(makeProgram));
  450. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  451. makeCommand.Add("-j");
  452. if (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  453. makeCommand.Add(std::to_string(jobs));
  454. }
  455. }
  456. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  457. for (auto tname : targetNames) {
  458. if (!tname.empty()) {
  459. if (fast) {
  460. tname += "/fast";
  461. }
  462. tname =
  463. mf->GetStateSnapshot().GetDirectory().ConvertToRelPathIfNotContained(
  464. mf->GetState()->GetBinaryDirectory(), tname);
  465. cmSystemTools::ConvertToOutputSlashes(tname);
  466. makeCommand.Add(std::move(tname));
  467. }
  468. }
  469. return { std::move(makeCommand) };
  470. }
  471. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules(
  472. std::ostream& ruleFileStream, std::set<std::string>& emitted)
  473. {
  474. std::vector<std::string> depends;
  475. std::vector<std::string> commands;
  476. bool regenerate = !this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  477. if (regenerate) {
  478. depends.emplace_back("cmake_check_build_system");
  479. }
  480. // write the target convenience rules
  481. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  482. cmLocalUnixMakefileGenerator3* lg =
  483. static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  484. // for each target Generate the rule files for each target.
  485. for (cmGeneratorTarget* gtarget : lg->GetGeneratorTargets()) {
  486. // Don't emit the same rule twice (e.g. two targets with the same
  487. // simple name)
  488. int type = gtarget->GetType();
  489. std::string name = gtarget->GetName();
  490. if (!name.empty() && emitted.insert(name).second &&
  491. // Handle user targets here. Global targets are handled in
  492. // the local generator on a per-directory basis.
  493. ((type == cmStateEnums::EXECUTABLE) ||
  494. (type == cmStateEnums::STATIC_LIBRARY) ||
  495. (type == cmStateEnums::SHARED_LIBRARY) ||
  496. (type == cmStateEnums::MODULE_LIBRARY) ||
  497. (type == cmStateEnums::OBJECT_LIBRARY) ||
  498. (type == cmStateEnums::UTILITY))) {
  499. // Add a rule to build the target by name.
  500. lg->WriteDivider(ruleFileStream);
  501. ruleFileStream << "# Target rules for targets named " << name
  502. << "\n\n";
  503. // Write the rule.
  504. commands.clear();
  505. std::string tmp = "CMakeFiles/Makefile2";
  506. commands.push_back(lg->GetRecursiveMakeCall(tmp, name));
  507. depends.clear();
  508. if (regenerate) {
  509. depends.emplace_back("cmake_check_build_system");
  510. }
  511. lg->WriteMakeRule(ruleFileStream, "Build rule for target.", name,
  512. depends, commands, true);
  513. // Add a fast rule to build the target
  514. std::string localName = lg->GetRelativeTargetDirectory(gtarget);
  515. std::string makefileName;
  516. makefileName = cmStrCat(localName, "/build.make");
  517. depends.clear();
  518. commands.clear();
  519. std::string makeTargetName = cmStrCat(localName, "/build");
  520. localName = cmStrCat(name, "/fast");
  521. commands.push_back(
  522. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  523. lg->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  524. localName, depends, commands, true);
  525. // Add a local name for the rule to relink the target before
  526. // installation.
  527. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  528. makeTargetName =
  529. cmStrCat(lg->GetRelativeTargetDirectory(gtarget), "/preinstall");
  530. localName = cmStrCat(name, "/preinstall");
  531. depends.clear();
  532. commands.clear();
  533. commands.push_back(
  534. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  535. lg->WriteMakeRule(ruleFileStream,
  536. "Manual pre-install relink rule for target.",
  537. localName, depends, commands, true);
  538. }
  539. }
  540. }
  541. }
  542. }
  543. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2(
  544. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  545. {
  546. std::vector<std::string> depends;
  547. std::vector<std::string> commands;
  548. std::string localName;
  549. std::string makeTargetName;
  550. bool regenerate = !this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  551. if (regenerate) {
  552. depends.emplace_back("cmake_check_build_system");
  553. }
  554. // for each target Generate the rule files for each target.
  555. for (cmGeneratorTarget* gtarget : lg->GetGeneratorTargets()) {
  556. int type = gtarget->GetType();
  557. std::string name = gtarget->GetName();
  558. if (!name.empty() &&
  559. ((type == cmStateEnums::EXECUTABLE) ||
  560. (type == cmStateEnums::STATIC_LIBRARY) ||
  561. (type == cmStateEnums::SHARED_LIBRARY) ||
  562. (type == cmStateEnums::MODULE_LIBRARY) ||
  563. (type == cmStateEnums::OBJECT_LIBRARY) ||
  564. (type == cmStateEnums::UTILITY))) {
  565. std::string makefileName;
  566. // Add a rule to build the target by name.
  567. localName = lg->GetRelativeTargetDirectory(gtarget);
  568. makefileName = cmStrCat(localName, "/build.make");
  569. lg->WriteDivider(ruleFileStream);
  570. ruleFileStream << "# Target rules for target " << localName << "\n\n";
  571. commands.clear();
  572. makeTargetName = cmStrCat(localName, "/depend");
  573. commands.push_back(
  574. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  575. makeTargetName = cmStrCat(localName, "/build");
  576. commands.push_back(
  577. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  578. // Write the rule.
  579. localName += "/all";
  580. depends.clear();
  581. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  582. progress.Dir = cmStrCat(lg->GetBinaryDirectory(), "/CMakeFiles");
  583. {
  584. std::ostringstream progressArg;
  585. const char* sep = "";
  586. for (unsigned long progFile : this->ProgressMap[gtarget].Marks) {
  587. progressArg << sep << progFile;
  588. sep = ",";
  589. }
  590. progress.Arg = progressArg.str();
  591. }
  592. bool targetMessages = true;
  593. if (const char* tgtMsg =
  594. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  595. "TARGET_MESSAGES")) {
  596. targetMessages = cmIsOn(tgtMsg);
  597. }
  598. if (targetMessages) {
  599. lg->AppendEcho(commands, "Built target " + name,
  600. cmLocalUnixMakefileGenerator3::EchoNormal, &progress);
  601. }
  602. this->AppendGlobalTargetDepends(depends, gtarget);
  603. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  604. localName, depends, commands, true);
  605. // Write the rule.
  606. commands.clear();
  607. {
  608. // TODO: Convert the total progress count to a make variable.
  609. std::ostringstream progCmd;
  610. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  611. // # in target
  612. progCmd << lg->ConvertToOutputFormat(
  613. cmSystemTools::CollapseFullPath(progress.Dir),
  614. cmOutputConverter::SHELL);
  615. //
  616. std::set<cmGeneratorTarget const*> emitted;
  617. progCmd << " " << this->CountProgressMarksInTarget(gtarget, 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 = cmStrCat(lg->GetRelativeTargetDirectory(gtarget), "/rule");
  636. lg->WriteMakeRule(ruleFileStream,
  637. "Build rule for subdir invocation for target.",
  638. localName, depends, commands, true);
  639. // Add a target with the canonical name (no prefix, suffix or path).
  640. commands.clear();
  641. depends.clear();
  642. depends.push_back(localName);
  643. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.", name,
  644. depends, commands, true);
  645. // Add rules to prepare the target for installation.
  646. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  647. localName =
  648. cmStrCat(lg->GetRelativeTargetDirectory(gtarget), "/preinstall");
  649. depends.clear();
  650. commands.clear();
  651. commands.push_back(lg->GetRecursiveMakeCall(makefileName, localName));
  652. lg->WriteMakeRule(ruleFileStream,
  653. "Pre-install relink rule for target.", localName,
  654. depends, commands, true);
  655. }
  656. // add the clean rule
  657. localName = lg->GetRelativeTargetDirectory(gtarget);
  658. makeTargetName = cmStrCat(localName, "/clean");
  659. depends.clear();
  660. commands.clear();
  661. commands.push_back(
  662. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  663. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  664. makeTargetName, depends, commands, true);
  665. commands.clear();
  666. }
  667. }
  668. }
  669. // Build a map that contains the set of targets used by each local
  670. // generator directory level.
  671. void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks()
  672. {
  673. this->DirectoryTargetsMap.clear();
  674. // Loop over all targets in all local generators.
  675. for (cmLocalGenerator* lg : this->LocalGenerators) {
  676. for (cmGeneratorTarget* gt : lg->GetGeneratorTargets()) {
  677. cmLocalGenerator* tlg = gt->GetLocalGenerator();
  678. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  679. gt->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  680. continue;
  681. }
  682. cmStateSnapshot csnp = lg->GetStateSnapshot();
  683. cmStateSnapshot tsnp = tlg->GetStateSnapshot();
  684. // Consider the directory containing the target and all its
  685. // parents until something excludes the target.
  686. for (; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  687. csnp = csnp.GetBuildsystemDirectoryParent()) {
  688. // This local generator includes the target.
  689. std::set<cmGeneratorTarget const*>& targetSet =
  690. this->DirectoryTargetsMap[csnp];
  691. targetSet.insert(gt);
  692. // Add dependencies of the included target. An excluded
  693. // target may still be included if it is a dependency of a
  694. // non-excluded target.
  695. for (cmTargetDepend const& tgtdep : this->GetTargetDirectDepends(gt)) {
  696. targetSet.insert(tgtdep);
  697. }
  698. }
  699. }
  700. }
  701. }
  702. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget(
  703. cmGeneratorTarget const* target, std::set<cmGeneratorTarget const*>& emitted)
  704. {
  705. size_t count = 0;
  706. if (emitted.insert(target).second) {
  707. count = this->ProgressMap[target].Marks.size();
  708. for (cmTargetDepend const& depend : this->GetTargetDirectDepends(target)) {
  709. if (depend->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  710. continue;
  711. }
  712. count += this->CountProgressMarksInTarget(depend, emitted);
  713. }
  714. }
  715. return count;
  716. }
  717. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll(
  718. cmLocalGenerator* lg)
  719. {
  720. size_t count = 0;
  721. std::set<cmGeneratorTarget const*> emitted;
  722. for (cmGeneratorTarget const* target :
  723. this->DirectoryTargetsMap[lg->GetStateSnapshot()]) {
  724. count += this->CountProgressMarksInTarget(target, emitted);
  725. }
  726. return count;
  727. }
  728. void cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  729. cmMakefileTargetGenerator* tg)
  730. {
  731. TargetProgress& tp = this->ProgressMap[tg->GetGeneratorTarget()];
  732. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  733. tp.VariableFile = tg->GetProgressFileNameFull();
  734. }
  735. void cmGlobalUnixMakefileGenerator3::TargetProgress::WriteProgressVariables(
  736. unsigned long total, unsigned long& current)
  737. {
  738. cmGeneratedFileStream fout(this->VariableFile);
  739. for (unsigned long i = 1; i <= this->NumberOfActions; ++i) {
  740. fout << "CMAKE_PROGRESS_" << i << " = ";
  741. if (total <= 100) {
  742. unsigned long num = i + current;
  743. fout << num;
  744. this->Marks.push_back(num);
  745. } else if (((i + current) * 100) / total >
  746. ((i - 1 + current) * 100) / total) {
  747. unsigned long num = ((i + current) * 100) / total;
  748. fout << num;
  749. this->Marks.push_back(num);
  750. }
  751. fout << "\n";
  752. }
  753. fout << "\n";
  754. current += this->NumberOfActions;
  755. }
  756. void cmGlobalUnixMakefileGenerator3::AppendGlobalTargetDepends(
  757. std::vector<std::string>& depends, cmGeneratorTarget* target)
  758. {
  759. for (cmTargetDepend const& i : this->GetTargetDirectDepends(target)) {
  760. // Create the target-level dependency.
  761. cmGeneratorTarget const* dep = i;
  762. if (dep->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  763. continue;
  764. }
  765. cmLocalUnixMakefileGenerator3* lg3 =
  766. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  767. std::string tgtName = cmStrCat(
  768. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep)),
  769. "/all");
  770. depends.push_back(tgtName);
  771. }
  772. }
  773. void cmGlobalUnixMakefileGenerator3::WriteHelpRule(
  774. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  775. {
  776. // add the help target
  777. std::string path;
  778. std::vector<std::string> no_depends;
  779. std::vector<std::string> commands;
  780. lg->AppendEcho(commands,
  781. "The following are some of the valid targets "
  782. "for this Makefile:");
  783. lg->AppendEcho(commands, "... all (the default if no target is provided)");
  784. lg->AppendEcho(commands, "... clean");
  785. if (!this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  786. lg->AppendEcho(commands, "... depend");
  787. }
  788. // Keep track of targets already listed.
  789. std::set<std::string> emittedTargets;
  790. // for each local generator
  791. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  792. cmLocalUnixMakefileGenerator3* lg2 =
  793. static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  794. // for the passed in makefile or if this is the top Makefile wripte out
  795. // the targets
  796. if (lg2 == lg || lg->IsRootMakefile()) {
  797. // for each target Generate the rule files for each target.
  798. for (cmGeneratorTarget* target : lg2->GetGeneratorTargets()) {
  799. cmStateEnums::TargetType type = target->GetType();
  800. if ((type == cmStateEnums::EXECUTABLE) ||
  801. (type == cmStateEnums::STATIC_LIBRARY) ||
  802. (type == cmStateEnums::SHARED_LIBRARY) ||
  803. (type == cmStateEnums::MODULE_LIBRARY) ||
  804. (type == cmStateEnums::OBJECT_LIBRARY) ||
  805. (type == cmStateEnums::GLOBAL_TARGET) ||
  806. (type == cmStateEnums::UTILITY)) {
  807. std::string const& name = target->GetName();
  808. if (emittedTargets.insert(name).second) {
  809. path = cmStrCat("... ", name);
  810. lg->AppendEcho(commands, path);
  811. }
  812. }
  813. }
  814. }
  815. }
  816. for (std::string const& o : lg->GetLocalHelp()) {
  817. path = cmStrCat("... ", o);
  818. lg->AppendEcho(commands, path);
  819. }
  820. lg->WriteMakeRule(ruleFileStream, "Help Target", "help", no_depends,
  821. commands, true);
  822. ruleFileStream << "\n\n";
  823. }