cmGlobalUnixMakefileGenerator3.cxx 37 KB

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