cmGlobalGenerator.cxx 50 KB

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