cmGlobalUnixMakefileGenerator3.cxx 39 KB

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