cmGlobalUnixMakefileGenerator3.cxx 35 KB

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