cmGlobalUnixMakefileGenerator3.cxx 37 KB

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