cmGlobalGenerator.cxx 52 KB

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