cmGlobalUnixMakefileGenerator3.cxx 35 KB

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