cmGlobalGenerator.cxx 47 KB

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