cmGlobalUnixMakefileGenerator3.cxx 39 KB

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