cmGlobalUnixMakefileGenerator3.cxx 35 KB

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