cmGlobalGenerator.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. void cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
  91. cmMakefile *mf)
  92. {
  93. if(languages.size() == 0)
  94. {
  95. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  96. cmSystemTools::SetFatalErrorOccured();
  97. return;
  98. }
  99. mf->AddDefinition("RUN_CONFIGURE", true);
  100. bool needTestLanguage = false;
  101. std::string rootBin = mf->GetHomeOutputDirectory();
  102. // If the configuration files path has been set,
  103. // then we are in a try compile and need to copy the enable language
  104. // files into the try compile directory
  105. if(m_ConfiguredFilesPath.size())
  106. {
  107. std::string src = m_ConfiguredFilesPath;
  108. src += "/CMakeSystem.cmake";
  109. std::string dst = rootBin;
  110. dst += "/CMakeSystem.cmake";
  111. cmSystemTools::CopyFileIfDifferent(src.c_str(), dst.c_str());
  112. for(std::vector<std::string>::const_iterator l = languages.begin();
  113. l != languages.end(); ++l)
  114. {
  115. if(*l == "NONE")
  116. {
  117. this->SetLanguageEnabled("NONE", mf);
  118. continue;
  119. }
  120. const char* lang = l->c_str();
  121. std::string src2 = m_ConfiguredFilesPath;
  122. src2 += "/CMake";
  123. src2 += lang;
  124. src2 += "Compiler.cmake";
  125. std::string dst2 = rootBin;
  126. dst2 += "/CMake";
  127. dst2 += lang;
  128. dst2 += "Compiler.cmake";
  129. cmSystemTools::CopyFileIfDifferent(src2.c_str(), dst2.c_str());
  130. src2 = m_ConfiguredFilesPath;
  131. src2 += "/CMake";
  132. src2 += lang;
  133. src2 += "Platform.cmake";
  134. dst2 = rootBin;
  135. dst2 += "/CMake";
  136. dst2 += lang;
  137. dst2 += "Platform.cmake";
  138. cmSystemTools::CopyFileIfDifferent(src2.c_str(), dst2.c_str());
  139. }
  140. rootBin = m_ConfiguredFilesPath;
  141. }
  142. // **** Step 1, find and make sure CMAKE_MAKE_PROGRAM is defined
  143. this->FindMakeProgram(mf);
  144. // try and load the CMakeSystem.cmake if it is there
  145. std::string fpath = rootBin;
  146. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  147. {
  148. fpath += "/CMakeSystem.cmake";
  149. if(cmSystemTools::FileExists(fpath.c_str()))
  150. {
  151. mf->ReadListFile(0,fpath.c_str());
  152. }
  153. }
  154. // **** Step 2, Load the CMakeDetermineSystem.cmake file and find out
  155. // what platform we are running on
  156. if (!mf->GetDefinition("CMAKE_SYSTEM_NAME"))
  157. {
  158. #if defined(_WIN32) && !defined(__CYGWIN__)
  159. /* Windows version number data. */
  160. OSVERSIONINFO osvi;
  161. ZeroMemory(&osvi, sizeof(osvi));
  162. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  163. GetVersionEx (&osvi);
  164. cmOStringStream windowsVersionString;
  165. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  166. windowsVersionString.str();
  167. mf->AddDefinition("CMAKE_SYSTEM_VERSION", windowsVersionString.str().c_str());
  168. #endif
  169. // Read the DetermineSystem file
  170. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  171. mf->ReadListFile(0, systemFile.c_str());
  172. }
  173. // **** Step 3, load the CMakeSystem.cmake from the binary directory
  174. // this file is configured by the CMakeDetermineSystem.cmake file
  175. fpath = rootBin;
  176. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  177. {
  178. fpath += "/CMakeSystem.cmake";
  179. mf->ReadListFile(0,fpath.c_str());
  180. }
  181. // **** Step 4, foreach language
  182. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  183. // the compiler
  184. for(std::vector<std::string>::const_iterator l = languages.begin();
  185. l != languages.end(); ++l)
  186. {
  187. const char* lang = l->c_str();
  188. if(*l == "NONE")
  189. {
  190. this->SetLanguageEnabled("NONE", mf);
  191. continue;
  192. }
  193. if(!this->GetLanguageEnabled(lang) )
  194. {
  195. if (m_CMakeInstance->GetIsInTryCompile())
  196. {
  197. cmSystemTools::Error("This should not have happen. "
  198. "If you see this message, you are probably using a "
  199. "broken CMakeLists.txt file or a problematic release of "
  200. "CMake");
  201. }
  202. // If the existing build tree was already configured with this
  203. // version of CMake then try to load the configured file first
  204. // to avoid duplicate compiler tests.
  205. unsigned int cacheMajor = mf->GetCacheMajorVersion();
  206. unsigned int cacheMinor = mf->GetCacheMinorVersion();
  207. unsigned int selfMajor = cmMakefile::GetMajorVersion();
  208. unsigned int selfMinor = cmMakefile::GetMinorVersion();
  209. if(selfMajor == cacheMajor && selfMinor == cacheMinor)
  210. {
  211. std::string loadedLang = "CMAKE_";
  212. loadedLang += lang;
  213. loadedLang += "_COMPILER_LOADED";
  214. if(!mf->GetDefinition(loadedLang.c_str()))
  215. {
  216. fpath = rootBin;
  217. fpath += "/CMake";
  218. fpath += lang;
  219. fpath += "Compiler.cmake";
  220. if(cmSystemTools::FileExists(fpath.c_str()))
  221. {
  222. if(!mf->ReadListFile(0,fpath.c_str()))
  223. {
  224. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  225. }
  226. this->SetLanguageEnabled(lang, mf);
  227. }
  228. }
  229. }
  230. needTestLanguage = true; // must test a language after finding it
  231. // read determine LANG compiler
  232. std::string determineCompiler = "CMakeDetermine";
  233. determineCompiler += lang;
  234. determineCompiler += "Compiler.cmake";
  235. std::string determineFile = mf->GetModulesFile(determineCompiler.c_str());
  236. if(!mf->ReadListFile(0,determineFile.c_str()))
  237. {
  238. cmSystemTools::Error("Could not find cmake module file:", determineFile.c_str());
  239. }
  240. // Some generators like visual studio should not use the env variables
  241. // So the global generator can specify that in this variable
  242. if(!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV"))
  243. {
  244. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER into the
  245. // environment, in case user scripts want to run configure, or sub cmakes
  246. std::string compilerName = "CMAKE_";
  247. compilerName += lang;
  248. compilerName += "_COMPILER";
  249. std::string compilerEnv = "CMAKE_";
  250. compilerEnv += lang;
  251. compilerEnv += "_COMPILER_ENV_VAR";
  252. std::string envVar = mf->GetRequiredDefinition(compilerEnv.c_str());
  253. std::string envVarValue = mf->GetRequiredDefinition(compilerName.c_str());
  254. std::string env = envVar;
  255. env += "=";
  256. env += envVarValue;
  257. cmSystemTools::PutEnv(env.c_str());
  258. }
  259. }
  260. // **** Step 5, Load the configured language compiler file, if not loaded.
  261. // look to see if CMAKE_(LANG)_COMPILER_LOADED is set,
  262. // if not then load the CMake(LANG)Compiler.cmake file from the
  263. // binary tree, this is a configured file provided by
  264. // CMakeDetermine(LANG)Compiler.cmake
  265. std::string loadedLang = "CMAKE_";
  266. loadedLang += lang;
  267. loadedLang += "_COMPILER_LOADED";
  268. if(!mf->GetDefinition(loadedLang.c_str()))
  269. {
  270. fpath = rootBin;
  271. fpath += "/CMake";
  272. fpath += lang;
  273. fpath += "Compiler.cmake";
  274. if(!mf->ReadListFile(0,fpath.c_str()))
  275. {
  276. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  277. }
  278. this->SetLanguageEnabled(lang, mf);
  279. }
  280. }
  281. // **** Step 6, Load the system specific information if not yet loaded
  282. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  283. {
  284. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  285. if(!mf->ReadListFile(0,fpath.c_str()))
  286. {
  287. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  288. }
  289. }
  290. for(std::vector<std::string>::const_iterator l = languages.begin();
  291. l != languages.end(); ++l)
  292. {
  293. const char* lang = l->c_str();
  294. if(*l == "NONE")
  295. {
  296. this->SetLanguageEnabled("NONE", mf);
  297. continue;
  298. }
  299. std::string langLoadedVar = "CMAKE_";
  300. langLoadedVar += lang;
  301. langLoadedVar += "_INFORMATION_LOADED";
  302. if (!mf->GetDefinition(langLoadedVar.c_str()))
  303. {
  304. fpath = "CMake";
  305. fpath += lang;
  306. fpath += "Information.cmake";
  307. fpath = mf->GetModulesFile(fpath.c_str());
  308. if(!mf->ReadListFile(0,fpath.c_str()))
  309. {
  310. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  311. }
  312. }
  313. // **** Step 7, Test the compiler for the language just setup
  314. // At this point we should have enough info for a try compile
  315. // which is used in the backward stuff
  316. if(needTestLanguage)
  317. {
  318. if (!m_CMakeInstance->GetIsInTryCompile())
  319. {
  320. std::string testLang = "CMakeTest";
  321. testLang += lang;
  322. testLang += "Compiler.cmake";
  323. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  324. if(!mf->ReadListFile(0,ifpath.c_str()))
  325. {
  326. cmSystemTools::Error("Could not find cmake module file:", ifpath.c_str());
  327. }
  328. // **** Step 8, load backwards compatibility stuff for C and CXX
  329. // for old versions of CMake ListFiles C and CXX had some
  330. // backwards compatibility files they have to load
  331. const char* versionValue
  332. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  333. if (atof(versionValue) <= 1.4)
  334. {
  335. if(strcmp(lang, "C") == 0)
  336. {
  337. ifpath = mf->GetModulesFile("CMakeBackwardCompatibilityC.cmake");
  338. mf->ReadListFile(0,ifpath.c_str());
  339. }
  340. if(strcmp(lang, "CXX") == 0)
  341. {
  342. ifpath = mf->GetModulesFile("CMakeBackwardCompatibilityCXX.cmake");
  343. mf->ReadListFile(0,ifpath.c_str());
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. const char* cmGlobalGenerator::GetLanguageOutputExtensionForLanguage(const char* lang)
  351. {
  352. if(!lang)
  353. {
  354. return "";
  355. }
  356. if(m_LanguageToOutputExtension.count(lang) > 0)
  357. {
  358. return m_LanguageToOutputExtension[lang].c_str();
  359. }
  360. return "";
  361. }
  362. const char* cmGlobalGenerator::GetLanguageOutputExtensionFromExtension(const char* ext)
  363. {
  364. if(!ext)
  365. {
  366. return "";
  367. }
  368. const char* lang = this->GetLanguageFromExtension(ext);
  369. if(!lang || *lang == 0)
  370. {
  371. // if no language is found then check to see if it is already an
  372. // ouput extension for some language. In that case it should be ignored
  373. // and in this map, so it will not be compiled but will just be used.
  374. if(m_OutputExtensions.count(ext))
  375. {
  376. return ext;
  377. }
  378. }
  379. return this->GetLanguageOutputExtensionForLanguage(lang);
  380. }
  381. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  382. {
  383. // if there is an extension and it starts with . then
  384. // move past the . because the extensions are not stored with a .
  385. // in the map
  386. if(ext && *ext == '.')
  387. {
  388. ++ext;
  389. }
  390. if(m_ExtensionToLanguage.count(ext) > 0)
  391. {
  392. return m_ExtensionToLanguage[ext].c_str();
  393. }
  394. return 0;
  395. }
  396. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  397. {
  398. if(m_LanguageEnabled.count(l) > 0)
  399. {
  400. return;
  401. }
  402. std::string outputExtensionVar = std::string("CMAKE_") +
  403. std::string(l) + std::string("_OUTPUT_EXTENSION");
  404. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  405. if(outputExtension)
  406. {
  407. m_LanguageToOutputExtension[l] = outputExtension;
  408. m_OutputExtensions[outputExtension] = outputExtension;
  409. if(outputExtension[0] == '.')
  410. {
  411. m_OutputExtensions[outputExtension+1] = outputExtension+1;
  412. }
  413. }
  414. std::string linkerPrefVar = std::string("CMAKE_") +
  415. std::string(l) + std::string("_LINKER_PREFERENCE");
  416. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  417. if(!linkerPref)
  418. {
  419. linkerPref = "None";
  420. }
  421. m_LanguageToLinkerPreference[l] = linkerPref;
  422. std::string extensionsVar = std::string("CMAKE_") +
  423. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  424. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  425. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  426. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  427. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  428. std::vector<std::string> extensionList;
  429. cmSystemTools::ExpandListArgument(exts, extensionList);
  430. for(std::vector<std::string>::iterator i = extensionList.begin();
  431. i != extensionList.end(); ++i)
  432. {
  433. m_ExtensionToLanguage[*i] = l;
  434. }
  435. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  436. for(std::vector<std::string>::iterator i = extensionList.begin();
  437. i != extensionList.end(); ++i)
  438. {
  439. m_IgnoreExtensions[*i] = true;
  440. }
  441. m_LanguageEnabled[l] = true;
  442. }
  443. bool cmGlobalGenerator::IgnoreFile(const char* l)
  444. {
  445. if(this->GetLanguageFromExtension(l))
  446. {
  447. return false;
  448. }
  449. return (m_IgnoreExtensions.count(l) > 0);
  450. }
  451. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  452. {
  453. return (m_LanguageEnabled.count(l) > 0);
  454. }
  455. void cmGlobalGenerator::ClearEnabledLanguages()
  456. {
  457. m_LanguageEnabled.clear();
  458. }
  459. void cmGlobalGenerator::Configure()
  460. {
  461. // Setup the current output directory components for use by
  462. // ConvertToRelativePath.
  463. std::string outdir =
  464. cmSystemTools::CollapseFullPath(m_CMakeInstance->GetHomeOutputDirectory());
  465. cmSystemTools::SplitPath(outdir.c_str(), m_HomeOutputDirectoryComponents);
  466. // Delete any existing cmLocalGenerators
  467. unsigned int i;
  468. for (i = 0; i < m_LocalGenerators.size(); ++i)
  469. {
  470. delete m_LocalGenerators[i];
  471. }
  472. m_LocalGenerators.clear();
  473. // Setup relative path generation.
  474. this->ConfigureRelativePaths();
  475. // start with this directory
  476. cmLocalGenerator *lg = this->CreateLocalGenerator();
  477. m_LocalGenerators.push_back(lg);
  478. // set the Start directories
  479. lg->GetMakefile()->SetStartDirectory
  480. (m_CMakeInstance->GetStartDirectory());
  481. lg->GetMakefile()->SetStartOutputDirectory
  482. (m_CMakeInstance->GetStartOutputDirectory());
  483. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  484. // now do it
  485. lg->Configure();
  486. // update the cache entry for the number of local generators, this is used
  487. // for progress
  488. char num[100];
  489. sprintf(num,"%d",static_cast<int>(m_LocalGenerators.size()));
  490. this->GetCMakeInstance()->AddCacheEntry
  491. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  492. "number of local generators", cmCacheManager::INTERNAL);
  493. std::set<cmStdString> notFoundMap;
  494. // after it is all done do a ConfigureFinalPass
  495. cmCacheManager* manager = 0;
  496. for (i = 0; i < m_LocalGenerators.size(); ++i)
  497. {
  498. manager = m_LocalGenerators[i]->GetMakefile()->GetCacheManager();
  499. m_LocalGenerators[i]->ConfigureFinalPass();
  500. cmTargets const& targets =
  501. m_LocalGenerators[i]->GetMakefile()->GetTargets();
  502. for (cmTargets::const_iterator l = targets.begin();
  503. l != targets.end(); l++)
  504. {
  505. cmTarget::LinkLibraries libs = l->second.GetLinkLibraries();
  506. for(cmTarget::LinkLibraries::iterator lib = libs.begin();
  507. lib != libs.end(); ++lib)
  508. {
  509. if(lib->first.size() > 9 &&
  510. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  511. {
  512. std::string varName = lib->first.substr(0, lib->first.size()-9);
  513. notFoundMap.insert(varName);
  514. }
  515. }
  516. std::vector<std::string>& incs =
  517. m_LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  518. for( std::vector<std::string>::iterator lib = incs.begin();
  519. lib != incs.end(); ++lib)
  520. {
  521. if(lib->size() > 9 &&
  522. cmSystemTools::IsNOTFOUND(lib->c_str()))
  523. {
  524. std::string varName = lib->substr(0, lib->size()-9);
  525. notFoundMap.insert(varName);
  526. }
  527. }
  528. m_CMakeInstance->UpdateProgress("Configuring",
  529. 0.9f+0.1f*(i+1.0f)/m_LocalGenerators.size());
  530. m_LocalGenerators[i]->GetMakefile()->CheckInfiniteLoops();
  531. }
  532. }
  533. if(notFoundMap.size())
  534. {
  535. std::string notFoundVars;
  536. for(std::set<cmStdString>::iterator ii = notFoundMap.begin();
  537. ii != notFoundMap.end(); ++ii)
  538. {
  539. notFoundVars += *ii;
  540. if(manager)
  541. {
  542. cmCacheManager::CacheIterator it =
  543. manager->GetCacheIterator(ii->c_str());
  544. if(it.GetPropertyAsBool("ADVANCED"))
  545. {
  546. notFoundVars += " (ADVANCED)";
  547. }
  548. }
  549. notFoundVars += "\n";
  550. }
  551. cmSystemTools::Error("This project requires some variables to be set,\n"
  552. "and cmake can not find them.\n"
  553. "Please set the following variables:\n",
  554. notFoundVars.c_str());
  555. }
  556. // at this point m_LocalGenerators has been filled,
  557. // so create the map from project name to vector of local generators
  558. this->FillProjectMap();
  559. if ( !m_CMakeInstance->GetScriptMode() )
  560. {
  561. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  562. }
  563. }
  564. void cmGlobalGenerator::Generate()
  565. {
  566. // For each existing cmLocalGenerator
  567. unsigned int i;
  568. for (i = 0; i < m_LocalGenerators.size(); ++i)
  569. {
  570. m_LocalGenerators[i]->Generate();
  571. m_LocalGenerators[i]->GenerateInstallRules();
  572. m_LocalGenerators[i]->GenerateTestFiles();
  573. m_CMakeInstance->UpdateProgress("Generating",
  574. (i+1.0f)/m_LocalGenerators.size());
  575. }
  576. m_CMakeInstance->UpdateProgress("Generating done", -1);
  577. }
  578. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  579. const char *projectName,
  580. const char *target,
  581. std::string *output, cmMakefile *mf)
  582. {
  583. std::string makeCommand =
  584. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  585. if(makeCommand.size() == 0)
  586. {
  587. cmSystemTools::Error(
  588. "Generator cannot find the appropriate make command.");
  589. return 1;
  590. }
  591. std::string newTarget;
  592. if (target && strlen(target))
  593. {
  594. newTarget += target;
  595. #if 0
  596. #if defined(_WIN32) || defined(__CYGWIN__)
  597. std::string tmp = target;
  598. // if the target does not already end in . something
  599. // then assume .exe
  600. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  601. {
  602. newTarget += ".exe";
  603. }
  604. #endif // WIN32
  605. #endif
  606. }
  607. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  608. return this->Build(srcdir,bindir,projectName,
  609. newTarget.c_str(),
  610. output,makeCommand.c_str(),config,false);
  611. }
  612. std::string cmGlobalGenerator::GenerateBuildCommand(const char* makeProgram,
  613. const char *projectName, const char *targetName, const char* config,
  614. bool ignoreErrors)
  615. {
  616. // Project name and config are not used yet.
  617. (void)projectName;
  618. (void)config;
  619. std::string makeCommand = cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  620. // Since we have full control over the invocation of nmake, let us
  621. // make it quiet.
  622. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  623. {
  624. makeCommand += " /NOLOGO ";
  625. }
  626. if ( ignoreErrors )
  627. {
  628. makeCommand += " -i";
  629. }
  630. if ( targetName )
  631. {
  632. makeCommand += " ";
  633. makeCommand += targetName;
  634. }
  635. return makeCommand;
  636. }
  637. int cmGlobalGenerator::Build(
  638. const char *, const char *bindir,
  639. const char *projectName, const char *target,
  640. std::string *output,
  641. const char *makeCommandCSTR,
  642. const char *config,
  643. bool clean)
  644. {
  645. *output += "\nTesting TryCompileWithoutMakefile\n";
  646. /**
  647. * Run an executable command and put the stdout in output.
  648. */
  649. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  650. cmSystemTools::ChangeDirectory(bindir);
  651. int retVal;
  652. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  653. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  654. cmSystemTools::SetRunCommandHideConsole(true);
  655. // should we do a clean first?
  656. if (clean)
  657. {
  658. std::string cleanCommand = this->GenerateBuildCommand(makeCommandCSTR, projectName, "clean", config, false);
  659. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), output,
  660. &retVal, 0, false, timeout))
  661. {
  662. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  663. cmSystemTools::Error("Generator: execution of make clean failed.");
  664. if (output)
  665. {
  666. *output += "\nGenerator: execution of make clean failed.\n";
  667. }
  668. // return to the original directory
  669. cmSystemTools::ChangeDirectory(cwd.c_str());
  670. return 1;
  671. }
  672. }
  673. // now build
  674. std::string makeCommand = this->GenerateBuildCommand(makeCommandCSTR, projectName, target, config, false);
  675. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  676. &retVal, 0, false, timeout))
  677. {
  678. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  679. cmSystemTools::Error("Generator: execution of make failed. Make command was: ",
  680. makeCommand.c_str());
  681. if (output)
  682. {
  683. *output += "\nGenerator: execution of make failed. Make command was: " +
  684. makeCommand + "\n";
  685. }
  686. // return to the original directory
  687. cmSystemTools::ChangeDirectory(cwd.c_str());
  688. return 1;
  689. }
  690. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  691. // The SGI MipsPro 7.3 compiler does not return an error code when
  692. // the source has a #error in it! This is a work-around for such
  693. // compilers.
  694. if((retVal == 0) && (output->find("#error") != std::string::npos))
  695. {
  696. retVal = 1;
  697. }
  698. cmSystemTools::ChangeDirectory(cwd.c_str());
  699. return retVal;
  700. }
  701. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  702. {
  703. m_LocalGenerators.push_back(lg);
  704. // update progress
  705. // estimate how many lg there will be
  706. const char *numGenC =
  707. m_CMakeInstance->GetCacheManager()->GetCacheValue
  708. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  709. if (!numGenC)
  710. {
  711. return;
  712. }
  713. int numGen = atoi(numGenC);
  714. float prog = 0.9f*m_LocalGenerators.size()/numGen;
  715. if (prog > 0.9f)
  716. {
  717. prog = 0.9f;
  718. }
  719. m_CMakeInstance->UpdateProgress("Configuring", prog);
  720. }
  721. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  722. {
  723. cmLocalGenerator *lg = new cmLocalGenerator;
  724. lg->SetGlobalGenerator(this);
  725. return lg;
  726. }
  727. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  728. {
  729. this->SetConfiguredFilesPath(
  730. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  731. const char* make =
  732. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  733. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  734. "make program",
  735. cmCacheManager::FILEPATH);
  736. // copy the enabled languages
  737. this->m_LanguageEnabled = gen->m_LanguageEnabled;
  738. this->m_ExtensionToLanguage = gen->m_ExtensionToLanguage;
  739. this->m_IgnoreExtensions = gen->m_IgnoreExtensions;
  740. this->m_LanguageToOutputExtension = gen->m_LanguageToOutputExtension;
  741. this->m_LanguageToLinkerPreference = gen->m_LanguageToLinkerPreference;
  742. this->m_OutputExtensions = gen->m_OutputExtensions;
  743. }
  744. //----------------------------------------------------------------------------
  745. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  746. {
  747. entry.name = this->GetName();
  748. entry.brief = "";
  749. entry.full = "";
  750. }
  751. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  752. cmLocalGenerator* gen)
  753. {
  754. cmLocalGenerator* cur = gen->GetParent();
  755. while(cur && cur != root)
  756. {
  757. if(cur->GetExcludeAll())
  758. {
  759. return true;
  760. }
  761. cur = cur->GetParent();
  762. }
  763. return false;
  764. }
  765. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  766. {
  767. for(std::map<cmStdString, bool>::iterator i = m_LanguageEnabled.begin();
  768. i != m_LanguageEnabled.end(); ++i)
  769. {
  770. lang.push_back(i->first);
  771. }
  772. }
  773. const char* cmGlobalGenerator::GetLinkerPreference(const char* lang)
  774. {
  775. if(m_LanguageToLinkerPreference.count(lang))
  776. {
  777. return m_LanguageToLinkerPreference[lang].c_str();
  778. }
  779. return "None";
  780. }
  781. void cmGlobalGenerator::FillProjectMap()
  782. {
  783. m_ProjectMap.clear(); // make sure we start with a clean map
  784. unsigned int i;
  785. for(i = 0; i < m_LocalGenerators.size(); ++i)
  786. {
  787. // for each local generator add all projects
  788. cmLocalGenerator *lg = m_LocalGenerators[i];
  789. std::string name;
  790. do
  791. {
  792. if (name != lg->GetMakefile()->GetProjectName())
  793. {
  794. name = lg->GetMakefile()->GetProjectName();
  795. m_ProjectMap[name].push_back(m_LocalGenerators[i]);
  796. }
  797. lg = lg->GetParent();
  798. }
  799. while (lg);
  800. }
  801. }
  802. cmTarget* cmGlobalGenerator::FindTarget(const char* project,
  803. const char* name)
  804. {
  805. std::vector<cmLocalGenerator*>* gens = &m_LocalGenerators;
  806. if(project)
  807. {
  808. gens = &m_ProjectMap[project];
  809. }
  810. for(unsigned int i = 0; i < gens->size(); ++i)
  811. {
  812. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name);
  813. if(ret)
  814. {
  815. return ret;
  816. }
  817. }
  818. return 0;
  819. }
  820. //----------------------------------------------------------------------------
  821. void cmGlobalGenerator::ConfigureRelativePaths()
  822. {
  823. // Identify the longest shared path component between the source
  824. // directory and the build directory.
  825. std::vector<std::string> source;
  826. std::vector<std::string> binary;
  827. cmSystemTools::SplitPath(m_CMakeInstance->GetHomeDirectory(), source);
  828. cmSystemTools::SplitPath(m_CMakeInstance->GetHomeOutputDirectory(), binary);
  829. unsigned int common=0;
  830. while(common < source.size() && common < binary.size() &&
  831. cmSystemTools::ComparePath(source[common].c_str(),
  832. binary[common].c_str()))
  833. {
  834. ++common;
  835. }
  836. // Require more than just the root portion of the path to be in
  837. // common before allowing relative paths. Also disallow relative
  838. // paths if the build tree is a network path. The current working
  839. // directory on Windows cannot be a network path. Therefore
  840. // relative paths cannot work with network paths.
  841. if(common > 1 && source[0] != "//")
  842. {
  843. // Build the minimum prefix required of a path to be converted to
  844. // a relative path.
  845. source.erase(source.begin()+common, source.end());
  846. m_RelativePathTop = cmSystemTools::JoinPath(source);
  847. }
  848. else
  849. {
  850. // Disable relative paths.
  851. m_RelativePathTop = "";
  852. }
  853. }
  854. //----------------------------------------------------------------------------
  855. std::string
  856. cmGlobalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
  857. const char* in_remote)
  858. {
  859. // The path should never be quoted.
  860. assert(in_remote[0] != '\"');
  861. // The local path should never have a trailing slash.
  862. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  863. // If the path is already relative or relative paths are disabled
  864. // then just return the path.
  865. if(m_RelativePathTop.size() == 0 ||
  866. !cmSystemTools::FileIsFullPath(in_remote))
  867. {
  868. return in_remote;
  869. }
  870. // If the path does not begin with the minimum relative path prefix
  871. // then do not convert it.
  872. std::string original = in_remote;
  873. if(original.size() < m_RelativePathTop.size() ||
  874. !cmSystemTools::ComparePath(
  875. original.substr(0, m_RelativePathTop.size()).c_str(),
  876. m_RelativePathTop.c_str()))
  877. {
  878. return in_remote;
  879. }
  880. // Identify the longest shared path component between the remote
  881. // path and the local path.
  882. std::vector<std::string> remote;
  883. cmSystemTools::SplitPath(in_remote, remote);
  884. unsigned int common=0;
  885. while(common < remote.size() &&
  886. common < local.size() &&
  887. cmSystemTools::ComparePath(remote[common].c_str(),
  888. local[common].c_str()))
  889. {
  890. ++common;
  891. }
  892. // If the entire path is in common then just return a ".".
  893. if(common == remote.size() &&
  894. common == local.size())
  895. {
  896. return ".";
  897. }
  898. // If the entire path is in common except for a trailing slash then
  899. // just return a "./".
  900. if(common+1 == remote.size() &&
  901. remote[common].size() == 0 &&
  902. common == local.size())
  903. {
  904. return "./";
  905. }
  906. // Construct the relative path.
  907. std::string relative;
  908. // First add enough ../ to get up to the level of the shared portion
  909. // of the path. Leave off the trailing slash. Note that the last
  910. // component of local will never be empty because local should never
  911. // have a trailing slash.
  912. for(unsigned int i=common; i < local.size(); ++i)
  913. {
  914. relative += "..";
  915. if(i < local.size()-1)
  916. {
  917. relative += "/";
  918. }
  919. }
  920. // Now add the portion of the destination path that is not included
  921. // in the shared portion of the path. Add a slash the first time
  922. // only if there was already something in the path. If there was a
  923. // trailing slash in the input then the last iteration of the loop
  924. // will add a slash followed by an empty string which will preserve
  925. // the trailing slash in the output.
  926. for(unsigned int i=common; i < remote.size(); ++i)
  927. {
  928. if(relative.size() > 0)
  929. {
  930. relative += "/";
  931. }
  932. relative += remote[i];
  933. }
  934. // Finally return the path.
  935. return relative;
  936. }
  937. inline std::string removeQuotes(const std::string& s)
  938. {
  939. if(s[0] == '\"' && s[s.size()-1] == '\"')
  940. {
  941. return s.substr(1, s.size()-2);
  942. }
  943. return s;
  944. }
  945. void cmGlobalGenerator::SetupTests()
  946. {
  947. std::string ctest =
  948. m_LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  949. ctest = removeQuotes(ctest);
  950. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  951. ctest += "/";
  952. ctest += "ctest";
  953. ctest += cmSystemTools::GetExecutableExtension();
  954. if(!cmSystemTools::FileExists(ctest.c_str()))
  955. {
  956. ctest =
  957. m_LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  958. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  959. ctest += "/Debug/";
  960. ctest += "ctest";
  961. ctest += cmSystemTools::GetExecutableExtension();
  962. }
  963. if(!cmSystemTools::FileExists(ctest.c_str()))
  964. {
  965. ctest =
  966. m_LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  967. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  968. ctest += "/Release/";
  969. ctest += "ctest";
  970. ctest += cmSystemTools::GetExecutableExtension();
  971. }
  972. // if we found ctest
  973. if (cmSystemTools::FileExists(ctest.c_str()))
  974. {
  975. // Create a full path filename for output Testfile
  976. std::string fname;
  977. fname = m_CMakeInstance->GetStartOutputDirectory();
  978. fname += "/";
  979. if ( m_LocalGenerators[0]->GetMakefile()->IsSet("CTEST_NEW_FORMAT") )
  980. {
  981. fname += "CTestTestfile.txt";
  982. }
  983. else
  984. {
  985. fname += "DartTestfile.txt";
  986. }
  987. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  988. if (cmSystemTools::FileExists(fname.c_str()))
  989. {
  990. const char* no_output = 0;
  991. std::vector<std::string> no_depends;
  992. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  993. for(it = m_ProjectMap.begin(); it!= m_ProjectMap.end(); ++it)
  994. {
  995. std::vector<cmLocalGenerator*>& gen = it->second;
  996. // add the ALL_BUILD to the first local generator of each project
  997. if(gen.size())
  998. {
  999. gen[0]->GetMakefile()->
  1000. AddUtilityCommand("RUN_TESTS", false, no_output, no_depends,
  1001. ctest.c_str(), "-C", "$(IntDir)");
  1002. }
  1003. }
  1004. }
  1005. }
  1006. }
  1007. //----------------------------------------------------------------------------
  1008. std::string cmGlobalGenerator::ConvertToHomeRelativePath(const char* remote)
  1009. {
  1010. return (this->ConvertToRelativePath(m_HomeOutputDirectoryComponents,remote));
  1011. }
  1012. //----------------------------------------------------------------------------
  1013. std::string
  1014. cmGlobalGenerator::ConvertToHomeRelativeOutputPath(const char* remote)
  1015. {
  1016. return cmSystemTools::ConvertToOutputPath
  1017. (this->ConvertToRelativePath(m_HomeOutputDirectoryComponents,remote).c_str());
  1018. }