cmGlobalGenerator.cxx 74 KB

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