cmGlobalGenerator.cxx 57 KB

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