cmGlobalUnixMakefileGenerator3.cxx 36 KB

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