cmGlobalUnixMakefileGenerator3.cxx 39 KB

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