cmGlobalUnixMakefileGenerator3.cxx 39 KB

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