cmGlobalUnixMakefileGenerator3.cxx 37 KB

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