cmGlobalUnixMakefileGenerator3.cxx 34 KB

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