cmGlobalUnixMakefileGenerator3.cxx 33 KB

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