cmGlobalGenerator.cxx 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  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 "cmGlobalGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmExternalMakefileProjectGenerator.h"
  16. #include "cmake.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmVersion.h"
  20. #include "cmInstallExportGenerator.h"
  21. #include <stdlib.h> // required for atof
  22. #if defined(_WIN32) && !defined(__CYGWIN__)
  23. #include <windows.h>
  24. // workaround against Windows name mangling:
  25. #undef GetCurrentDirectory
  26. #endif
  27. #include <assert.h>
  28. cmGlobalGenerator::cmGlobalGenerator()
  29. {
  30. // By default the .SYMBOLIC dependency is not needed on symbolic rules.
  31. this->NeedSymbolicMark = false;
  32. // by default use the native paths
  33. this->ForceUnixPaths = false;
  34. // By default do not try to support color.
  35. this->ToolSupportsColor = false;
  36. // By default do not use link scripts.
  37. this->UseLinkScript = false;
  38. // Whether an install target is needed.
  39. this->InstallTargetEnabled = false;
  40. // how long to let try compiles run
  41. this->TryCompileTimeout = 0;
  42. this->ExtraGenerator = 0;
  43. this->CurrentLocalGenerator = 0;
  44. }
  45. cmGlobalGenerator::~cmGlobalGenerator()
  46. {
  47. // Delete any existing cmLocalGenerators
  48. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  49. {
  50. delete this->LocalGenerators[i];
  51. }
  52. this->LocalGenerators.clear();
  53. if (this->ExtraGenerator)
  54. {
  55. delete this->ExtraGenerator;
  56. }
  57. for (std::map<cmStdString, std::vector<cmTargetExport*> >::iterator
  58. setIt = this->ExportSets.begin();
  59. setIt != this->ExportSets.end();
  60. ++setIt)
  61. {
  62. for (unsigned int i = 0; i < setIt->second.size(); ++i)
  63. {
  64. delete setIt->second[i];
  65. }
  66. }
  67. }
  68. // Find the make program for the generator, required for try compiles
  69. void cmGlobalGenerator::FindMakeProgram(cmMakefile* mf)
  70. {
  71. if(this->FindMakeProgramFile.size() == 0)
  72. {
  73. cmSystemTools::Error(
  74. "Generator implementation error, "
  75. "all generators must specify this->FindMakeProgramFile");
  76. }
  77. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  78. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  79. {
  80. std::string setMakeProgram =
  81. mf->GetModulesFile(this->FindMakeProgramFile.c_str());
  82. if(setMakeProgram.size())
  83. {
  84. mf->ReadListFile(0, setMakeProgram.c_str());
  85. }
  86. }
  87. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  88. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  89. {
  90. cmOStringStream err;
  91. err << "CMake was unable to find a build program corresponding to \""
  92. << this->GetName() << "\". CMAKE_MAKE_PROGRAM is not set. You "
  93. << "probably need to select a different build tool.";
  94. cmSystemTools::Error(err.str().c_str());
  95. cmSystemTools::SetFatalErrorOccured();
  96. return;
  97. }
  98. std::string makeProgram = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  99. // if there are spaces in the make program use short path
  100. // but do not short path the actual program name, as
  101. // this can cause trouble with VSExpress
  102. if(makeProgram.find(' ') != makeProgram.npos)
  103. {
  104. std::string dir;
  105. std::string file;
  106. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  107. dir, file);
  108. std::string saveFile = file;
  109. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  110. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  111. dir, file);
  112. makeProgram = dir;
  113. makeProgram += "/";
  114. makeProgram += saveFile;
  115. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  116. "make program",
  117. cmCacheManager::FILEPATH);
  118. }
  119. if(makeProgram.find("xcodebuild") != makeProgram.npos)
  120. {
  121. // due to the text file busy /bin/sh problem with xcodebuild
  122. // use the cmakexbuild wrapper instead. This program
  123. // will run xcodebuild and if it sees the error text file busy
  124. // it will stop forwarding output, and let the build finish.
  125. // Then it will retry the build. It will continue this
  126. // untill no text file busy errors occur.
  127. std::string cmakexbuild =
  128. this->CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_COMMAND");
  129. cmakexbuild = cmakexbuild.substr(0, cmakexbuild.length()-5);
  130. cmakexbuild += "cmakexbuild";
  131. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM",
  132. cmakexbuild.c_str(),
  133. "make program",
  134. cmCacheManager::FILEPATH);
  135. }
  136. }
  137. // enable the given language
  138. //
  139. // The following files are loaded in this order:
  140. //
  141. // First figure out what OS we are running on:
  142. //
  143. // CMakeSystem.cmake - configured file created by CMakeDetermineSystem.cmake
  144. // CMakeDetermineSystem.cmake - figure out os info and create
  145. // CMakeSystem.cmake IF CMAKE_SYSTEM
  146. // not set
  147. // CMakeSystem.cmake - configured file created by
  148. // CMakeDetermineSystem.cmake IF CMAKE_SYSTEM_LOADED
  149. // Next try and enable all languages found in the languages vector
  150. //
  151. // FOREACH LANG in languages
  152. // CMake(LANG)Compiler.cmake - configured file create by
  153. // CMakeDetermine(LANG)Compiler.cmake
  154. // CMakeDetermine(LANG)Compiler.cmake - Finds compiler for LANG and
  155. // creates CMake(LANG)Compiler.cmake
  156. // CMake(LANG)Compiler.cmake - configured file created by
  157. // CMakeDetermine(LANG)Compiler.cmake
  158. //
  159. // CMakeSystemSpecificInformation.cmake
  160. // - includes Platform/${CMAKE_SYSTEM_NAME}.cmake
  161. // may use compiler stuff
  162. // FOREACH LANG in languages
  163. // CMake(LANG)Information.cmake
  164. // - loads Platform/${CMAKE_SYSTEM_NAME}-${COMPILER}.cmake
  165. // CMakeTest(LANG)Compiler.cmake
  166. // - Make sure the compiler works with a try compile if
  167. // CMakeDetermine(LANG) was loaded
  168. //
  169. // Now load a few files that can override values set in any of the above
  170. // (PROJECTNAME)Compatibility.cmake
  171. // - load any backwards compatibility stuff for current project
  172. // ${CMAKE_USER_MAKE_RULES_OVERRIDE}
  173. // - allow users a chance to override system variables
  174. //
  175. //
  176. void
  177. cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
  178. cmMakefile *mf, bool)
  179. {
  180. if(languages.size() == 0)
  181. {
  182. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  183. cmSystemTools::SetFatalErrorOccured();
  184. return;
  185. }
  186. mf->AddDefinition("RUN_CONFIGURE", true);
  187. std::string rootBin = mf->GetHomeOutputDirectory();
  188. rootBin += cmake::GetCMakeFilesDirectory();
  189. // If the configuration files path has been set,
  190. // then we are in a try compile and need to copy the enable language
  191. // files from the parent cmake bin dir, into the try compile bin dir
  192. if(this->ConfiguredFilesPath.size())
  193. {
  194. for(std::vector<std::string>::const_iterator l = languages.begin();
  195. l != languages.end(); ++l)
  196. {
  197. if(*l == "NONE")
  198. {
  199. this->SetLanguageEnabled("NONE", mf);
  200. break;
  201. }
  202. }
  203. rootBin = this->ConfiguredFilesPath;
  204. }
  205. // set the dir for parent files so they can be used by modules
  206. mf->AddDefinition("CMAKE_PLATFORM_ROOT_BIN",rootBin.c_str());
  207. // find and make sure CMAKE_MAKE_PROGRAM is defined
  208. this->FindMakeProgram(mf);
  209. // try and load the CMakeSystem.cmake if it is there
  210. std::string fpath = rootBin;
  211. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  212. {
  213. fpath += "/CMakeSystem.cmake";
  214. if(cmSystemTools::FileExists(fpath.c_str()))
  215. {
  216. mf->ReadListFile(0,fpath.c_str());
  217. }
  218. }
  219. // Load the CMakeDetermineSystem.cmake file and find out
  220. // what platform we are running on
  221. if (!mf->GetDefinition("CMAKE_SYSTEM"))
  222. {
  223. #if defined(_WIN32) && !defined(__CYGWIN__)
  224. /* Windows version number data. */
  225. OSVERSIONINFO osvi;
  226. ZeroMemory(&osvi, sizeof(osvi));
  227. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  228. GetVersionEx (&osvi);
  229. cmOStringStream windowsVersionString;
  230. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  231. windowsVersionString.str();
  232. mf->AddDefinition("CMAKE_HOST_SYSTEM_VERSION",
  233. windowsVersionString.str().c_str());
  234. #endif
  235. // Read the DetermineSystem file
  236. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  237. mf->ReadListFile(0, systemFile.c_str());
  238. // load the CMakeSystem.cmake from the binary directory
  239. // this file is configured by the CMakeDetermineSystem.cmake file
  240. fpath = rootBin;
  241. fpath += "/CMakeSystem.cmake";
  242. mf->ReadListFile(0,fpath.c_str());
  243. }
  244. std::map<cmStdString, bool> needTestLanguage;
  245. std::map<cmStdString, bool> needSetLanguageEnabledMaps;
  246. // foreach language
  247. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  248. // the compiler
  249. for(std::vector<std::string>::const_iterator l = languages.begin();
  250. l != languages.end(); ++l)
  251. {
  252. const char* lang = l->c_str();
  253. needSetLanguageEnabledMaps[lang] = false;
  254. if(*l == "NONE")
  255. {
  256. this->SetLanguageEnabled("NONE", mf);
  257. continue;
  258. }
  259. std::string loadedLang = "CMAKE_";
  260. loadedLang += lang;
  261. loadedLang += "_COMPILER_LOADED";
  262. // If the existing build tree was already configured with this
  263. // version of CMake then try to load the configured file first
  264. // to avoid duplicate compiler tests.
  265. unsigned int cacheMajor = mf->GetCacheMajorVersion();
  266. unsigned int cacheMinor = mf->GetCacheMinorVersion();
  267. unsigned int selfMajor = cmVersion::GetMajorVersion();
  268. unsigned int selfMinor = cmVersion::GetMinorVersion();
  269. if((this->CMakeInstance->GetIsInTryCompile() ||
  270. (selfMajor == cacheMajor && selfMinor == cacheMinor))
  271. && !mf->GetDefinition(loadedLang.c_str()))
  272. {
  273. fpath = rootBin;
  274. fpath += "/CMake";
  275. fpath += lang;
  276. fpath += "Compiler.cmake";
  277. if(cmSystemTools::FileExists(fpath.c_str()))
  278. {
  279. if(!mf->ReadListFile(0,fpath.c_str()))
  280. {
  281. cmSystemTools::Error("Could not find cmake module file:",
  282. fpath.c_str());
  283. }
  284. // if this file was found then the language was already determined
  285. // to be working
  286. needTestLanguage[lang] = false;
  287. this->SetLanguageEnabledFlag(lang, mf);
  288. needSetLanguageEnabledMaps[lang] = true;
  289. // this can only be called after loading CMake(LANG)Compiler.cmake
  290. }
  291. }
  292. if(!this->GetLanguageEnabled(lang) )
  293. {
  294. if (this->CMakeInstance->GetIsInTryCompile())
  295. {
  296. cmSystemTools::Error("This should not have happen. "
  297. "If you see this message, you are probably "
  298. "using a broken CMakeLists.txt file or a "
  299. "problematic release of CMake");
  300. }
  301. // if the CMake(LANG)Compiler.cmake file was not found then
  302. // load CMakeDetermine(LANG)Compiler.cmake
  303. std::string determineCompiler = "CMakeDetermine";
  304. determineCompiler += lang;
  305. determineCompiler += "Compiler.cmake";
  306. std::string determineFile =
  307. mf->GetModulesFile(determineCompiler.c_str());
  308. if(!mf->ReadListFile(0,determineFile.c_str()))
  309. {
  310. cmSystemTools::Error("Could not find cmake module file:",
  311. determineFile.c_str());
  312. }
  313. needTestLanguage[lang] = true;
  314. // Some generators like visual studio should not use the env variables
  315. // So the global generator can specify that in this variable
  316. if(!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV"))
  317. {
  318. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER
  319. // into the environment, in case user scripts want to run
  320. // configure, or sub cmakes
  321. std::string compilerName = "CMAKE_";
  322. compilerName += lang;
  323. compilerName += "_COMPILER";
  324. std::string compilerEnv = "CMAKE_";
  325. compilerEnv += lang;
  326. compilerEnv += "_COMPILER_ENV_VAR";
  327. std::string envVar = mf->GetRequiredDefinition(compilerEnv.c_str());
  328. std::string envVarValue =
  329. mf->GetRequiredDefinition(compilerName.c_str());
  330. std::string env = envVar;
  331. env += "=";
  332. env += envVarValue;
  333. cmSystemTools::PutEnv(env.c_str());
  334. }
  335. // if determineLanguage was called then load the file it
  336. // configures CMake(LANG)Compiler.cmake
  337. fpath = rootBin;
  338. fpath += "/CMake";
  339. fpath += lang;
  340. fpath += "Compiler.cmake";
  341. if(!mf->ReadListFile(0,fpath.c_str()))
  342. {
  343. cmSystemTools::Error("Could not find cmake module file:",
  344. fpath.c_str());
  345. }
  346. this->SetLanguageEnabledFlag(lang, mf);
  347. needSetLanguageEnabledMaps[lang] = true;
  348. // this can only be called after loading CMake(LANG)Compiler.cmake
  349. // the language must be enabled for try compile to work, but we do
  350. // not know if it is a working compiler yet so set the test language
  351. // flag
  352. needTestLanguage[lang] = true;
  353. } // end if(!this->GetLanguageEnabled(lang) )
  354. } // end loop over languages
  355. // **** Load the system specific information if not yet loaded
  356. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  357. {
  358. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  359. if(!mf->ReadListFile(0,fpath.c_str()))
  360. {
  361. cmSystemTools::Error("Could not find cmake module file:",
  362. fpath.c_str());
  363. }
  364. }
  365. // loop over languages again loading CMake(LANG)Information.cmake
  366. //
  367. for(std::vector<std::string>::const_iterator l = languages.begin();
  368. l != languages.end(); ++l)
  369. {
  370. const char* lang = l->c_str();
  371. if(*l == "NONE")
  372. {
  373. this->SetLanguageEnabled("NONE", mf);
  374. continue;
  375. }
  376. std::string langLoadedVar = "CMAKE_";
  377. langLoadedVar += lang;
  378. langLoadedVar += "_INFORMATION_LOADED";
  379. if (!mf->GetDefinition(langLoadedVar.c_str()))
  380. {
  381. fpath = "CMake";
  382. fpath += lang;
  383. fpath += "Information.cmake";
  384. fpath = mf->GetModulesFile(fpath.c_str());
  385. if(!mf->ReadListFile(0,fpath.c_str()))
  386. {
  387. cmSystemTools::Error("Could not find cmake module file:",
  388. fpath.c_str());
  389. }
  390. }
  391. if (needSetLanguageEnabledMaps[lang])
  392. {
  393. this->SetLanguageEnabledMaps(lang, mf);
  394. }
  395. // Test the compiler for the language just setup
  396. // At this point we should have enough info for a try compile
  397. // which is used in the backward stuff
  398. // If the language is untested then test it now with a try compile.
  399. if(needTestLanguage[lang])
  400. {
  401. if (!this->CMakeInstance->GetIsInTryCompile())
  402. {
  403. std::string testLang = "CMakeTest";
  404. testLang += lang;
  405. testLang += "Compiler.cmake";
  406. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  407. if(!mf->ReadListFile(0,ifpath.c_str()))
  408. {
  409. cmSystemTools::Error("Could not find cmake module file:",
  410. ifpath.c_str());
  411. }
  412. std::string compilerWorks = "CMAKE_";
  413. compilerWorks += lang;
  414. compilerWorks += "_COMPILER_WORKS";
  415. // if the compiler did not work, then remove the
  416. // CMake(LANG)Compiler.cmake file so that it will get tested the
  417. // next time cmake is run
  418. if(!mf->IsOn(compilerWorks.c_str()))
  419. {
  420. fpath = rootBin;
  421. fpath += "/CMake";
  422. fpath += lang;
  423. fpath += "Compiler.cmake";
  424. cmSystemTools::RemoveFile(fpath.c_str());
  425. }
  426. else
  427. {
  428. // load backwards compatibility stuff for C and CXX
  429. // for old versions of CMake ListFiles C and CXX had some
  430. // backwards compatibility files they have to load
  431. // These files have a bunch of try compiles in them so
  432. // should only be done
  433. const char* versionValue
  434. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  435. if (atof(versionValue) <= 1.4)
  436. {
  437. if(strcmp(lang, "C") == 0)
  438. {
  439. ifpath =
  440. mf->GetModulesFile("CMakeBackwardCompatibilityC.cmake");
  441. mf->ReadListFile(0,ifpath.c_str());
  442. }
  443. if(strcmp(lang, "CXX") == 0)
  444. {
  445. ifpath =
  446. mf->GetModulesFile("CMakeBackwardCompatibilityCXX.cmake");
  447. mf->ReadListFile(0,ifpath.c_str());
  448. }
  449. }
  450. }
  451. } // end if in try compile
  452. } // end need test language
  453. } // end for each language
  454. // Now load files that can override any settings on the platform or for
  455. // the project First load the project compatibility file if it is in
  456. // cmake
  457. std::string projectCompatibility = mf->GetDefinition("CMAKE_ROOT");
  458. projectCompatibility += "/Modules/";
  459. projectCompatibility += mf->GetSafeDefinition("PROJECT_NAME");
  460. projectCompatibility += "Compatibility.cmake";
  461. if(cmSystemTools::FileExists(projectCompatibility.c_str()))
  462. {
  463. mf->ReadListFile(0,projectCompatibility.c_str());
  464. }
  465. }
  466. //----------------------------------------------------------------------------
  467. const char*
  468. cmGlobalGenerator::GetLanguageOutputExtension(cmSourceFile const& source)
  469. {
  470. if(const char* lang = source.GetLanguage())
  471. {
  472. if(this->LanguageToOutputExtension.count(lang) > 0)
  473. {
  474. return this->LanguageToOutputExtension[lang].c_str();
  475. }
  476. }
  477. else
  478. {
  479. // if no language is found then check to see if it is already an
  480. // ouput extension for some language. In that case it should be ignored
  481. // and in this map, so it will not be compiled but will just be used.
  482. std::string const& ext = source.GetExtension();
  483. if(!ext.empty())
  484. {
  485. if(this->OutputExtensions.count(ext))
  486. {
  487. return ext.c_str();
  488. }
  489. }
  490. }
  491. return "";
  492. }
  493. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  494. {
  495. // if there is an extension and it starts with . then move past the
  496. // . because the extensions are not stored with a . in the map
  497. if(ext && *ext == '.')
  498. {
  499. ++ext;
  500. }
  501. if(this->ExtensionToLanguage.count(ext) > 0)
  502. {
  503. return this->ExtensionToLanguage[ext].c_str();
  504. }
  505. return 0;
  506. }
  507. /* SetLanguageEnabled() is now split in two parts:
  508. at first the enabled-flag is set. This can then be used in EnabledLanguage()
  509. for checking whether the language is already enabled. After setting this
  510. flag still the values from the cmake variables have to be copied into the
  511. internal maps, this is done in SetLanguageEnabledMaps() which is called
  512. after the system- and compiler specific files have been loaded.
  513. */
  514. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  515. {
  516. this->SetLanguageEnabledFlag(l, mf);
  517. this->SetLanguageEnabledMaps(l, mf);
  518. }
  519. void cmGlobalGenerator::SetLanguageEnabledFlag(const char* l, cmMakefile*)
  520. {
  521. this->LanguageEnabled[l] = true;
  522. }
  523. void cmGlobalGenerator::SetLanguageEnabledMaps(const char* l, cmMakefile* mf)
  524. {
  525. // use LanguageToLinkerPreference to detect whether this functions has
  526. // run before
  527. if (this->LanguageToLinkerPreference.find(l) !=
  528. this->LanguageToLinkerPreference.end())
  529. {
  530. return;
  531. }
  532. std::string linkerPrefVar = std::string("CMAKE_") +
  533. std::string(l) + std::string("_LINKER_PREFERENCE");
  534. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  535. int preference = 0;
  536. if(linkerPref)
  537. {
  538. if (sscanf(linkerPref, "%d", &preference)!=1)
  539. {
  540. // backward compatibility: before 2.6 LINKER_PREFERENCE
  541. // was either "None" or "Prefered", and only the first character was
  542. // tested. So if there is a custom language out there and it is
  543. // "Prefered", set its preference high
  544. if (linkerPref[0]=='P')
  545. {
  546. preference = 100;
  547. }
  548. else
  549. {
  550. preference = 0;
  551. }
  552. }
  553. }
  554. if (preference < 0)
  555. {
  556. std::string msg = linkerPrefVar;
  557. msg += " is negative, adjusting it to 0";
  558. cmSystemTools::Message(msg.c_str(), "Warning");
  559. preference = 0;
  560. }
  561. this->LanguageToLinkerPreference[l] = preference;
  562. std::string outputExtensionVar = std::string("CMAKE_") +
  563. std::string(l) + std::string("_OUTPUT_EXTENSION");
  564. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  565. if(outputExtension)
  566. {
  567. this->LanguageToOutputExtension[l] = outputExtension;
  568. this->OutputExtensions[outputExtension] = outputExtension;
  569. if(outputExtension[0] == '.')
  570. {
  571. this->OutputExtensions[outputExtension+1] = outputExtension+1;
  572. }
  573. }
  574. std::string extensionsVar = std::string("CMAKE_") +
  575. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  576. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  577. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  578. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  579. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  580. std::vector<std::string> extensionList;
  581. cmSystemTools::ExpandListArgument(exts, extensionList);
  582. for(std::vector<std::string>::iterator i = extensionList.begin();
  583. i != extensionList.end(); ++i)
  584. {
  585. this->ExtensionToLanguage[*i] = l;
  586. }
  587. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  588. for(std::vector<std::string>::iterator i = extensionList.begin();
  589. i != extensionList.end(); ++i)
  590. {
  591. this->IgnoreExtensions[*i] = true;
  592. }
  593. }
  594. bool cmGlobalGenerator::IgnoreFile(const char* l)
  595. {
  596. if(this->GetLanguageFromExtension(l))
  597. {
  598. return false;
  599. }
  600. return (this->IgnoreExtensions.count(l) > 0);
  601. }
  602. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  603. {
  604. return (this->LanguageEnabled.count(l) > 0);
  605. }
  606. void cmGlobalGenerator::ClearEnabledLanguages()
  607. {
  608. this->LanguageEnabled.clear();
  609. }
  610. bool cmGlobalGenerator::IsDependedOn(const char* project,
  611. cmTarget* targetIn)
  612. {
  613. // Get all local gens for this project
  614. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  615. // loop over local gens and get the targets for each one
  616. for(unsigned int i = 0; i < gens->size(); ++i)
  617. {
  618. cmTargets& targets = (*gens)[i]->GetMakefile()->GetTargets();
  619. for (cmTargets::iterator l = targets.begin();
  620. l != targets.end(); l++)
  621. {
  622. cmTarget& target = l->second;
  623. std::set<cmStdString>::const_iterator pos =
  624. target.GetUtilities().find(targetIn->GetName());
  625. if(pos != target.GetUtilities().end())
  626. {
  627. return true;
  628. }
  629. }
  630. }
  631. return false;
  632. }
  633. void cmGlobalGenerator::Configure()
  634. {
  635. // Delete any existing cmLocalGenerators
  636. unsigned int i;
  637. for (i = 0; i < this->LocalGenerators.size(); ++i)
  638. {
  639. delete this->LocalGenerators[i];
  640. }
  641. this->LocalGenerators.clear();
  642. this->TargetDependencies.clear();
  643. this->TotalTargets.clear();
  644. this->ImportedTotalTargets.clear();
  645. this->LocalGeneratorToTargetMap.clear();
  646. this->ProjectMap.clear();
  647. // start with this directory
  648. cmLocalGenerator *lg = this->CreateLocalGenerator();
  649. this->LocalGenerators.push_back(lg);
  650. // set the Start directories
  651. lg->GetMakefile()->SetStartDirectory
  652. (this->CMakeInstance->GetStartDirectory());
  653. lg->GetMakefile()->SetStartOutputDirectory
  654. (this->CMakeInstance->GetStartOutputDirectory());
  655. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  656. // now do it
  657. lg->Configure();
  658. // update the cache entry for the number of local generators, this is used
  659. // for progress
  660. char num[100];
  661. sprintf(num,"%d",static_cast<int>(this->LocalGenerators.size()));
  662. this->GetCMakeInstance()->AddCacheEntry
  663. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  664. "number of local generators", cmCacheManager::INTERNAL);
  665. // check for link libraries and include directories containing "NOTFOUND"
  666. // and for infinite loops
  667. this->CheckLocalGenerators();
  668. // at this point this->LocalGenerators has been filled,
  669. // so create the map from project name to vector of local generators
  670. this->FillProjectMap();
  671. // Create a map from local generator to the complete set of targets
  672. // it builds by default.
  673. this->FillLocalGeneratorToTargetMap();
  674. if ( !this->CMakeInstance->GetScriptMode() )
  675. {
  676. this->CMakeInstance->UpdateProgress("Configuring done", -1);
  677. }
  678. }
  679. void cmGlobalGenerator::Generate()
  680. {
  681. // For each existing cmLocalGenerator
  682. unsigned int i;
  683. // Consolidate global targets
  684. cmTargets globalTargets;
  685. this->CreateDefaultGlobalTargets(&globalTargets);
  686. for (i = 0; i < this->LocalGenerators.size(); ++i)
  687. {
  688. cmTargets* targets =
  689. &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  690. cmTargets::iterator tarIt;
  691. for ( tarIt = targets->begin(); tarIt != targets->end(); ++ tarIt )
  692. {
  693. if ( tarIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  694. {
  695. globalTargets[tarIt->first] = tarIt->second;
  696. }
  697. }
  698. }
  699. for (i = 0; i < this->LocalGenerators.size(); ++i)
  700. {
  701. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  702. cmTargets* targets = &(mf->GetTargets());
  703. cmTargets::iterator tit;
  704. for ( tit = globalTargets.begin(); tit != globalTargets.end(); ++ tit )
  705. {
  706. (*targets)[tit->first] = tit->second;
  707. (*targets)[tit->first].SetMakefile(mf);
  708. }
  709. }
  710. // Add generator specific helper commands
  711. for (i = 0; i < this->LocalGenerators.size(); ++i)
  712. {
  713. this->LocalGenerators[i]->AddHelperCommands();
  714. }
  715. // Trace the dependencies, after that no custom commands should be added
  716. // because their dependencies might not be handled correctly
  717. for (i = 0; i < this->LocalGenerators.size(); ++i)
  718. {
  719. this->LocalGenerators[i]->TraceDependencies();
  720. }
  721. // Compute the manifest of main targets generated.
  722. for (i = 0; i < this->LocalGenerators.size(); ++i)
  723. {
  724. this->LocalGenerators[i]->GenerateTargetManifest(this->TargetManifest);
  725. }
  726. // Generate project files
  727. for (i = 0; i < this->LocalGenerators.size(); ++i)
  728. {
  729. this->SetCurrentLocalGenerator(this->LocalGenerators[i]);
  730. this->LocalGenerators[i]->Generate();
  731. this->LocalGenerators[i]->GenerateInstallRules();
  732. this->LocalGenerators[i]->GenerateTestFiles();
  733. this->CMakeInstance->UpdateProgress("Generating",
  734. (i+1.0f)/this->LocalGenerators.size());
  735. }
  736. this->SetCurrentLocalGenerator(0);
  737. if (this->ExtraGenerator != 0)
  738. {
  739. this->ExtraGenerator->Generate();
  740. }
  741. this->CMakeInstance->UpdateProgress("Generating done", -1);
  742. }
  743. void cmGlobalGenerator::CheckLocalGenerators()
  744. {
  745. std::map<cmStdString, cmStdString> notFoundMap;
  746. // std::set<cmStdString> notFoundMap;
  747. // after it is all done do a ConfigureFinalPass
  748. cmCacheManager* manager = 0;
  749. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  750. {
  751. manager = this->LocalGenerators[i]->GetMakefile()->GetCacheManager();
  752. this->LocalGenerators[i]->ConfigureFinalPass();
  753. const cmTargets & targets =
  754. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  755. for (cmTargets::const_iterator l = targets.begin();
  756. l != targets.end(); l++)
  757. {
  758. const cmTarget::LinkLibraryVectorType& libs=l->second.GetLinkLibraries();
  759. for(cmTarget::LinkLibraryVectorType::const_iterator lib = libs.begin();
  760. lib != libs.end(); ++lib)
  761. {
  762. if(lib->first.size() > 9 &&
  763. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  764. {
  765. std::string varName = lib->first.substr(0, lib->first.size()-9);
  766. cmCacheManager::CacheIterator it =
  767. manager->GetCacheIterator(varName.c_str());
  768. if(it.GetPropertyAsBool("ADVANCED"))
  769. {
  770. varName += " (ADVANCED)";
  771. }
  772. std::string text = notFoundMap[varName];
  773. text += "\n linked by target \"";
  774. text += l->second.GetName();
  775. text += "\" in directory ";
  776. text+=this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  777. notFoundMap[varName] = text;
  778. }
  779. }
  780. }
  781. const std::vector<std::string>& incs =
  782. this->LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  783. for( std::vector<std::string>::const_iterator incDir = incs.begin();
  784. incDir != incs.end(); ++incDir)
  785. {
  786. if(incDir->size() > 9 &&
  787. cmSystemTools::IsNOTFOUND(incDir->c_str()))
  788. {
  789. std::string varName = incDir->substr(0, incDir->size()-9);
  790. cmCacheManager::CacheIterator it =
  791. manager->GetCacheIterator(varName.c_str());
  792. if(it.GetPropertyAsBool("ADVANCED"))
  793. {
  794. varName += " (ADVANCED)";
  795. }
  796. std::string text = notFoundMap[varName];
  797. text += "\n used as include directory in directory ";
  798. text += this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  799. notFoundMap[varName] = text;
  800. }
  801. }
  802. this->CMakeInstance->UpdateProgress
  803. ("Configuring", 0.9f+0.1f*(i+1.0f)/this->LocalGenerators.size());
  804. this->LocalGenerators[i]->GetMakefile()->CheckInfiniteLoops();
  805. }
  806. if(notFoundMap.size())
  807. {
  808. std::string notFoundVars;
  809. for(std::map<cmStdString, cmStdString>::const_iterator
  810. ii = notFoundMap.begin();
  811. ii != notFoundMap.end();
  812. ++ii)
  813. {
  814. notFoundVars += ii->first;
  815. notFoundVars += ii->second;
  816. notFoundVars += "\n";
  817. }
  818. cmSystemTools::Error("The following variables are used in this project, "
  819. "but they are set to NOTFOUND.\n"
  820. "Please set them or make sure they are set and "
  821. "tested correctly in the CMake files:\n",
  822. notFoundVars.c_str());
  823. }
  824. }
  825. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  826. const char *projectName,
  827. const char *target,
  828. std::string *output, cmMakefile *mf)
  829. {
  830. std::string makeCommand = this->CMakeInstance->
  831. GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  832. if(makeCommand.size() == 0)
  833. {
  834. cmSystemTools::Error(
  835. "Generator cannot find the appropriate make command.");
  836. return 1;
  837. }
  838. std::string newTarget;
  839. if (target && strlen(target))
  840. {
  841. newTarget += target;
  842. #if 0
  843. #if defined(_WIN32) || defined(__CYGWIN__)
  844. std::string tmp = target;
  845. // if the target does not already end in . something
  846. // then assume .exe
  847. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  848. {
  849. newTarget += ".exe";
  850. }
  851. #endif // WIN32
  852. #endif
  853. }
  854. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  855. return this->Build(srcdir,bindir,projectName,
  856. newTarget.c_str(),
  857. output,makeCommand.c_str(),config,false,true,
  858. this->TryCompileTimeout);
  859. }
  860. std::string cmGlobalGenerator
  861. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  862. const char* additionalOptions, const char *targetName,
  863. const char* config, bool ignoreErrors, bool)
  864. {
  865. // Project name and config are not used yet.
  866. (void)projectName;
  867. (void)config;
  868. std::string makeCommand =
  869. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  870. // Since we have full control over the invocation of nmake, let us
  871. // make it quiet.
  872. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  873. {
  874. makeCommand += " /NOLOGO ";
  875. }
  876. if ( ignoreErrors )
  877. {
  878. makeCommand += " -i";
  879. }
  880. if ( additionalOptions )
  881. {
  882. makeCommand += " ";
  883. makeCommand += additionalOptions;
  884. }
  885. if ( targetName )
  886. {
  887. makeCommand += " ";
  888. makeCommand += targetName;
  889. }
  890. return makeCommand;
  891. }
  892. int cmGlobalGenerator::Build(
  893. const char *, const char *bindir,
  894. const char *projectName, const char *target,
  895. std::string *output,
  896. const char *makeCommandCSTR,
  897. const char *config,
  898. bool clean, bool fast,
  899. double timeout)
  900. {
  901. *output += "\nTesting TryCompileWithoutMakefile\n";
  902. /**
  903. * Run an executable command and put the stdout in output.
  904. */
  905. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  906. cmSystemTools::ChangeDirectory(bindir);
  907. int retVal;
  908. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  909. cmSystemTools::SetRunCommandHideConsole(true);
  910. // should we do a clean first?
  911. if (clean)
  912. {
  913. std::string cleanCommand =
  914. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  915. 0, "clean", config, false, fast);
  916. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), output,
  917. &retVal, 0, false, timeout))
  918. {
  919. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  920. cmSystemTools::Error("Generator: execution of make clean failed.");
  921. if (output)
  922. {
  923. *output += "\nGenerator: execution of make clean failed.\n";
  924. }
  925. // return to the original directory
  926. cmSystemTools::ChangeDirectory(cwd.c_str());
  927. return 1;
  928. }
  929. }
  930. // now build
  931. std::string makeCommand =
  932. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  933. 0, target, config, false, fast);
  934. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  935. &retVal, 0, false, timeout))
  936. {
  937. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  938. cmSystemTools::Error
  939. ("Generator: execution of make failed. Make command was: ",
  940. makeCommand.c_str());
  941. if (output)
  942. {
  943. *output += "\nGenerator: execution of make failed. Make command was: "
  944. + makeCommand + "\n";
  945. }
  946. // return to the original directory
  947. cmSystemTools::ChangeDirectory(cwd.c_str());
  948. return 1;
  949. }
  950. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  951. // The SGI MipsPro 7.3 compiler does not return an error code when
  952. // the source has a #error in it! This is a work-around for such
  953. // compilers.
  954. if((retVal == 0) && (output->find("#error") != std::string::npos))
  955. {
  956. retVal = 1;
  957. }
  958. cmSystemTools::ChangeDirectory(cwd.c_str());
  959. return retVal;
  960. }
  961. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  962. {
  963. this->LocalGenerators.push_back(lg);
  964. // update progress
  965. // estimate how many lg there will be
  966. const char *numGenC =
  967. this->CMakeInstance->GetCacheManager()->GetCacheValue
  968. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  969. if (!numGenC)
  970. {
  971. return;
  972. }
  973. int numGen = atoi(numGenC);
  974. float prog = 0.9f*this->LocalGenerators.size()/numGen;
  975. if (prog > 0.9f)
  976. {
  977. prog = 0.9f;
  978. }
  979. this->CMakeInstance->UpdateProgress("Configuring", prog);
  980. }
  981. void cmGlobalGenerator::AddInstallComponent(const char* component)
  982. {
  983. if(component && *component)
  984. {
  985. this->InstallComponents.insert(component);
  986. }
  987. }
  988. void cmGlobalGenerator::AddTargetToExports(const char* exportSetName,
  989. cmTarget* target,
  990. cmInstallTargetGenerator* archive,
  991. cmInstallTargetGenerator* runTime,
  992. cmInstallTargetGenerator* library)
  993. {
  994. if ((exportSetName) && (*exportSetName) && (target))
  995. {
  996. cmTargetExport* te = new cmTargetExport(target, archive, runTime, library);
  997. this->ExportSets[exportSetName].push_back(te);
  998. }
  999. }
  1000. const std::vector<cmTargetExport*>* cmGlobalGenerator::GetExportSet(
  1001. const char* name) const
  1002. {
  1003. std::map<cmStdString, std::vector<cmTargetExport*> >::const_iterator
  1004. exportSetIt = this->ExportSets.find(name);
  1005. if (exportSetIt != this->ExportSets.end())
  1006. {
  1007. return &exportSetIt->second;
  1008. }
  1009. return 0;
  1010. }
  1011. void cmGlobalGenerator::EnableInstallTarget()
  1012. {
  1013. this->InstallTargetEnabled = true;
  1014. }
  1015. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  1016. {
  1017. cmLocalGenerator *lg = new cmLocalGenerator;
  1018. lg->SetGlobalGenerator(this);
  1019. return lg;
  1020. }
  1021. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  1022. {
  1023. std::string cfp = gen->GetCMakeInstance()->GetHomeOutputDirectory();
  1024. cfp += cmake::GetCMakeFilesDirectory();
  1025. this->SetConfiguredFilesPath(cfp.c_str());
  1026. const char* make =
  1027. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  1028. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  1029. "make program",
  1030. cmCacheManager::FILEPATH);
  1031. // copy the enabled languages
  1032. this->LanguageEnabled = gen->LanguageEnabled;
  1033. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  1034. this->IgnoreExtensions = gen->IgnoreExtensions;
  1035. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  1036. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  1037. this->OutputExtensions = gen->OutputExtensions;
  1038. }
  1039. //----------------------------------------------------------------------------
  1040. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  1041. {
  1042. entry.name = this->GetName();
  1043. entry.brief = "";
  1044. entry.full = "";
  1045. }
  1046. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1047. cmLocalGenerator* gen)
  1048. {
  1049. if(!gen || gen == root)
  1050. {
  1051. // No directory excludes itself.
  1052. return false;
  1053. }
  1054. if(gen->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1055. {
  1056. // This directory is excluded from its parent.
  1057. return true;
  1058. }
  1059. // This directory is included in its parent. Check whether the
  1060. // parent is excluded.
  1061. return this->IsExcluded(root, gen->GetParent());
  1062. }
  1063. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1064. cmTarget& target)
  1065. {
  1066. if(target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1067. {
  1068. // This target is excluded from its directory.
  1069. return true;
  1070. }
  1071. else
  1072. {
  1073. // This target is included in its directory. Check whether the
  1074. // directory is excluded.
  1075. return this->IsExcluded(root, target.GetMakefile()->GetLocalGenerator());
  1076. }
  1077. }
  1078. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  1079. {
  1080. for(std::map<cmStdString, bool>::iterator i =
  1081. this->LanguageEnabled.begin(); i != this->LanguageEnabled.end(); ++i)
  1082. {
  1083. lang.push_back(i->first);
  1084. }
  1085. }
  1086. int cmGlobalGenerator::GetLinkerPreference(const char* lang)
  1087. {
  1088. std::map<cmStdString, int>::const_iterator it =
  1089. this->LanguageToLinkerPreference.find(lang);
  1090. if (it != this->LanguageToLinkerPreference.end())
  1091. {
  1092. return it->second;
  1093. }
  1094. return 0;
  1095. }
  1096. void cmGlobalGenerator::FillProjectMap()
  1097. {
  1098. this->ProjectMap.clear(); // make sure we start with a clean map
  1099. unsigned int i;
  1100. for(i = 0; i < this->LocalGenerators.size(); ++i)
  1101. {
  1102. // for each local generator add all projects
  1103. cmLocalGenerator *lg = this->LocalGenerators[i];
  1104. std::string name;
  1105. do
  1106. {
  1107. if (name != lg->GetMakefile()->GetProjectName())
  1108. {
  1109. name = lg->GetMakefile()->GetProjectName();
  1110. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  1111. }
  1112. lg = lg->GetParent();
  1113. }
  1114. while (lg);
  1115. }
  1116. }
  1117. // Build a map that contains a the set of targets used by each local
  1118. // generator directory level.
  1119. void cmGlobalGenerator::FillLocalGeneratorToTargetMap()
  1120. {
  1121. // Loop over all targets in all local generators.
  1122. for(std::vector<cmLocalGenerator*>::const_iterator
  1123. lgi = this->LocalGenerators.begin();
  1124. lgi != this->LocalGenerators.end(); ++lgi)
  1125. {
  1126. cmLocalGenerator* lg = *lgi;
  1127. cmMakefile* mf = lg->GetMakefile();
  1128. cmTargets& targets = mf->GetTargets();
  1129. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  1130. {
  1131. cmTarget& target = t->second;
  1132. // Consider the directory containing the target and all its
  1133. // parents until something excludes the target.
  1134. for(cmLocalGenerator* clg = lg; clg && !this->IsExcluded(clg, target);
  1135. clg = clg->GetParent())
  1136. {
  1137. // This local generator includes the target.
  1138. std::set<cmTarget*>& targetSet =
  1139. this->LocalGeneratorToTargetMap[clg];
  1140. targetSet.insert(&target);
  1141. // Add dependencies of the included target. An excluded
  1142. // target may still be included if it is a dependency of a
  1143. // non-excluded target.
  1144. std::vector<cmTarget *>& tgtdeps = this->GetTargetDepends(target);
  1145. targetSet.insert(tgtdeps.begin(), tgtdeps.end());
  1146. }
  1147. }
  1148. }
  1149. }
  1150. ///! Find a local generator by its startdirectory
  1151. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(const char* start_dir)
  1152. {
  1153. std::vector<cmLocalGenerator*>* gens = &this->LocalGenerators;
  1154. for(unsigned int i = 0; i < gens->size(); ++i)
  1155. {
  1156. std::string sd = (*gens)[i]->GetMakefile()->GetStartDirectory();
  1157. if (sd == start_dir)
  1158. {
  1159. return (*gens)[i];
  1160. }
  1161. }
  1162. return 0;
  1163. }
  1164. cmTarget* cmGlobalGenerator::FindTarget(const char* project,
  1165. const char* name,
  1166. bool useImportedTargets)
  1167. {
  1168. // if project specific
  1169. if(project)
  1170. {
  1171. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  1172. for(unsigned int i = 0; i < gens->size(); ++i)
  1173. {
  1174. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name,
  1175. useImportedTargets);
  1176. if(ret)
  1177. {
  1178. return ret;
  1179. }
  1180. }
  1181. }
  1182. // if all projects/directories
  1183. else
  1184. {
  1185. std::map<cmStdString,cmTarget *>::iterator i =
  1186. this->TotalTargets.find ( name );
  1187. if ( i != this->TotalTargets.end() )
  1188. {
  1189. return i->second;
  1190. }
  1191. if ( useImportedTargets )
  1192. {
  1193. std::map<cmStdString,cmTarget *>::iterator importedTarget =
  1194. this->ImportedTotalTargets.find ( name );
  1195. if ( importedTarget != this->ImportedTotalTargets.end() )
  1196. {
  1197. return importedTarget->second;
  1198. }
  1199. }
  1200. }
  1201. return 0;
  1202. }
  1203. inline std::string removeQuotes(const std::string& s)
  1204. {
  1205. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1206. {
  1207. return s.substr(1, s.size()-2);
  1208. }
  1209. return s;
  1210. }
  1211. void cmGlobalGenerator::SetCMakeInstance(cmake* cm)
  1212. {
  1213. // Store a pointer to the cmake object instance.
  1214. this->CMakeInstance = cm;
  1215. }
  1216. void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
  1217. {
  1218. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  1219. const char* cmakeCfgIntDir = this->GetCMakeCFGInitDirectory();
  1220. const char* cmakeCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1221. // CPack
  1222. cmCustomCommandLines cpackCommandLines;
  1223. std::vector<std::string> depends;
  1224. cmCustomCommandLine singleLine;
  1225. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1226. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1227. {
  1228. singleLine.push_back("-C");
  1229. singleLine.push_back(cmakeCfgIntDir);
  1230. }
  1231. singleLine.push_back("--config");
  1232. std::string configFile = mf->GetStartOutputDirectory();;
  1233. configFile += "/CPackConfig.cmake";
  1234. singleLine.push_back(configFile);
  1235. cpackCommandLines.push_back(singleLine);
  1236. if ( this->GetPreinstallTargetName() )
  1237. {
  1238. depends.push_back("preinstall");
  1239. }
  1240. if(cmSystemTools::FileExists(configFile.c_str()))
  1241. {
  1242. (*targets)[this->GetPackageTargetName()]
  1243. = this->CreateGlobalTarget(this->GetPackageTargetName(),
  1244. "Run CPack packaging tool...",
  1245. &cpackCommandLines, depends);
  1246. }
  1247. // CPack source
  1248. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1249. if ( packageSourceTargetName )
  1250. {
  1251. cpackCommandLines.erase(cpackCommandLines.begin(),
  1252. cpackCommandLines.end());
  1253. singleLine.erase(singleLine.begin(), singleLine.end());
  1254. depends.erase(depends.begin(), depends.end());
  1255. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1256. singleLine.push_back("--config");
  1257. configFile = mf->GetStartOutputDirectory();;
  1258. configFile += "/CPackSourceConfig.cmake";
  1259. if(cmSystemTools::FileExists(configFile.c_str()))
  1260. {
  1261. singleLine.push_back(configFile);
  1262. cpackCommandLines.push_back(singleLine);
  1263. (*targets)[packageSourceTargetName]
  1264. = this->CreateGlobalTarget(packageSourceTargetName,
  1265. "Run CPack packaging tool for source...",
  1266. &cpackCommandLines, depends);
  1267. }
  1268. }
  1269. // Test
  1270. if(mf->IsOn("CMAKE_TESTING_ENABLED"))
  1271. {
  1272. cpackCommandLines.erase(cpackCommandLines.begin(),
  1273. cpackCommandLines.end());
  1274. singleLine.erase(singleLine.begin(), singleLine.end());
  1275. depends.erase(depends.begin(), depends.end());
  1276. singleLine.push_back(this->GetCMakeInstance()->GetCTestCommand());
  1277. singleLine.push_back("--force-new-ctest-process");
  1278. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1279. {
  1280. singleLine.push_back("-C");
  1281. singleLine.push_back(cmakeCfgIntDir);
  1282. }
  1283. else // TODO: This is a hack. Should be something to do with the generator
  1284. {
  1285. singleLine.push_back("$(ARGS)");
  1286. }
  1287. cpackCommandLines.push_back(singleLine);
  1288. (*targets)[this->GetTestTargetName()]
  1289. = this->CreateGlobalTarget(this->GetTestTargetName(),
  1290. "Running tests...", &cpackCommandLines, depends);
  1291. }
  1292. //Edit Cache
  1293. const char* editCacheTargetName = this->GetEditCacheTargetName();
  1294. if ( editCacheTargetName )
  1295. {
  1296. cpackCommandLines.erase(cpackCommandLines.begin(),
  1297. cpackCommandLines.end());
  1298. singleLine.erase(singleLine.begin(), singleLine.end());
  1299. depends.erase(depends.begin(), depends.end());
  1300. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1301. // Otherwise default to the interactive command-line interface.
  1302. if(mf->GetDefinition("CMAKE_EDIT_COMMAND"))
  1303. {
  1304. singleLine.push_back(mf->GetDefinition("CMAKE_EDIT_COMMAND"));
  1305. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1306. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1307. cpackCommandLines.push_back(singleLine);
  1308. (*targets)[editCacheTargetName] =
  1309. this->CreateGlobalTarget(
  1310. editCacheTargetName, "Running CMake cache editor...",
  1311. &cpackCommandLines, depends);
  1312. }
  1313. else
  1314. {
  1315. singleLine.push_back(cmakeCommand);
  1316. singleLine.push_back("-i");
  1317. singleLine.push_back(".");
  1318. cpackCommandLines.push_back(singleLine);
  1319. (*targets)[editCacheTargetName] =
  1320. this->CreateGlobalTarget(
  1321. editCacheTargetName,
  1322. "Running interactive CMake command-line interface...",
  1323. &cpackCommandLines, depends);
  1324. }
  1325. }
  1326. //Rebuild Cache
  1327. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  1328. if ( rebuildCacheTargetName )
  1329. {
  1330. cpackCommandLines.erase(cpackCommandLines.begin(),
  1331. cpackCommandLines.end());
  1332. singleLine.erase(singleLine.begin(), singleLine.end());
  1333. depends.erase(depends.begin(), depends.end());
  1334. singleLine.push_back(cmakeCommand);
  1335. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1336. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1337. cpackCommandLines.push_back(singleLine);
  1338. (*targets)[rebuildCacheTargetName] =
  1339. this->CreateGlobalTarget(
  1340. rebuildCacheTargetName, "Running CMake to regenerate build system...",
  1341. &cpackCommandLines, depends);
  1342. }
  1343. //Install
  1344. if(this->InstallTargetEnabled)
  1345. {
  1346. if(!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.')
  1347. {
  1348. std::set<cmStdString>* componentsSet = &this->InstallComponents;
  1349. cpackCommandLines.erase(cpackCommandLines.begin(),
  1350. cpackCommandLines.end());
  1351. depends.erase(depends.begin(), depends.end());
  1352. cmOStringStream ostr;
  1353. if ( componentsSet->size() > 0 )
  1354. {
  1355. ostr << "Available install components are:";
  1356. std::set<cmStdString>::iterator it;
  1357. for (
  1358. it = componentsSet->begin();
  1359. it != componentsSet->end();
  1360. ++ it )
  1361. {
  1362. ostr << " \"" << it->c_str() << "\"";
  1363. }
  1364. }
  1365. else
  1366. {
  1367. ostr << "Only default component available";
  1368. }
  1369. singleLine.push_back(ostr.str().c_str());
  1370. (*targets)["list_install_components"]
  1371. = this->CreateGlobalTarget("list_install_components",
  1372. ostr.str().c_str(),
  1373. &cpackCommandLines, depends);
  1374. }
  1375. std::string cmd;
  1376. cpackCommandLines.erase(cpackCommandLines.begin(),
  1377. cpackCommandLines.end());
  1378. singleLine.erase(singleLine.begin(), singleLine.end());
  1379. depends.erase(depends.begin(), depends.end());
  1380. if ( this->GetPreinstallTargetName() )
  1381. {
  1382. depends.push_back(this->GetPreinstallTargetName());
  1383. }
  1384. else
  1385. {
  1386. const char* noall =
  1387. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1388. if(!noall || cmSystemTools::IsOff(noall))
  1389. {
  1390. depends.push_back(this->GetAllTargetName());
  1391. }
  1392. }
  1393. if(mf->GetDefinition("CMake_BINARY_DIR"))
  1394. {
  1395. // We are building CMake itself. We cannot use the original
  1396. // executable to install over itself.
  1397. cmd = mf->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1398. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1399. {
  1400. cmd += "/";
  1401. cmd += cmakeCfgIntDir;
  1402. }
  1403. cmd += "/cmake";
  1404. }
  1405. else
  1406. {
  1407. cmd = cmakeCommand;
  1408. }
  1409. singleLine.push_back(cmd.c_str());
  1410. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1411. {
  1412. std::string cfgArg = "-DBUILD_TYPE=";
  1413. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  1414. singleLine.push_back(cfgArg);
  1415. }
  1416. singleLine.push_back("-P");
  1417. singleLine.push_back("cmake_install.cmake");
  1418. cpackCommandLines.push_back(singleLine);
  1419. (*targets)[this->GetInstallTargetName()] =
  1420. this->CreateGlobalTarget(
  1421. this->GetInstallTargetName(), "Install the project...",
  1422. &cpackCommandLines, depends);
  1423. // install_local
  1424. if(const char* install_local = this->GetInstallLocalTargetName())
  1425. {
  1426. cmCustomCommandLine localCmdLine = singleLine;
  1427. localCmdLine.insert(localCmdLine.begin()+1,
  1428. "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  1429. cpackCommandLines.erase(cpackCommandLines.begin(),
  1430. cpackCommandLines.end());
  1431. cpackCommandLines.push_back(localCmdLine);
  1432. (*targets)[install_local] =
  1433. this->CreateGlobalTarget(
  1434. install_local, "Installing only the local directory...",
  1435. &cpackCommandLines, depends);
  1436. }
  1437. // install_strip
  1438. const char* install_strip = this->GetInstallStripTargetName();
  1439. if((install_strip !=0) && (mf->IsSet("CMAKE_STRIP")))
  1440. {
  1441. cmCustomCommandLine stripCmdLine = singleLine;
  1442. stripCmdLine.insert(stripCmdLine.begin()+1,"-DCMAKE_INSTALL_DO_STRIP=1");
  1443. cpackCommandLines.erase(cpackCommandLines.begin(),
  1444. cpackCommandLines.end());
  1445. cpackCommandLines.push_back(stripCmdLine);
  1446. (*targets)[install_strip] =
  1447. this->CreateGlobalTarget(
  1448. install_strip, "Installing the project stripped...",
  1449. &cpackCommandLines, depends);
  1450. }
  1451. }
  1452. }
  1453. cmTarget cmGlobalGenerator::CreateGlobalTarget(
  1454. const char* name, const char* message,
  1455. const cmCustomCommandLines* commandLines,
  1456. std::vector<std::string> depends,
  1457. bool depends_on_all /* = false */)
  1458. {
  1459. // Package
  1460. cmTarget target;
  1461. target.GetProperties().SetCMakeInstance(this->CMakeInstance);
  1462. target.SetType(cmTarget::GLOBAL_TARGET, name);
  1463. target.SetProperty("EXCLUDE_FROM_ALL","TRUE");
  1464. std::vector<std::string> no_outputs;
  1465. std::vector<std::string> no_depends;
  1466. // Store the custom command in the target.
  1467. cmCustomCommand cc(no_outputs, no_depends, *commandLines, 0, 0);
  1468. target.GetPostBuildCommands().push_back(cc);
  1469. target.SetProperty("EchoString", message);
  1470. if ( depends_on_all )
  1471. {
  1472. target.AddUtility("all");
  1473. }
  1474. std::vector<std::string>::iterator dit;
  1475. for ( dit = depends.begin(); dit != depends.end(); ++ dit )
  1476. {
  1477. target.AddUtility(dit->c_str());
  1478. }
  1479. return target;
  1480. }
  1481. //----------------------------------------------------------------------------
  1482. void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
  1483. const char*, std::string&)
  1484. {
  1485. // Subclasses that support multiple configurations should implement
  1486. // this method to append the subdirectory for the given build
  1487. // configuration.
  1488. }
  1489. //----------------------------------------------------------------------------
  1490. void cmGlobalGenerator::CheckMultipleOutputs(cmMakefile*, bool)
  1491. {
  1492. // Only certain generators need this check. They define this
  1493. // method.
  1494. }
  1495. //----------------------------------------------------------------------------
  1496. std::vector<cmTarget *>& cmGlobalGenerator
  1497. ::GetTargetDepends(cmTarget& target)
  1498. {
  1499. // if the depends are already in the map then return
  1500. std::map<cmStdString, std::vector<cmTarget *> >::iterator tgtI =
  1501. this->TargetDependencies.find(target.GetName());
  1502. if (tgtI != this->TargetDependencies.end())
  1503. {
  1504. return tgtI->second;
  1505. }
  1506. // A target should not depend on itself.
  1507. std::set<cmStdString> emitted;
  1508. emitted.insert(target.GetName());
  1509. // the vector of results
  1510. std::vector<cmTarget *>& result =
  1511. this->TargetDependencies[target.GetName()];
  1512. // Loop over all library dependencies but not for static libs
  1513. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  1514. {
  1515. const cmTarget::LinkLibraryVectorType& tlibs = target.GetLinkLibraries();
  1516. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  1517. lib != tlibs.end(); ++lib)
  1518. {
  1519. // Don't emit the same library twice for this target.
  1520. if(emitted.insert(lib->first).second)
  1521. {
  1522. cmTarget *target2 =
  1523. target.GetMakefile()->FindTarget(lib->first.c_str(), false);
  1524. // search each local generator until a match is found
  1525. if (!target2)
  1526. {
  1527. target2 = this->FindTarget(0,lib->first.c_str(), false);
  1528. }
  1529. // if a match was found then ...
  1530. if (target2)
  1531. {
  1532. // Add this dependency.
  1533. result.push_back(target2);
  1534. }
  1535. }
  1536. }
  1537. }
  1538. // Loop over all utility dependencies.
  1539. const std::set<cmStdString>& tutils = target.GetUtilities();
  1540. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1541. util != tutils.end(); ++util)
  1542. {
  1543. // Don't emit the same utility twice for this target.
  1544. if(emitted.insert(*util).second)
  1545. {
  1546. cmTarget *target2=target.GetMakefile()->FindTarget(util->c_str(), false);
  1547. // search each local generator until a match is found
  1548. if (!target2)
  1549. {
  1550. target2 = this->FindTarget(0,util->c_str(), false);
  1551. }
  1552. // if a match was found then ...
  1553. if (target2)
  1554. {
  1555. // Add this dependency.
  1556. result.push_back(target2);
  1557. }
  1558. }
  1559. }
  1560. return result;
  1561. }
  1562. void cmGlobalGenerator::AddTarget(cmTargets::value_type &v)
  1563. {
  1564. if (v.second.IsImported())
  1565. {
  1566. this->ImportedTotalTargets[v.first] = &v.second;
  1567. }
  1568. else
  1569. {
  1570. this->TotalTargets[v.first] = &v.second;
  1571. }
  1572. }
  1573. void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
  1574. cmExternalMakefileProjectGenerator *extraGenerator)
  1575. {
  1576. this->ExtraGenerator = extraGenerator;
  1577. if (this->ExtraGenerator!=0)
  1578. {
  1579. this->ExtraGenerator->SetGlobalGenerator(this);
  1580. }
  1581. }
  1582. const char* cmGlobalGenerator::GetExtraGeneratorName() const
  1583. {
  1584. return this->ExtraGenerator==0 ? 0 : this->ExtraGenerator->GetName();
  1585. }