cmGlobalUnixMakefileGenerator3.cxx 38 KB

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