cmGlobalUnixMakefileGenerator3.cxx 38 KB

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