cmGlobalGenerator.cxx 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  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. #if defined(_WIN32) && !defined(__CYGWIN__)
  14. #include "windows.h" // this must be first to define GetCurrentDirectory
  15. #endif
  16. #include "cmGlobalGenerator.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmExternalMakefileProjectGenerator.h"
  19. #include "cmake.h"
  20. #include "cmMakefile.h"
  21. #include "cmSourceFile.h"
  22. #include "cmVersion.h"
  23. #include "cmExportInstallFileGenerator.h"
  24. #include "cmComputeTargetDepends.h"
  25. #include "cmGeneratedFileStream.h"
  26. #include <cmsys/Directory.hxx>
  27. #if defined(CMAKE_BUILD_WITH_CMAKE)
  28. # include <cmsys/MD5.h>
  29. #endif
  30. #include <stdlib.h> // required for atof
  31. #include <assert.h>
  32. cmGlobalGenerator::cmGlobalGenerator()
  33. {
  34. // By default the .SYMBOLIC dependency is not needed on symbolic rules.
  35. this->NeedSymbolicMark = false;
  36. // by default use the native paths
  37. this->ForceUnixPaths = false;
  38. // By default do not try to support color.
  39. this->ToolSupportsColor = false;
  40. // By default do not use link scripts.
  41. this->UseLinkScript = false;
  42. // Whether an install target is needed.
  43. this->InstallTargetEnabled = false;
  44. // how long to let try compiles run
  45. this->TryCompileTimeout = 0;
  46. this->ExtraGenerator = 0;
  47. this->CurrentLocalGenerator = 0;
  48. }
  49. cmGlobalGenerator::~cmGlobalGenerator()
  50. {
  51. // Delete any existing cmLocalGenerators
  52. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  53. {
  54. delete this->LocalGenerators[i];
  55. }
  56. this->LocalGenerators.clear();
  57. if (this->ExtraGenerator)
  58. {
  59. delete this->ExtraGenerator;
  60. }
  61. this->ClearExportSets();
  62. }
  63. // Find the make program for the generator, required for try compiles
  64. void cmGlobalGenerator::FindMakeProgram(cmMakefile* mf)
  65. {
  66. if(this->FindMakeProgramFile.size() == 0)
  67. {
  68. cmSystemTools::Error(
  69. "Generator implementation error, "
  70. "all generators must specify this->FindMakeProgramFile");
  71. }
  72. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  73. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  74. {
  75. std::string setMakeProgram =
  76. mf->GetModulesFile(this->FindMakeProgramFile.c_str());
  77. if(setMakeProgram.size())
  78. {
  79. mf->ReadListFile(0, setMakeProgram.c_str());
  80. }
  81. }
  82. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  83. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  84. {
  85. cmOStringStream err;
  86. err << "CMake was unable to find a build program corresponding to \""
  87. << this->GetName() << "\". CMAKE_MAKE_PROGRAM is not set. You "
  88. << "probably need to select a different build tool.";
  89. cmSystemTools::Error(err.str().c_str());
  90. cmSystemTools::SetFatalErrorOccured();
  91. return;
  92. }
  93. std::string makeProgram = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  94. // if there are spaces in the make program use short path
  95. // but do not short path the actual program name, as
  96. // this can cause trouble with VSExpress
  97. if(makeProgram.find(' ') != makeProgram.npos)
  98. {
  99. std::string dir;
  100. std::string file;
  101. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  102. dir, file);
  103. std::string saveFile = file;
  104. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  105. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  106. dir, file);
  107. makeProgram = dir;
  108. makeProgram += "/";
  109. makeProgram += saveFile;
  110. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  111. "make program",
  112. cmCacheManager::FILEPATH);
  113. }
  114. if(makeProgram.find("xcodebuild") != makeProgram.npos)
  115. {
  116. // due to the text file busy /bin/sh problem with xcodebuild
  117. // use the cmakexbuild wrapper instead. This program
  118. // will run xcodebuild and if it sees the error text file busy
  119. // it will stop forwarding output, and let the build finish.
  120. // Then it will retry the build. It will continue this
  121. // untill no text file busy errors occur.
  122. std::string cmakexbuild =
  123. this->CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_COMMAND");
  124. cmakexbuild = cmakexbuild.substr(0, cmakexbuild.length()-5);
  125. cmakexbuild += "cmakexbuild";
  126. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM",
  127. cmakexbuild.c_str(),
  128. "make program",
  129. cmCacheManager::FILEPATH);
  130. }
  131. }
  132. // enable the given language
  133. //
  134. // The following files are loaded in this order:
  135. //
  136. // First figure out what OS we are running on:
  137. //
  138. // CMakeSystem.cmake - configured file created by CMakeDetermineSystem.cmake
  139. // CMakeDetermineSystem.cmake - figure out os info and create
  140. // CMakeSystem.cmake IF CMAKE_SYSTEM
  141. // not set
  142. // CMakeSystem.cmake - configured file created by
  143. // CMakeDetermineSystem.cmake IF CMAKE_SYSTEM_LOADED
  144. // Next try and enable all languages found in the languages vector
  145. //
  146. // FOREACH LANG in languages
  147. // CMake(LANG)Compiler.cmake - configured file create by
  148. // CMakeDetermine(LANG)Compiler.cmake
  149. // CMakeDetermine(LANG)Compiler.cmake - Finds compiler for LANG and
  150. // creates CMake(LANG)Compiler.cmake
  151. // CMake(LANG)Compiler.cmake - configured file created by
  152. // CMakeDetermine(LANG)Compiler.cmake
  153. //
  154. // CMakeSystemSpecificInformation.cmake
  155. // - includes Platform/${CMAKE_SYSTEM_NAME}.cmake
  156. // may use compiler stuff
  157. // FOREACH LANG in languages
  158. // CMake(LANG)Information.cmake
  159. // - loads Platform/${CMAKE_SYSTEM_NAME}-${COMPILER}.cmake
  160. // CMakeTest(LANG)Compiler.cmake
  161. // - Make sure the compiler works with a try compile if
  162. // CMakeDetermine(LANG) was loaded
  163. //
  164. // Now load a few files that can override values set in any of the above
  165. // (PROJECTNAME)Compatibility.cmake
  166. // - load any backwards compatibility stuff for current project
  167. // ${CMAKE_USER_MAKE_RULES_OVERRIDE}
  168. // - allow users a chance to override system variables
  169. //
  170. //
  171. void
  172. cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
  173. cmMakefile *mf, bool optional)
  174. {
  175. if(languages.size() == 0)
  176. {
  177. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  178. cmSystemTools::SetFatalErrorOccured();
  179. return;
  180. }
  181. mf->AddDefinition("RUN_CONFIGURE", true);
  182. std::string rootBin = mf->GetHomeOutputDirectory();
  183. rootBin += cmake::GetCMakeFilesDirectory();
  184. // If the configuration files path has been set,
  185. // then we are in a try compile and need to copy the enable language
  186. // files from the parent cmake bin dir, into the try compile bin dir
  187. if(this->ConfiguredFilesPath.size())
  188. {
  189. for(std::vector<std::string>::const_iterator l = languages.begin();
  190. l != languages.end(); ++l)
  191. {
  192. if(*l == "NONE")
  193. {
  194. this->SetLanguageEnabled("NONE", mf);
  195. break;
  196. }
  197. }
  198. rootBin = this->ConfiguredFilesPath;
  199. }
  200. // set the dir for parent files so they can be used by modules
  201. mf->AddDefinition("CMAKE_PLATFORM_ROOT_BIN",rootBin.c_str());
  202. // find and make sure CMAKE_MAKE_PROGRAM is defined
  203. this->FindMakeProgram(mf);
  204. // try and load the CMakeSystem.cmake if it is there
  205. std::string fpath = rootBin;
  206. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  207. {
  208. fpath += "/CMakeSystem.cmake";
  209. if(cmSystemTools::FileExists(fpath.c_str()))
  210. {
  211. mf->ReadListFile(0,fpath.c_str());
  212. }
  213. }
  214. // Load the CMakeDetermineSystem.cmake file and find out
  215. // what platform we are running on
  216. if (!mf->GetDefinition("CMAKE_SYSTEM"))
  217. {
  218. #if defined(_WIN32) && !defined(__CYGWIN__)
  219. /* Windows version number data. */
  220. OSVERSIONINFO osvi;
  221. ZeroMemory(&osvi, sizeof(osvi));
  222. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  223. GetVersionEx (&osvi);
  224. cmOStringStream windowsVersionString;
  225. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  226. windowsVersionString.str();
  227. mf->AddDefinition("CMAKE_HOST_SYSTEM_VERSION",
  228. windowsVersionString.str().c_str());
  229. #endif
  230. // Read the DetermineSystem file
  231. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  232. mf->ReadListFile(0, systemFile.c_str());
  233. // load the CMakeSystem.cmake from the binary directory
  234. // this file is configured by the CMakeDetermineSystem.cmake file
  235. fpath = rootBin;
  236. fpath += "/CMakeSystem.cmake";
  237. mf->ReadListFile(0,fpath.c_str());
  238. }
  239. std::map<cmStdString, bool> needTestLanguage;
  240. std::map<cmStdString, bool> needSetLanguageEnabledMaps;
  241. // foreach language
  242. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  243. // the compiler
  244. for(std::vector<std::string>::const_iterator l = languages.begin();
  245. l != languages.end(); ++l)
  246. {
  247. const char* lang = l->c_str();
  248. needSetLanguageEnabledMaps[lang] = false;
  249. if(*l == "NONE")
  250. {
  251. this->SetLanguageEnabled("NONE", mf);
  252. continue;
  253. }
  254. std::string loadedLang = "CMAKE_";
  255. loadedLang += lang;
  256. loadedLang += "_COMPILER_LOADED";
  257. // If the existing build tree was already configured with this
  258. // version of CMake then try to load the configured file first
  259. // to avoid duplicate compiler tests.
  260. unsigned int cacheMajor = mf->GetCacheMajorVersion();
  261. unsigned int cacheMinor = mf->GetCacheMinorVersion();
  262. unsigned int selfMajor = cmVersion::GetMajorVersion();
  263. unsigned int selfMinor = cmVersion::GetMinorVersion();
  264. if((this->CMakeInstance->GetIsInTryCompile() ||
  265. (selfMajor == cacheMajor && selfMinor == cacheMinor))
  266. && !mf->GetDefinition(loadedLang.c_str()))
  267. {
  268. fpath = rootBin;
  269. fpath += "/CMake";
  270. fpath += lang;
  271. fpath += "Compiler.cmake";
  272. if(cmSystemTools::FileExists(fpath.c_str()))
  273. {
  274. if(!mf->ReadListFile(0,fpath.c_str()))
  275. {
  276. cmSystemTools::Error("Could not find cmake module file:",
  277. fpath.c_str());
  278. }
  279. // if this file was found then the language was already determined
  280. // to be working
  281. needTestLanguage[lang] = false;
  282. this->SetLanguageEnabledFlag(lang, mf);
  283. needSetLanguageEnabledMaps[lang] = true;
  284. // this can only be called after loading CMake(LANG)Compiler.cmake
  285. }
  286. }
  287. if(!this->GetLanguageEnabled(lang) )
  288. {
  289. if (this->CMakeInstance->GetIsInTryCompile())
  290. {
  291. cmSystemTools::Error("This should not have happen. "
  292. "If you see this message, you are probably "
  293. "using a broken CMakeLists.txt file or a "
  294. "problematic release of CMake");
  295. }
  296. // if the CMake(LANG)Compiler.cmake file was not found then
  297. // load CMakeDetermine(LANG)Compiler.cmake
  298. std::string determineCompiler = "CMakeDetermine";
  299. determineCompiler += lang;
  300. determineCompiler += "Compiler.cmake";
  301. std::string determineFile =
  302. mf->GetModulesFile(determineCompiler.c_str());
  303. if(!mf->ReadListFile(0,determineFile.c_str()))
  304. {
  305. cmSystemTools::Error("Could not find cmake module file:",
  306. determineFile.c_str());
  307. }
  308. needTestLanguage[lang] = true;
  309. // Some generators like visual studio should not use the env variables
  310. // So the global generator can specify that in this variable
  311. if(!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV"))
  312. {
  313. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER
  314. // into the environment, in case user scripts want to run
  315. // configure, or sub cmakes
  316. std::string compilerName = "CMAKE_";
  317. compilerName += lang;
  318. compilerName += "_COMPILER";
  319. std::string compilerEnv = "CMAKE_";
  320. compilerEnv += lang;
  321. compilerEnv += "_COMPILER_ENV_VAR";
  322. std::string envVar = mf->GetRequiredDefinition(compilerEnv.c_str());
  323. std::string envVarValue =
  324. mf->GetRequiredDefinition(compilerName.c_str());
  325. std::string env = envVar;
  326. env += "=";
  327. env += envVarValue;
  328. cmSystemTools::PutEnv(env.c_str());
  329. }
  330. // if determineLanguage was called then load the file it
  331. // configures CMake(LANG)Compiler.cmake
  332. fpath = rootBin;
  333. fpath += "/CMake";
  334. fpath += lang;
  335. fpath += "Compiler.cmake";
  336. if(!mf->ReadListFile(0,fpath.c_str()))
  337. {
  338. cmSystemTools::Error("Could not find cmake module file:",
  339. fpath.c_str());
  340. }
  341. this->SetLanguageEnabledFlag(lang, mf);
  342. needSetLanguageEnabledMaps[lang] = true;
  343. // this can only be called after loading CMake(LANG)Compiler.cmake
  344. // the language must be enabled for try compile to work, but we do
  345. // not know if it is a working compiler yet so set the test language
  346. // flag
  347. needTestLanguage[lang] = true;
  348. } // end if(!this->GetLanguageEnabled(lang) )
  349. } // end loop over languages
  350. // **** Load the system specific information if not yet loaded
  351. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  352. {
  353. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  354. if(!mf->ReadListFile(0,fpath.c_str()))
  355. {
  356. cmSystemTools::Error("Could not find cmake module file:",
  357. fpath.c_str());
  358. }
  359. }
  360. // loop over languages again loading CMake(LANG)Information.cmake
  361. //
  362. for(std::vector<std::string>::const_iterator l = languages.begin();
  363. l != languages.end(); ++l)
  364. {
  365. const char* lang = l->c_str();
  366. if(*l == "NONE")
  367. {
  368. this->SetLanguageEnabled("NONE", mf);
  369. continue;
  370. }
  371. std::string langLoadedVar = "CMAKE_";
  372. langLoadedVar += lang;
  373. langLoadedVar += "_INFORMATION_LOADED";
  374. if (!mf->GetDefinition(langLoadedVar.c_str()))
  375. {
  376. fpath = "CMake";
  377. fpath += lang;
  378. fpath += "Information.cmake";
  379. std::string informationFile = mf->GetModulesFile(fpath.c_str());
  380. if (informationFile.empty())
  381. {
  382. cmSystemTools::Error("Could not find cmake module file:",
  383. fpath.c_str());
  384. }
  385. else if(!mf->ReadListFile(0, informationFile.c_str()))
  386. {
  387. cmSystemTools::Error("Could not process cmake module file:",
  388. informationFile.c_str());
  389. }
  390. }
  391. if (needSetLanguageEnabledMaps[lang])
  392. {
  393. this->SetLanguageEnabledMaps(lang, mf);
  394. }
  395. std::string compilerName = "CMAKE_";
  396. compilerName += lang;
  397. compilerName += "_COMPILER";
  398. std::string compilerLangFile = rootBin;
  399. compilerLangFile += "/CMake";
  400. compilerLangFile += lang;
  401. compilerLangFile += "Compiler.cmake";
  402. // Test the compiler for the language just setup
  403. // (but only if a compiler has been actually found)
  404. // At this point we should have enough info for a try compile
  405. // which is used in the backward stuff
  406. // If the language is untested then test it now with a try compile.
  407. if (!mf->IsSet(compilerName.c_str()))
  408. {
  409. // if the compiler did not work, then remove the
  410. // CMake(LANG)Compiler.cmake file so that it will get tested the
  411. // next time cmake is run
  412. cmSystemTools::RemoveFile(compilerLangFile.c_str());
  413. }
  414. else if(needTestLanguage[lang])
  415. {
  416. if (!this->CMakeInstance->GetIsInTryCompile())
  417. {
  418. std::string testLang = "CMakeTest";
  419. testLang += lang;
  420. testLang += "Compiler.cmake";
  421. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  422. if(!mf->ReadListFile(0,ifpath.c_str()))
  423. {
  424. cmSystemTools::Error("Could not find cmake module file:",
  425. ifpath.c_str());
  426. }
  427. std::string compilerWorks = "CMAKE_";
  428. compilerWorks += lang;
  429. compilerWorks += "_COMPILER_WORKS";
  430. // if the compiler did not work, then remove the
  431. // CMake(LANG)Compiler.cmake file so that it will get tested the
  432. // next time cmake is run
  433. if(!mf->IsOn(compilerWorks.c_str()))
  434. {
  435. cmSystemTools::RemoveFile(compilerLangFile.c_str());
  436. }
  437. else
  438. {
  439. // load backwards compatibility stuff for C and CXX
  440. // for old versions of CMake ListFiles C and CXX had some
  441. // backwards compatibility files they have to load
  442. // These files have a bunch of try compiles in them so
  443. // should only be done
  444. if (mf->NeedBackwardsCompatibility(1,4))
  445. {
  446. if(strcmp(lang, "C") == 0)
  447. {
  448. ifpath =
  449. mf->GetModulesFile("CMakeBackwardCompatibilityC.cmake");
  450. mf->ReadListFile(0,ifpath.c_str());
  451. }
  452. if(strcmp(lang, "CXX") == 0)
  453. {
  454. ifpath =
  455. mf->GetModulesFile("CMakeBackwardCompatibilityCXX.cmake");
  456. mf->ReadListFile(0,ifpath.c_str());
  457. }
  458. }
  459. }
  460. } // end if in try compile
  461. } // end need test language
  462. } // end for each language
  463. // Now load files that can override any settings on the platform or for
  464. // the project First load the project compatibility file if it is in
  465. // cmake
  466. std::string projectCompatibility = mf->GetDefinition("CMAKE_ROOT");
  467. projectCompatibility += "/Modules/";
  468. projectCompatibility += mf->GetSafeDefinition("PROJECT_NAME");
  469. projectCompatibility += "Compatibility.cmake";
  470. if(cmSystemTools::FileExists(projectCompatibility.c_str()))
  471. {
  472. mf->ReadListFile(0,projectCompatibility.c_str());
  473. }
  474. }
  475. //----------------------------------------------------------------------------
  476. const char*
  477. cmGlobalGenerator::GetLanguageOutputExtension(cmSourceFile const& source)
  478. {
  479. if(const char* lang = source.GetLanguage())
  480. {
  481. if(this->LanguageToOutputExtension.count(lang) > 0)
  482. {
  483. return this->LanguageToOutputExtension[lang].c_str();
  484. }
  485. }
  486. else
  487. {
  488. // if no language is found then check to see if it is already an
  489. // ouput extension for some language. In that case it should be ignored
  490. // and in this map, so it will not be compiled but will just be used.
  491. std::string const& ext = source.GetExtension();
  492. if(!ext.empty())
  493. {
  494. if(this->OutputExtensions.count(ext))
  495. {
  496. return ext.c_str();
  497. }
  498. }
  499. }
  500. return "";
  501. }
  502. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  503. {
  504. // if there is an extension and it starts with . then move past the
  505. // . because the extensions are not stored with a . in the map
  506. if(ext && *ext == '.')
  507. {
  508. ++ext;
  509. }
  510. if(this->ExtensionToLanguage.count(ext) > 0)
  511. {
  512. return this->ExtensionToLanguage[ext].c_str();
  513. }
  514. return 0;
  515. }
  516. /* SetLanguageEnabled() is now split in two parts:
  517. at first the enabled-flag is set. This can then be used in EnabledLanguage()
  518. for checking whether the language is already enabled. After setting this
  519. flag still the values from the cmake variables have to be copied into the
  520. internal maps, this is done in SetLanguageEnabledMaps() which is called
  521. after the system- and compiler specific files have been loaded.
  522. */
  523. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  524. {
  525. this->SetLanguageEnabledFlag(l, mf);
  526. this->SetLanguageEnabledMaps(l, mf);
  527. }
  528. void cmGlobalGenerator::SetLanguageEnabledFlag(const char* l, cmMakefile*)
  529. {
  530. this->LanguageEnabled[l] = true;
  531. }
  532. void cmGlobalGenerator::SetLanguageEnabledMaps(const char* l, cmMakefile* mf)
  533. {
  534. // use LanguageToLinkerPreference to detect whether this functions has
  535. // run before
  536. if (this->LanguageToLinkerPreference.find(l) !=
  537. this->LanguageToLinkerPreference.end())
  538. {
  539. return;
  540. }
  541. std::string linkerPrefVar = std::string("CMAKE_") +
  542. std::string(l) + std::string("_LINKER_PREFERENCE");
  543. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  544. int preference = 0;
  545. if(linkerPref)
  546. {
  547. if (sscanf(linkerPref, "%d", &preference)!=1)
  548. {
  549. // backward compatibility: before 2.6 LINKER_PREFERENCE
  550. // was either "None" or "Prefered", and only the first character was
  551. // tested. So if there is a custom language out there and it is
  552. // "Prefered", set its preference high
  553. if (linkerPref[0]=='P')
  554. {
  555. preference = 100;
  556. }
  557. else
  558. {
  559. preference = 0;
  560. }
  561. }
  562. }
  563. if (preference < 0)
  564. {
  565. std::string msg = linkerPrefVar;
  566. msg += " is negative, adjusting it to 0";
  567. cmSystemTools::Message(msg.c_str(), "Warning");
  568. preference = 0;
  569. }
  570. this->LanguageToLinkerPreference[l] = preference;
  571. std::string outputExtensionVar = std::string("CMAKE_") +
  572. std::string(l) + std::string("_OUTPUT_EXTENSION");
  573. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  574. if(outputExtension)
  575. {
  576. this->LanguageToOutputExtension[l] = outputExtension;
  577. this->OutputExtensions[outputExtension] = outputExtension;
  578. if(outputExtension[0] == '.')
  579. {
  580. this->OutputExtensions[outputExtension+1] = outputExtension+1;
  581. }
  582. }
  583. std::string extensionsVar = std::string("CMAKE_") +
  584. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  585. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  586. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  587. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  588. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  589. std::vector<std::string> extensionList;
  590. cmSystemTools::ExpandListArgument(exts, extensionList);
  591. for(std::vector<std::string>::iterator i = extensionList.begin();
  592. i != extensionList.end(); ++i)
  593. {
  594. this->ExtensionToLanguage[*i] = l;
  595. }
  596. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  597. for(std::vector<std::string>::iterator i = extensionList.begin();
  598. i != extensionList.end(); ++i)
  599. {
  600. this->IgnoreExtensions[*i] = true;
  601. }
  602. }
  603. bool cmGlobalGenerator::IgnoreFile(const char* l)
  604. {
  605. if(this->GetLanguageFromExtension(l))
  606. {
  607. return false;
  608. }
  609. return (this->IgnoreExtensions.count(l) > 0);
  610. }
  611. bool cmGlobalGenerator::GetLanguageEnabled(const char* l) const
  612. {
  613. return (this->LanguageEnabled.find(l)!= this->LanguageEnabled.end());
  614. }
  615. void cmGlobalGenerator::ClearEnabledLanguages()
  616. {
  617. this->LanguageEnabled.clear();
  618. }
  619. bool cmGlobalGenerator::IsDependedOn(const char* project,
  620. cmTarget* targetIn)
  621. {
  622. // Get all local gens for this project
  623. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  624. // loop over local gens and get the targets for each one
  625. for(unsigned int i = 0; i < gens->size(); ++i)
  626. {
  627. cmTargets& targets = (*gens)[i]->GetMakefile()->GetTargets();
  628. for (cmTargets::iterator l = targets.begin();
  629. l != targets.end(); l++)
  630. {
  631. cmTarget& target = l->second;
  632. std::set<cmStdString>::const_iterator pos =
  633. target.GetUtilities().find(targetIn->GetName());
  634. if(pos != target.GetUtilities().end())
  635. {
  636. return true;
  637. }
  638. }
  639. }
  640. return false;
  641. }
  642. void cmGlobalGenerator::Configure()
  643. {
  644. this->FirstTimeProgress = 0.0f;
  645. this->ClearExportSets();
  646. // Delete any existing cmLocalGenerators
  647. unsigned int i;
  648. for (i = 0; i < this->LocalGenerators.size(); ++i)
  649. {
  650. delete this->LocalGenerators[i];
  651. }
  652. this->LocalGenerators.clear();
  653. this->TargetDependencies.clear();
  654. this->TotalTargets.clear();
  655. this->LocalGeneratorToTargetMap.clear();
  656. this->ProjectMap.clear();
  657. this->RuleHashes.clear();
  658. // start with this directory
  659. cmLocalGenerator *lg = this->CreateLocalGenerator();
  660. this->LocalGenerators.push_back(lg);
  661. // set the Start directories
  662. lg->GetMakefile()->SetStartDirectory
  663. (this->CMakeInstance->GetStartDirectory());
  664. lg->GetMakefile()->SetStartOutputDirectory
  665. (this->CMakeInstance->GetStartOutputDirectory());
  666. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  667. // now do it
  668. lg->Configure();
  669. // update the cache entry for the number of local generators, this is used
  670. // for progress
  671. char num[100];
  672. sprintf(num,"%d",static_cast<int>(this->LocalGenerators.size()));
  673. this->GetCMakeInstance()->AddCacheEntry
  674. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  675. "number of local generators", cmCacheManager::INTERNAL);
  676. // check for link libraries and include directories containing "NOTFOUND"
  677. // and for infinite loops
  678. this->CheckLocalGenerators();
  679. // at this point this->LocalGenerators has been filled,
  680. // so create the map from project name to vector of local generators
  681. this->FillProjectMap();
  682. if ( !this->CMakeInstance->GetScriptMode() )
  683. {
  684. this->CMakeInstance->UpdateProgress("Configuring done", -1);
  685. }
  686. }
  687. bool cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS()
  688. {
  689. // If the property is not enabled then okay.
  690. if(!this->CMakeInstance
  691. ->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  692. {
  693. return true;
  694. }
  695. // This generator does not support duplicate custom targets.
  696. cmOStringStream e;
  697. e << "This project has enabled the ALLOW_DUPLICATE_CUSTOM_TARGETS "
  698. << "global property. "
  699. << "The \"" << this->GetName() << "\" generator does not support "
  700. << "duplicate custom targets. "
  701. << "Consider using a Makefiles generator or fix the project to not "
  702. << "use duplicat target names.";
  703. cmSystemTools::Error(e.str().c_str());
  704. return false;
  705. }
  706. void cmGlobalGenerator::Generate()
  707. {
  708. // Some generators track files replaced during the Generate.
  709. // Start with an empty vector:
  710. this->FilesReplacedDuringGenerate.clear();
  711. // Check whether this generator is allowed to run.
  712. if(!this->CheckALLOW_DUPLICATE_CUSTOM_TARGETS())
  713. {
  714. return;
  715. }
  716. // Check that all targets are valid.
  717. if(!this->CheckTargets())
  718. {
  719. return;
  720. }
  721. // For each existing cmLocalGenerator
  722. unsigned int i;
  723. // Consolidate global targets
  724. cmTargets globalTargets;
  725. this->CreateDefaultGlobalTargets(&globalTargets);
  726. for (i = 0; i < this->LocalGenerators.size(); ++i)
  727. {
  728. cmTargets* targets =
  729. &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  730. cmTargets::iterator tarIt;
  731. for ( tarIt = targets->begin(); tarIt != targets->end(); ++ tarIt )
  732. {
  733. if ( tarIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  734. {
  735. globalTargets[tarIt->first] = tarIt->second;
  736. }
  737. }
  738. }
  739. for (i = 0; i < this->LocalGenerators.size(); ++i)
  740. {
  741. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  742. cmTargets* targets = &(mf->GetTargets());
  743. cmTargets::iterator tit;
  744. for ( tit = globalTargets.begin(); tit != globalTargets.end(); ++ tit )
  745. {
  746. (*targets)[tit->first] = tit->second;
  747. (*targets)[tit->first].SetMakefile(mf);
  748. }
  749. }
  750. // Add generator specific helper commands
  751. for (i = 0; i < this->LocalGenerators.size(); ++i)
  752. {
  753. this->LocalGenerators[i]->AddHelperCommands();
  754. }
  755. // Trace the dependencies, after that no custom commands should be added
  756. // because their dependencies might not be handled correctly
  757. for (i = 0; i < this->LocalGenerators.size(); ++i)
  758. {
  759. this->LocalGenerators[i]->TraceDependencies();
  760. }
  761. // Compute the manifest of main targets generated.
  762. for (i = 0; i < this->LocalGenerators.size(); ++i)
  763. {
  764. this->LocalGenerators[i]->GenerateTargetManifest();
  765. }
  766. // Compute the inter-target dependencies.
  767. {
  768. cmComputeTargetDepends ctd(this);
  769. ctd.Compute();
  770. std::vector<cmTarget*> const& targets = ctd.GetTargets();
  771. for(std::vector<cmTarget*>::const_iterator ti = targets.begin();
  772. ti != targets.end(); ++ti)
  773. {
  774. ctd.GetTargetDirectDepends(*ti, this->TargetDependencies[*ti]);
  775. }
  776. }
  777. // Create a map from local generator to the complete set of targets
  778. // it builds by default.
  779. this->FillLocalGeneratorToTargetMap();
  780. // Generate project files
  781. for (i = 0; i < this->LocalGenerators.size(); ++i)
  782. {
  783. this->SetCurrentLocalGenerator(this->LocalGenerators[i]);
  784. this->LocalGenerators[i]->Generate();
  785. this->LocalGenerators[i]->GenerateInstallRules();
  786. this->LocalGenerators[i]->GenerateTestFiles();
  787. this->CMakeInstance->UpdateProgress("Generating",
  788. (i+1.0f)/this->LocalGenerators.size());
  789. }
  790. this->SetCurrentLocalGenerator(0);
  791. // Update rule hashes.
  792. this->CheckRuleHashes();
  793. if (this->ExtraGenerator != 0)
  794. {
  795. this->ExtraGenerator->Generate();
  796. }
  797. this->CMakeInstance->UpdateProgress("Generating done", -1);
  798. }
  799. //----------------------------------------------------------------------------
  800. bool cmGlobalGenerator::CheckTargets()
  801. {
  802. // Make sure all targets can find their source files.
  803. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  804. {
  805. cmTargets& targets =
  806. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  807. for(cmTargets::iterator ti = targets.begin();
  808. ti != targets.end(); ++ti)
  809. {
  810. cmTarget& target = ti->second;
  811. if(target.GetType() == cmTarget::EXECUTABLE ||
  812. target.GetType() == cmTarget::STATIC_LIBRARY ||
  813. target.GetType() == cmTarget::SHARED_LIBRARY ||
  814. target.GetType() == cmTarget::MODULE_LIBRARY ||
  815. target.GetType() == cmTarget::UTILITY)
  816. {
  817. if(!target.FindSourceFiles())
  818. {
  819. return false;
  820. }
  821. }
  822. }
  823. }
  824. return true;
  825. }
  826. void cmGlobalGenerator::CheckLocalGenerators()
  827. {
  828. std::map<cmStdString, cmStdString> notFoundMap;
  829. // std::set<cmStdString> notFoundMap;
  830. // after it is all done do a ConfigureFinalPass
  831. cmCacheManager* manager = 0;
  832. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  833. {
  834. manager = this->LocalGenerators[i]->GetMakefile()->GetCacheManager();
  835. this->LocalGenerators[i]->ConfigureFinalPass();
  836. const cmTargets & targets =
  837. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  838. for (cmTargets::const_iterator l = targets.begin();
  839. l != targets.end(); l++)
  840. {
  841. const cmTarget::LinkLibraryVectorType& libs=l->second.GetLinkLibraries();
  842. for(cmTarget::LinkLibraryVectorType::const_iterator lib = libs.begin();
  843. lib != libs.end(); ++lib)
  844. {
  845. if(lib->first.size() > 9 &&
  846. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  847. {
  848. std::string varName = lib->first.substr(0, lib->first.size()-9);
  849. cmCacheManager::CacheIterator it =
  850. manager->GetCacheIterator(varName.c_str());
  851. if(it.GetPropertyAsBool("ADVANCED"))
  852. {
  853. varName += " (ADVANCED)";
  854. }
  855. std::string text = notFoundMap[varName];
  856. text += "\n linked by target \"";
  857. text += l->second.GetName();
  858. text += "\" in directory ";
  859. text+=this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  860. notFoundMap[varName] = text;
  861. }
  862. }
  863. }
  864. const std::vector<std::string>& incs =
  865. this->LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  866. for( std::vector<std::string>::const_iterator incDir = incs.begin();
  867. incDir != incs.end(); ++incDir)
  868. {
  869. if(incDir->size() > 9 &&
  870. cmSystemTools::IsNOTFOUND(incDir->c_str()))
  871. {
  872. std::string varName = incDir->substr(0, incDir->size()-9);
  873. cmCacheManager::CacheIterator it =
  874. manager->GetCacheIterator(varName.c_str());
  875. if(it.GetPropertyAsBool("ADVANCED"))
  876. {
  877. varName += " (ADVANCED)";
  878. }
  879. std::string text = notFoundMap[varName];
  880. text += "\n used as include directory in directory ";
  881. text += this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  882. notFoundMap[varName] = text;
  883. }
  884. }
  885. this->CMakeInstance->UpdateProgress
  886. ("Configuring", 0.9f+0.1f*(i+1.0f)/this->LocalGenerators.size());
  887. }
  888. if(notFoundMap.size())
  889. {
  890. std::string notFoundVars;
  891. for(std::map<cmStdString, cmStdString>::const_iterator
  892. ii = notFoundMap.begin();
  893. ii != notFoundMap.end();
  894. ++ii)
  895. {
  896. notFoundVars += ii->first;
  897. notFoundVars += ii->second;
  898. notFoundVars += "\n";
  899. }
  900. cmSystemTools::Error("The following variables are used in this project, "
  901. "but they are set to NOTFOUND.\n"
  902. "Please set them or make sure they are set and "
  903. "tested correctly in the CMake files:\n",
  904. notFoundVars.c_str());
  905. }
  906. }
  907. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  908. const char *projectName,
  909. const char *target,
  910. std::string *output, cmMakefile *mf)
  911. {
  912. // if this is not set, then this is a first time configure
  913. // and there is a good chance that the try compile stuff will
  914. // take the bulk of the time, so try and guess some progress
  915. // by getting closer and closer to 100 without actually getting there.
  916. if (!this->CMakeInstance->GetCacheManager()->GetCacheValue
  917. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS"))
  918. {
  919. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  920. // we are in the first time progress and we have no
  921. // idea how long it will be. So, just move 1/10th of the way
  922. // there each time, and don't go over 95%
  923. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  924. if(this->FirstTimeProgress > 0.95f)
  925. {
  926. this->FirstTimeProgress = 0.95f;
  927. }
  928. this->CMakeInstance->UpdateProgress("Configuring",
  929. this->FirstTimeProgress);
  930. }
  931. std::string makeCommand = this->CMakeInstance->
  932. GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  933. if(makeCommand.size() == 0)
  934. {
  935. cmSystemTools::Error(
  936. "Generator cannot find the appropriate make command.");
  937. return 1;
  938. }
  939. std::string newTarget;
  940. if (target && strlen(target))
  941. {
  942. newTarget += target;
  943. #if 0
  944. #if defined(_WIN32) || defined(__CYGWIN__)
  945. std::string tmp = target;
  946. // if the target does not already end in . something
  947. // then assume .exe
  948. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  949. {
  950. newTarget += ".exe";
  951. }
  952. #endif // WIN32
  953. #endif
  954. }
  955. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  956. return this->Build(srcdir,bindir,projectName,
  957. newTarget.c_str(),
  958. output,makeCommand.c_str(),config,false,true,
  959. this->TryCompileTimeout);
  960. }
  961. std::string cmGlobalGenerator
  962. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  963. const char* additionalOptions, const char *targetName,
  964. const char* config, bool ignoreErrors, bool)
  965. {
  966. // Project name and config are not used yet.
  967. (void)projectName;
  968. (void)config;
  969. std::string makeCommand =
  970. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  971. // Since we have full control over the invocation of nmake, let us
  972. // make it quiet.
  973. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  974. {
  975. makeCommand += " /NOLOGO ";
  976. }
  977. if ( ignoreErrors )
  978. {
  979. makeCommand += " -i";
  980. }
  981. if ( additionalOptions )
  982. {
  983. makeCommand += " ";
  984. makeCommand += additionalOptions;
  985. }
  986. if ( targetName )
  987. {
  988. makeCommand += " ";
  989. makeCommand += targetName;
  990. }
  991. return makeCommand;
  992. }
  993. int cmGlobalGenerator::Build(
  994. const char *, const char *bindir,
  995. const char *projectName, const char *target,
  996. std::string *output,
  997. const char *makeCommandCSTR,
  998. const char *config,
  999. bool clean, bool fast,
  1000. double timeout)
  1001. {
  1002. /**
  1003. * Run an executable command and put the stdout in output.
  1004. */
  1005. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1006. cmSystemTools::ChangeDirectory(bindir);
  1007. if(output)
  1008. {
  1009. *output += "Change Dir: ";
  1010. *output += bindir;
  1011. *output += "\n";
  1012. }
  1013. int retVal;
  1014. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  1015. cmSystemTools::SetRunCommandHideConsole(true);
  1016. std::string outputBuffer;
  1017. std::string* outputPtr = 0;
  1018. if(output)
  1019. {
  1020. outputPtr = &outputBuffer;
  1021. }
  1022. // should we do a clean first?
  1023. if (clean)
  1024. {
  1025. std::string cleanCommand =
  1026. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  1027. 0, "clean", config, false, fast);
  1028. if(output)
  1029. {
  1030. *output += "\nRun Clean Command:";
  1031. *output += cleanCommand;
  1032. *output += "\n";
  1033. }
  1034. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), outputPtr,
  1035. &retVal, 0, false, timeout))
  1036. {
  1037. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1038. cmSystemTools::Error("Generator: execution of make clean failed.");
  1039. if (output)
  1040. {
  1041. *output += *outputPtr;
  1042. *output += "\nGenerator: execution of make clean failed.\n";
  1043. }
  1044. // return to the original directory
  1045. cmSystemTools::ChangeDirectory(cwd.c_str());
  1046. return 1;
  1047. }
  1048. if (output)
  1049. {
  1050. *output += *outputPtr;
  1051. }
  1052. }
  1053. // now build
  1054. std::string makeCommand =
  1055. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  1056. 0, target, config, false, fast);
  1057. if(output)
  1058. {
  1059. *output += "\nRun Build Command:";
  1060. *output += makeCommand;
  1061. *output += "\n";
  1062. }
  1063. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), outputPtr,
  1064. &retVal, 0, false, timeout))
  1065. {
  1066. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1067. cmSystemTools::Error
  1068. ("Generator: execution of make failed. Make command was: ",
  1069. makeCommand.c_str());
  1070. if (output)
  1071. {
  1072. *output += *outputPtr;
  1073. *output += "\nGenerator: execution of make failed. Make command was: "
  1074. + makeCommand + "\n";
  1075. }
  1076. // return to the original directory
  1077. cmSystemTools::ChangeDirectory(cwd.c_str());
  1078. return 1;
  1079. }
  1080. if (output)
  1081. {
  1082. *output += *outputPtr;
  1083. }
  1084. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1085. // The SGI MipsPro 7.3 compiler does not return an error code when
  1086. // the source has a #error in it! This is a work-around for such
  1087. // compilers.
  1088. if((retVal == 0) && (output->find("#error") != std::string::npos))
  1089. {
  1090. retVal = 1;
  1091. }
  1092. cmSystemTools::ChangeDirectory(cwd.c_str());
  1093. return retVal;
  1094. }
  1095. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  1096. {
  1097. this->LocalGenerators.push_back(lg);
  1098. // update progress
  1099. // estimate how many lg there will be
  1100. const char *numGenC =
  1101. this->CMakeInstance->GetCacheManager()->GetCacheValue
  1102. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  1103. if (!numGenC)
  1104. {
  1105. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  1106. // we are in the first time progress and we have no
  1107. // idea how long it will be. So, just move half way
  1108. // there each time, and don't go over 95%
  1109. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  1110. if(this->FirstTimeProgress > 0.95f)
  1111. {
  1112. this->FirstTimeProgress = 0.95f;
  1113. }
  1114. this->CMakeInstance->UpdateProgress("Configuring",
  1115. this->FirstTimeProgress);
  1116. return;
  1117. }
  1118. int numGen = atoi(numGenC);
  1119. float prog = 0.9f*this->LocalGenerators.size()/numGen;
  1120. if (prog > 0.9f)
  1121. {
  1122. prog = 0.9f;
  1123. }
  1124. this->CMakeInstance->UpdateProgress("Configuring", prog);
  1125. }
  1126. void cmGlobalGenerator::AddInstallComponent(const char* component)
  1127. {
  1128. if(component && *component)
  1129. {
  1130. this->InstallComponents.insert(component);
  1131. }
  1132. }
  1133. void cmGlobalGenerator::AddTargetToExports(const char* exportSetName,
  1134. cmTarget* target,
  1135. cmInstallTargetGenerator* archive,
  1136. cmInstallTargetGenerator* runTime,
  1137. cmInstallTargetGenerator* library,
  1138. cmInstallTargetGenerator* framework,
  1139. cmInstallTargetGenerator* bundle,
  1140. cmInstallFilesGenerator* headers)
  1141. {
  1142. if ((exportSetName) && (*exportSetName) && (target))
  1143. {
  1144. cmTargetExport* te = new cmTargetExport(target, archive, runTime, library,
  1145. framework, bundle, headers);
  1146. this->ExportSets[exportSetName].push_back(te);
  1147. }
  1148. }
  1149. //----------------------------------------------------------------------------
  1150. void cmGlobalGenerator::ClearExportSets()
  1151. {
  1152. for(std::map<cmStdString, std::vector<cmTargetExport*> >::iterator
  1153. setIt = this->ExportSets.begin();
  1154. setIt != this->ExportSets.end(); ++setIt)
  1155. {
  1156. for(unsigned int i = 0; i < setIt->second.size(); ++i)
  1157. {
  1158. delete setIt->second[i];
  1159. }
  1160. }
  1161. this->ExportSets.clear();
  1162. }
  1163. const std::vector<cmTargetExport*>* cmGlobalGenerator::GetExportSet(
  1164. const char* name) const
  1165. {
  1166. std::map<cmStdString, std::vector<cmTargetExport*> >::const_iterator
  1167. exportSetIt = this->ExportSets.find(name);
  1168. if (exportSetIt != this->ExportSets.end())
  1169. {
  1170. return &exportSetIt->second;
  1171. }
  1172. return 0;
  1173. }
  1174. void cmGlobalGenerator::EnableInstallTarget()
  1175. {
  1176. this->InstallTargetEnabled = true;
  1177. }
  1178. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  1179. {
  1180. cmLocalGenerator *lg = new cmLocalGenerator;
  1181. lg->SetGlobalGenerator(this);
  1182. return lg;
  1183. }
  1184. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  1185. {
  1186. std::string cfp = gen->GetCMakeInstance()->GetHomeOutputDirectory();
  1187. cfp += cmake::GetCMakeFilesDirectory();
  1188. this->SetConfiguredFilesPath(cfp.c_str());
  1189. const char* make =
  1190. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  1191. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  1192. "make program",
  1193. cmCacheManager::FILEPATH);
  1194. // copy the enabled languages
  1195. this->LanguageEnabled = gen->LanguageEnabled;
  1196. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  1197. this->IgnoreExtensions = gen->IgnoreExtensions;
  1198. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  1199. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  1200. this->OutputExtensions = gen->OutputExtensions;
  1201. }
  1202. //----------------------------------------------------------------------------
  1203. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  1204. {
  1205. entry.Name = this->GetName();
  1206. entry.Brief = "";
  1207. entry.Full = "";
  1208. }
  1209. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1210. cmLocalGenerator* gen)
  1211. {
  1212. if(!gen || gen == root)
  1213. {
  1214. // No directory excludes itself.
  1215. return false;
  1216. }
  1217. if(gen->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1218. {
  1219. // This directory is excluded from its parent.
  1220. return true;
  1221. }
  1222. // This directory is included in its parent. Check whether the
  1223. // parent is excluded.
  1224. return this->IsExcluded(root, gen->GetParent());
  1225. }
  1226. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1227. cmTarget& target)
  1228. {
  1229. if(target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1230. {
  1231. // This target is excluded from its directory.
  1232. return true;
  1233. }
  1234. else
  1235. {
  1236. // This target is included in its directory. Check whether the
  1237. // directory is excluded.
  1238. return this->IsExcluded(root, target.GetMakefile()->GetLocalGenerator());
  1239. }
  1240. }
  1241. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  1242. {
  1243. for(std::map<cmStdString, bool>::iterator i =
  1244. this->LanguageEnabled.begin(); i != this->LanguageEnabled.end(); ++i)
  1245. {
  1246. lang.push_back(i->first);
  1247. }
  1248. }
  1249. int cmGlobalGenerator::GetLinkerPreference(const char* lang)
  1250. {
  1251. std::map<cmStdString, int>::const_iterator it =
  1252. this->LanguageToLinkerPreference.find(lang);
  1253. if (it != this->LanguageToLinkerPreference.end())
  1254. {
  1255. return it->second;
  1256. }
  1257. return 0;
  1258. }
  1259. void cmGlobalGenerator::FillProjectMap()
  1260. {
  1261. this->ProjectMap.clear(); // make sure we start with a clean map
  1262. unsigned int i;
  1263. for(i = 0; i < this->LocalGenerators.size(); ++i)
  1264. {
  1265. // for each local generator add all projects
  1266. cmLocalGenerator *lg = this->LocalGenerators[i];
  1267. std::string name;
  1268. do
  1269. {
  1270. if (name != lg->GetMakefile()->GetProjectName())
  1271. {
  1272. name = lg->GetMakefile()->GetProjectName();
  1273. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  1274. }
  1275. lg = lg->GetParent();
  1276. }
  1277. while (lg);
  1278. }
  1279. }
  1280. // Build a map that contains a the set of targets used by each local
  1281. // generator directory level.
  1282. void cmGlobalGenerator::FillLocalGeneratorToTargetMap()
  1283. {
  1284. this->LocalGeneratorToTargetMap.clear();
  1285. // Loop over all targets in all local generators.
  1286. for(std::vector<cmLocalGenerator*>::const_iterator
  1287. lgi = this->LocalGenerators.begin();
  1288. lgi != this->LocalGenerators.end(); ++lgi)
  1289. {
  1290. cmLocalGenerator* lg = *lgi;
  1291. cmMakefile* mf = lg->GetMakefile();
  1292. cmTargets& targets = mf->GetTargets();
  1293. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  1294. {
  1295. cmTarget& target = t->second;
  1296. // Consider the directory containing the target and all its
  1297. // parents until something excludes the target.
  1298. for(cmLocalGenerator* clg = lg; clg && !this->IsExcluded(clg, target);
  1299. clg = clg->GetParent())
  1300. {
  1301. // This local generator includes the target.
  1302. std::set<cmTarget*>& targetSet =
  1303. this->LocalGeneratorToTargetMap[clg];
  1304. targetSet.insert(&target);
  1305. // Add dependencies of the included target. An excluded
  1306. // target may still be included if it is a dependency of a
  1307. // non-excluded target.
  1308. TargetDependSet & tgtdeps = this->GetTargetDirectDepends(target);
  1309. for(TargetDependSet::const_iterator ti = tgtdeps.begin();
  1310. ti != tgtdeps.end(); ++ti)
  1311. {
  1312. targetSet.insert(*ti);
  1313. }
  1314. }
  1315. }
  1316. }
  1317. }
  1318. ///! Find a local generator by its startdirectory
  1319. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(const char* start_dir)
  1320. {
  1321. std::vector<cmLocalGenerator*>* gens = &this->LocalGenerators;
  1322. for(unsigned int i = 0; i < gens->size(); ++i)
  1323. {
  1324. std::string sd = (*gens)[i]->GetMakefile()->GetStartDirectory();
  1325. if (sd == start_dir)
  1326. {
  1327. return (*gens)[i];
  1328. }
  1329. }
  1330. return 0;
  1331. }
  1332. //----------------------------------------------------------------------------
  1333. cmTarget*
  1334. cmGlobalGenerator::FindTarget(const char* project, const char* name)
  1335. {
  1336. // if project specific
  1337. if(project)
  1338. {
  1339. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  1340. for(unsigned int i = 0; i < gens->size(); ++i)
  1341. {
  1342. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name);
  1343. if(ret)
  1344. {
  1345. return ret;
  1346. }
  1347. }
  1348. }
  1349. // if all projects/directories
  1350. else
  1351. {
  1352. std::map<cmStdString,cmTarget *>::iterator i =
  1353. this->TotalTargets.find ( name );
  1354. if ( i != this->TotalTargets.end() )
  1355. {
  1356. return i->second;
  1357. }
  1358. }
  1359. return 0;
  1360. }
  1361. //----------------------------------------------------------------------------
  1362. bool cmGlobalGenerator::NameResolvesToFramework(const std::string& libname)
  1363. {
  1364. if(cmSystemTools::IsPathToFramework(libname.c_str()))
  1365. {
  1366. return true;
  1367. }
  1368. if(cmTarget* tgt = this->FindTarget(0, libname.c_str()))
  1369. {
  1370. if(tgt->IsFrameworkOnApple())
  1371. {
  1372. return true;
  1373. }
  1374. }
  1375. return false;
  1376. }
  1377. //----------------------------------------------------------------------------
  1378. inline std::string removeQuotes(const std::string& s)
  1379. {
  1380. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1381. {
  1382. return s.substr(1, s.size()-2);
  1383. }
  1384. return s;
  1385. }
  1386. void cmGlobalGenerator::SetCMakeInstance(cmake* cm)
  1387. {
  1388. // Store a pointer to the cmake object instance.
  1389. this->CMakeInstance = cm;
  1390. }
  1391. void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
  1392. {
  1393. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  1394. const char* cmakeCfgIntDir = this->GetCMakeCFGInitDirectory();
  1395. const char* cmakeCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1396. // CPack
  1397. cmCustomCommandLines cpackCommandLines;
  1398. std::vector<std::string> depends;
  1399. cmCustomCommandLine singleLine;
  1400. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1401. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1402. {
  1403. singleLine.push_back("-C");
  1404. singleLine.push_back(cmakeCfgIntDir);
  1405. }
  1406. singleLine.push_back("--config");
  1407. std::string configFile = mf->GetStartOutputDirectory();;
  1408. configFile += "/CPackConfig.cmake";
  1409. singleLine.push_back(configFile);
  1410. cpackCommandLines.push_back(singleLine);
  1411. if ( this->GetPreinstallTargetName() )
  1412. {
  1413. depends.push_back("preinstall");
  1414. }
  1415. else
  1416. {
  1417. const char* noPackageAll =
  1418. mf->GetDefinition("CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY");
  1419. if(!noPackageAll || cmSystemTools::IsOff(noPackageAll))
  1420. {
  1421. depends.push_back(this->GetAllTargetName());
  1422. }
  1423. }
  1424. if(cmSystemTools::FileExists(configFile.c_str()))
  1425. {
  1426. (*targets)[this->GetPackageTargetName()]
  1427. = this->CreateGlobalTarget(this->GetPackageTargetName(),
  1428. "Run CPack packaging tool...",
  1429. &cpackCommandLines, depends);
  1430. }
  1431. // CPack source
  1432. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1433. if ( packageSourceTargetName )
  1434. {
  1435. cpackCommandLines.erase(cpackCommandLines.begin(),
  1436. cpackCommandLines.end());
  1437. singleLine.erase(singleLine.begin(), singleLine.end());
  1438. depends.erase(depends.begin(), depends.end());
  1439. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1440. singleLine.push_back("--config");
  1441. configFile = mf->GetStartOutputDirectory();;
  1442. configFile += "/CPackSourceConfig.cmake";
  1443. if(cmSystemTools::FileExists(configFile.c_str()))
  1444. {
  1445. singleLine.push_back(configFile);
  1446. cpackCommandLines.push_back(singleLine);
  1447. (*targets)[packageSourceTargetName]
  1448. = this->CreateGlobalTarget(packageSourceTargetName,
  1449. "Run CPack packaging tool for source...",
  1450. &cpackCommandLines, depends);
  1451. }
  1452. }
  1453. // Test
  1454. if(mf->IsOn("CMAKE_TESTING_ENABLED"))
  1455. {
  1456. cpackCommandLines.erase(cpackCommandLines.begin(),
  1457. cpackCommandLines.end());
  1458. singleLine.erase(singleLine.begin(), singleLine.end());
  1459. depends.erase(depends.begin(), depends.end());
  1460. singleLine.push_back(this->GetCMakeInstance()->GetCTestCommand());
  1461. singleLine.push_back("--force-new-ctest-process");
  1462. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1463. {
  1464. singleLine.push_back("-C");
  1465. singleLine.push_back(cmakeCfgIntDir);
  1466. }
  1467. else // TODO: This is a hack. Should be something to do with the generator
  1468. {
  1469. singleLine.push_back("$(ARGS)");
  1470. }
  1471. cpackCommandLines.push_back(singleLine);
  1472. (*targets)[this->GetTestTargetName()]
  1473. = this->CreateGlobalTarget(this->GetTestTargetName(),
  1474. "Running tests...", &cpackCommandLines, depends);
  1475. }
  1476. //Edit Cache
  1477. const char* editCacheTargetName = this->GetEditCacheTargetName();
  1478. if ( editCacheTargetName )
  1479. {
  1480. cpackCommandLines.erase(cpackCommandLines.begin(),
  1481. cpackCommandLines.end());
  1482. singleLine.erase(singleLine.begin(), singleLine.end());
  1483. depends.erase(depends.begin(), depends.end());
  1484. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1485. // Otherwise default to the interactive command-line interface.
  1486. if(mf->GetDefinition("CMAKE_EDIT_COMMAND"))
  1487. {
  1488. singleLine.push_back(mf->GetDefinition("CMAKE_EDIT_COMMAND"));
  1489. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1490. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1491. cpackCommandLines.push_back(singleLine);
  1492. (*targets)[editCacheTargetName] =
  1493. this->CreateGlobalTarget(
  1494. editCacheTargetName, "Running CMake cache editor...",
  1495. &cpackCommandLines, depends);
  1496. }
  1497. else
  1498. {
  1499. singleLine.push_back(cmakeCommand);
  1500. singleLine.push_back("-i");
  1501. singleLine.push_back(".");
  1502. cpackCommandLines.push_back(singleLine);
  1503. (*targets)[editCacheTargetName] =
  1504. this->CreateGlobalTarget(
  1505. editCacheTargetName,
  1506. "Running interactive CMake command-line interface...",
  1507. &cpackCommandLines, depends);
  1508. }
  1509. }
  1510. //Rebuild Cache
  1511. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  1512. if ( rebuildCacheTargetName )
  1513. {
  1514. cpackCommandLines.erase(cpackCommandLines.begin(),
  1515. cpackCommandLines.end());
  1516. singleLine.erase(singleLine.begin(), singleLine.end());
  1517. depends.erase(depends.begin(), depends.end());
  1518. singleLine.push_back(cmakeCommand);
  1519. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1520. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1521. cpackCommandLines.push_back(singleLine);
  1522. (*targets)[rebuildCacheTargetName] =
  1523. this->CreateGlobalTarget(
  1524. rebuildCacheTargetName, "Running CMake to regenerate build system...",
  1525. &cpackCommandLines, depends);
  1526. }
  1527. //Install
  1528. if(this->InstallTargetEnabled)
  1529. {
  1530. if(!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.')
  1531. {
  1532. std::set<cmStdString>* componentsSet = &this->InstallComponents;
  1533. cpackCommandLines.erase(cpackCommandLines.begin(),
  1534. cpackCommandLines.end());
  1535. depends.erase(depends.begin(), depends.end());
  1536. cmOStringStream ostr;
  1537. if ( componentsSet->size() > 0 )
  1538. {
  1539. ostr << "Available install components are:";
  1540. std::set<cmStdString>::iterator it;
  1541. for (
  1542. it = componentsSet->begin();
  1543. it != componentsSet->end();
  1544. ++ it )
  1545. {
  1546. ostr << " \"" << it->c_str() << "\"";
  1547. }
  1548. }
  1549. else
  1550. {
  1551. ostr << "Only default component available";
  1552. }
  1553. singleLine.push_back(ostr.str().c_str());
  1554. (*targets)["list_install_components"]
  1555. = this->CreateGlobalTarget("list_install_components",
  1556. ostr.str().c_str(),
  1557. &cpackCommandLines, depends);
  1558. }
  1559. std::string cmd;
  1560. cpackCommandLines.erase(cpackCommandLines.begin(),
  1561. cpackCommandLines.end());
  1562. singleLine.erase(singleLine.begin(), singleLine.end());
  1563. depends.erase(depends.begin(), depends.end());
  1564. if ( this->GetPreinstallTargetName() )
  1565. {
  1566. depends.push_back(this->GetPreinstallTargetName());
  1567. }
  1568. else
  1569. {
  1570. const char* noall =
  1571. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1572. if(!noall || cmSystemTools::IsOff(noall))
  1573. {
  1574. depends.push_back(this->GetAllTargetName());
  1575. }
  1576. }
  1577. if(mf->GetDefinition("CMake_BINARY_DIR"))
  1578. {
  1579. // We are building CMake itself. We cannot use the original
  1580. // executable to install over itself.
  1581. cmd = mf->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1582. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1583. {
  1584. cmd += "/";
  1585. cmd += cmakeCfgIntDir;
  1586. }
  1587. cmd += "/cmake";
  1588. }
  1589. else
  1590. {
  1591. cmd = cmakeCommand;
  1592. }
  1593. singleLine.push_back(cmd.c_str());
  1594. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1595. {
  1596. std::string cfgArg = "-DBUILD_TYPE=";
  1597. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  1598. singleLine.push_back(cfgArg);
  1599. }
  1600. singleLine.push_back("-P");
  1601. singleLine.push_back("cmake_install.cmake");
  1602. cpackCommandLines.push_back(singleLine);
  1603. (*targets)[this->GetInstallTargetName()] =
  1604. this->CreateGlobalTarget(
  1605. this->GetInstallTargetName(), "Install the project...",
  1606. &cpackCommandLines, depends);
  1607. // install_local
  1608. if(const char* install_local = this->GetInstallLocalTargetName())
  1609. {
  1610. cmCustomCommandLine localCmdLine = singleLine;
  1611. localCmdLine.insert(localCmdLine.begin()+1,
  1612. "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  1613. cpackCommandLines.erase(cpackCommandLines.begin(),
  1614. cpackCommandLines.end());
  1615. cpackCommandLines.push_back(localCmdLine);
  1616. (*targets)[install_local] =
  1617. this->CreateGlobalTarget(
  1618. install_local, "Installing only the local directory...",
  1619. &cpackCommandLines, depends);
  1620. }
  1621. // install_strip
  1622. const char* install_strip = this->GetInstallStripTargetName();
  1623. if((install_strip !=0) && (mf->IsSet("CMAKE_STRIP")))
  1624. {
  1625. cmCustomCommandLine stripCmdLine = singleLine;
  1626. stripCmdLine.insert(stripCmdLine.begin()+1,"-DCMAKE_INSTALL_DO_STRIP=1");
  1627. cpackCommandLines.erase(cpackCommandLines.begin(),
  1628. cpackCommandLines.end());
  1629. cpackCommandLines.push_back(stripCmdLine);
  1630. (*targets)[install_strip] =
  1631. this->CreateGlobalTarget(
  1632. install_strip, "Installing the project stripped...",
  1633. &cpackCommandLines, depends);
  1634. }
  1635. }
  1636. }
  1637. cmTarget cmGlobalGenerator::CreateGlobalTarget(
  1638. const char* name, const char* message,
  1639. const cmCustomCommandLines* commandLines,
  1640. std::vector<std::string> depends,
  1641. bool depends_on_all /* = false */)
  1642. {
  1643. // Package
  1644. cmTarget target;
  1645. target.GetProperties().SetCMakeInstance(this->CMakeInstance);
  1646. target.SetType(cmTarget::GLOBAL_TARGET, name);
  1647. target.SetProperty("EXCLUDE_FROM_ALL","TRUE");
  1648. std::vector<std::string> no_outputs;
  1649. std::vector<std::string> no_depends;
  1650. // Store the custom command in the target.
  1651. cmCustomCommand cc(no_outputs, no_depends, *commandLines, 0, 0);
  1652. target.GetPostBuildCommands().push_back(cc);
  1653. target.SetProperty("EchoString", message);
  1654. if ( depends_on_all )
  1655. {
  1656. target.AddUtility("all");
  1657. }
  1658. std::vector<std::string>::iterator dit;
  1659. for ( dit = depends.begin(); dit != depends.end(); ++ dit )
  1660. {
  1661. target.AddUtility(dit->c_str());
  1662. }
  1663. return target;
  1664. }
  1665. //----------------------------------------------------------------------------
  1666. void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
  1667. const char*, std::string&)
  1668. {
  1669. // Subclasses that support multiple configurations should implement
  1670. // this method to append the subdirectory for the given build
  1671. // configuration.
  1672. }
  1673. //----------------------------------------------------------------------------
  1674. cmGlobalGenerator::TargetDependSet &
  1675. cmGlobalGenerator::GetTargetDirectDepends(cmTarget & target)
  1676. {
  1677. return this->TargetDependencies[&target];
  1678. }
  1679. void cmGlobalGenerator::AddTarget(cmTargets::value_type &v)
  1680. {
  1681. assert(!v.second.IsImported());
  1682. this->TotalTargets[v.first] = &v.second;
  1683. }
  1684. void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
  1685. cmExternalMakefileProjectGenerator *extraGenerator)
  1686. {
  1687. this->ExtraGenerator = extraGenerator;
  1688. if (this->ExtraGenerator!=0)
  1689. {
  1690. this->ExtraGenerator->SetGlobalGenerator(this);
  1691. }
  1692. }
  1693. const char* cmGlobalGenerator::GetExtraGeneratorName() const
  1694. {
  1695. return this->ExtraGenerator==0 ? 0 : this->ExtraGenerator->GetName();
  1696. }
  1697. void cmGlobalGenerator::FileReplacedDuringGenerate(const std::string& filename)
  1698. {
  1699. this->FilesReplacedDuringGenerate.push_back(filename);
  1700. }
  1701. void
  1702. cmGlobalGenerator
  1703. ::GetFilesReplacedDuringGenerate(std::vector<std::string>& filenames)
  1704. {
  1705. filenames.clear();
  1706. std::copy(
  1707. this->FilesReplacedDuringGenerate.begin(),
  1708. this->FilesReplacedDuringGenerate.end(),
  1709. std::back_inserter(filenames));
  1710. }
  1711. void
  1712. cmGlobalGenerator
  1713. ::GetTargetSets(cmGlobalGenerator::TargetDependSet& projectTargets,
  1714. cmGlobalGenerator::TargetDependSet& originalTargets,
  1715. cmLocalGenerator* root,
  1716. std::vector<cmLocalGenerator*> const& generators)
  1717. {
  1718. // loop over all local generators
  1719. for(std::vector<cmLocalGenerator*>::const_iterator i = generators.begin();
  1720. i != generators.end(); ++i)
  1721. {
  1722. // check to make sure generator is not excluded
  1723. if(this->IsExcluded(root, *i))
  1724. {
  1725. continue;
  1726. }
  1727. cmMakefile* mf = (*i)->GetMakefile();
  1728. // Get the targets in the makefile
  1729. cmTargets &tgts = mf->GetTargets();
  1730. // loop over all the targets
  1731. for (cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  1732. {
  1733. cmTarget* target = &l->second;
  1734. // put the target in the set of original targets
  1735. originalTargets.insert(target);
  1736. // Get the set of targets that depend on target
  1737. this->AddTargetDepends(target,
  1738. projectTargets);
  1739. }
  1740. }
  1741. }
  1742. void
  1743. cmGlobalGenerator::AddTargetDepends(cmTarget* target,
  1744. cmGlobalGenerator::TargetDependSet&
  1745. projectTargets)
  1746. {
  1747. // add the target itself
  1748. projectTargets.insert(target);
  1749. // get the direct depends of target
  1750. cmGlobalGenerator::TargetDependSet const& tset
  1751. = this->GetTargetDirectDepends(*target);
  1752. if(tset.size())
  1753. {
  1754. // if there are targets that depend on target
  1755. // add them and their depends as well
  1756. for(cmGlobalGenerator::TargetDependSet::const_iterator i =
  1757. tset.begin(); i != tset.end(); ++i)
  1758. {
  1759. cmTarget* dtarget = *i;
  1760. this->AddTargetDepends(dtarget, projectTargets);
  1761. }
  1762. }
  1763. }
  1764. //----------------------------------------------------------------------------
  1765. void cmGlobalGenerator::AddToManifest(const char* config,
  1766. std::string const& f)
  1767. {
  1768. // Add to the main manifest for this configuration.
  1769. this->TargetManifest[config].insert(f);
  1770. // Add to the content listing for the file's directory.
  1771. std::string dir = cmSystemTools::GetFilenamePath(f);
  1772. std::string file = cmSystemTools::GetFilenameName(f);
  1773. this->DirectoryContentMap[dir].insert(file);
  1774. }
  1775. //----------------------------------------------------------------------------
  1776. std::set<cmStdString> const&
  1777. cmGlobalGenerator::GetDirectoryContent(std::string const& dir, bool needDisk)
  1778. {
  1779. DirectoryContent& dc = this->DirectoryContentMap[dir];
  1780. if(needDisk && !dc.LoadedFromDisk)
  1781. {
  1782. // Load the directory content from disk.
  1783. cmsys::Directory d;
  1784. if(d.Load(dir.c_str()))
  1785. {
  1786. unsigned long n = d.GetNumberOfFiles();
  1787. for(unsigned long i = 0; i < n; ++i)
  1788. {
  1789. const char* f = d.GetFile(i);
  1790. if(strcmp(f, ".") != 0 && strcmp(f, "..") != 0)
  1791. {
  1792. dc.insert(f);
  1793. }
  1794. }
  1795. }
  1796. dc.LoadedFromDisk = true;
  1797. }
  1798. return dc;
  1799. }
  1800. //----------------------------------------------------------------------------
  1801. void
  1802. cmGlobalGenerator::AddRuleHash(const std::vector<std::string>& outputs,
  1803. std::vector<std::string>::const_iterator first,
  1804. std::vector<std::string>::const_iterator last)
  1805. {
  1806. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1807. // Ignore if there are no outputs.
  1808. if(outputs.empty())
  1809. {
  1810. return;
  1811. }
  1812. // Compute a hash of the rule.
  1813. RuleHash hash;
  1814. {
  1815. unsigned char const* data;
  1816. int length;
  1817. cmsysMD5* sum = cmsysMD5_New();
  1818. cmsysMD5_Initialize(sum);
  1819. for(std::vector<std::string>::const_iterator i = first; i != last; ++i)
  1820. {
  1821. data = reinterpret_cast<unsigned char const*>(i->c_str());
  1822. length = static_cast<int>(i->length());
  1823. cmsysMD5_Append(sum, data, length);
  1824. }
  1825. cmsysMD5_FinalizeHex(sum, hash.Data);
  1826. cmsysMD5_Delete(sum);
  1827. }
  1828. // Shorten the output name (in expected use case).
  1829. cmLocalGenerator* lg = this->GetLocalGenerators()[0];
  1830. std::string fname = lg->Convert(outputs[0].c_str(),
  1831. cmLocalGenerator::HOME_OUTPUT);
  1832. // Associate the hash with this output.
  1833. this->RuleHashes[fname] = hash;
  1834. #else
  1835. (void)outputs;
  1836. (void)first;
  1837. (void)last;
  1838. #endif
  1839. }
  1840. //----------------------------------------------------------------------------
  1841. void cmGlobalGenerator::CheckRuleHashes()
  1842. {
  1843. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1844. std::string home = this->GetCMakeInstance()->GetHomeOutputDirectory();
  1845. std::string pfile = home;
  1846. pfile += this->GetCMakeInstance()->GetCMakeFilesDirectory();
  1847. pfile += "/CMakeRuleHashes.txt";
  1848. #if defined(_WIN32) || defined(__CYGWIN__)
  1849. std::ifstream fin(pfile.c_str(), std::ios::in | std::ios::binary);
  1850. #else
  1851. std::ifstream fin(pfile.c_str(), std::ios::in);
  1852. #endif
  1853. std::string line;
  1854. std::string fname;
  1855. while(cmSystemTools::GetLineFromStream(fin, line))
  1856. {
  1857. // Line format is a 32-byte hex string followed by a space
  1858. // followed by a file name (with no escaping).
  1859. // Skip blank and comment lines.
  1860. if(line.size() < 34 || line[0] == '#')
  1861. {
  1862. continue;
  1863. }
  1864. // Get the filename.
  1865. fname = line.substr(33, line.npos);
  1866. // Look for a hash for this file's rule.
  1867. std::map<cmStdString, RuleHash>::const_iterator rhi =
  1868. this->RuleHashes.find(fname);
  1869. if(rhi != this->RuleHashes.end())
  1870. {
  1871. // Compare the rule hash in the file to that we were given.
  1872. if(strncmp(line.c_str(), rhi->second.Data, 32) != 0)
  1873. {
  1874. // The rule has changed. Delete the output so it will be
  1875. // built again.
  1876. fname = cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  1877. cmSystemTools::RemoveFile(fname.c_str());
  1878. }
  1879. }
  1880. else
  1881. {
  1882. // We have no hash for a rule previously listed. This may be a
  1883. // case where a user has turned off a build option and might
  1884. // want to turn it back on later, so do not delete the file.
  1885. // Instead, we keep the rule hash as long as the file exists so
  1886. // that if the feature is turned back on and the rule has
  1887. // changed the file is still rebuilt.
  1888. std::string fpath =
  1889. cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  1890. if(cmSystemTools::FileExists(fpath.c_str()))
  1891. {
  1892. RuleHash hash;
  1893. strncpy(hash.Data, line.c_str(), 32);
  1894. this->RuleHashes[fname] = hash;
  1895. }
  1896. }
  1897. }
  1898. // Now generate a new persistence file with the current hashes.
  1899. if(this->RuleHashes.empty())
  1900. {
  1901. cmSystemTools::RemoveFile(pfile.c_str());
  1902. }
  1903. else
  1904. {
  1905. cmGeneratedFileStream fout(pfile.c_str());
  1906. fout << "# Hashes of file build rules.\n";
  1907. for(std::map<cmStdString, RuleHash>::const_iterator
  1908. rhi = this->RuleHashes.begin(); rhi != this->RuleHashes.end(); ++rhi)
  1909. {
  1910. fout.write(rhi->second.Data, 32);
  1911. fout << " " << rhi->first << "\n";
  1912. }
  1913. }
  1914. #endif
  1915. }