cmGlobalGenerator.cxx 48 KB

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