cmGlobalGenerator.cxx 49 KB

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