cmGlobalGenerator.cxx 50 KB

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