cmGlobalGenerator.cxx 33 KB

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