cmGlobalUnixMakefileGenerator3.cxx 37 KB

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