cmGlobalUnixMakefileGenerator3.cxx 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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<cmStateSnapshot> children = lg->GetStateSnapshot().GetChildren();
  389. for (std::vector<cmStateSnapshot>::const_iterator ci = children.begin();
  390. ci != children.end(); ++ci) {
  391. std::string subdir = ci->GetDirectory().GetCurrentBinary();
  392. subdir += "/";
  393. subdir += pass;
  394. depends.push_back(subdir);
  395. }
  396. // Work-around for makes that drop rules that have no dependencies
  397. // or commands.
  398. if (depends.empty() && this->EmptyRuleHackDepends != "") {
  399. depends.push_back(this->EmptyRuleHackDepends);
  400. }
  401. // Write the rule.
  402. std::string doc = "Convenience name for \"";
  403. doc += pass;
  404. doc += "\" pass in the directory.";
  405. std::vector<std::string> no_commands;
  406. lg->WriteMakeRule(ruleFileStream, doc.c_str(), makeTarget, depends,
  407. no_commands, true);
  408. }
  409. void cmGlobalUnixMakefileGenerator3::WriteDirectoryRules2(
  410. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  411. {
  412. // Only subdirectories need these rules.
  413. if (lg->IsRootMakefile()) {
  414. return;
  415. }
  416. // Begin the directory-level rules section.
  417. std::string dir = cmSystemTools::ConvertToOutputPath(
  418. lg->ConvertToRelativePath(lg->GetBinaryDirectory(),
  419. lg->GetCurrentBinaryDirectory())
  420. .c_str());
  421. lg->WriteDivider(ruleFileStream);
  422. ruleFileStream << "# Directory level rules for directory " << dir << "\n\n";
  423. // Write directory-level rules for "all".
  424. this->WriteDirectoryRule2(ruleFileStream, lg, "all", true, false);
  425. // Write directory-level rules for "clean".
  426. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false);
  427. // Write directory-level rules for "preinstall".
  428. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", true, true);
  429. }
  430. void cmGlobalUnixMakefileGenerator3::GenerateBuildCommand(
  431. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  432. const std::string& /*projectName*/, const std::string& /*projectDir*/,
  433. const std::string& targetName, const std::string& /*config*/, bool fast,
  434. bool /*verbose*/, std::vector<std::string> const& makeOptions)
  435. {
  436. makeCommand.push_back(this->SelectMakeProgram(makeProgram));
  437. // Since we have full control over the invocation of nmake, let us
  438. // make it quiet.
  439. if (cmHasLiteralPrefix(this->GetName(), "NMake Makefiles")) {
  440. makeCommand.push_back("/NOLOGO");
  441. }
  442. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  443. makeOptions.end());
  444. if (!targetName.empty()) {
  445. cmMakefile* mf;
  446. if (!this->Makefiles.empty()) {
  447. mf = this->Makefiles[0];
  448. } else {
  449. cmStateSnapshot snapshot = this->CMakeInstance->GetCurrentSnapshot();
  450. snapshot.GetDirectory().SetCurrentSource(
  451. this->CMakeInstance->GetHomeDirectory());
  452. snapshot.GetDirectory().SetCurrentBinary(
  453. this->CMakeInstance->GetHomeOutputDirectory());
  454. snapshot.SetDefaultDefinitions();
  455. mf = new cmMakefile(this, snapshot);
  456. }
  457. std::string tname = targetName;
  458. if (fast) {
  459. tname += "/fast";
  460. }
  461. cmOutputConverter conv(mf->GetStateSnapshot());
  462. tname =
  463. conv.ConvertToRelativePath(mf->GetState()->GetBinaryDirectory(), tname);
  464. cmSystemTools::ConvertToOutputSlashes(tname);
  465. makeCommand.push_back(tname);
  466. if (this->Makefiles.empty()) {
  467. delete mf;
  468. }
  469. }
  470. }
  471. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules(
  472. std::ostream& ruleFileStream, std::set<std::string>& emitted)
  473. {
  474. std::vector<std::string> depends;
  475. std::vector<std::string> commands;
  476. depends.push_back("cmake_check_build_system");
  477. // write the target convenience rules
  478. unsigned int i;
  479. cmLocalUnixMakefileGenerator3* lg;
  480. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  481. lg = static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[i]);
  482. // for each target Generate the rule files for each target.
  483. std::vector<cmGeneratorTarget*> targets = lg->GetGeneratorTargets();
  484. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  485. t != targets.end(); ++t) {
  486. cmGeneratorTarget* gtarget = *t;
  487. // Don't emit the same rule twice (e.g. two targets with the same
  488. // simple name)
  489. int type = gtarget->GetType();
  490. std::string name = gtarget->GetName();
  491. if (!name.empty() && emitted.insert(name).second &&
  492. // Handle user targets here. Global targets are handled in
  493. // the local generator on a per-directory basis.
  494. ((type == cmStateEnums::EXECUTABLE) ||
  495. (type == cmStateEnums::STATIC_LIBRARY) ||
  496. (type == cmStateEnums::SHARED_LIBRARY) ||
  497. (type == cmStateEnums::MODULE_LIBRARY) ||
  498. (type == cmStateEnums::OBJECT_LIBRARY) ||
  499. (type == cmStateEnums::UTILITY))) {
  500. // Add a rule to build the target by name.
  501. lg->WriteDivider(ruleFileStream);
  502. ruleFileStream << "# Target rules for targets named " << name
  503. << "\n\n";
  504. // Write the rule.
  505. commands.clear();
  506. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  507. tmp += "Makefile2";
  508. commands.push_back(lg->GetRecursiveMakeCall(tmp.c_str(), name));
  509. depends.clear();
  510. depends.push_back("cmake_check_build_system");
  511. lg->WriteMakeRule(ruleFileStream, "Build rule for target.", name,
  512. depends, commands, true);
  513. // Add a fast rule to build the target
  514. std::string localName = lg->GetRelativeTargetDirectory(gtarget);
  515. std::string makefileName;
  516. makefileName = localName;
  517. makefileName += "/build.make";
  518. depends.clear();
  519. commands.clear();
  520. std::string makeTargetName = localName;
  521. makeTargetName += "/build";
  522. localName = name;
  523. localName += "/fast";
  524. commands.push_back(
  525. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  526. lg->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  527. localName, depends, commands, true);
  528. // Add a local name for the rule to relink the target before
  529. // installation.
  530. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  531. makeTargetName = lg->GetRelativeTargetDirectory(gtarget);
  532. makeTargetName += "/preinstall";
  533. localName = name;
  534. localName += "/preinstall";
  535. depends.clear();
  536. commands.clear();
  537. commands.push_back(
  538. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  539. lg->WriteMakeRule(ruleFileStream,
  540. "Manual pre-install relink rule for target.",
  541. localName, depends, commands, true);
  542. }
  543. }
  544. }
  545. }
  546. }
  547. void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2(
  548. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  549. {
  550. std::vector<std::string> depends;
  551. std::vector<std::string> commands;
  552. std::string localName;
  553. std::string makeTargetName;
  554. // write the directory level rules for this local gen
  555. this->WriteDirectoryRules2(ruleFileStream, lg);
  556. depends.push_back("cmake_check_build_system");
  557. // for each target Generate the rule files for each target.
  558. std::vector<cmGeneratorTarget*> targets = lg->GetGeneratorTargets();
  559. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  560. t != targets.end(); ++t) {
  561. cmGeneratorTarget* gtarget = *t;
  562. int type = gtarget->GetType();
  563. std::string name = gtarget->GetName();
  564. if (!name.empty() && ((type == cmStateEnums::EXECUTABLE) ||
  565. (type == cmStateEnums::STATIC_LIBRARY) ||
  566. (type == cmStateEnums::SHARED_LIBRARY) ||
  567. (type == cmStateEnums::MODULE_LIBRARY) ||
  568. (type == cmStateEnums::OBJECT_LIBRARY) ||
  569. (type == cmStateEnums::UTILITY))) {
  570. std::string makefileName;
  571. // Add a rule to build the target by name.
  572. localName = lg->GetRelativeTargetDirectory(gtarget);
  573. makefileName = localName;
  574. makefileName += "/build.make";
  575. bool needRequiresStep = this->NeedRequiresStep(gtarget);
  576. lg->WriteDivider(ruleFileStream);
  577. ruleFileStream << "# Target rules for target " << localName << "\n\n";
  578. commands.clear();
  579. makeTargetName = localName;
  580. makeTargetName += "/depend";
  581. commands.push_back(
  582. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  583. // add requires if we need it for this generator
  584. if (needRequiresStep) {
  585. makeTargetName = localName;
  586. makeTargetName += "/requires";
  587. commands.push_back(
  588. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  589. }
  590. makeTargetName = localName;
  591. makeTargetName += "/build";
  592. commands.push_back(
  593. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  594. // Write the rule.
  595. localName += "/all";
  596. depends.clear();
  597. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  598. progress.Dir = lg->GetBinaryDirectory();
  599. progress.Dir += cmake::GetCMakeFilesDirectory();
  600. {
  601. std::ostringstream progressArg;
  602. const char* sep = "";
  603. std::vector<unsigned long>& progFiles =
  604. this->ProgressMap[gtarget].Marks;
  605. for (std::vector<unsigned long>::iterator i = progFiles.begin();
  606. i != progFiles.end(); ++i) {
  607. progressArg << sep << *i;
  608. sep = ",";
  609. }
  610. progress.Arg = progressArg.str();
  611. }
  612. bool targetMessages = true;
  613. if (const char* tgtMsg =
  614. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  615. "TARGET_MESSAGES")) {
  616. targetMessages = cmSystemTools::IsOn(tgtMsg);
  617. }
  618. if (targetMessages) {
  619. lg->AppendEcho(commands, "Built target " + name,
  620. cmLocalUnixMakefileGenerator3::EchoNormal, &progress);
  621. }
  622. this->AppendGlobalTargetDepends(depends, gtarget);
  623. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  624. localName, depends, commands, true);
  625. // add the all/all dependency
  626. if (!this->IsExcluded(this->LocalGenerators[0], gtarget)) {
  627. depends.clear();
  628. depends.push_back(localName);
  629. commands.clear();
  630. lg->WriteMakeRule(ruleFileStream, "Include target in all.", "all",
  631. depends, commands, true);
  632. }
  633. // Write the rule.
  634. commands.clear();
  635. {
  636. // TODO: Convert the total progress count to a make variable.
  637. std::ostringstream progCmd;
  638. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  639. // # in target
  640. progCmd << lg->ConvertToOutputFormat(
  641. cmSystemTools::CollapseFullPath(progress.Dir),
  642. cmOutputConverter::SHELL);
  643. //
  644. std::set<cmGeneratorTarget const*> emitted;
  645. progCmd << " " << this->CountProgressMarksInTarget(gtarget, emitted);
  646. commands.push_back(progCmd.str());
  647. }
  648. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  649. tmp += "Makefile2";
  650. commands.push_back(lg->GetRecursiveMakeCall(tmp.c_str(), localName));
  651. {
  652. std::ostringstream progCmd;
  653. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  654. progCmd << lg->ConvertToOutputFormat(
  655. cmSystemTools::CollapseFullPath(progress.Dir),
  656. cmOutputConverter::SHELL);
  657. progCmd << " 0";
  658. commands.push_back(progCmd.str());
  659. }
  660. depends.clear();
  661. depends.push_back("cmake_check_build_system");
  662. localName = lg->GetRelativeTargetDirectory(gtarget);
  663. localName += "/rule";
  664. lg->WriteMakeRule(ruleFileStream,
  665. "Build rule for subdir invocation for target.",
  666. localName, depends, commands, true);
  667. // Add a target with the canonical name (no prefix, suffix or path).
  668. commands.clear();
  669. depends.clear();
  670. depends.push_back(localName);
  671. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.", name,
  672. depends, commands, true);
  673. // Add rules to prepare the target for installation.
  674. if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) {
  675. localName = lg->GetRelativeTargetDirectory(gtarget);
  676. localName += "/preinstall";
  677. depends.clear();
  678. commands.clear();
  679. commands.push_back(
  680. lg->GetRecursiveMakeCall(makefileName.c_str(), localName));
  681. lg->WriteMakeRule(ruleFileStream,
  682. "Pre-install relink rule for target.", localName,
  683. depends, commands, true);
  684. if (!this->IsExcluded(this->LocalGenerators[0], gtarget)) {
  685. depends.clear();
  686. depends.push_back(localName);
  687. commands.clear();
  688. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  689. "preinstall", depends, commands, true);
  690. }
  691. }
  692. // add the clean rule
  693. localName = lg->GetRelativeTargetDirectory(gtarget);
  694. makeTargetName = localName;
  695. makeTargetName += "/clean";
  696. depends.clear();
  697. commands.clear();
  698. commands.push_back(
  699. lg->GetRecursiveMakeCall(makefileName.c_str(), makeTargetName));
  700. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  701. makeTargetName, depends, commands, true);
  702. commands.clear();
  703. depends.push_back(makeTargetName);
  704. lg->WriteMakeRule(ruleFileStream, "clean rule for target.", "clean",
  705. depends, commands, true);
  706. }
  707. }
  708. }
  709. // Build a map that contains a the set of targets used by each local
  710. // generator directory level.
  711. void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks()
  712. {
  713. this->DirectoryTargetsMap.clear();
  714. // Loop over all targets in all local generators.
  715. for (std::vector<cmLocalGenerator*>::const_iterator lgi =
  716. this->LocalGenerators.begin();
  717. lgi != this->LocalGenerators.end(); ++lgi) {
  718. cmLocalGenerator* lg = *lgi;
  719. std::vector<cmGeneratorTarget*> targets = lg->GetGeneratorTargets();
  720. for (std::vector<cmGeneratorTarget*>::const_iterator t = targets.begin();
  721. t != targets.end(); ++t) {
  722. cmGeneratorTarget* gt = *t;
  723. cmLocalGenerator* tlg = gt->GetLocalGenerator();
  724. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  725. gt->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  726. continue;
  727. }
  728. cmStateSnapshot csnp = lg->GetStateSnapshot();
  729. cmStateSnapshot tsnp = tlg->GetStateSnapshot();
  730. // Consider the directory containing the target and all its
  731. // parents until something excludes the target.
  732. for (; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  733. csnp = csnp.GetBuildsystemDirectoryParent()) {
  734. // This local generator includes the target.
  735. std::set<cmGeneratorTarget const*>& targetSet =
  736. this->DirectoryTargetsMap[csnp];
  737. targetSet.insert(gt);
  738. // Add dependencies of the included target. An excluded
  739. // target may still be included if it is a dependency of a
  740. // non-excluded target.
  741. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(gt);
  742. for (TargetDependSet::const_iterator ti = tgtdeps.begin();
  743. ti != tgtdeps.end(); ++ti) {
  744. targetSet.insert(*ti);
  745. }
  746. }
  747. }
  748. }
  749. }
  750. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget(
  751. cmGeneratorTarget const* target, std::set<cmGeneratorTarget const*>& emitted)
  752. {
  753. size_t count = 0;
  754. if (emitted.insert(target).second) {
  755. count = this->ProgressMap[target].Marks.size();
  756. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  757. for (TargetDependSet::const_iterator di = depends.begin();
  758. di != depends.end(); ++di) {
  759. if ((*di)->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  760. continue;
  761. }
  762. count += this->CountProgressMarksInTarget(*di, emitted);
  763. }
  764. }
  765. return count;
  766. }
  767. size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll(
  768. cmLocalGenerator* lg)
  769. {
  770. size_t count = 0;
  771. std::set<cmGeneratorTarget const*> emitted;
  772. std::set<cmGeneratorTarget const*> const& targets =
  773. this->DirectoryTargetsMap[lg->GetStateSnapshot()];
  774. for (std::set<cmGeneratorTarget const*>::const_iterator t = targets.begin();
  775. t != targets.end(); ++t) {
  776. count += this->CountProgressMarksInTarget(*t, emitted);
  777. }
  778. return count;
  779. }
  780. void cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  781. cmMakefileTargetGenerator* tg)
  782. {
  783. TargetProgress& tp = this->ProgressMap[tg->GetGeneratorTarget()];
  784. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  785. tp.VariableFile = tg->GetProgressFileNameFull();
  786. }
  787. void cmGlobalUnixMakefileGenerator3::TargetProgress::WriteProgressVariables(
  788. unsigned long total, unsigned long& current)
  789. {
  790. cmGeneratedFileStream fout(this->VariableFile.c_str());
  791. for (unsigned long i = 1; i <= this->NumberOfActions; ++i) {
  792. fout << "CMAKE_PROGRESS_" << i << " = ";
  793. if (total <= 100) {
  794. unsigned long num = i + current;
  795. fout << num;
  796. this->Marks.push_back(num);
  797. } else if (((i + current) * 100) / total >
  798. ((i - 1 + current) * 100) / total) {
  799. unsigned long num = ((i + current) * 100) / total;
  800. fout << num;
  801. this->Marks.push_back(num);
  802. }
  803. fout << "\n";
  804. }
  805. fout << "\n";
  806. current += this->NumberOfActions;
  807. }
  808. void cmGlobalUnixMakefileGenerator3::AppendGlobalTargetDepends(
  809. std::vector<std::string>& depends, cmGeneratorTarget* target)
  810. {
  811. TargetDependSet const& depends_set = this->GetTargetDirectDepends(target);
  812. for (TargetDependSet::const_iterator i = depends_set.begin();
  813. i != depends_set.end(); ++i) {
  814. // Create the target-level dependency.
  815. cmGeneratorTarget const* dep = *i;
  816. if (dep->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  817. continue;
  818. }
  819. cmLocalUnixMakefileGenerator3* lg3 =
  820. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  821. std::string tgtName =
  822. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep));
  823. tgtName += "/all";
  824. depends.push_back(tgtName);
  825. }
  826. }
  827. void cmGlobalUnixMakefileGenerator3::WriteHelpRule(
  828. std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg)
  829. {
  830. // add the help target
  831. std::string path;
  832. std::vector<std::string> no_depends;
  833. std::vector<std::string> commands;
  834. lg->AppendEcho(commands, "The following are some of the valid targets "
  835. "for this Makefile:");
  836. lg->AppendEcho(commands, "... all (the default if no target is provided)");
  837. lg->AppendEcho(commands, "... clean");
  838. lg->AppendEcho(commands, "... depend");
  839. // Keep track of targets already listed.
  840. std::set<std::string> emittedTargets;
  841. // for each local generator
  842. unsigned int i;
  843. cmLocalUnixMakefileGenerator3* lg2;
  844. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  845. lg2 =
  846. static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[i]);
  847. // for the passed in makefile or if this is the top Makefile wripte out
  848. // the targets
  849. if (lg2 == lg || lg->IsRootMakefile()) {
  850. // for each target Generate the rule files for each target.
  851. std::vector<cmGeneratorTarget*> targets = lg2->GetGeneratorTargets();
  852. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  853. t != targets.end(); ++t) {
  854. cmGeneratorTarget* target = *t;
  855. cmStateEnums::TargetType type = target->GetType();
  856. if ((type == cmStateEnums::EXECUTABLE) ||
  857. (type == cmStateEnums::STATIC_LIBRARY) ||
  858. (type == cmStateEnums::SHARED_LIBRARY) ||
  859. (type == cmStateEnums::MODULE_LIBRARY) ||
  860. (type == cmStateEnums::OBJECT_LIBRARY) ||
  861. (type == cmStateEnums::GLOBAL_TARGET) ||
  862. (type == cmStateEnums::UTILITY)) {
  863. std::string name = target->GetName();
  864. if (emittedTargets.insert(name).second) {
  865. path = "... ";
  866. path += name;
  867. lg->AppendEcho(commands, path);
  868. }
  869. }
  870. }
  871. }
  872. }
  873. std::vector<std::string> const& localHelp = lg->GetLocalHelp();
  874. for (std::vector<std::string>::const_iterator o = localHelp.begin();
  875. o != localHelp.end(); ++o) {
  876. path = "... ";
  877. path += *o;
  878. lg->AppendEcho(commands, path);
  879. }
  880. lg->WriteMakeRule(ruleFileStream, "Help Target", "help", no_depends,
  881. commands, true);
  882. ruleFileStream << "\n\n";
  883. }
  884. bool cmGlobalUnixMakefileGenerator3::NeedRequiresStep(
  885. const cmGeneratorTarget* target)
  886. {
  887. std::set<std::string> languages;
  888. target->GetLanguages(
  889. languages,
  890. target->Target->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  891. for (std::set<std::string>::const_iterator l = languages.begin();
  892. l != languages.end(); ++l) {
  893. std::string var = "CMAKE_NEEDS_REQUIRES_STEP_";
  894. var += *l;
  895. var += "_FLAG";
  896. if (target->Target->GetMakefile()->GetDefinition(var)) {
  897. return true;
  898. }
  899. }
  900. return false;
  901. }