cmGlobalUnixMakefileGenerator3.cxx 37 KB

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