cmGlobalUnixMakefileGenerator3.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. Basically 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.ExcludedFromAllInConfigs.empty()) &&
  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->IsInBuildSystem() || IsExcluded(lg.get(), gt.get())) {
  757. continue;
  758. }
  759. cmStateSnapshot csnp = lg->GetStateSnapshot();
  760. cmStateSnapshot tsnp = tlg->GetStateSnapshot();
  761. // Consider the directory containing the target and all its
  762. // parents until something excludes the target.
  763. for (; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  764. csnp = csnp.GetBuildsystemDirectoryParent()) {
  765. // This local generator includes the target.
  766. std::set<cmGeneratorTarget const*>& targetSet =
  767. this->DirectoryTargetsMap[csnp];
  768. targetSet.insert(gt.get());
  769. // Add dependencies of the included target. An excluded
  770. // target may still be included if it is a dependency of a
  771. // non-excluded target.
  772. for (cmTargetDepend const& tgtdep :
  773. this->GetTargetDirectDepends(gt.get())) {
  774. targetSet.insert(tgtdep);
  775. }
  776. }
  777. }
  778. }
  779. }
  780. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget(
  781. cmGeneratorTarget const* target, std::set<cmGeneratorTarget const*>& emitted)
  782. {
  783. size_t count = 0;
  784. if (emitted.insert(target).second) {
  785. count = this->ProgressMap[target].Marks.size();
  786. for (cmTargetDepend const& depend : this->GetTargetDirectDepends(target)) {
  787. if (!depend->IsInBuildSystem()) {
  788. continue;
  789. }
  790. count += this->CountProgressMarksInTarget(depend, emitted);
  791. }
  792. }
  793. return count;
  794. }
  795. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll(
  796. const cmLocalGenerator& lg)
  797. {
  798. size_t count = 0;
  799. std::set<cmGeneratorTarget const*> emitted;
  800. for (cmGeneratorTarget const* target :
  801. this->DirectoryTargetsMap[lg.GetStateSnapshot()]) {
  802. count += this->CountProgressMarksInTarget(target, emitted);
  803. }
  804. return count;
  805. }
  806. void cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  807. cmMakefileTargetGenerator* tg)
  808. {
  809. TargetProgress& tp = this->ProgressMap[tg->GetGeneratorTarget()];
  810. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  811. tp.VariableFile = tg->GetProgressFileNameFull();
  812. }
  813. void cmGlobalUnixMakefileGenerator3::TargetProgress::WriteProgressVariables(
  814. unsigned long total, unsigned long& current)
  815. {
  816. cmGeneratedFileStream fout(this->VariableFile);
  817. for (unsigned long i = 1; i <= this->NumberOfActions; ++i) {
  818. fout << "CMAKE_PROGRESS_" << i << " = ";
  819. if (total <= 100) {
  820. unsigned long num = i + current;
  821. fout << num;
  822. this->Marks.push_back(num);
  823. } else if (((i + current) * 100) / total >
  824. ((i - 1 + current) * 100) / total) {
  825. unsigned long num = ((i + current) * 100) / total;
  826. fout << num;
  827. this->Marks.push_back(num);
  828. }
  829. fout << "\n";
  830. }
  831. fout << "\n";
  832. current += this->NumberOfActions;
  833. }
  834. void cmGlobalUnixMakefileGenerator3::AppendGlobalTargetDepends(
  835. std::vector<std::string>& depends, cmGeneratorTarget* target)
  836. {
  837. for (cmTargetDepend const& i : this->GetTargetDirectDepends(target)) {
  838. // Create the target-level dependency.
  839. cmGeneratorTarget const* dep = i;
  840. if (!dep->IsInBuildSystem()) {
  841. continue;
  842. }
  843. cmLocalUnixMakefileGenerator3* lg3 =
  844. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  845. std::string tgtName = cmStrCat(
  846. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep)),
  847. "/all");
  848. depends.push_back(tgtName);
  849. }
  850. }
  851. void cmGlobalUnixMakefileGenerator3::WriteHelpRule(
  852. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  853. {
  854. // add the help target
  855. std::string path;
  856. std::vector<std::string> no_depends;
  857. std::vector<std::string> commands;
  858. lg->AppendEcho(commands,
  859. "The following are some of the valid targets "
  860. "for this Makefile:");
  861. lg->AppendEcho(commands, "... all (the default if no target is provided)");
  862. lg->AppendEcho(commands, "... clean");
  863. if (!this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  864. lg->AppendEcho(commands, "... depend");
  865. }
  866. // Keep track of targets already listed.
  867. std::set<std::string> emittedTargets;
  868. std::set<std::string> utility_targets;
  869. std::set<std::string> globals_targets;
  870. std::set<std::string> project_targets;
  871. // for each local generator
  872. for (const auto& localGen : this->LocalGenerators) {
  873. const auto& lg2 =
  874. cm::static_reference_cast<cmLocalUnixMakefileGenerator3>(localGen);
  875. // for the passed in makefile or if this is the top Makefile wripte out
  876. // the targets
  877. if (&lg2 == lg || lg->IsRootMakefile()) {
  878. // for each target Generate the rule files for each target.
  879. for (const auto& target : lg2.GetGeneratorTargets()) {
  880. cmStateEnums::TargetType type = target->GetType();
  881. if ((type == cmStateEnums::EXECUTABLE) ||
  882. (type == cmStateEnums::STATIC_LIBRARY) ||
  883. (type == cmStateEnums::SHARED_LIBRARY) ||
  884. (type == cmStateEnums::MODULE_LIBRARY) ||
  885. (type == cmStateEnums::OBJECT_LIBRARY)) {
  886. project_targets.insert(target->GetName());
  887. } else if (type == cmStateEnums::GLOBAL_TARGET) {
  888. globals_targets.insert(target->GetName());
  889. } else if (type == cmStateEnums::UTILITY) {
  890. utility_targets.insert(target->GetName());
  891. }
  892. }
  893. }
  894. }
  895. for (std::string const& name : globals_targets) {
  896. path = cmStrCat("... ", name);
  897. lg->AppendEcho(commands, path);
  898. }
  899. for (std::string const& name : utility_targets) {
  900. path = cmStrCat("... ", name);
  901. lg->AppendEcho(commands, path);
  902. }
  903. for (std::string const& name : project_targets) {
  904. path = cmStrCat("... ", name);
  905. lg->AppendEcho(commands, path);
  906. }
  907. for (std::string const& o : lg->GetLocalHelp()) {
  908. path = cmStrCat("... ", o);
  909. lg->AppendEcho(commands, path);
  910. }
  911. lg->WriteMakeRule(ruleFileStream, "Help Target", "help", no_depends,
  912. commands, true);
  913. ruleFileStream << "\n\n";
  914. }