cmGlobalUnixMakefileGenerator3.cxx 39 KB

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