cmGlobalUnixMakefileGenerator3.cxx 35 KB

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