cmGlobalUnixMakefileGenerator3.cxx 37 KB

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