cmGlobalUnixMakefileGenerator3.cxx 37 KB

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