cmGlobalGenerator.cxx 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. mf->AddCacheDefinition("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. mf->AddCacheDefinition("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. this->TotalTargets.clear();
  551. // start with this directory
  552. cmLocalGenerator *lg = this->CreateLocalGenerator();
  553. this->LocalGenerators.push_back(lg);
  554. // set the Start directories
  555. lg->GetMakefile()->SetStartDirectory
  556. (this->CMakeInstance->GetStartDirectory());
  557. lg->GetMakefile()->SetStartOutputDirectory
  558. (this->CMakeInstance->GetStartOutputDirectory());
  559. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  560. // now do it
  561. lg->Configure();
  562. // update the cache entry for the number of local generators, this is used
  563. // for progress
  564. char num[100];
  565. sprintf(num,"%d",static_cast<int>(this->LocalGenerators.size()));
  566. this->GetCMakeInstance()->AddCacheEntry
  567. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  568. "number of local generators", cmCacheManager::INTERNAL);
  569. std::set<cmStdString> notFoundMap;
  570. // after it is all done do a ConfigureFinalPass
  571. cmCacheManager* manager = 0;
  572. for (i = 0; i < this->LocalGenerators.size(); ++i)
  573. {
  574. manager = this->LocalGenerators[i]->GetMakefile()->GetCacheManager();
  575. this->LocalGenerators[i]->ConfigureFinalPass();
  576. cmTargets & targets =
  577. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  578. for (cmTargets::iterator l = targets.begin();
  579. l != targets.end(); l++)
  580. {
  581. cmTarget::LinkLibraryVectorType libs = l->second.GetLinkLibraries();
  582. for(cmTarget::LinkLibraryVectorType::iterator lib = libs.begin();
  583. lib != libs.end(); ++lib)
  584. {
  585. if(lib->first.size() > 9 &&
  586. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  587. {
  588. std::string varName = lib->first.substr(0, lib->first.size()-9);
  589. notFoundMap.insert(varName);
  590. }
  591. }
  592. std::vector<std::string>& incs =
  593. this->LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  594. for( std::vector<std::string>::iterator lib = incs.begin();
  595. lib != incs.end(); ++lib)
  596. {
  597. if(lib->size() > 9 &&
  598. cmSystemTools::IsNOTFOUND(lib->c_str()))
  599. {
  600. std::string varName = lib->substr(0, lib->size()-9);
  601. notFoundMap.insert(varName);
  602. }
  603. }
  604. this->CMakeInstance->UpdateProgress("Configuring",
  605. 0.9f+0.1f*(i+1.0f)/this->LocalGenerators.size());
  606. this->LocalGenerators[i]->GetMakefile()->CheckInfiniteLoops();
  607. }
  608. }
  609. if(notFoundMap.size())
  610. {
  611. std::string notFoundVars;
  612. for(std::set<cmStdString>::iterator ii = notFoundMap.begin();
  613. ii != notFoundMap.end(); ++ii)
  614. {
  615. notFoundVars += *ii;
  616. if(manager)
  617. {
  618. cmCacheManager::CacheIterator it =
  619. manager->GetCacheIterator(ii->c_str());
  620. if(it.GetPropertyAsBool("ADVANCED"))
  621. {
  622. notFoundVars += " (ADVANCED)";
  623. }
  624. }
  625. notFoundVars += "\n";
  626. }
  627. cmSystemTools::Error("This project requires some variables to be set,\n"
  628. "and cmake can not find them.\n"
  629. "Please set the following variables:\n",
  630. notFoundVars.c_str());
  631. }
  632. // at this point this->LocalGenerators has been filled,
  633. // so create the map from project name to vector of local generators
  634. this->FillProjectMap();
  635. if ( !this->CMakeInstance->GetScriptMode() )
  636. {
  637. this->CMakeInstance->UpdateProgress("Configuring done", -1);
  638. }
  639. }
  640. void cmGlobalGenerator::Generate()
  641. {
  642. // For each existing cmLocalGenerator
  643. unsigned int i;
  644. // Consolidate global targets
  645. cmTargets globalTargets;
  646. this->CreateDefaultGlobalTargets(&globalTargets);
  647. for (i = 0; i < this->LocalGenerators.size(); ++i)
  648. {
  649. cmTargets* targets = &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  650. cmTargets::iterator tarIt;
  651. for ( tarIt = targets->begin(); tarIt != targets->end(); ++ tarIt )
  652. {
  653. if ( tarIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  654. {
  655. globalTargets[tarIt->first] = tarIt->second;
  656. }
  657. }
  658. }
  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. }
  668. // Compute the manifest of main targets generated.
  669. for (i = 0; i < this->LocalGenerators.size(); ++i)
  670. {
  671. this->LocalGenerators[i]->GenerateTargetManifest(this->TargetManifest);
  672. }
  673. // Generate project files
  674. for (i = 0; i < this->LocalGenerators.size(); ++i)
  675. {
  676. this->LocalGenerators[i]->Generate();
  677. this->LocalGenerators[i]->GenerateInstallRules();
  678. this->LocalGenerators[i]->GenerateTestFiles();
  679. this->CMakeInstance->UpdateProgress("Generating",
  680. (i+1.0f)/this->LocalGenerators.size());
  681. }
  682. this->CMakeInstance->UpdateProgress("Generating done", -1);
  683. }
  684. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  685. const char *projectName,
  686. const char *target,
  687. std::string *output, cmMakefile *mf)
  688. {
  689. std::string makeCommand =
  690. this->CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  691. if(makeCommand.size() == 0)
  692. {
  693. cmSystemTools::Error(
  694. "Generator cannot find the appropriate make command.");
  695. return 1;
  696. }
  697. std::string newTarget;
  698. if (target && strlen(target))
  699. {
  700. newTarget += target;
  701. #if 0
  702. #if defined(_WIN32) || defined(__CYGWIN__)
  703. std::string tmp = target;
  704. // if the target does not already end in . something
  705. // then assume .exe
  706. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  707. {
  708. newTarget += ".exe";
  709. }
  710. #endif // WIN32
  711. #endif
  712. }
  713. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  714. return this->Build(srcdir,bindir,projectName,
  715. newTarget.c_str(),
  716. output,makeCommand.c_str(),config,false);
  717. }
  718. std::string cmGlobalGenerator::GenerateBuildCommand(const char* makeProgram,
  719. const char *projectName, const char* additionalOptions, const char *targetName,
  720. const char* config, bool ignoreErrors)
  721. {
  722. // Project name and config are not used yet.
  723. (void)projectName;
  724. (void)config;
  725. std::string makeCommand = cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  726. // Since we have full control over the invocation of nmake, let us
  727. // make it quiet.
  728. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  729. {
  730. makeCommand += " /NOLOGO ";
  731. }
  732. if ( ignoreErrors )
  733. {
  734. makeCommand += " -i";
  735. }
  736. if ( additionalOptions )
  737. {
  738. makeCommand += " ";
  739. makeCommand += additionalOptions;
  740. }
  741. if ( targetName )
  742. {
  743. makeCommand += " ";
  744. makeCommand += targetName;
  745. }
  746. return makeCommand;
  747. }
  748. int cmGlobalGenerator::Build(
  749. const char *, const char *bindir,
  750. const char *projectName, const char *target,
  751. std::string *output,
  752. const char *makeCommandCSTR,
  753. const char *config,
  754. bool clean)
  755. {
  756. *output += "\nTesting TryCompileWithoutMakefile\n";
  757. /**
  758. * Run an executable command and put the stdout in output.
  759. */
  760. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  761. cmSystemTools::ChangeDirectory(bindir);
  762. int retVal;
  763. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  764. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  765. cmSystemTools::SetRunCommandHideConsole(true);
  766. // should we do a clean first?
  767. if (clean)
  768. {
  769. std::string cleanCommand = this->GenerateBuildCommand(makeCommandCSTR, projectName,
  770. 0, "clean", config, false);
  771. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), output,
  772. &retVal, 0, false, timeout))
  773. {
  774. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  775. cmSystemTools::Error("Generator: execution of make clean failed.");
  776. if (output)
  777. {
  778. *output += "\nGenerator: execution of make clean failed.\n";
  779. }
  780. // return to the original directory
  781. cmSystemTools::ChangeDirectory(cwd.c_str());
  782. return 1;
  783. }
  784. }
  785. // now build
  786. std::string makeCommand = this->GenerateBuildCommand(makeCommandCSTR, projectName,
  787. 0, target, config, false);
  788. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  789. &retVal, 0, false, timeout))
  790. {
  791. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  792. cmSystemTools::Error("Generator: execution of make failed. Make command was: ",
  793. makeCommand.c_str());
  794. if (output)
  795. {
  796. *output += "\nGenerator: execution of make failed. Make command was: " +
  797. makeCommand + "\n";
  798. }
  799. // return to the original directory
  800. cmSystemTools::ChangeDirectory(cwd.c_str());
  801. return 1;
  802. }
  803. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  804. // The SGI MipsPro 7.3 compiler does not return an error code when
  805. // the source has a #error in it! This is a work-around for such
  806. // compilers.
  807. if((retVal == 0) && (output->find("#error") != std::string::npos))
  808. {
  809. retVal = 1;
  810. }
  811. cmSystemTools::ChangeDirectory(cwd.c_str());
  812. return retVal;
  813. }
  814. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  815. {
  816. this->LocalGenerators.push_back(lg);
  817. // update progress
  818. // estimate how many lg there will be
  819. const char *numGenC =
  820. this->CMakeInstance->GetCacheManager()->GetCacheValue
  821. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  822. if (!numGenC)
  823. {
  824. return;
  825. }
  826. int numGen = atoi(numGenC);
  827. float prog = 0.9f*this->LocalGenerators.size()/numGen;
  828. if (prog > 0.9f)
  829. {
  830. prog = 0.9f;
  831. }
  832. this->CMakeInstance->UpdateProgress("Configuring", prog);
  833. }
  834. void cmGlobalGenerator::AddInstallComponent(const char* component)
  835. {
  836. if(component && *component)
  837. {
  838. this->InstallComponents.insert(component);
  839. }
  840. }
  841. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  842. {
  843. cmLocalGenerator *lg = new cmLocalGenerator;
  844. lg->SetGlobalGenerator(this);
  845. return lg;
  846. }
  847. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  848. {
  849. std::string cfp = gen->GetCMakeInstance()->GetHomeOutputDirectory();
  850. cfp += "/CMakeFiles";
  851. this->SetConfiguredFilesPath(cfp.c_str());
  852. const char* make =
  853. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  854. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  855. "make program",
  856. cmCacheManager::FILEPATH);
  857. // copy the enabled languages
  858. this->LanguageEnabled = gen->LanguageEnabled;
  859. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  860. this->IgnoreExtensions = gen->IgnoreExtensions;
  861. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  862. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  863. this->OutputExtensions = gen->OutputExtensions;
  864. }
  865. //----------------------------------------------------------------------------
  866. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  867. {
  868. entry.name = this->GetName();
  869. entry.brief = "";
  870. entry.full = "";
  871. }
  872. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  873. cmLocalGenerator* gen)
  874. {
  875. cmLocalGenerator* cur = gen->GetParent();
  876. while(cur && cur != root)
  877. {
  878. if(cur->GetExcludeAll())
  879. {
  880. return true;
  881. }
  882. cur = cur->GetParent();
  883. }
  884. return false;
  885. }
  886. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  887. {
  888. for(std::map<cmStdString, bool>::iterator i = this->LanguageEnabled.begin();
  889. i != this->LanguageEnabled.end(); ++i)
  890. {
  891. lang.push_back(i->first);
  892. }
  893. }
  894. const char* cmGlobalGenerator::GetLinkerPreference(const char* lang)
  895. {
  896. if(this->LanguageToLinkerPreference.count(lang))
  897. {
  898. return this->LanguageToLinkerPreference[lang].c_str();
  899. }
  900. return "None";
  901. }
  902. void cmGlobalGenerator::FillProjectMap()
  903. {
  904. this->ProjectMap.clear(); // make sure we start with a clean map
  905. unsigned int i;
  906. for(i = 0; i < this->LocalGenerators.size(); ++i)
  907. {
  908. // for each local generator add all projects
  909. cmLocalGenerator *lg = this->LocalGenerators[i];
  910. std::string name;
  911. do
  912. {
  913. if (name != lg->GetMakefile()->GetProjectName())
  914. {
  915. name = lg->GetMakefile()->GetProjectName();
  916. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  917. }
  918. lg = lg->GetParent();
  919. }
  920. while (lg);
  921. }
  922. }
  923. ///! Find a local generator by its startdirectory
  924. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(const char* start_dir)
  925. {
  926. std::vector<cmLocalGenerator*>* gens = &this->LocalGenerators;
  927. for(unsigned int i = 0; i < gens->size(); ++i)
  928. {
  929. std::string sd = (*gens)[i]->GetMakefile()->GetStartDirectory();
  930. if (sd == start_dir)
  931. {
  932. return (*gens)[i];
  933. }
  934. }
  935. return 0;
  936. }
  937. cmTarget* cmGlobalGenerator::FindTarget(const char* project,
  938. const char* name)
  939. {
  940. // if project specific
  941. if(project)
  942. {
  943. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  944. for(unsigned int i = 0; i < gens->size(); ++i)
  945. {
  946. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name);
  947. if(ret)
  948. {
  949. return ret;
  950. }
  951. }
  952. }
  953. // if all projects/directories
  954. else
  955. {
  956. std::map<cmStdString,cmTarget *>::iterator i = this->TotalTargets.find(name);
  957. if (i == this->TotalTargets.end())
  958. {
  959. return 0;
  960. }
  961. return i->second;
  962. }
  963. return 0;
  964. }
  965. //----------------------------------------------------------------------------
  966. void cmGlobalGenerator::ConfigureRelativePaths()
  967. {
  968. // The current working directory on Windows cannot be a network
  969. // path. Therefore relative paths cannot work when the build tree
  970. // is a network path.
  971. std::string source = this->CMakeInstance->GetHomeDirectory();
  972. std::string binary = this->CMakeInstance->GetHomeOutputDirectory();
  973. if(binary.size() < 2 || binary.substr(0, 2) != "//")
  974. {
  975. this->RelativePathTopSource = source;
  976. this->RelativePathTopBinary = binary;
  977. }
  978. else
  979. {
  980. this->RelativePathTopSource = "";
  981. this->RelativePathTopBinary = "";
  982. }
  983. }
  984. //----------------------------------------------------------------------------
  985. std::string
  986. cmGlobalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
  987. const char* in_remote)
  988. {
  989. // The path should never be quoted.
  990. assert(in_remote[0] != '\"');
  991. // The local path should never have a trailing slash.
  992. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  993. // If the path is already relative then just return the path.
  994. if(!cmSystemTools::FileIsFullPath(in_remote))
  995. {
  996. return in_remote;
  997. }
  998. std::string original = in_remote;
  999. // Skip conversion if the path and local are not both in the source or both
  1000. // in the binary tree
  1001. std::string local_path = cmSystemTools::JoinPath(local);
  1002. bool should_convert = false;
  1003. // is the root in the binary tree?
  1004. if (local_path.size() >= this->RelativePathTopBinary.size() &&
  1005. cmSystemTools::ComparePath
  1006. (local_path.substr(0, this->RelativePathTopBinary.size()).c_str(),
  1007. this->RelativePathTopBinary.c_str()))
  1008. {
  1009. // is the source also in the binary tree?
  1010. if (original.size() >= this->RelativePathTopBinary.size() &&
  1011. cmSystemTools::ComparePath
  1012. (original.substr(0, this->RelativePathTopBinary.size()).c_str(),
  1013. this->RelativePathTopBinary.c_str()))
  1014. {
  1015. should_convert = true;
  1016. }
  1017. }
  1018. if (local_path.size() >= this->RelativePathTopSource.size() &&
  1019. cmSystemTools::ComparePath
  1020. (local_path.substr(0, this->RelativePathTopSource.size()).c_str(),
  1021. this->RelativePathTopSource.c_str()))
  1022. {
  1023. // is the source also in the binary tree?
  1024. if (original.size() >= this->RelativePathTopSource.size() &&
  1025. cmSystemTools::ComparePath
  1026. (original.substr(0, this->RelativePathTopSource.size()).c_str(),
  1027. this->RelativePathTopSource.c_str()))
  1028. {
  1029. should_convert = true;
  1030. }
  1031. }
  1032. if (!should_convert)
  1033. {
  1034. return in_remote;
  1035. }
  1036. // Identify the longest shared path component between the remote
  1037. // path and the local path.
  1038. std::vector<std::string> remote;
  1039. cmSystemTools::SplitPath(in_remote, remote);
  1040. unsigned int common=0;
  1041. while(common < remote.size() &&
  1042. common < local.size() &&
  1043. cmSystemTools::ComparePath(remote[common].c_str(),
  1044. local[common].c_str()))
  1045. {
  1046. ++common;
  1047. }
  1048. // If no part of the path is in common then return the full path.
  1049. if(common == 0)
  1050. {
  1051. return in_remote;
  1052. }
  1053. // If the entire path is in common then just return a ".".
  1054. if(common == remote.size() &&
  1055. common == local.size())
  1056. {
  1057. return ".";
  1058. }
  1059. // If the entire path is in common except for a trailing slash then
  1060. // just return a "./".
  1061. if(common+1 == remote.size() &&
  1062. remote[common].size() == 0 &&
  1063. common == local.size())
  1064. {
  1065. return "./";
  1066. }
  1067. // Construct the relative path.
  1068. std::string relative;
  1069. // First add enough ../ to get up to the level of the shared portion
  1070. // of the path. Leave off the trailing slash. Note that the last
  1071. // component of local will never be empty because local should never
  1072. // have a trailing slash.
  1073. for(unsigned int i=common; i < local.size(); ++i)
  1074. {
  1075. relative += "..";
  1076. if(i < local.size()-1)
  1077. {
  1078. relative += "/";
  1079. }
  1080. }
  1081. // Now add the portion of the destination path that is not included
  1082. // in the shared portion of the path. Add a slash the first time
  1083. // only if there was already something in the path. If there was a
  1084. // trailing slash in the input then the last iteration of the loop
  1085. // will add a slash followed by an empty string which will preserve
  1086. // the trailing slash in the output.
  1087. for(unsigned int i=common; i < remote.size(); ++i)
  1088. {
  1089. if(relative.size() > 0)
  1090. {
  1091. relative += "/";
  1092. }
  1093. relative += remote[i];
  1094. }
  1095. // Finally return the path.
  1096. return relative;
  1097. }
  1098. inline std::string removeQuotes(const std::string& s)
  1099. {
  1100. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1101. {
  1102. return s.substr(1, s.size()-2);
  1103. }
  1104. return s;
  1105. }
  1106. void cmGlobalGenerator::SetupTests()
  1107. {
  1108. std::string ctest =
  1109. this->LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  1110. ctest = removeQuotes(ctest);
  1111. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1112. ctest += "/";
  1113. ctest += "ctest";
  1114. ctest += cmSystemTools::GetExecutableExtension();
  1115. if(!cmSystemTools::FileExists(ctest.c_str()))
  1116. {
  1117. ctest =
  1118. this->LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  1119. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1120. ctest += "/Debug/";
  1121. ctest += "ctest";
  1122. ctest += cmSystemTools::GetExecutableExtension();
  1123. }
  1124. if(!cmSystemTools::FileExists(ctest.c_str()))
  1125. {
  1126. ctest =
  1127. this->LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  1128. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1129. ctest += "/Release/";
  1130. ctest += "ctest";
  1131. ctest += cmSystemTools::GetExecutableExtension();
  1132. }
  1133. // if we found ctest
  1134. if (cmSystemTools::FileExists(ctest.c_str()))
  1135. {
  1136. // Create a full path filename for output Testfile
  1137. std::string fname;
  1138. fname = this->CMakeInstance->GetStartOutputDirectory();
  1139. fname += "/";
  1140. if ( this->LocalGenerators[0]->GetMakefile()->IsSet("CTEST_NEW_FORMAT") )
  1141. {
  1142. fname += "CTestTestfile.txt";
  1143. }
  1144. else
  1145. {
  1146. fname += "DartTestfile.txt";
  1147. }
  1148. // Add run_test only if any tests are foun
  1149. size_t total_tests = 0;
  1150. size_t i;
  1151. for (i = 0; i < this->LocalGenerators.size(); ++i)
  1152. {
  1153. total_tests += this->LocalGenerators[i]->GetMakefile()->GetTests()->size();
  1154. }
  1155. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  1156. if (total_tests > 0)
  1157. {
  1158. const char* no_output = 0;
  1159. const char* no_working_dir = 0;
  1160. std::vector<std::string> no_depends;
  1161. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  1162. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  1163. {
  1164. std::vector<cmLocalGenerator*>& gen = it->second;
  1165. // add the RUN_TESTS to the first local generator of each project
  1166. if(gen.size())
  1167. {
  1168. cmMakefile* mf = gen[0]->GetMakefile();
  1169. if(const char* outDir = mf->GetDefinition("CMAKE_CFG_INTDIR"))
  1170. {
  1171. mf->AddUtilityCommand("RUN_TESTS", false, no_output, no_depends,
  1172. no_working_dir,
  1173. ctest.c_str(), "-C", outDir);
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }
  1180. void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
  1181. {
  1182. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  1183. const char* cmakeCfgIntDir = this->GetCMakeCFGInitDirectory();
  1184. const char* cmakeCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1185. // CPack
  1186. cmCustomCommandLines cpackCommandLines;
  1187. std::vector<std::string> depends;
  1188. cmCustomCommandLine singleLine;
  1189. cpackCommandLines.erase(cpackCommandLines.begin(), cpackCommandLines.end());
  1190. singleLine.erase(singleLine.begin(), singleLine.end());
  1191. depends.erase(depends.begin(), depends.end());
  1192. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1193. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1194. {
  1195. singleLine.push_back("-C");
  1196. singleLine.push_back(mf->GetDefinition("CMAKE_CFG_INTDIR"));
  1197. }
  1198. singleLine.push_back("--config");
  1199. std::string configFile = mf->GetStartOutputDirectory();;
  1200. configFile += "/CPackConfig.cmake";
  1201. singleLine.push_back(configFile);
  1202. cpackCommandLines.push_back(singleLine);
  1203. if ( this->GetPreinstallTargetName() )
  1204. {
  1205. depends.push_back("preinstall");
  1206. }
  1207. (*targets)[this->GetPackageTargetName()]
  1208. = this->CreateGlobalTarget(this->GetPackageTargetName(),
  1209. "Run CPack packaging tool...", &cpackCommandLines, depends);
  1210. // CPack source
  1211. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1212. if ( packageSourceTargetName )
  1213. {
  1214. cpackCommandLines.erase(cpackCommandLines.begin(), cpackCommandLines.end());
  1215. singleLine.erase(singleLine.begin(), singleLine.end());
  1216. depends.erase(depends.begin(), depends.end());
  1217. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1218. singleLine.push_back("--config");
  1219. configFile = mf->GetStartOutputDirectory();;
  1220. configFile += "/CPackSourceConfig.cmake";
  1221. singleLine.push_back(configFile);
  1222. cpackCommandLines.push_back(singleLine);
  1223. (*targets)[packageSourceTargetName]
  1224. = this->CreateGlobalTarget(packageSourceTargetName,
  1225. "Run CPack packaging tool for source...", &cpackCommandLines, depends);
  1226. }
  1227. // Test
  1228. if(mf->IsOn("CMAKE_TESTING_ENABLED"))
  1229. {
  1230. cpackCommandLines.erase(cpackCommandLines.begin(), cpackCommandLines.end());
  1231. singleLine.erase(singleLine.begin(), singleLine.end());
  1232. depends.erase(depends.begin(), depends.end());
  1233. singleLine.push_back(this->GetCMakeInstance()->GetCTestCommand());
  1234. singleLine.push_back("--force-new-ctest-process");
  1235. cpackCommandLines.push_back(singleLine);
  1236. (*targets)[this->GetTestTargetName()]
  1237. = this->CreateGlobalTarget(this->GetTestTargetName(),
  1238. "Running tests...", &cpackCommandLines, depends);
  1239. }
  1240. //Edit Cache
  1241. const char* editCacheTargetName = this->GetEditCacheTargetName();
  1242. if ( editCacheTargetName )
  1243. {
  1244. cpackCommandLines.erase(cpackCommandLines.begin(), cpackCommandLines.end());
  1245. singleLine.erase(singleLine.begin(), singleLine.end());
  1246. depends.erase(depends.begin(), depends.end());
  1247. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1248. // Otherwise default to the interactive command-line interface.
  1249. if(mf->GetDefinition("CMAKE_EDIT_COMMAND"))
  1250. {
  1251. singleLine.push_back(mf->GetDefinition("CMAKE_EDIT_COMMAND"));
  1252. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1253. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1254. cpackCommandLines.push_back(singleLine);
  1255. (*targets)[editCacheTargetName] =
  1256. this->CreateGlobalTarget(
  1257. editCacheTargetName, "Running CMake cache editor...",
  1258. &cpackCommandLines, depends);
  1259. }
  1260. else
  1261. {
  1262. singleLine.push_back(cmakeCommand);
  1263. singleLine.push_back("-i");
  1264. singleLine.push_back(".");
  1265. cpackCommandLines.push_back(singleLine);
  1266. (*targets)[editCacheTargetName] =
  1267. this->CreateGlobalTarget(
  1268. editCacheTargetName, "Running interactive CMake command-line interface...",
  1269. &cpackCommandLines, depends);
  1270. }
  1271. }
  1272. //Rebuild Cache
  1273. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  1274. if ( rebuildCacheTargetName )
  1275. {
  1276. cpackCommandLines.erase(cpackCommandLines.begin(), cpackCommandLines.end());
  1277. singleLine.erase(singleLine.begin(), singleLine.end());
  1278. depends.erase(depends.begin(), depends.end());
  1279. singleLine.push_back(cmakeCommand);
  1280. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1281. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1282. cpackCommandLines.push_back(singleLine);
  1283. (*targets)[rebuildCacheTargetName] =
  1284. this->CreateGlobalTarget(
  1285. rebuildCacheTargetName, "Running CMake to regenerate build system...",
  1286. &cpackCommandLines, depends);
  1287. }
  1288. //Install
  1289. std::string cmd;
  1290. cpackCommandLines.erase(cpackCommandLines.begin(), cpackCommandLines.end());
  1291. singleLine.erase(singleLine.begin(), singleLine.end());
  1292. depends.erase(depends.begin(), depends.end());
  1293. if ( this->GetPreinstallTargetName() )
  1294. {
  1295. depends.push_back(this->GetPreinstallTargetName());
  1296. }
  1297. else
  1298. {
  1299. const char* noall =
  1300. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1301. if(!noall || cmSystemTools::IsOff(noall))
  1302. {
  1303. depends.push_back(this->GetAllTargetName());
  1304. }
  1305. }
  1306. if(mf->GetDefinition("CMake_BINARY_DIR"))
  1307. {
  1308. // We are building CMake itself. We cannot use the original
  1309. // executable to install over itself.
  1310. cmd = mf->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1311. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1312. {
  1313. cmd += "/";
  1314. cmd += cmakeCfgIntDir;
  1315. }
  1316. cmd += "/cmake";
  1317. }
  1318. else
  1319. {
  1320. cmd = cmakeCommand;
  1321. }
  1322. singleLine.push_back(cmd.c_str());
  1323. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1324. {
  1325. std::string cfgArg = "-DBUILD_TYPE=";
  1326. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  1327. singleLine.push_back(cfgArg);
  1328. }
  1329. singleLine.push_back("-P");
  1330. singleLine.push_back("cmake_install.cmake");
  1331. cpackCommandLines.push_back(singleLine);
  1332. (*targets)[this->GetInstallTargetName()] =
  1333. this->CreateGlobalTarget(
  1334. this->GetInstallTargetName(), "Install the project...",
  1335. &cpackCommandLines, depends);
  1336. }
  1337. cmTarget cmGlobalGenerator::CreateGlobalTarget(
  1338. const char* name, const char* message,
  1339. const cmCustomCommandLines* commandLines,
  1340. std::vector<std::string> depends,
  1341. bool depends_on_all /* = false */)
  1342. {
  1343. // Package
  1344. cmTarget target;
  1345. target.SetType(cmTarget::GLOBAL_TARGET, name);
  1346. target.SetInAll(false);
  1347. std::vector<std::string> no_outputs;
  1348. std::vector<std::string> no_depends;
  1349. // Store the custom command in the target.
  1350. cmCustomCommand cc(no_outputs, no_depends, *commandLines, 0, 0);
  1351. target.GetPostBuildCommands().push_back(cc);
  1352. target.SetProperty("EchoString", message);
  1353. if ( depends_on_all )
  1354. {
  1355. target.AddUtility("all");
  1356. }
  1357. std::vector<std::string>::iterator dit;
  1358. for ( dit = depends.begin(); dit != depends.end(); ++ dit )
  1359. {
  1360. target.AddUtility(dit->c_str());
  1361. }
  1362. return target;
  1363. }
  1364. //----------------------------------------------------------------------------
  1365. void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
  1366. const char*, std::string&)
  1367. {
  1368. // Subclasses that support multiple configurations should implement
  1369. // this method to append the subdirectory for the given build
  1370. // configuration.
  1371. }
  1372. //----------------------------------------------------------------------------
  1373. void cmGlobalGenerator::CheckMultipleOutputs(cmMakefile*, bool)
  1374. {
  1375. // Only certain generators need this check. They define this
  1376. // method.
  1377. }