cmGlobalGenerator.cxx 48 KB

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