cmGlobalGenerator.cxx 71 KB

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