cmGlobalGenerator.cxx 88 KB

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