cmGlobalUnixMakefileGenerator3.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 target convenience rules
  208. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  209. this->WriteConvenienceRules2(
  210. makefileStream, static_cast<cmLocalUnixMakefileGenerator3*>(localGen));
  211. }
  212. // Write special bottom targets
  213. lg->WriteSpecialTargetsBottom(makefileStream);
  214. }
  215. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile()
  216. {
  217. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  218. return;
  219. }
  220. // Open the output file. This should not be copy-if-different
  221. // because the check-build-system step compares the makefile time to
  222. // see if the build system must be regenerated.
  223. std::string cmakefileName =
  224. cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(),
  225. "/CMakeFiles/Makefile.cmake");
  226. cmGeneratedFileStream cmakefileStream(cmakefileName);
  227. if (!cmakefileStream) {
  228. return;
  229. }
  230. std::string makefileName =
  231. cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), "/Makefile");
  232. // get a local generator for some useful methods
  233. cmLocalUnixMakefileGenerator3* lg =
  234. static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  235. // Write the do not edit header.
  236. lg->WriteDisclaimer(cmakefileStream);
  237. // Save the generator name
  238. cmakefileStream << "# The generator used is:\n"
  239. << "set(CMAKE_DEPENDS_GENERATOR \"" << this->GetName()
  240. << "\")\n\n";
  241. // for each cmMakefile get its list of dependencies
  242. std::vector<std::string> lfiles;
  243. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  244. // Get the list of files contributing to this generation step.
  245. cmAppend(lfiles, localGen->GetMakefile()->GetListFiles());
  246. }
  247. cmake* cm = this->GetCMakeInstance();
  248. if (cm->DoWriteGlobVerifyTarget()) {
  249. lfiles.push_back(cm->GetGlobVerifyScript());
  250. lfiles.push_back(cm->GetGlobVerifyStamp());
  251. }
  252. // Sort the list and remove duplicates.
  253. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  254. #if !defined(__VMS) // The Compaq STL on VMS crashes, so accept duplicates.
  255. auto new_end = std::unique(lfiles.begin(), lfiles.end());
  256. lfiles.erase(new_end, lfiles.end());
  257. #endif
  258. // reset lg to the first makefile
  259. lg = static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  260. std::string currentBinDir = lg->GetCurrentBinaryDirectory();
  261. // Save the list to the cmake file.
  262. cmakefileStream
  263. << "# The top level Makefile was generated from the following files:\n"
  264. << "set(CMAKE_MAKEFILE_DEPENDS\n"
  265. << " \"CMakeCache.txt\"\n";
  266. for (std::string const& f : lfiles) {
  267. cmakefileStream << " \""
  268. << lg->MaybeConvertToRelativePath(currentBinDir, f)
  269. << "\"\n";
  270. }
  271. cmakefileStream << " )\n\n";
  272. // Build the path to the cache check file.
  273. std::string check =
  274. cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(),
  275. "/CMakeFiles/cmake.check_cache");
  276. // Set the corresponding makefile in the cmake file.
  277. cmakefileStream << "# The corresponding makefile is:\n"
  278. << "set(CMAKE_MAKEFILE_OUTPUTS\n"
  279. << " \""
  280. << lg->MaybeConvertToRelativePath(currentBinDir,
  281. makefileName)
  282. << "\"\n"
  283. << " \""
  284. << lg->MaybeConvertToRelativePath(currentBinDir, check)
  285. << "\"\n";
  286. cmakefileStream << " )\n\n";
  287. const std::string binDir = lg->GetBinaryDirectory();
  288. // CMake must rerun if a byproduct is missing.
  289. {
  290. cmakefileStream << "# Byproducts of CMake generate step:\n"
  291. << "set(CMAKE_MAKEFILE_PRODUCTS\n";
  292. for (std::string const& outfile : lg->GetMakefile()->GetOutputFiles()) {
  293. cmakefileStream << " \""
  294. << lg->MaybeConvertToRelativePath(binDir, outfile)
  295. << "\"\n";
  296. }
  297. // add in all the directory information files
  298. std::string tmpStr;
  299. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  300. lg = static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  301. tmpStr = cmStrCat(lg->GetCurrentBinaryDirectory(),
  302. "/CMakeFiles/CMakeDirectoryInformation.cmake");
  303. cmakefileStream << " \""
  304. << lg->MaybeConvertToRelativePath(binDir, tmpStr)
  305. << "\"\n";
  306. }
  307. cmakefileStream << " )\n\n";
  308. }
  309. this->WriteMainCMakefileLanguageRules(cmakefileStream,
  310. this->LocalGenerators);
  311. }
  312. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefileLanguageRules(
  313. cmGeneratedFileStream& cmakefileStream,
  314. std::vector<cmLocalGenerator*>& lGenerators)
  315. {
  316. cmLocalUnixMakefileGenerator3* lg;
  317. // now list all the target info files
  318. cmakefileStream << "# Dependency information for all targets:\n";
  319. cmakefileStream << "set(CMAKE_DEPEND_INFO_FILES\n";
  320. for (cmLocalGenerator* lGenerator : lGenerators) {
  321. lg = static_cast<cmLocalUnixMakefileGenerator3*>(lGenerator);
  322. // for all of out targets
  323. for (cmGeneratorTarget* tgt : lg->GetGeneratorTargets()) {
  324. if ((tgt->GetType() == cmStateEnums::EXECUTABLE) ||
  325. (tgt->GetType() == cmStateEnums::STATIC_LIBRARY) ||
  326. (tgt->GetType() == cmStateEnums::SHARED_LIBRARY) ||
  327. (tgt->GetType() == cmStateEnums::MODULE_LIBRARY) ||
  328. (tgt->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
  329. (tgt->GetType() == cmStateEnums::UTILITY)) {
  330. cmGeneratorTarget* gt = tgt;
  331. std::string tname =
  332. cmStrCat(lg->GetRelativeTargetDirectory(gt), "/DependInfo.cmake");
  333. cmSystemTools::ConvertToUnixSlashes(tname);
  334. cmakefileStream << " \"" << tname << "\"\n";
  335. }
  336. }
  337. }
  338. cmakefileStream << " )\n";
  339. }
  340. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRule2(
  341. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg,
  342. const char* pass, bool check_all, bool check_relink,
  343. std::vector<std::string> const& commands)
  344. {
  345. // Get the relative path to the subdirectory from the top.
  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 (cmGeneratorTarget* gtarget : lg->GetGeneratorTargets()) {
  352. int type = gtarget->GetType();
  353. if ((type == cmStateEnums::EXECUTABLE) ||
  354. (type == cmStateEnums::STATIC_LIBRARY) ||
  355. (type == cmStateEnums::SHARED_LIBRARY) ||
  356. (type == cmStateEnums::MODULE_LIBRARY) ||
  357. (type == cmStateEnums::OBJECT_LIBRARY) ||
  358. (type == cmStateEnums::UTILITY)) {
  359. // Add this to the list of depends rules in this directory.
  360. if ((!check_all || !gtarget->GetPropertyAsBool("EXCLUDE_FROM_ALL")) &&
  361. (!check_relink ||
  362. gtarget->NeedRelinkBeforeInstall(lg->GetConfigName()))) {
  363. std::string tname =
  364. cmStrCat(lg->GetRelativeTargetDirectory(gtarget), '/', pass);
  365. depends.push_back(std::move(tname));
  366. }
  367. }
  368. }
  369. // The directory-level rule should depend on the directory-level
  370. // rules of the subdirectories.
  371. for (cmStateSnapshot const& c : lg->GetStateSnapshot().GetChildren()) {
  372. std::string subdir =
  373. cmStrCat(c.GetDirectory().GetCurrentBinary(), '/', 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, cmLocalUnixMakefileGenerator3* lg)
  393. {
  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, lg, "all", true, false);
  409. // Write directory-level rules for "clean".
  410. {
  411. std::vector<std::string> cmds;
  412. lg->AppendDirectoryCleanCommand(cmds);
  413. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false, cmds);
  414. }
  415. // Write directory-level rules for "preinstall".
  416. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", true, true);
  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 (cmGeneratorTarget* 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);
  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 =
  528. cmStrCat(lg->GetRelativeTargetDirectory(gtarget), "/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. // write the directory level rules for this local gen
  550. this->WriteDirectoryRules2(ruleFileStream, lg);
  551. bool regenerate = !this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  552. if (regenerate) {
  553. depends.emplace_back("cmake_check_build_system");
  554. }
  555. // for each target Generate the rule files for each target.
  556. for (cmGeneratorTarget* gtarget : lg->GetGeneratorTargets()) {
  557. int type = gtarget->GetType();
  558. std::string name = gtarget->GetName();
  559. if (!name.empty() &&
  560. ((type == cmStateEnums::EXECUTABLE) ||
  561. (type == cmStateEnums::STATIC_LIBRARY) ||
  562. (type == cmStateEnums::SHARED_LIBRARY) ||
  563. (type == cmStateEnums::MODULE_LIBRARY) ||
  564. (type == cmStateEnums::OBJECT_LIBRARY) ||
  565. (type == cmStateEnums::UTILITY))) {
  566. std::string makefileName;
  567. // Add a rule to build the target by name.
  568. localName = lg->GetRelativeTargetDirectory(gtarget);
  569. makefileName = cmStrCat(localName, "/build.make");
  570. lg->WriteDivider(ruleFileStream);
  571. ruleFileStream << "# Target rules for target " << localName << "\n\n";
  572. commands.clear();
  573. makeTargetName = cmStrCat(localName, "/depend");
  574. commands.push_back(
  575. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  576. makeTargetName = cmStrCat(localName, "/build");
  577. commands.push_back(
  578. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  579. // Write the rule.
  580. localName += "/all";
  581. depends.clear();
  582. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  583. progress.Dir = cmStrCat(lg->GetBinaryDirectory(), "/CMakeFiles");
  584. {
  585. std::ostringstream progressArg;
  586. const char* sep = "";
  587. for (unsigned long progFile : this->ProgressMap[gtarget].Marks) {
  588. progressArg << sep << progFile;
  589. sep = ",";
  590. }
  591. progress.Arg = progressArg.str();
  592. }
  593. bool targetMessages = true;
  594. if (const char* tgtMsg =
  595. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  596. "TARGET_MESSAGES")) {
  597. targetMessages = cmIsOn(tgtMsg);
  598. }
  599. if (targetMessages) {
  600. lg->AppendEcho(commands, "Built target " + name,
  601. cmLocalUnixMakefileGenerator3::EchoNormal, &progress);
  602. }
  603. this->AppendGlobalTargetDepends(depends, gtarget);
  604. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  605. localName, depends, commands, true);
  606. // Write the rule.
  607. commands.clear();
  608. {
  609. // TODO: Convert the total progress count to a make variable.
  610. std::ostringstream progCmd;
  611. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  612. // # in target
  613. progCmd << lg->ConvertToOutputFormat(
  614. cmSystemTools::CollapseFullPath(progress.Dir),
  615. cmOutputConverter::SHELL);
  616. //
  617. std::set<cmGeneratorTarget const*> emitted;
  618. progCmd << " " << this->CountProgressMarksInTarget(gtarget, emitted);
  619. commands.push_back(progCmd.str());
  620. }
  621. std::string tmp = "CMakeFiles/Makefile2";
  622. commands.push_back(lg->GetRecursiveMakeCall(tmp, localName));
  623. {
  624. std::ostringstream progCmd;
  625. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  626. progCmd << lg->ConvertToOutputFormat(
  627. cmSystemTools::CollapseFullPath(progress.Dir),
  628. cmOutputConverter::SHELL);
  629. progCmd << " 0";
  630. commands.push_back(progCmd.str());
  631. }
  632. depends.clear();
  633. if (regenerate) {
  634. depends.emplace_back("cmake_check_build_system");
  635. }
  636. localName = cmStrCat(lg->GetRelativeTargetDirectory(gtarget), "/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 =
  649. cmStrCat(lg->GetRelativeTargetDirectory(gtarget), "/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. if (!this->IsExcluded(gtarget)) {
  657. depends.clear();
  658. depends.push_back(localName);
  659. commands.clear();
  660. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  661. "preinstall", depends, commands, true);
  662. }
  663. }
  664. // add the clean rule
  665. localName = lg->GetRelativeTargetDirectory(gtarget);
  666. makeTargetName = cmStrCat(localName, "/clean");
  667. depends.clear();
  668. commands.clear();
  669. commands.push_back(
  670. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  671. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  672. makeTargetName, depends, commands, true);
  673. commands.clear();
  674. }
  675. }
  676. }
  677. // Build a map that contains the set of targets used by each local
  678. // generator directory level.
  679. void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks()
  680. {
  681. this->DirectoryTargetsMap.clear();
  682. // Loop over all targets in all local generators.
  683. for (cmLocalGenerator* lg : this->LocalGenerators) {
  684. for (cmGeneratorTarget* gt : lg->GetGeneratorTargets()) {
  685. cmLocalGenerator* tlg = gt->GetLocalGenerator();
  686. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  687. gt->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  688. continue;
  689. }
  690. cmStateSnapshot csnp = lg->GetStateSnapshot();
  691. cmStateSnapshot tsnp = tlg->GetStateSnapshot();
  692. // Consider the directory containing the target and all its
  693. // parents until something excludes the target.
  694. for (; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  695. csnp = csnp.GetBuildsystemDirectoryParent()) {
  696. // This local generator includes the target.
  697. std::set<cmGeneratorTarget const*>& targetSet =
  698. this->DirectoryTargetsMap[csnp];
  699. targetSet.insert(gt);
  700. // Add dependencies of the included target. An excluded
  701. // target may still be included if it is a dependency of a
  702. // non-excluded target.
  703. for (cmTargetDepend const& tgtdep : this->GetTargetDirectDepends(gt)) {
  704. targetSet.insert(tgtdep);
  705. }
  706. }
  707. }
  708. }
  709. }
  710. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget(
  711. cmGeneratorTarget const* target, std::set<cmGeneratorTarget const*>& emitted)
  712. {
  713. size_t count = 0;
  714. if (emitted.insert(target).second) {
  715. count = this->ProgressMap[target].Marks.size();
  716. for (cmTargetDepend const& depend : this->GetTargetDirectDepends(target)) {
  717. if (depend->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  718. continue;
  719. }
  720. count += this->CountProgressMarksInTarget(depend, emitted);
  721. }
  722. }
  723. return count;
  724. }
  725. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll(
  726. cmLocalGenerator* lg)
  727. {
  728. size_t count = 0;
  729. std::set<cmGeneratorTarget const*> emitted;
  730. for (cmGeneratorTarget const* target :
  731. this->DirectoryTargetsMap[lg->GetStateSnapshot()]) {
  732. count += this->CountProgressMarksInTarget(target, emitted);
  733. }
  734. return count;
  735. }
  736. void cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  737. cmMakefileTargetGenerator* tg)
  738. {
  739. TargetProgress& tp = this->ProgressMap[tg->GetGeneratorTarget()];
  740. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  741. tp.VariableFile = tg->GetProgressFileNameFull();
  742. }
  743. void cmGlobalUnixMakefileGenerator3::TargetProgress::WriteProgressVariables(
  744. unsigned long total, unsigned long& current)
  745. {
  746. cmGeneratedFileStream fout(this->VariableFile);
  747. for (unsigned long i = 1; i <= this->NumberOfActions; ++i) {
  748. fout << "CMAKE_PROGRESS_" << i << " = ";
  749. if (total <= 100) {
  750. unsigned long num = i + current;
  751. fout << num;
  752. this->Marks.push_back(num);
  753. } else if (((i + current) * 100) / total >
  754. ((i - 1 + current) * 100) / total) {
  755. unsigned long num = ((i + current) * 100) / total;
  756. fout << num;
  757. this->Marks.push_back(num);
  758. }
  759. fout << "\n";
  760. }
  761. fout << "\n";
  762. current += this->NumberOfActions;
  763. }
  764. void cmGlobalUnixMakefileGenerator3::AppendGlobalTargetDepends(
  765. std::vector<std::string>& depends, cmGeneratorTarget* target)
  766. {
  767. for (cmTargetDepend const& i : this->GetTargetDirectDepends(target)) {
  768. // Create the target-level dependency.
  769. cmGeneratorTarget const* dep = i;
  770. if (dep->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  771. continue;
  772. }
  773. cmLocalUnixMakefileGenerator3* lg3 =
  774. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  775. std::string tgtName = cmStrCat(
  776. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep)),
  777. "/all");
  778. depends.push_back(tgtName);
  779. }
  780. }
  781. void cmGlobalUnixMakefileGenerator3::WriteHelpRule(
  782. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  783. {
  784. // add the help target
  785. std::string path;
  786. std::vector<std::string> no_depends;
  787. std::vector<std::string> commands;
  788. lg->AppendEcho(commands,
  789. "The following are some of the valid targets "
  790. "for this Makefile:");
  791. lg->AppendEcho(commands, "... all (the default if no target is provided)");
  792. lg->AppendEcho(commands, "... clean");
  793. if (!this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  794. lg->AppendEcho(commands, "... depend");
  795. }
  796. // Keep track of targets already listed.
  797. std::set<std::string> emittedTargets;
  798. // for each local generator
  799. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  800. cmLocalUnixMakefileGenerator3* lg2 =
  801. static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  802. // for the passed in makefile or if this is the top Makefile wripte out
  803. // the targets
  804. if (lg2 == lg || lg->IsRootMakefile()) {
  805. // for each target Generate the rule files for each target.
  806. for (cmGeneratorTarget* target : lg2->GetGeneratorTargets()) {
  807. cmStateEnums::TargetType type = target->GetType();
  808. if ((type == cmStateEnums::EXECUTABLE) ||
  809. (type == cmStateEnums::STATIC_LIBRARY) ||
  810. (type == cmStateEnums::SHARED_LIBRARY) ||
  811. (type == cmStateEnums::MODULE_LIBRARY) ||
  812. (type == cmStateEnums::OBJECT_LIBRARY) ||
  813. (type == cmStateEnums::GLOBAL_TARGET) ||
  814. (type == cmStateEnums::UTILITY)) {
  815. std::string const& name = target->GetName();
  816. if (emittedTargets.insert(name).second) {
  817. path = cmStrCat("... ", name);
  818. lg->AppendEcho(commands, path);
  819. }
  820. }
  821. }
  822. }
  823. }
  824. for (std::string const& o : lg->GetLocalHelp()) {
  825. path = cmStrCat("... ", o);
  826. lg->AppendEcho(commands, path);
  827. }
  828. lg->WriteMakeRule(ruleFileStream, "Help Target", "help", no_depends,
  829. commands, true);
  830. ruleFileStream << "\n\n";
  831. }