cmGlobalGenerator.cxx 45 KB

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