cmGlobalUnixMakefileGenerator3.cxx 35 KB

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