cmGlobalGenerator.cxx 52 KB

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