cmGlobalGenerator.cxx 44 KB

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