cmGlobalUnixMakefileGenerator3.cxx 40 KB

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