cmGlobalUnixMakefileGenerator3.cxx 36 KB

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