cmGlobalUnixMakefileGenerator3.cxx 37 KB

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