cmGlobalUnixMakefileGenerator3.cxx 39 KB

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