cmGlobalUnixMakefileGenerator3.cxx 39 KB

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