cmGlobalUnixMakefileGenerator3.cxx 39 KB

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