cmGlobalUnixMakefileGenerator3.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. 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. << R"( "directory": ")"
  162. << cmGlobalGenerator::EscapeJSON(workingDirectory)
  163. << "\"," << std::endl
  164. << R"( "command": ")"
  165. << cmGlobalGenerator::EscapeJSON(compileCommand)
  166. << "\"," << std::endl
  167. << R"( "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 out the "special" stuff
  209. lg->WriteSpecialTargetsTop(makefileStream);
  210. // Write the target convenience rules
  211. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  212. this->WriteConvenienceRules2(
  213. makefileStream, static_cast<cmLocalUnixMakefileGenerator3*>(localGen));
  214. }
  215. // Write special bottom targets
  216. lg->WriteSpecialTargetsBottom(makefileStream);
  217. }
  218. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile()
  219. {
  220. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  221. return;
  222. }
  223. // Open the output file. This should not be copy-if-different
  224. // because the check-build-system step compares the makefile time to
  225. // see if the build system must be regenerated.
  226. std::string cmakefileName =
  227. this->GetCMakeInstance()->GetHomeOutputDirectory();
  228. cmakefileName += "/CMakeFiles";
  229. cmakefileName += "/Makefile.cmake";
  230. cmGeneratedFileStream cmakefileStream(cmakefileName);
  231. if (!cmakefileStream) {
  232. return;
  233. }
  234. std::string makefileName =
  235. this->GetCMakeInstance()->GetHomeOutputDirectory();
  236. makefileName += "/Makefile";
  237. // get a local generator for some useful methods
  238. cmLocalUnixMakefileGenerator3* lg =
  239. static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  240. // Write the do not edit header.
  241. lg->WriteDisclaimer(cmakefileStream);
  242. // Save the generator name
  243. cmakefileStream << "# The generator used is:\n"
  244. << "set(CMAKE_DEPENDS_GENERATOR \"" << this->GetName()
  245. << "\")\n\n";
  246. // for each cmMakefile get its list of dependencies
  247. std::vector<std::string> lfiles;
  248. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  249. // Get the list of files contributing to this generation step.
  250. cmAppend(lfiles, localGen->GetMakefile()->GetListFiles());
  251. }
  252. cmake* cm = this->GetCMakeInstance();
  253. if (cm->DoWriteGlobVerifyTarget()) {
  254. lfiles.push_back(cm->GetGlobVerifyScript());
  255. lfiles.push_back(cm->GetGlobVerifyStamp());
  256. }
  257. // Sort the list and remove duplicates.
  258. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  259. #if !defined(__VMS) // The Compaq STL on VMS crashes, so accept duplicates.
  260. std::vector<std::string>::iterator new_end =
  261. std::unique(lfiles.begin(), lfiles.end());
  262. lfiles.erase(new_end, lfiles.end());
  263. #endif
  264. // reset lg to the first makefile
  265. lg = static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]);
  266. std::string currentBinDir = lg->GetCurrentBinaryDirectory();
  267. // Save the list to the cmake file.
  268. cmakefileStream
  269. << "# The top level Makefile was generated from the following files:\n"
  270. << "set(CMAKE_MAKEFILE_DEPENDS\n"
  271. << " \"CMakeCache.txt\"\n";
  272. for (std::string const& f : lfiles) {
  273. cmakefileStream << " \""
  274. << lg->MaybeConvertToRelativePath(currentBinDir, f)
  275. << "\"\n";
  276. }
  277. cmakefileStream << " )\n\n";
  278. // Build the path to the cache check file.
  279. std::string check = this->GetCMakeInstance()->GetHomeOutputDirectory();
  280. check += "/CMakeFiles";
  281. check += "/cmake.check_cache";
  282. // Set the corresponding makefile in the cmake file.
  283. cmakefileStream << "# The corresponding makefile is:\n"
  284. << "set(CMAKE_MAKEFILE_OUTPUTS\n"
  285. << " \""
  286. << lg->MaybeConvertToRelativePath(currentBinDir,
  287. makefileName)
  288. << "\"\n"
  289. << " \""
  290. << lg->MaybeConvertToRelativePath(currentBinDir, check)
  291. << "\"\n";
  292. cmakefileStream << " )\n\n";
  293. const std::string binDir = lg->GetBinaryDirectory();
  294. // CMake must rerun if a byproduct is missing.
  295. {
  296. cmakefileStream << "# Byproducts of CMake generate step:\n"
  297. << "set(CMAKE_MAKEFILE_PRODUCTS\n";
  298. for (std::string const& outfile : lg->GetMakefile()->GetOutputFiles()) {
  299. cmakefileStream << " \""
  300. << lg->MaybeConvertToRelativePath(binDir, outfile)
  301. << "\"\n";
  302. }
  303. // add in all the directory information files
  304. std::string tmpStr;
  305. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  306. lg = static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  307. tmpStr = lg->GetCurrentBinaryDirectory();
  308. tmpStr += "/CMakeFiles";
  309. tmpStr += "/CMakeDirectoryInformation.cmake";
  310. cmakefileStream << " \""
  311. << lg->MaybeConvertToRelativePath(binDir, tmpStr)
  312. << "\"\n";
  313. }
  314. cmakefileStream << " )\n\n";
  315. }
  316. this->WriteMainCMakefileLanguageRules(cmakefileStream,
  317. this->LocalGenerators);
  318. }
  319. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefileLanguageRules(
  320. cmGeneratedFileStream& cmakefileStream,
  321. std::vector<cmLocalGenerator*>& lGenerators)
  322. {
  323. cmLocalUnixMakefileGenerator3* lg;
  324. // now list all the target info files
  325. cmakefileStream << "# Dependency information for all targets:\n";
  326. cmakefileStream << "set(CMAKE_DEPEND_INFO_FILES\n";
  327. for (cmLocalGenerator* lGenerator : lGenerators) {
  328. lg = static_cast<cmLocalUnixMakefileGenerator3*>(lGenerator);
  329. // for all of out targets
  330. for (cmGeneratorTarget* tgt : lg->GetGeneratorTargets()) {
  331. if ((tgt->GetType() == cmStateEnums::EXECUTABLE) ||
  332. (tgt->GetType() == cmStateEnums::STATIC_LIBRARY) ||
  333. (tgt->GetType() == cmStateEnums::SHARED_LIBRARY) ||
  334. (tgt->GetType() == cmStateEnums::MODULE_LIBRARY) ||
  335. (tgt->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
  336. (tgt->GetType() == cmStateEnums::UTILITY)) {
  337. cmGeneratorTarget* gt = tgt;
  338. std::string tname = lg->GetRelativeTargetDirectory(gt);
  339. tname += "/DependInfo.cmake";
  340. cmSystemTools::ConvertToUnixSlashes(tname);
  341. cmakefileStream << " \"" << tname << "\"\n";
  342. }
  343. }
  344. }
  345. cmakefileStream << " )\n";
  346. }
  347. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRule2(
  348. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg,
  349. const char* pass, bool check_all, bool check_relink,
  350. std::vector<std::string> const& commands)
  351. {
  352. // Get the relative path to the subdirectory from the top.
  353. std::string makeTarget = lg->GetCurrentBinaryDirectory();
  354. makeTarget += '/';
  355. makeTarget += pass;
  356. // The directory-level rule should depend on the target-level rules
  357. // for all targets in the directory.
  358. std::vector<std::string> depends;
  359. for (cmGeneratorTarget* gtarget : lg->GetGeneratorTargets()) {
  360. int type = gtarget->GetType();
  361. if ((type == cmStateEnums::EXECUTABLE) ||
  362. (type == cmStateEnums::STATIC_LIBRARY) ||
  363. (type == cmStateEnums::SHARED_LIBRARY) ||
  364. (type == cmStateEnums::MODULE_LIBRARY) ||
  365. (type == cmStateEnums::OBJECT_LIBRARY) ||
  366. (type == cmStateEnums::UTILITY)) {
  367. // Add this to the list of depends rules in this directory.
  368. if ((!check_all || !gtarget->GetPropertyAsBool("EXCLUDE_FROM_ALL")) &&
  369. (!check_relink ||
  370. gtarget->NeedRelinkBeforeInstall(lg->GetConfigName()))) {
  371. std::string tname = lg->GetRelativeTargetDirectory(gtarget);
  372. tname += "/";
  373. tname += pass;
  374. depends.push_back(std::move(tname));
  375. }
  376. }
  377. }
  378. // The directory-level rule should depend on the directory-level
  379. // rules of the subdirectories.
  380. for (cmStateSnapshot const& c : lg->GetStateSnapshot().GetChildren()) {
  381. std::string subdir = c.GetDirectory().GetCurrentBinary();
  382. subdir += '/';
  383. subdir += pass;
  384. depends.push_back(std::move(subdir));
  385. }
  386. // Work-around for makes that drop rules that have no dependencies
  387. // or commands.
  388. if (depends.empty() && !this->EmptyRuleHackDepends.empty()) {
  389. depends.push_back(this->EmptyRuleHackDepends);
  390. }
  391. // Write the rule.
  392. std::string doc;
  393. if (lg->IsRootMakefile()) {
  394. doc = "The main recursive \"";
  395. doc += pass;
  396. doc += "\" target.";
  397. } else {
  398. doc = "Recursive \"";
  399. doc += pass;
  400. doc += "\" directory target.";
  401. }
  402. lg->WriteMakeRule(ruleFileStream, doc.c_str(), makeTarget, depends, commands,
  403. true);
  404. }
  405. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRules2(
  406. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  407. {
  408. // Begin the directory-level rules section.
  409. {
  410. std::string dir =
  411. cmSystemTools::ConvertToOutputPath(lg->MaybeConvertToRelativePath(
  412. lg->GetBinaryDirectory(), lg->GetCurrentBinaryDirectory()));
  413. lg->WriteDivider(ruleFileStream);
  414. if (lg->IsRootMakefile()) {
  415. ruleFileStream << "# Directory level rules for the build root directory";
  416. } else {
  417. ruleFileStream << "# Directory level rules for directory " << dir;
  418. }
  419. ruleFileStream << "\n\n";
  420. }
  421. // Write directory-level rules for "all".
  422. this->WriteDirectoryRule2(ruleFileStream, lg, "all", true, false);
  423. // Write directory-level rules for "clean".
  424. {
  425. std::vector<std::string> cmds;
  426. lg->AppendDirectoryCleanCommand(cmds);
  427. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false, cmds);
  428. }
  429. // Write directory-level rules for "preinstall".
  430. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", true, true);
  431. }
  432. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  433. cmGlobalUnixMakefileGenerator3::GenerateBuildCommand(
  434. const std::string& makeProgram, const std::string& /*projectName*/,
  435. const std::string& /*projectDir*/,
  436. std::vector<std::string> const& targetNames, const std::string& /*config*/,
  437. bool fast, int jobs, bool verbose,
  438. 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. GeneratedMakeCommand makeCommand;
  455. // Make it possible to set verbosity also from command line
  456. if (verbose) {
  457. makeCommand.Add(cmSystemTools::GetCMakeCommand());
  458. makeCommand.Add("-E");
  459. makeCommand.Add("env");
  460. makeCommand.Add("VERBOSE=1");
  461. }
  462. makeCommand.Add(this->SelectMakeProgram(makeProgram));
  463. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  464. makeCommand.Add("-j");
  465. if (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  466. makeCommand.Add(std::to_string(jobs));
  467. }
  468. }
  469. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  470. for (auto tname : targetNames) {
  471. if (!tname.empty()) {
  472. if (fast) {
  473. tname += "/fast";
  474. }
  475. tname =
  476. mf->GetStateSnapshot().GetDirectory().ConvertToRelPathIfNotContained(
  477. mf->GetState()->GetBinaryDirectory(), tname);
  478. cmSystemTools::ConvertToOutputSlashes(tname);
  479. makeCommand.Add(std::move(tname));
  480. }
  481. }
  482. return { std::move(makeCommand) };
  483. }
  484. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules(
  485. std::ostream& ruleFileStream, std::set<std::string>& emitted)
  486. {
  487. std::vector<std::string> depends;
  488. std::vector<std::string> commands;
  489. bool regenerate = !this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  490. if (regenerate) {
  491. depends.emplace_back("cmake_check_build_system");
  492. }
  493. // write the target convenience rules
  494. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  495. cmLocalUnixMakefileGenerator3* lg =
  496. static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  497. // for each target Generate the rule files for each target.
  498. for (cmGeneratorTarget* gtarget : lg->GetGeneratorTargets()) {
  499. // Don't emit the same rule twice (e.g. two targets with the same
  500. // simple name)
  501. int type = gtarget->GetType();
  502. std::string name = gtarget->GetName();
  503. if (!name.empty() && emitted.insert(name).second &&
  504. // Handle user targets here. Global targets are handled in
  505. // the local generator on a per-directory basis.
  506. ((type == cmStateEnums::EXECUTABLE) ||
  507. (type == cmStateEnums::STATIC_LIBRARY) ||
  508. (type == cmStateEnums::SHARED_LIBRARY) ||
  509. (type == cmStateEnums::MODULE_LIBRARY) ||
  510. (type == cmStateEnums::OBJECT_LIBRARY) ||
  511. (type == cmStateEnums::UTILITY))) {
  512. // Add a rule to build the target by name.
  513. lg->WriteDivider(ruleFileStream);
  514. ruleFileStream << "# Target rules for targets named " << name
  515. << "\n\n";
  516. // Write the rule.
  517. commands.clear();
  518. std::string tmp = "CMakeFiles/";
  519. tmp += "Makefile2";
  520. commands.push_back(lg->GetRecursiveMakeCall(tmp, name));
  521. depends.clear();
  522. if (regenerate) {
  523. depends.emplace_back("cmake_check_build_system");
  524. }
  525. lg->WriteMakeRule(ruleFileStream, "Build rule for target.", name,
  526. depends, commands, true);
  527. // Add a fast rule to build the target
  528. std::string localName = lg->GetRelativeTargetDirectory(gtarget);
  529. std::string makefileName;
  530. makefileName = localName;
  531. makefileName += "/build.make";
  532. depends.clear();
  533. commands.clear();
  534. std::string makeTargetName = localName;
  535. makeTargetName += "/build";
  536. localName = name;
  537. localName += "/fast";
  538. commands.push_back(
  539. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  540. lg->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  541. localName, depends, commands, true);
  542. // Add a local name for the rule to relink the target before
  543. // installation.
  544. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  545. makeTargetName = lg->GetRelativeTargetDirectory(gtarget);
  546. makeTargetName += "/preinstall";
  547. localName = name;
  548. localName += "/preinstall";
  549. depends.clear();
  550. commands.clear();
  551. commands.push_back(
  552. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  553. lg->WriteMakeRule(ruleFileStream,
  554. "Manual pre-install relink rule for target.",
  555. localName, depends, commands, true);
  556. }
  557. }
  558. }
  559. }
  560. }
  561. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2(
  562. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  563. {
  564. std::vector<std::string> depends;
  565. std::vector<std::string> commands;
  566. std::string localName;
  567. std::string makeTargetName;
  568. // write the directory level rules for this local gen
  569. this->WriteDirectoryRules2(ruleFileStream, lg);
  570. bool regenerate = !this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  571. if (regenerate) {
  572. depends.emplace_back("cmake_check_build_system");
  573. }
  574. // for each target Generate the rule files for each target.
  575. for (cmGeneratorTarget* gtarget : lg->GetGeneratorTargets()) {
  576. int type = gtarget->GetType();
  577. std::string name = gtarget->GetName();
  578. if (!name.empty() &&
  579. ((type == cmStateEnums::EXECUTABLE) ||
  580. (type == cmStateEnums::STATIC_LIBRARY) ||
  581. (type == cmStateEnums::SHARED_LIBRARY) ||
  582. (type == cmStateEnums::MODULE_LIBRARY) ||
  583. (type == cmStateEnums::OBJECT_LIBRARY) ||
  584. (type == cmStateEnums::UTILITY))) {
  585. std::string makefileName;
  586. // Add a rule to build the target by name.
  587. localName = lg->GetRelativeTargetDirectory(gtarget);
  588. makefileName = localName;
  589. makefileName += "/build.make";
  590. lg->WriteDivider(ruleFileStream);
  591. ruleFileStream << "# Target rules for target " << localName << "\n\n";
  592. commands.clear();
  593. makeTargetName = localName;
  594. makeTargetName += "/depend";
  595. commands.push_back(
  596. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  597. makeTargetName = localName;
  598. makeTargetName += "/build";
  599. commands.push_back(
  600. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  601. // Write the rule.
  602. localName += "/all";
  603. depends.clear();
  604. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  605. progress.Dir = lg->GetBinaryDirectory();
  606. progress.Dir += "/CMakeFiles";
  607. {
  608. std::ostringstream progressArg;
  609. const char* sep = "";
  610. for (unsigned long progFile : this->ProgressMap[gtarget].Marks) {
  611. progressArg << sep << progFile;
  612. sep = ",";
  613. }
  614. progress.Arg = progressArg.str();
  615. }
  616. bool targetMessages = true;
  617. if (const char* tgtMsg =
  618. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  619. "TARGET_MESSAGES")) {
  620. targetMessages = cmSystemTools::IsOn(tgtMsg);
  621. }
  622. if (targetMessages) {
  623. lg->AppendEcho(commands, "Built target " + name,
  624. cmLocalUnixMakefileGenerator3::EchoNormal, &progress);
  625. }
  626. this->AppendGlobalTargetDepends(depends, gtarget);
  627. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  628. localName, depends, commands, true);
  629. // Write the rule.
  630. commands.clear();
  631. {
  632. // TODO: Convert the total progress count to a make variable.
  633. std::ostringstream progCmd;
  634. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  635. // # in target
  636. progCmd << lg->ConvertToOutputFormat(
  637. cmSystemTools::CollapseFullPath(progress.Dir),
  638. cmOutputConverter::SHELL);
  639. //
  640. std::set<cmGeneratorTarget const*> emitted;
  641. progCmd << " " << this->CountProgressMarksInTarget(gtarget, emitted);
  642. commands.push_back(progCmd.str());
  643. }
  644. std::string tmp = "CMakeFiles/";
  645. tmp += "Makefile2";
  646. commands.push_back(lg->GetRecursiveMakeCall(tmp, localName));
  647. {
  648. std::ostringstream progCmd;
  649. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  650. progCmd << lg->ConvertToOutputFormat(
  651. cmSystemTools::CollapseFullPath(progress.Dir),
  652. cmOutputConverter::SHELL);
  653. progCmd << " 0";
  654. commands.push_back(progCmd.str());
  655. }
  656. depends.clear();
  657. if (regenerate) {
  658. depends.emplace_back("cmake_check_build_system");
  659. }
  660. localName = lg->GetRelativeTargetDirectory(gtarget);
  661. localName += "/rule";
  662. lg->WriteMakeRule(ruleFileStream,
  663. "Build rule for subdir invocation for target.",
  664. localName, depends, commands, true);
  665. // Add a target with the canonical name (no prefix, suffix or path).
  666. commands.clear();
  667. depends.clear();
  668. depends.push_back(localName);
  669. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.", name,
  670. depends, commands, true);
  671. // Add rules to prepare the target for installation.
  672. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  673. localName = lg->GetRelativeTargetDirectory(gtarget);
  674. localName += "/preinstall";
  675. depends.clear();
  676. commands.clear();
  677. commands.push_back(lg->GetRecursiveMakeCall(makefileName, localName));
  678. lg->WriteMakeRule(ruleFileStream,
  679. "Pre-install relink rule for target.", localName,
  680. depends, commands, true);
  681. if (!this->IsExcluded(gtarget)) {
  682. depends.clear();
  683. depends.push_back(localName);
  684. commands.clear();
  685. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  686. "preinstall", depends, commands, true);
  687. }
  688. }
  689. // add the clean rule
  690. localName = lg->GetRelativeTargetDirectory(gtarget);
  691. makeTargetName = localName;
  692. makeTargetName += "/clean";
  693. depends.clear();
  694. commands.clear();
  695. commands.push_back(
  696. lg->GetRecursiveMakeCall(makefileName, makeTargetName));
  697. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  698. makeTargetName, depends, commands, true);
  699. commands.clear();
  700. }
  701. }
  702. }
  703. // Build a map that contains the set of targets used by each local
  704. // generator directory level.
  705. void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks()
  706. {
  707. this->DirectoryTargetsMap.clear();
  708. // Loop over all targets in all local generators.
  709. for (cmLocalGenerator* lg : this->LocalGenerators) {
  710. for (cmGeneratorTarget* gt : lg->GetGeneratorTargets()) {
  711. cmLocalGenerator* tlg = gt->GetLocalGenerator();
  712. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  713. gt->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  714. continue;
  715. }
  716. cmStateSnapshot csnp = lg->GetStateSnapshot();
  717. cmStateSnapshot tsnp = tlg->GetStateSnapshot();
  718. // Consider the directory containing the target and all its
  719. // parents until something excludes the target.
  720. for (; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  721. csnp = csnp.GetBuildsystemDirectoryParent()) {
  722. // This local generator includes the target.
  723. std::set<cmGeneratorTarget const*>& targetSet =
  724. this->DirectoryTargetsMap[csnp];
  725. targetSet.insert(gt);
  726. // Add dependencies of the included target. An excluded
  727. // target may still be included if it is a dependency of a
  728. // non-excluded target.
  729. for (cmTargetDepend const& tgtdep : this->GetTargetDirectDepends(gt)) {
  730. targetSet.insert(tgtdep);
  731. }
  732. }
  733. }
  734. }
  735. }
  736. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget(
  737. cmGeneratorTarget const* target, std::set<cmGeneratorTarget const*>& emitted)
  738. {
  739. size_t count = 0;
  740. if (emitted.insert(target).second) {
  741. count = this->ProgressMap[target].Marks.size();
  742. for (cmTargetDepend const& depend : this->GetTargetDirectDepends(target)) {
  743. if (depend->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  744. continue;
  745. }
  746. count += this->CountProgressMarksInTarget(depend, emitted);
  747. }
  748. }
  749. return count;
  750. }
  751. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll(
  752. cmLocalGenerator* lg)
  753. {
  754. size_t count = 0;
  755. std::set<cmGeneratorTarget const*> emitted;
  756. for (cmGeneratorTarget const* target :
  757. this->DirectoryTargetsMap[lg->GetStateSnapshot()]) {
  758. count += this->CountProgressMarksInTarget(target, emitted);
  759. }
  760. return count;
  761. }
  762. void cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  763. cmMakefileTargetGenerator* tg)
  764. {
  765. TargetProgress& tp = this->ProgressMap[tg->GetGeneratorTarget()];
  766. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  767. tp.VariableFile = tg->GetProgressFileNameFull();
  768. }
  769. void cmGlobalUnixMakefileGenerator3::TargetProgress::WriteProgressVariables(
  770. unsigned long total, unsigned long& current)
  771. {
  772. cmGeneratedFileStream fout(this->VariableFile);
  773. for (unsigned long i = 1; i <= this->NumberOfActions; ++i) {
  774. fout << "CMAKE_PROGRESS_" << i << " = ";
  775. if (total <= 100) {
  776. unsigned long num = i + current;
  777. fout << num;
  778. this->Marks.push_back(num);
  779. } else if (((i + current) * 100) / total >
  780. ((i - 1 + current) * 100) / total) {
  781. unsigned long num = ((i + current) * 100) / total;
  782. fout << num;
  783. this->Marks.push_back(num);
  784. }
  785. fout << "\n";
  786. }
  787. fout << "\n";
  788. current += this->NumberOfActions;
  789. }
  790. void cmGlobalUnixMakefileGenerator3::AppendGlobalTargetDepends(
  791. std::vector<std::string>& depends, cmGeneratorTarget* target)
  792. {
  793. for (cmTargetDepend const& i : this->GetTargetDirectDepends(target)) {
  794. // Create the target-level dependency.
  795. cmGeneratorTarget const* dep = i;
  796. if (dep->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  797. continue;
  798. }
  799. cmLocalUnixMakefileGenerator3* lg3 =
  800. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  801. std::string tgtName =
  802. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep));
  803. tgtName += "/all";
  804. depends.push_back(tgtName);
  805. }
  806. }
  807. void cmGlobalUnixMakefileGenerator3::WriteHelpRule(
  808. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  809. {
  810. // add the help target
  811. std::string path;
  812. std::vector<std::string> no_depends;
  813. std::vector<std::string> commands;
  814. lg->AppendEcho(commands,
  815. "The following are some of the valid targets "
  816. "for this Makefile:");
  817. lg->AppendEcho(commands, "... all (the default if no target is provided)");
  818. lg->AppendEcho(commands, "... clean");
  819. if (!this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  820. lg->AppendEcho(commands, "... depend");
  821. }
  822. // Keep track of targets already listed.
  823. std::set<std::string> emittedTargets;
  824. // for each local generator
  825. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  826. cmLocalUnixMakefileGenerator3* lg2 =
  827. static_cast<cmLocalUnixMakefileGenerator3*>(localGen);
  828. // for the passed in makefile or if this is the top Makefile wripte out
  829. // the targets
  830. if (lg2 == lg || lg->IsRootMakefile()) {
  831. // for each target Generate the rule files for each target.
  832. for (cmGeneratorTarget* target : lg2->GetGeneratorTargets()) {
  833. cmStateEnums::TargetType type = target->GetType();
  834. if ((type == cmStateEnums::EXECUTABLE) ||
  835. (type == cmStateEnums::STATIC_LIBRARY) ||
  836. (type == cmStateEnums::SHARED_LIBRARY) ||
  837. (type == cmStateEnums::MODULE_LIBRARY) ||
  838. (type == cmStateEnums::OBJECT_LIBRARY) ||
  839. (type == cmStateEnums::GLOBAL_TARGET) ||
  840. (type == cmStateEnums::UTILITY)) {
  841. std::string const& name = target->GetName();
  842. if (emittedTargets.insert(name).second) {
  843. path = "... ";
  844. path += name;
  845. lg->AppendEcho(commands, path);
  846. }
  847. }
  848. }
  849. }
  850. }
  851. for (std::string const& o : lg->GetLocalHelp()) {
  852. path = "... ";
  853. path += o;
  854. lg->AppendEcho(commands, path);
  855. }
  856. lg->WriteMakeRule(ruleFileStream, "Help Target", "help", no_depends,
  857. commands, true);
  858. ruleFileStream << "\n\n";
  859. }