cmGlobalUnixMakefileGenerator3.cxx 37 KB

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