cmGlobalUnixMakefileGenerator3.cxx 37 KB

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