cmGlobalUnixMakefileGenerator3.cxx 37 KB

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