cmGlobalUnixMakefileGenerator3.cxx 35 KB

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