cmGlobalUnixMakefileGenerator3.cxx 38 KB

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