cmGlobalUnixMakefileGenerator3.cxx 37 KB

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