cmGlobalUnixMakefileGenerator3.cxx 39 KB

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