cmGlobalUnixMakefileGenerator3.cxx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. cmGeneratorTarget* gt = this->GetGeneratorTarget(&l->second);
  389. std::string tname = lg->GetRelativeTargetDirectory(gt);
  390. tname += "/DependInfo.cmake";
  391. cmSystemTools::ConvertToUnixSlashes(tname);
  392. cmakefileStream << " \"" << tname << "\"\n";
  393. }
  394. }
  395. }
  396. cmakefileStream << " )\n";
  397. }
  398. //----------------------------------------------------------------------------
  399. void
  400. cmGlobalUnixMakefileGenerator3
  401. ::WriteDirectoryRule2(std::ostream& ruleFileStream,
  402. cmLocalUnixMakefileGenerator3* lg,
  403. const char* pass, bool check_all,
  404. bool check_relink)
  405. {
  406. // Get the relative path to the subdirectory from the top.
  407. std::string makeTarget = lg->GetCurrentBinaryDirectory();
  408. makeTarget += "/";
  409. makeTarget += pass;
  410. // The directory-level rule should depend on the target-level rules
  411. // for all targets in the directory.
  412. std::vector<std::string> depends;
  413. cmGeneratorTargetsType targets = lg->GetGeneratorTargets();
  414. for(cmGeneratorTargetsType::iterator l = targets.begin();
  415. l != targets.end(); ++l)
  416. {
  417. cmGeneratorTarget* gtarget = l->second;
  418. int type = gtarget->GetType();
  419. if((type == cmTarget::EXECUTABLE) ||
  420. (type == cmTarget::STATIC_LIBRARY) ||
  421. (type == cmTarget::SHARED_LIBRARY) ||
  422. (type == cmTarget::MODULE_LIBRARY) ||
  423. (type == cmTarget::OBJECT_LIBRARY) ||
  424. (type == cmTarget::UTILITY))
  425. {
  426. if(gtarget->Target->IsImported())
  427. {
  428. continue;
  429. }
  430. // Add this to the list of depends rules in this directory.
  431. if((!check_all || !gtarget->GetPropertyAsBool("EXCLUDE_FROM_ALL")) &&
  432. (!check_relink ||
  433. gtarget
  434. ->NeedRelinkBeforeInstall(lg->GetConfigName())))
  435. {
  436. std::string tname = lg->GetRelativeTargetDirectory(gtarget);
  437. tname += "/";
  438. tname += pass;
  439. depends.push_back(tname);
  440. }
  441. }
  442. }
  443. // The directory-level rule should depend on the directory-level
  444. // rules of the subdirectories.
  445. std::vector<cmState::Snapshot> children
  446. = lg->GetStateSnapshot().GetChildren();
  447. for(std::vector<cmState::Snapshot>::const_iterator
  448. ci = children.begin(); ci != children.end(); ++ci)
  449. {
  450. std::string subdir = ci->GetDirectory().GetCurrentBinary();
  451. subdir += "/";
  452. subdir += pass;
  453. depends.push_back(subdir);
  454. }
  455. // Work-around for makes that drop rules that have no dependencies
  456. // or commands.
  457. if(depends.empty() && this->EmptyRuleHackDepends != "")
  458. {
  459. depends.push_back(this->EmptyRuleHackDepends);
  460. }
  461. // Write the rule.
  462. std::string doc = "Convenience name for \"";
  463. doc += pass;
  464. doc += "\" pass in the directory.";
  465. std::vector<std::string> no_commands;
  466. lg->WriteMakeRule(ruleFileStream, doc.c_str(),
  467. makeTarget, depends, no_commands, true);
  468. }
  469. //----------------------------------------------------------------------------
  470. void
  471. cmGlobalUnixMakefileGenerator3
  472. ::WriteDirectoryRules2(std::ostream& ruleFileStream,
  473. cmLocalUnixMakefileGenerator3* lg)
  474. {
  475. // Only subdirectories need these rules.
  476. if(lg->GetMakefile()->IsRootMakefile())
  477. {
  478. return;
  479. }
  480. // Begin the directory-level rules section.
  481. std::string dir = lg->GetCurrentBinaryDirectory();
  482. dir = lg->Convert(dir, cmLocalGenerator::HOME_OUTPUT,
  483. cmLocalGenerator::MAKERULE);
  484. lg->WriteDivider(ruleFileStream);
  485. ruleFileStream
  486. << "# Directory level rules for directory "
  487. << dir << "\n\n";
  488. // Write directory-level rules for "all".
  489. this->WriteDirectoryRule2(ruleFileStream, lg, "all", true, false);
  490. // Write directory-level rules for "clean".
  491. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false);
  492. // Write directory-level rules for "preinstall".
  493. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", true, true);
  494. }
  495. //----------------------------------------------------------------------------
  496. void cmGlobalUnixMakefileGenerator3
  497. ::GenerateBuildCommand(std::vector<std::string>& makeCommand,
  498. const std::string& makeProgram,
  499. const std::string& /*projectName*/,
  500. const std::string& /*projectDir*/,
  501. const std::string& targetName,
  502. const std::string& /*config*/,
  503. bool fast, bool /*verbose*/,
  504. std::vector<std::string> const& makeOptions)
  505. {
  506. makeCommand.push_back(
  507. this->SelectMakeProgram(makeProgram)
  508. );
  509. // Since we have full control over the invocation of nmake, let us
  510. // make it quiet.
  511. if (cmHasLiteralPrefix(this->GetName(), "NMake Makefiles"))
  512. {
  513. makeCommand.push_back("/NOLOGO");
  514. }
  515. makeCommand.insert(makeCommand.end(),
  516. makeOptions.begin(), makeOptions.end());
  517. if (!targetName.empty())
  518. {
  519. cmMakefile* mf;
  520. if (!this->Makefiles.empty())
  521. {
  522. mf = this->Makefiles[0];
  523. }
  524. else
  525. {
  526. cmState::Snapshot snapshot = this->CMakeInstance->GetCurrentSnapshot();
  527. snapshot.GetDirectory().SetCurrentSource
  528. (this->CMakeInstance->GetHomeDirectory());
  529. snapshot.GetDirectory().SetCurrentBinary
  530. (this->CMakeInstance->GetHomeOutputDirectory());
  531. snapshot.SetDefaultDefinitions();
  532. mf = new cmMakefile(this, snapshot);
  533. }
  534. std::string tname = targetName;
  535. if(fast)
  536. {
  537. tname += "/fast";
  538. }
  539. cmOutputConverter conv(mf->GetStateSnapshot());
  540. tname = conv.Convert(tname,cmOutputConverter::HOME_OUTPUT);
  541. cmSystemTools::ConvertToOutputSlashes(tname);
  542. makeCommand.push_back(tname);
  543. if (this->Makefiles.empty())
  544. {
  545. delete mf;
  546. }
  547. }
  548. }
  549. //----------------------------------------------------------------------------
  550. void
  551. cmGlobalUnixMakefileGenerator3
  552. ::WriteConvenienceRules(std::ostream& ruleFileStream,
  553. std::set<std::string> &emitted)
  554. {
  555. std::vector<std::string> depends;
  556. std::vector<std::string> commands;
  557. depends.push_back("cmake_check_build_system");
  558. // write the target convenience rules
  559. unsigned int i;
  560. cmLocalUnixMakefileGenerator3 *lg;
  561. for (i = 0; i < this->LocalGenerators.size(); ++i)
  562. {
  563. lg = static_cast<cmLocalUnixMakefileGenerator3 *>
  564. (this->LocalGenerators[i]);
  565. // for each target Generate the rule files for each target.
  566. cmGeneratorTargetsType targets = lg->GetGeneratorTargets();
  567. for(cmGeneratorTargetsType::iterator t = targets.begin();
  568. t != targets.end(); ++t)
  569. {
  570. cmGeneratorTarget* gtarget = t->second;
  571. if(gtarget->Target->IsImported())
  572. {
  573. continue;
  574. }
  575. // Don't emit the same rule twice (e.g. two targets with the same
  576. // simple name)
  577. int type = gtarget->GetType();
  578. std::string name = gtarget->GetName();
  579. if(!name.empty() &&
  580. emitted.insert(name).second &&
  581. // Handle user targets here. Global targets are handled in
  582. // the local generator on a per-directory basis.
  583. ((type == cmTarget::EXECUTABLE) ||
  584. (type == cmTarget::STATIC_LIBRARY) ||
  585. (type == cmTarget::SHARED_LIBRARY) ||
  586. (type == cmTarget::MODULE_LIBRARY) ||
  587. (type == cmTarget::OBJECT_LIBRARY) ||
  588. (type == cmTarget::UTILITY)))
  589. {
  590. // Add a rule to build the target by name.
  591. lg->WriteDivider(ruleFileStream);
  592. ruleFileStream
  593. << "# Target rules for targets named "
  594. << name << "\n\n";
  595. // Write the rule.
  596. commands.clear();
  597. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  598. tmp += "Makefile2";
  599. commands.push_back(lg->GetRecursiveMakeCall
  600. (tmp.c_str(),name));
  601. depends.clear();
  602. depends.push_back("cmake_check_build_system");
  603. lg->WriteMakeRule(ruleFileStream,
  604. "Build rule for target.",
  605. name, depends, commands,
  606. true);
  607. // Add a fast rule to build the target
  608. std::string localName =
  609. lg->GetRelativeTargetDirectory(gtarget);
  610. std::string makefileName;
  611. makefileName = localName;
  612. makefileName += "/build.make";
  613. depends.clear();
  614. commands.clear();
  615. std::string makeTargetName = localName;
  616. makeTargetName += "/build";
  617. localName = name;
  618. localName += "/fast";
  619. commands.push_back(lg->GetRecursiveMakeCall
  620. (makefileName.c_str(), makeTargetName));
  621. lg->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  622. localName, depends, commands, true);
  623. // Add a local name for the rule to relink the target before
  624. // installation.
  625. if(gtarget
  626. ->NeedRelinkBeforeInstall(lg->GetConfigName()))
  627. {
  628. makeTargetName = lg->GetRelativeTargetDirectory(gtarget);
  629. makeTargetName += "/preinstall";
  630. localName = name;
  631. localName += "/preinstall";
  632. depends.clear();
  633. commands.clear();
  634. commands.push_back(lg->GetRecursiveMakeCall
  635. (makefileName.c_str(), makeTargetName));
  636. lg->WriteMakeRule(ruleFileStream,
  637. "Manual pre-install relink rule for target.",
  638. localName, depends, commands, true);
  639. }
  640. }
  641. }
  642. }
  643. }
  644. //----------------------------------------------------------------------------
  645. void
  646. cmGlobalUnixMakefileGenerator3
  647. ::WriteConvenienceRules2(std::ostream& ruleFileStream,
  648. cmLocalUnixMakefileGenerator3 *lg)
  649. {
  650. std::vector<std::string> depends;
  651. std::vector<std::string> commands;
  652. std::string localName;
  653. std::string makeTargetName;
  654. // write the directory level rules for this local gen
  655. this->WriteDirectoryRules2(ruleFileStream,lg);
  656. depends.push_back("cmake_check_build_system");
  657. // for each target Generate the rule files for each target.
  658. cmGeneratorTargetsType targets = lg->GetGeneratorTargets();
  659. for(cmGeneratorTargetsType::iterator t = targets.begin();
  660. t != targets.end(); ++t)
  661. {
  662. cmGeneratorTarget* gtarget = t->second;
  663. if(gtarget->Target->IsImported())
  664. {
  665. continue;
  666. }
  667. int type = gtarget->GetType();
  668. std::string name = gtarget->GetName();
  669. if (!name.empty()
  670. && ( (type == cmTarget::EXECUTABLE)
  671. || (type == cmTarget::STATIC_LIBRARY)
  672. || (type == cmTarget::SHARED_LIBRARY)
  673. || (type == cmTarget::MODULE_LIBRARY)
  674. || (type == cmTarget::OBJECT_LIBRARY)
  675. || (type == cmTarget::UTILITY)))
  676. {
  677. std::string makefileName;
  678. // Add a rule to build the target by name.
  679. localName = lg->GetRelativeTargetDirectory(gtarget);
  680. makefileName = localName;
  681. makefileName += "/build.make";
  682. bool needRequiresStep = this->NeedRequiresStep(*gtarget->Target);
  683. lg->WriteDivider(ruleFileStream);
  684. ruleFileStream
  685. << "# Target rules for target "
  686. << localName << "\n\n";
  687. commands.clear();
  688. makeTargetName = localName;
  689. makeTargetName += "/depend";
  690. commands.push_back(lg->GetRecursiveMakeCall
  691. (makefileName.c_str(),makeTargetName));
  692. // add requires if we need it for this generator
  693. if (needRequiresStep)
  694. {
  695. makeTargetName = localName;
  696. makeTargetName += "/requires";
  697. commands.push_back(lg->GetRecursiveMakeCall
  698. (makefileName.c_str(),makeTargetName));
  699. }
  700. makeTargetName = localName;
  701. makeTargetName += "/build";
  702. commands.push_back(lg->GetRecursiveMakeCall
  703. (makefileName.c_str(),makeTargetName));
  704. // Write the rule.
  705. localName += "/all";
  706. depends.clear();
  707. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  708. progress.Dir = lg->GetBinaryDirectory();
  709. progress.Dir += cmake::GetCMakeFilesDirectory();
  710. {
  711. std::ostringstream progressArg;
  712. const char* sep = "";
  713. std::vector<unsigned long>& progFiles =
  714. this->ProgressMap[gtarget->Target].Marks;
  715. for (std::vector<unsigned long>::iterator i = progFiles.begin();
  716. i != progFiles.end(); ++i)
  717. {
  718. progressArg << sep << *i;
  719. sep = ",";
  720. }
  721. progress.Arg = progressArg.str();
  722. }
  723. bool targetMessages = true;
  724. if (const char* tgtMsg = this->GetCMakeInstance()
  725. ->GetState()
  726. ->GetGlobalProperty("TARGET_MESSAGES"))
  727. {
  728. targetMessages = cmSystemTools::IsOn(tgtMsg);
  729. }
  730. if (targetMessages)
  731. {
  732. lg->AppendEcho(commands, "Built target " + name,
  733. cmLocalUnixMakefileGenerator3::EchoNormal, &progress);
  734. }
  735. this->AppendGlobalTargetDepends(depends, gtarget);
  736. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  737. localName, depends, commands, true);
  738. // add the all/all dependency
  739. if(!this->IsExcluded(this->LocalGenerators[0], gtarget))
  740. {
  741. depends.clear();
  742. depends.push_back(localName);
  743. commands.clear();
  744. lg->WriteMakeRule(ruleFileStream, "Include target in all.",
  745. "all", depends, commands, true);
  746. }
  747. // Write the rule.
  748. commands.clear();
  749. {
  750. // TODO: Convert the total progress count to a make variable.
  751. std::ostringstream progCmd;
  752. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  753. // # in target
  754. progCmd << lg->Convert(progress.Dir,
  755. cmLocalGenerator::FULL,
  756. cmLocalGenerator::SHELL);
  757. //
  758. std::set<cmGeneratorTarget const*> emitted;
  759. progCmd << " "
  760. << this->CountProgressMarksInTarget(gtarget, emitted);
  761. commands.push_back(progCmd.str());
  762. }
  763. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  764. tmp += "Makefile2";
  765. commands.push_back(lg->GetRecursiveMakeCall
  766. (tmp.c_str(),localName));
  767. {
  768. std::ostringstream progCmd;
  769. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  770. progCmd << lg->Convert(progress.Dir,
  771. cmLocalGenerator::FULL,
  772. cmLocalGenerator::SHELL);
  773. progCmd << " 0";
  774. commands.push_back(progCmd.str());
  775. }
  776. depends.clear();
  777. depends.push_back("cmake_check_build_system");
  778. localName = lg->GetRelativeTargetDirectory(gtarget);
  779. localName += "/rule";
  780. lg->WriteMakeRule(ruleFileStream,
  781. "Build rule for subdir invocation for target.",
  782. localName, depends, commands, true);
  783. // Add a target with the canonical name (no prefix, suffix or path).
  784. commands.clear();
  785. depends.clear();
  786. depends.push_back(localName);
  787. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  788. name, depends, commands, true);
  789. // Add rules to prepare the target for installation.
  790. if(gtarget
  791. ->NeedRelinkBeforeInstall(lg->GetConfigName()))
  792. {
  793. localName = lg->GetRelativeTargetDirectory(gtarget);
  794. localName += "/preinstall";
  795. depends.clear();
  796. commands.clear();
  797. commands.push_back(lg->GetRecursiveMakeCall
  798. (makefileName.c_str(), localName));
  799. lg->WriteMakeRule(ruleFileStream,
  800. "Pre-install relink rule for target.",
  801. localName, depends, commands, true);
  802. if(!this->IsExcluded(this->LocalGenerators[0], gtarget))
  803. {
  804. depends.clear();
  805. depends.push_back(localName);
  806. commands.clear();
  807. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  808. "preinstall", depends, commands, true);
  809. }
  810. }
  811. // add the clean rule
  812. localName = lg->GetRelativeTargetDirectory(gtarget);
  813. makeTargetName = localName;
  814. makeTargetName += "/clean";
  815. depends.clear();
  816. commands.clear();
  817. commands.push_back(lg->GetRecursiveMakeCall
  818. (makefileName.c_str(), makeTargetName));
  819. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  820. makeTargetName, depends, commands, true);
  821. commands.clear();
  822. depends.push_back(makeTargetName);
  823. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  824. "clean", depends, commands, true);
  825. }
  826. }
  827. }
  828. // Build a map that contains a the set of targets used by each local
  829. // generator directory level.
  830. void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks()
  831. {
  832. this->DirectoryTargetsMap.clear();
  833. // Loop over all targets in all local generators.
  834. for(std::vector<cmLocalGenerator*>::const_iterator
  835. lgi = this->LocalGenerators.begin();
  836. lgi != this->LocalGenerators.end(); ++lgi)
  837. {
  838. cmLocalGenerator* lg = *lgi;
  839. cmMakefile* mf = lg->GetMakefile();
  840. cmTargets const& targets = mf->GetTargets();
  841. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  842. {
  843. cmTarget const& target = t->second;
  844. cmGeneratorTarget* gt = this->GetGeneratorTarget(&target);
  845. cmLocalGenerator* tlg = gt->GetLocalGenerator();
  846. if(gt->GetType() == cmTarget::INTERFACE_LIBRARY
  847. || gt->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  848. {
  849. continue;
  850. }
  851. cmState::Snapshot csnp = lg->GetStateSnapshot();
  852. cmState::Snapshot tsnp = tlg->GetStateSnapshot();
  853. // Consider the directory containing the target and all its
  854. // parents until something excludes the target.
  855. for( ; csnp.IsValid() && !this->IsExcluded(csnp, tsnp);
  856. csnp = csnp.GetBuildsystemDirectoryParent())
  857. {
  858. // This local generator includes the target.
  859. std::set<cmGeneratorTarget const*>& targetSet =
  860. this->DirectoryTargetsMap[csnp];
  861. targetSet.insert(gt);
  862. // Add dependencies of the included target. An excluded
  863. // target may still be included if it is a dependency of a
  864. // non-excluded target.
  865. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(gt);
  866. for(TargetDependSet::const_iterator ti = tgtdeps.begin();
  867. ti != tgtdeps.end(); ++ti)
  868. {
  869. targetSet.insert(*ti);
  870. }
  871. }
  872. }
  873. }
  874. }
  875. //----------------------------------------------------------------------------
  876. size_t
  877. cmGlobalUnixMakefileGenerator3
  878. ::CountProgressMarksInTarget(cmGeneratorTarget const* target,
  879. std::set<cmGeneratorTarget const*>& emitted)
  880. {
  881. size_t count = 0;
  882. if(emitted.insert(target).second)
  883. {
  884. count = this->ProgressMap[target->Target].Marks.size();
  885. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  886. for(TargetDependSet::const_iterator di = depends.begin();
  887. di != depends.end(); ++di)
  888. {
  889. if ((*di)->GetType() == cmTarget::INTERFACE_LIBRARY)
  890. {
  891. continue;
  892. }
  893. count += this->CountProgressMarksInTarget(*di, emitted);
  894. }
  895. }
  896. return count;
  897. }
  898. //----------------------------------------------------------------------------
  899. size_t
  900. cmGlobalUnixMakefileGenerator3
  901. ::CountProgressMarksInAll(cmLocalUnixMakefileGenerator3* lg)
  902. {
  903. size_t count = 0;
  904. std::set<cmGeneratorTarget const*> emitted;
  905. std::set<cmGeneratorTarget const*> const& targets
  906. = this->DirectoryTargetsMap[lg->GetStateSnapshot()];
  907. for(std::set<cmGeneratorTarget const*>::const_iterator t = targets.begin();
  908. t != targets.end(); ++t)
  909. {
  910. count += this->CountProgressMarksInTarget(*t, emitted);
  911. }
  912. return count;
  913. }
  914. //----------------------------------------------------------------------------
  915. void
  916. cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  917. cmMakefileTargetGenerator* tg)
  918. {
  919. TargetProgress& tp = this->ProgressMap[tg->GetTarget()];
  920. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  921. tp.VariableFile = tg->GetProgressFileNameFull();
  922. }
  923. //----------------------------------------------------------------------------
  924. void
  925. cmGlobalUnixMakefileGenerator3::TargetProgress
  926. ::WriteProgressVariables(unsigned long total, unsigned long &current)
  927. {
  928. cmGeneratedFileStream fout(this->VariableFile.c_str());
  929. for(unsigned long i = 1; i <= this->NumberOfActions; ++i)
  930. {
  931. fout << "CMAKE_PROGRESS_" << i << " = ";
  932. if (total <= 100)
  933. {
  934. unsigned long num = i + current;
  935. fout << num;
  936. this->Marks.push_back(num);
  937. }
  938. else if (((i+current)*100)/total > ((i-1+current)*100)/total)
  939. {
  940. unsigned long num = ((i+current)*100)/total;
  941. fout << num;
  942. this->Marks.push_back(num);
  943. }
  944. fout << "\n";
  945. }
  946. fout << "\n";
  947. current += this->NumberOfActions;
  948. }
  949. //----------------------------------------------------------------------------
  950. void
  951. cmGlobalUnixMakefileGenerator3
  952. ::AppendGlobalTargetDepends(std::vector<std::string>& depends,
  953. cmGeneratorTarget* target)
  954. {
  955. TargetDependSet const& depends_set = this->GetTargetDirectDepends(target);
  956. for(TargetDependSet::const_iterator i = depends_set.begin();
  957. i != depends_set.end(); ++i)
  958. {
  959. // Create the target-level dependency.
  960. cmGeneratorTarget const* dep = *i;
  961. if (dep->GetType() == cmTarget::INTERFACE_LIBRARY)
  962. {
  963. continue;
  964. }
  965. cmLocalUnixMakefileGenerator3* lg3 =
  966. static_cast<cmLocalUnixMakefileGenerator3*>(dep->GetLocalGenerator());
  967. std::string tgtName =
  968. lg3->GetRelativeTargetDirectory(const_cast<cmGeneratorTarget*>(dep));
  969. tgtName += "/all";
  970. depends.push_back(tgtName);
  971. }
  972. }
  973. //----------------------------------------------------------------------------
  974. void cmGlobalUnixMakefileGenerator3::WriteHelpRule
  975. (std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3 *lg)
  976. {
  977. // add the help target
  978. std::string path;
  979. std::vector<std::string> no_depends;
  980. std::vector<std::string> commands;
  981. lg->AppendEcho(commands,"The following are some of the valid targets "
  982. "for this Makefile:");
  983. lg->AppendEcho(commands,"... all (the default if no target is provided)");
  984. lg->AppendEcho(commands,"... clean");
  985. lg->AppendEcho(commands,"... depend");
  986. // Keep track of targets already listed.
  987. std::set<std::string> emittedTargets;
  988. // for each local generator
  989. unsigned int i;
  990. cmLocalUnixMakefileGenerator3 *lg2;
  991. for (i = 0; i < this->LocalGenerators.size(); ++i)
  992. {
  993. lg2 =
  994. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  995. // for the passed in makefile or if this is the top Makefile wripte out
  996. // the targets
  997. if (lg2 == lg || lg->GetMakefile()->IsRootMakefile())
  998. {
  999. // for each target Generate the rule files for each target.
  1000. cmTargets& targets = lg2->GetMakefile()->GetTargets();
  1001. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  1002. {
  1003. cmTarget const& target = t->second;
  1004. cmTarget::TargetType type = target.GetType();
  1005. if((type == cmTarget::EXECUTABLE) ||
  1006. (type == cmTarget::STATIC_LIBRARY) ||
  1007. (type == cmTarget::SHARED_LIBRARY) ||
  1008. (type == cmTarget::MODULE_LIBRARY) ||
  1009. (type == cmTarget::OBJECT_LIBRARY) ||
  1010. (type == cmTarget::GLOBAL_TARGET) ||
  1011. (type == cmTarget::UTILITY))
  1012. {
  1013. std::string name = target.GetName();
  1014. if(emittedTargets.insert(name).second)
  1015. {
  1016. path = "... ";
  1017. path += name;
  1018. lg->AppendEcho(commands,path.c_str());
  1019. }
  1020. }
  1021. }
  1022. }
  1023. }
  1024. std::vector<std::string> const& localHelp = lg->GetLocalHelp();
  1025. for(std::vector<std::string>::const_iterator o = localHelp.begin();
  1026. o != localHelp.end(); ++o)
  1027. {
  1028. path = "... ";
  1029. path += *o;
  1030. lg->AppendEcho(commands, path.c_str());
  1031. }
  1032. lg->WriteMakeRule(ruleFileStream, "Help Target",
  1033. "help",
  1034. no_depends, commands, true);
  1035. ruleFileStream << "\n\n";
  1036. }
  1037. bool cmGlobalUnixMakefileGenerator3
  1038. ::NeedRequiresStep(cmTarget const& target)
  1039. {
  1040. std::set<std::string> languages;
  1041. cmGeneratorTarget* gtgt = this->GetGeneratorTarget(&target);
  1042. gtgt->GetLanguages(languages,
  1043. target.GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1044. for(std::set<std::string>::const_iterator l = languages.begin();
  1045. l != languages.end(); ++l)
  1046. {
  1047. std::string var = "CMAKE_NEEDS_REQUIRES_STEP_";
  1048. var += *l;
  1049. var += "_FLAG";
  1050. if(target.GetMakefile()->GetDefinition(var))
  1051. {
  1052. return true;
  1053. }
  1054. }
  1055. return false;
  1056. }