cmGlobalUnixMakefileGenerator3.cxx 40 KB

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