cmGlobalGenerator.cxx 85 KB

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