cmGlobalUnixMakefileGenerator3.cxx 37 KB

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