cmGlobalGenerator.cxx 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #if defined(_WIN32) && !defined(__CYGWIN__)
  11. #include "windows.h" // this must be first to define GetCurrentDirectory
  12. #endif
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmExternalMakefileProjectGenerator.h"
  16. #include "cmake.h"
  17. #include "cmMakefile.h"
  18. #include "cmQtAutomoc.h"
  19. #include "cmSourceFile.h"
  20. #include "cmVersion.h"
  21. #include "cmTargetExport.h"
  22. #include "cmComputeTargetDepends.h"
  23. #include "cmGeneratedFileStream.h"
  24. #include "cmGeneratorTarget.h"
  25. #include "cmGeneratorExpression.h"
  26. #include "cmGeneratorExpressionEvaluationFile.h"
  27. #include "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. } // end if in try compile
  586. } // end need test language
  587. // Store the shared library flags so that we can satisfy CMP0018
  588. std::string sharedLibFlagsVar = "CMAKE_SHARED_LIBRARY_";
  589. sharedLibFlagsVar += lang;
  590. sharedLibFlagsVar += "_FLAGS";
  591. const char* sharedLibFlags =
  592. mf->GetSafeDefinition(sharedLibFlagsVar.c_str());
  593. if (sharedLibFlags)
  594. {
  595. this->LanguageToOriginalSharedLibFlags[lang] = sharedLibFlags;
  596. }
  597. // Translate compiler ids for compatibility.
  598. this->CheckCompilerIdCompatibility(mf, lang);
  599. } // end for each language
  600. // Now load files that can override any settings on the platform or for
  601. // the project First load the project compatibility file if it is in
  602. // cmake
  603. std::string projectCompatibility = mf->GetDefinition("CMAKE_ROOT");
  604. projectCompatibility += "/Modules/";
  605. projectCompatibility += mf->GetSafeDefinition("PROJECT_NAME");
  606. projectCompatibility += "Compatibility.cmake";
  607. if(cmSystemTools::FileExists(projectCompatibility.c_str()))
  608. {
  609. mf->ReadListFile(0,projectCompatibility.c_str());
  610. }
  611. }
  612. //----------------------------------------------------------------------------
  613. void cmGlobalGenerator::CheckCompilerIdCompatibility(cmMakefile* mf,
  614. std::string lang)
  615. {
  616. std::string compilerIdVar = "CMAKE_" + lang + "_COMPILER_ID";
  617. const char* compilerId = mf->GetDefinition(compilerIdVar.c_str());
  618. if(compilerId && strcmp(compilerId, "AppleClang") == 0)
  619. {
  620. cmPolicies* policies = this->CMakeInstance->GetPolicies();
  621. switch(mf->GetPolicyStatus(cmPolicies::CMP0025))
  622. {
  623. case cmPolicies::WARN:
  624. if(!this->CMakeInstance->GetIsInTryCompile())
  625. {
  626. cmOStringStream w;
  627. w << policies->GetPolicyWarning(cmPolicies::CMP0025) << "\n"
  628. "Converting " << lang <<
  629. " compiler id \"AppleClang\" to \"Clang\" for compatibility."
  630. ;
  631. mf->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  632. }
  633. case cmPolicies::OLD:
  634. // OLD behavior is to convert AppleClang to Clang.
  635. mf->AddDefinition(compilerIdVar.c_str(), "Clang");
  636. break;
  637. case cmPolicies::REQUIRED_IF_USED:
  638. case cmPolicies::REQUIRED_ALWAYS:
  639. mf->IssueMessage(
  640. cmake::FATAL_ERROR,
  641. policies->GetRequiredPolicyError(cmPolicies::CMP0025)
  642. );
  643. case cmPolicies::NEW:
  644. // NEW behavior is to keep AppleClang.
  645. break;
  646. }
  647. }
  648. }
  649. //----------------------------------------------------------------------------
  650. const char*
  651. cmGlobalGenerator::GetLanguageOutputExtension(cmSourceFile const& source)
  652. {
  653. if(const char* lang = source.GetLanguage())
  654. {
  655. if(this->LanguageToOutputExtension.count(lang) > 0)
  656. {
  657. return this->LanguageToOutputExtension[lang].c_str();
  658. }
  659. }
  660. else
  661. {
  662. // if no language is found then check to see if it is already an
  663. // ouput extension for some language. In that case it should be ignored
  664. // and in this map, so it will not be compiled but will just be used.
  665. std::string const& ext = source.GetExtension();
  666. if(!ext.empty())
  667. {
  668. if(this->OutputExtensions.count(ext))
  669. {
  670. return ext.c_str();
  671. }
  672. }
  673. }
  674. return "";
  675. }
  676. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  677. {
  678. // if there is an extension and it starts with . then move past the
  679. // . because the extensions are not stored with a . in the map
  680. if(ext && *ext == '.')
  681. {
  682. ++ext;
  683. }
  684. if(this->ExtensionToLanguage.count(ext) > 0)
  685. {
  686. return this->ExtensionToLanguage[ext].c_str();
  687. }
  688. return 0;
  689. }
  690. /* SetLanguageEnabled() is now split in two parts:
  691. at first the enabled-flag is set. This can then be used in EnabledLanguage()
  692. for checking whether the language is already enabled. After setting this
  693. flag still the values from the cmake variables have to be copied into the
  694. internal maps, this is done in SetLanguageEnabledMaps() which is called
  695. after the system- and compiler specific files have been loaded.
  696. This split was done originally so that compiler-specific configuration
  697. files could change the object file extension
  698. (CMAKE_<LANG>_OUTPUT_EXTENSION) before the CMake variables were copied
  699. to the C++ maps.
  700. */
  701. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  702. {
  703. this->SetLanguageEnabledFlag(l, mf);
  704. this->SetLanguageEnabledMaps(l, mf);
  705. }
  706. void cmGlobalGenerator::SetLanguageEnabledFlag(const char* l, cmMakefile* mf)
  707. {
  708. this->LanguageEnabled[l] = true;
  709. // Fill the language-to-extension map with the current variable
  710. // settings to make sure it is available for the try_compile()
  711. // command source file signature. In SetLanguageEnabledMaps this
  712. // will be done again to account for any compiler- or
  713. // platform-specific entries.
  714. this->FillExtensionToLanguageMap(l, mf);
  715. }
  716. void cmGlobalGenerator::SetLanguageEnabledMaps(const char* l, cmMakefile* mf)
  717. {
  718. // use LanguageToLinkerPreference to detect whether this functions has
  719. // run before
  720. if (this->LanguageToLinkerPreference.find(l) !=
  721. this->LanguageToLinkerPreference.end())
  722. {
  723. return;
  724. }
  725. std::string linkerPrefVar = std::string("CMAKE_") +
  726. std::string(l) + std::string("_LINKER_PREFERENCE");
  727. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  728. int preference = 0;
  729. if(linkerPref)
  730. {
  731. if (sscanf(linkerPref, "%d", &preference)!=1)
  732. {
  733. // backward compatibility: before 2.6 LINKER_PREFERENCE
  734. // was either "None" or "Prefered", and only the first character was
  735. // tested. So if there is a custom language out there and it is
  736. // "Prefered", set its preference high
  737. if (linkerPref[0]=='P')
  738. {
  739. preference = 100;
  740. }
  741. else
  742. {
  743. preference = 0;
  744. }
  745. }
  746. }
  747. if (preference < 0)
  748. {
  749. std::string msg = linkerPrefVar;
  750. msg += " is negative, adjusting it to 0";
  751. cmSystemTools::Message(msg.c_str(), "Warning");
  752. preference = 0;
  753. }
  754. this->LanguageToLinkerPreference[l] = preference;
  755. std::string outputExtensionVar = std::string("CMAKE_") +
  756. std::string(l) + std::string("_OUTPUT_EXTENSION");
  757. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  758. if(outputExtension)
  759. {
  760. this->LanguageToOutputExtension[l] = outputExtension;
  761. this->OutputExtensions[outputExtension] = outputExtension;
  762. if(outputExtension[0] == '.')
  763. {
  764. this->OutputExtensions[outputExtension+1] = outputExtension+1;
  765. }
  766. }
  767. // The map was originally filled by SetLanguageEnabledFlag, but
  768. // since then the compiler- and platform-specific files have been
  769. // loaded which might have added more entries.
  770. this->FillExtensionToLanguageMap(l, mf);
  771. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  772. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  773. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  774. std::vector<std::string> extensionList;
  775. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  776. for(std::vector<std::string>::iterator i = extensionList.begin();
  777. i != extensionList.end(); ++i)
  778. {
  779. this->IgnoreExtensions[*i] = true;
  780. }
  781. }
  782. void cmGlobalGenerator::FillExtensionToLanguageMap(const char* l,
  783. cmMakefile* mf)
  784. {
  785. std::string extensionsVar = std::string("CMAKE_") +
  786. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  787. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  788. std::vector<std::string> extensionList;
  789. cmSystemTools::ExpandListArgument(exts, extensionList);
  790. for(std::vector<std::string>::iterator i = extensionList.begin();
  791. i != extensionList.end(); ++i)
  792. {
  793. this->ExtensionToLanguage[*i] = l;
  794. }
  795. }
  796. bool cmGlobalGenerator::IgnoreFile(const char* l)
  797. {
  798. if(this->GetLanguageFromExtension(l))
  799. {
  800. return false;
  801. }
  802. return (this->IgnoreExtensions.count(l) > 0);
  803. }
  804. bool cmGlobalGenerator::GetLanguageEnabled(const char* l) const
  805. {
  806. return (this->LanguageEnabled.find(l)!= this->LanguageEnabled.end());
  807. }
  808. void cmGlobalGenerator::ClearEnabledLanguages()
  809. {
  810. this->LanguageEnabled.clear();
  811. }
  812. bool cmGlobalGenerator::IsDependedOn(const char* project,
  813. cmTarget* targetIn)
  814. {
  815. // Get all local gens for this project
  816. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  817. // loop over local gens and get the targets for each one
  818. for(unsigned int i = 0; i < gens->size(); ++i)
  819. {
  820. cmTargets& targets = (*gens)[i]->GetMakefile()->GetTargets();
  821. for (cmTargets::iterator l = targets.begin();
  822. l != targets.end(); l++)
  823. {
  824. cmTarget& target = l->second;
  825. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  826. if(tgtdeps.count(targetIn))
  827. {
  828. return true;
  829. }
  830. }
  831. }
  832. return false;
  833. }
  834. void cmGlobalGenerator::Configure()
  835. {
  836. this->FirstTimeProgress = 0.0f;
  837. this->ClearGeneratorTargets();
  838. this->ExportSets.clear();
  839. // Delete any existing cmLocalGenerators
  840. unsigned int i;
  841. for (i = 0; i < this->LocalGenerators.size(); ++i)
  842. {
  843. delete this->LocalGenerators[i];
  844. }
  845. this->LocalGenerators.clear();
  846. this->TargetDependencies.clear();
  847. this->TotalTargets.clear();
  848. this->ImportedTargets.clear();
  849. this->LocalGeneratorToTargetMap.clear();
  850. this->ProjectMap.clear();
  851. this->RuleHashes.clear();
  852. this->DirectoryContentMap.clear();
  853. this->BinaryDirectories.clear();
  854. // start with this directory
  855. cmLocalGenerator *lg = this->CreateLocalGenerator();
  856. this->LocalGenerators.push_back(lg);
  857. // set the Start directories
  858. cmMakefile* mf = lg->GetMakefile();
  859. lg->GetMakefile()->SetStartDirectory
  860. (this->CMakeInstance->GetStartDirectory());
  861. lg->GetMakefile()->SetStartOutputDirectory
  862. (this->CMakeInstance->GetStartOutputDirectory());
  863. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  864. this->BinaryDirectories.insert(mf->GetStartOutputDirectory());
  865. // now do it
  866. lg->Configure();
  867. // update the cache entry for the number of local generators, this is used
  868. // for progress
  869. char num[100];
  870. sprintf(num,"%d",static_cast<int>(this->LocalGenerators.size()));
  871. this->GetCMakeInstance()->AddCacheEntry
  872. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  873. "number of local generators", cmCacheManager::INTERNAL);
  874. // check for link libraries and include directories containing "NOTFOUND"
  875. // and for infinite loops
  876. this->CheckLocalGenerators();
  877. // at this point this->LocalGenerators has been filled,
  878. // so create the map from project name to vector of local generators
  879. this->FillProjectMap();
  880. if ( this->CMakeInstance->GetWorkingMode() == cmake::NORMAL_MODE)
  881. {
  882. cmOStringStream msg;
  883. if(cmSystemTools::GetErrorOccuredFlag())
  884. {
  885. msg << "Configuring incomplete, errors occurred!";
  886. const char* logs[] = {"CMakeOutput.log", "CMakeError.log", 0};
  887. for(const char** log = logs; *log; ++log)
  888. {
  889. std::string f = this->CMakeInstance->GetHomeOutputDirectory();
  890. f += this->CMakeInstance->GetCMakeFilesDirectory();
  891. f += "/";
  892. f += *log;
  893. if(cmSystemTools::FileExists(f.c_str()))
  894. {
  895. msg << "\nSee also \"" << f << "\".";
  896. }
  897. }
  898. }
  899. else
  900. {
  901. msg << "Configuring done";
  902. }
  903. this->CMakeInstance->UpdateProgress(msg.str().c_str(), -1);
  904. }
  905. }
  906. cmExportBuildFileGenerator*
  907. cmGlobalGenerator::GetExportedTargetsFile(const std::string &filename) const
  908. {
  909. std::map<std::string, cmExportBuildFileGenerator*>::const_iterator it
  910. = this->BuildExportSets.find(filename);
  911. return it == this->BuildExportSets.end() ? 0 : it->second;
  912. }
  913. bool cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS()
  914. {
  915. // If the property is not enabled then okay.
  916. if(!this->CMakeInstance
  917. ->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  918. {
  919. return true;
  920. }
  921. // This generator does not support duplicate custom targets.
  922. cmOStringStream e;
  923. e << "This project has enabled the ALLOW_DUPLICATE_CUSTOM_TARGETS "
  924. << "global property. "
  925. << "The \"" << this->GetName() << "\" generator does not support "
  926. << "duplicate custom targets. "
  927. << "Consider using a Makefiles generator or fix the project to not "
  928. << "use duplicate target names.";
  929. cmSystemTools::Error(e.str().c_str());
  930. return false;
  931. }
  932. void cmGlobalGenerator::Generate()
  933. {
  934. // Some generators track files replaced during the Generate.
  935. // Start with an empty vector:
  936. this->FilesReplacedDuringGenerate.clear();
  937. // Check whether this generator is allowed to run.
  938. if(!this->CheckALLOW_DUPLICATE_CUSTOM_TARGETS())
  939. {
  940. return;
  941. }
  942. // Check that all targets are valid.
  943. if(!this->CheckTargets())
  944. {
  945. return;
  946. }
  947. // Iterate through all targets and set up automoc for those which have
  948. // the AUTOMOC property set
  949. this->CreateAutomocTargets();
  950. // For each existing cmLocalGenerator
  951. unsigned int i;
  952. // Put a copy of each global target in every directory.
  953. cmTargets globalTargets;
  954. this->CreateDefaultGlobalTargets(&globalTargets);
  955. for (i = 0; i < this->LocalGenerators.size(); ++i)
  956. {
  957. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  958. cmTargets* targets = &(mf->GetTargets());
  959. cmTargets::iterator tit;
  960. for ( tit = globalTargets.begin(); tit != globalTargets.end(); ++ tit )
  961. {
  962. (*targets)[tit->first] = tit->second;
  963. (*targets)[tit->first].SetMakefile(mf);
  964. }
  965. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  966. {
  967. tit->second.AppendBuildInterfaceIncludes();
  968. }
  969. }
  970. // Add generator specific helper commands
  971. for (i = 0; i < this->LocalGenerators.size(); ++i)
  972. {
  973. this->LocalGenerators[i]->AddHelperCommands();
  974. }
  975. // Create per-target generator information.
  976. this->CreateGeneratorTargets();
  977. // Trace the dependencies, after that no custom commands should be added
  978. // because their dependencies might not be handled correctly
  979. for (i = 0; i < this->LocalGenerators.size(); ++i)
  980. {
  981. this->LocalGenerators[i]->TraceDependencies();
  982. }
  983. // Compute the manifest of main targets generated.
  984. for (i = 0; i < this->LocalGenerators.size(); ++i)
  985. {
  986. this->LocalGenerators[i]->GenerateTargetManifest();
  987. }
  988. this->ComputeGeneratorTargetObjects();
  989. this->ProcessEvaluationFiles();
  990. // Compute the inter-target dependencies.
  991. if(!this->ComputeTargetDepends())
  992. {
  993. return;
  994. }
  995. // Create a map from local generator to the complete set of targets
  996. // it builds by default.
  997. this->FillLocalGeneratorToTargetMap();
  998. for (i = 0; i < this->LocalGenerators.size(); ++i)
  999. {
  1000. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  1001. cmTargets* targets = &(mf->GetTargets());
  1002. for ( cmTargets::iterator it = targets->begin();
  1003. it != targets->end(); ++ it )
  1004. {
  1005. it->second.FinalizeSystemIncludeDirectories();
  1006. }
  1007. }
  1008. // Generate project files
  1009. for (i = 0; i < this->LocalGenerators.size(); ++i)
  1010. {
  1011. this->LocalGenerators[i]->GetMakefile()->SetGeneratingBuildSystem();
  1012. this->SetCurrentLocalGenerator(this->LocalGenerators[i]);
  1013. this->LocalGenerators[i]->Generate();
  1014. this->LocalGenerators[i]->GenerateInstallRules();
  1015. this->LocalGenerators[i]->GenerateTestFiles();
  1016. this->CMakeInstance->UpdateProgress("Generating",
  1017. (static_cast<float>(i)+1.0f)/
  1018. static_cast<float>(this->LocalGenerators.size()));
  1019. }
  1020. this->SetCurrentLocalGenerator(0);
  1021. for (std::map<std::string, cmExportBuildFileGenerator*>::iterator
  1022. it = this->BuildExportSets.begin(); it != this->BuildExportSets.end();
  1023. ++it)
  1024. {
  1025. if (!it->second->GenerateImportFile()
  1026. && !cmSystemTools::GetErrorOccuredFlag())
  1027. {
  1028. this->GetCMakeInstance()
  1029. ->IssueMessage(cmake::FATAL_ERROR, "Could not write export file.",
  1030. cmListFileBacktrace());
  1031. return;
  1032. }
  1033. }
  1034. // Update rule hashes.
  1035. this->CheckRuleHashes();
  1036. this->WriteSummary();
  1037. if (this->ExtraGenerator != 0)
  1038. {
  1039. this->ExtraGenerator->Generate();
  1040. }
  1041. this->CMakeInstance->UpdateProgress("Generating done", -1);
  1042. }
  1043. //----------------------------------------------------------------------------
  1044. bool cmGlobalGenerator::ComputeTargetDepends()
  1045. {
  1046. cmComputeTargetDepends ctd(this);
  1047. if(!ctd.Compute())
  1048. {
  1049. return false;
  1050. }
  1051. std::vector<cmTarget*> const& targets = ctd.GetTargets();
  1052. for(std::vector<cmTarget*>::const_iterator ti = targets.begin();
  1053. ti != targets.end(); ++ti)
  1054. {
  1055. ctd.GetTargetDirectDepends(*ti, this->TargetDependencies[*ti]);
  1056. }
  1057. return true;
  1058. }
  1059. //----------------------------------------------------------------------------
  1060. bool cmGlobalGenerator::CheckTargets()
  1061. {
  1062. // Make sure all targets can find their source files.
  1063. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  1064. {
  1065. cmTargets& targets =
  1066. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  1067. for(cmTargets::iterator ti = targets.begin();
  1068. ti != targets.end(); ++ti)
  1069. {
  1070. cmTarget& target = ti->second;
  1071. if(target.GetType() == cmTarget::EXECUTABLE ||
  1072. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1073. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1074. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1075. target.GetType() == cmTarget::UTILITY)
  1076. {
  1077. if(!target.FindSourceFiles())
  1078. {
  1079. return false;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. return true;
  1085. }
  1086. //----------------------------------------------------------------------------
  1087. void cmGlobalGenerator::CreateAutomocTargets()
  1088. {
  1089. #ifdef CMAKE_BUILD_WITH_CMAKE
  1090. typedef std::vector<std::pair<cmQtAutomoc, cmTarget*> > Automocs;
  1091. Automocs automocs;
  1092. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  1093. {
  1094. cmTargets& targets =
  1095. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  1096. for(cmTargets::iterator ti = targets.begin();
  1097. ti != targets.end(); ++ti)
  1098. {
  1099. cmTarget& target = ti->second;
  1100. if(target.GetType() == cmTarget::EXECUTABLE ||
  1101. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1102. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1103. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1104. target.GetType() == cmTarget::OBJECT_LIBRARY)
  1105. {
  1106. if(target.GetPropertyAsBool("AUTOMOC") && !target.IsImported())
  1107. {
  1108. cmQtAutomoc automoc;
  1109. if(automoc.InitializeMocSourceFile(&target))
  1110. {
  1111. automocs.push_back(std::make_pair(automoc, &target));
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. for (Automocs::iterator it = automocs.begin(); it != automocs.end();
  1118. ++it)
  1119. {
  1120. it->first.SetupAutomocTarget(it->second);
  1121. }
  1122. #endif
  1123. }
  1124. //----------------------------------------------------------------------------
  1125. void cmGlobalGenerator::CreateGeneratorTargets()
  1126. {
  1127. // Construct per-target generator information.
  1128. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  1129. {
  1130. cmGeneratorTargetsType generatorTargets;
  1131. cmMakefile *mf = this->LocalGenerators[i]->GetMakefile();
  1132. const std::vector<cmValueWithOrigin> noconfig_compile_definitions =
  1133. mf->GetCompileDefinitionsEntries();
  1134. std::vector<std::string> configs;
  1135. mf->GetConfigurations(configs);
  1136. cmTargets& targets = mf->GetTargets();
  1137. for(cmTargets::iterator ti = targets.begin();
  1138. ti != targets.end(); ++ti)
  1139. {
  1140. cmTarget* t = &ti->second;
  1141. {
  1142. for (std::vector<cmValueWithOrigin>::const_iterator it
  1143. = noconfig_compile_definitions.begin();
  1144. it != noconfig_compile_definitions.end(); ++it)
  1145. {
  1146. t->InsertCompileDefinition(*it);
  1147. }
  1148. for(std::vector<std::string>::const_iterator ci = configs.begin();
  1149. ci != configs.end(); ++ci)
  1150. {
  1151. std::string defPropName = "COMPILE_DEFINITIONS_";
  1152. defPropName += cmSystemTools::UpperCase(*ci);
  1153. t->AppendProperty(defPropName.c_str(),
  1154. mf->GetProperty(defPropName.c_str()));
  1155. }
  1156. }
  1157. cmGeneratorTarget* gt = new cmGeneratorTarget(t);
  1158. this->GeneratorTargets[t] = gt;
  1159. generatorTargets[t] = gt;
  1160. }
  1161. for(std::vector<cmTarget*>::const_iterator
  1162. j = mf->GetOwnedImportedTargets().begin();
  1163. j != mf->GetOwnedImportedTargets().end(); ++j)
  1164. {
  1165. cmGeneratorTarget* gt = new cmGeneratorTarget(*j);
  1166. this->GeneratorTargets[*j] = gt;
  1167. generatorTargets[*j] = gt;
  1168. }
  1169. mf->SetGeneratorTargets(generatorTargets);
  1170. }
  1171. }
  1172. //----------------------------------------------------------------------------
  1173. void cmGlobalGenerator::ComputeGeneratorTargetObjects()
  1174. {
  1175. // Construct per-target generator information.
  1176. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  1177. {
  1178. cmMakefile *mf = this->LocalGenerators[i]->GetMakefile();
  1179. cmGeneratorTargetsType targets = mf->GetGeneratorTargets();
  1180. for(cmGeneratorTargetsType::iterator ti = targets.begin();
  1181. ti != targets.end(); ++ti)
  1182. {
  1183. cmGeneratorTarget* gt = ti->second;
  1184. gt->ClassifySources();
  1185. gt->LookupObjectLibraries();
  1186. this->ComputeTargetObjects(gt);
  1187. }
  1188. }
  1189. }
  1190. //----------------------------------------------------------------------------
  1191. void cmGlobalGenerator::ClearGeneratorTargets()
  1192. {
  1193. for(cmGeneratorTargetsType::iterator i = this->GeneratorTargets.begin();
  1194. i != this->GeneratorTargets.end(); ++i)
  1195. {
  1196. delete i->second;
  1197. }
  1198. this->GeneratorTargets.clear();
  1199. }
  1200. //----------------------------------------------------------------------------
  1201. cmGeneratorTarget* cmGlobalGenerator::GetGeneratorTarget(cmTarget* t) const
  1202. {
  1203. cmGeneratorTargetsType::const_iterator ti = this->GeneratorTargets.find(t);
  1204. if(ti == this->GeneratorTargets.end())
  1205. {
  1206. this->CMakeInstance->IssueMessage(
  1207. cmake::INTERNAL_ERROR, "Missing cmGeneratorTarget instance!",
  1208. cmListFileBacktrace());
  1209. return 0;
  1210. }
  1211. return ti->second;
  1212. }
  1213. //----------------------------------------------------------------------------
  1214. void cmGlobalGenerator::ComputeTargetObjects(cmGeneratorTarget*) const
  1215. {
  1216. // Implemented in generator subclasses that need this.
  1217. }
  1218. void cmGlobalGenerator::CheckLocalGenerators()
  1219. {
  1220. std::map<cmStdString, cmStdString> notFoundMap;
  1221. // std::set<cmStdString> notFoundMap;
  1222. // after it is all done do a ConfigureFinalPass
  1223. cmCacheManager* manager = 0;
  1224. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  1225. {
  1226. manager = this->LocalGenerators[i]->GetMakefile()->GetCacheManager();
  1227. this->LocalGenerators[i]->ConfigureFinalPass();
  1228. cmTargets &targets =
  1229. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  1230. for (cmTargets::iterator l = targets.begin();
  1231. l != targets.end(); l++)
  1232. {
  1233. const cmTarget::LinkLibraryVectorType& libs =
  1234. l->second.GetOriginalLinkLibraries();
  1235. for(cmTarget::LinkLibraryVectorType::const_iterator lib = libs.begin();
  1236. lib != libs.end(); ++lib)
  1237. {
  1238. if(lib->first.size() > 9 &&
  1239. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  1240. {
  1241. std::string varName = lib->first.substr(0, lib->first.size()-9);
  1242. cmCacheManager::CacheIterator it =
  1243. manager->GetCacheIterator(varName.c_str());
  1244. if(it.GetPropertyAsBool("ADVANCED"))
  1245. {
  1246. varName += " (ADVANCED)";
  1247. }
  1248. std::string text = notFoundMap[varName];
  1249. text += "\n linked by target \"";
  1250. text += l->second.GetName();
  1251. text += "\" in directory ";
  1252. text+=this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  1253. notFoundMap[varName] = text;
  1254. }
  1255. }
  1256. std::vector<std::string> incs;
  1257. const char *incDirProp = l->second.GetProperty("INCLUDE_DIRECTORIES");
  1258. if (!incDirProp)
  1259. {
  1260. continue;
  1261. }
  1262. std::string incDirs = cmGeneratorExpression::Preprocess(incDirProp,
  1263. cmGeneratorExpression::StripAllGeneratorExpressions);
  1264. cmSystemTools::ExpandListArgument(incDirs.c_str(), incs);
  1265. for( std::vector<std::string>::const_iterator incDir = incs.begin();
  1266. incDir != incs.end(); ++incDir)
  1267. {
  1268. if(incDir->size() > 9 &&
  1269. cmSystemTools::IsNOTFOUND(incDir->c_str()))
  1270. {
  1271. std::string varName = incDir->substr(0, incDir->size()-9);
  1272. cmCacheManager::CacheIterator it =
  1273. manager->GetCacheIterator(varName.c_str());
  1274. if(it.GetPropertyAsBool("ADVANCED"))
  1275. {
  1276. varName += " (ADVANCED)";
  1277. }
  1278. std::string text = notFoundMap[varName];
  1279. text += "\n used as include directory in directory ";
  1280. text += this->LocalGenerators[i]
  1281. ->GetMakefile()->GetCurrentDirectory();
  1282. notFoundMap[varName] = text;
  1283. }
  1284. }
  1285. }
  1286. this->CMakeInstance->UpdateProgress
  1287. ("Configuring", 0.9f+0.1f*(static_cast<float>(i)+1.0f)/
  1288. static_cast<float>(this->LocalGenerators.size()));
  1289. }
  1290. if(notFoundMap.size())
  1291. {
  1292. std::string notFoundVars;
  1293. for(std::map<cmStdString, cmStdString>::const_iterator
  1294. ii = notFoundMap.begin();
  1295. ii != notFoundMap.end();
  1296. ++ii)
  1297. {
  1298. notFoundVars += ii->first;
  1299. notFoundVars += ii->second;
  1300. notFoundVars += "\n";
  1301. }
  1302. cmSystemTools::Error("The following variables are used in this project, "
  1303. "but they are set to NOTFOUND.\n"
  1304. "Please set them or make sure they are set and "
  1305. "tested correctly in the CMake files:\n",
  1306. notFoundVars.c_str());
  1307. }
  1308. }
  1309. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  1310. const char *projectName,
  1311. const char *target, bool fast,
  1312. std::string *output, cmMakefile *mf)
  1313. {
  1314. // if this is not set, then this is a first time configure
  1315. // and there is a good chance that the try compile stuff will
  1316. // take the bulk of the time, so try and guess some progress
  1317. // by getting closer and closer to 100 without actually getting there.
  1318. if (!this->CMakeInstance->GetCacheManager()->GetCacheValue
  1319. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS"))
  1320. {
  1321. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  1322. // we are in the first time progress and we have no
  1323. // idea how long it will be. So, just move 1/10th of the way
  1324. // there each time, and don't go over 95%
  1325. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  1326. if(this->FirstTimeProgress > 0.95f)
  1327. {
  1328. this->FirstTimeProgress = 0.95f;
  1329. }
  1330. this->CMakeInstance->UpdateProgress("Configuring",
  1331. this->FirstTimeProgress);
  1332. }
  1333. std::string makeCommand = this->CMakeInstance->
  1334. GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  1335. if(makeCommand.size() == 0)
  1336. {
  1337. cmSystemTools::Error(
  1338. "Generator cannot find the appropriate make command.");
  1339. return 1;
  1340. }
  1341. std::string newTarget;
  1342. if (target && strlen(target))
  1343. {
  1344. newTarget += target;
  1345. #if 0
  1346. #if defined(_WIN32) || defined(__CYGWIN__)
  1347. std::string tmp = target;
  1348. // if the target does not already end in . something
  1349. // then assume .exe
  1350. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  1351. {
  1352. newTarget += ".exe";
  1353. }
  1354. #endif // WIN32
  1355. #endif
  1356. }
  1357. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  1358. return this->Build(srcdir,bindir,projectName,
  1359. newTarget.c_str(),
  1360. output,makeCommand.c_str(),config,false,fast,
  1361. this->TryCompileTimeout);
  1362. }
  1363. std::string cmGlobalGenerator
  1364. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  1365. const char *projectDir, const char* additionalOptions,
  1366. const char *targetName, const char* config,
  1367. bool ignoreErrors, bool)
  1368. {
  1369. // Project name & dir and config are not used yet.
  1370. (void)projectName;
  1371. (void)projectDir;
  1372. (void)config;
  1373. std::string makeCommand =
  1374. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  1375. // Since we have full control over the invocation of nmake, let us
  1376. // make it quiet.
  1377. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  1378. {
  1379. makeCommand += " /NOLOGO ";
  1380. }
  1381. if ( ignoreErrors )
  1382. {
  1383. makeCommand += " -i";
  1384. }
  1385. if ( additionalOptions )
  1386. {
  1387. makeCommand += " ";
  1388. makeCommand += additionalOptions;
  1389. }
  1390. if ( targetName )
  1391. {
  1392. makeCommand += " ";
  1393. makeCommand += targetName;
  1394. }
  1395. return makeCommand;
  1396. }
  1397. int cmGlobalGenerator::Build(
  1398. const char *, const char *bindir,
  1399. const char *projectName, const char *target,
  1400. std::string *output,
  1401. const char *makeCommandCSTR,
  1402. const char *config,
  1403. bool clean, bool fast,
  1404. double timeout,
  1405. cmSystemTools::OutputOption outputflag,
  1406. const char* extraOptions,
  1407. std::vector<std::string> const& nativeOptions)
  1408. {
  1409. /**
  1410. * Run an executable command and put the stdout in output.
  1411. */
  1412. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1413. cmSystemTools::ChangeDirectory(bindir);
  1414. if(output)
  1415. {
  1416. *output += "Change Dir: ";
  1417. *output += bindir;
  1418. *output += "\n";
  1419. }
  1420. int retVal;
  1421. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  1422. cmSystemTools::SetRunCommandHideConsole(true);
  1423. std::string outputBuffer;
  1424. std::string* outputPtr = 0;
  1425. if(output)
  1426. {
  1427. outputPtr = &outputBuffer;
  1428. }
  1429. // should we do a clean first?
  1430. if (clean)
  1431. {
  1432. std::string cleanCommand =
  1433. this->GenerateBuildCommand(makeCommandCSTR, projectName, bindir,
  1434. 0, "clean", config, false, fast);
  1435. if(output)
  1436. {
  1437. *output += "\nRun Clean Command:";
  1438. *output += cleanCommand;
  1439. *output += "\n";
  1440. }
  1441. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), outputPtr,
  1442. &retVal, 0, outputflag, timeout))
  1443. {
  1444. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1445. cmSystemTools::Error("Generator: execution of make clean failed.");
  1446. if (output)
  1447. {
  1448. *output += *outputPtr;
  1449. *output += "\nGenerator: execution of make clean failed.\n";
  1450. }
  1451. // return to the original directory
  1452. cmSystemTools::ChangeDirectory(cwd.c_str());
  1453. return 1;
  1454. }
  1455. if (output)
  1456. {
  1457. *output += *outputPtr;
  1458. }
  1459. }
  1460. // now build
  1461. std::string makeCommand =
  1462. this->GenerateBuildCommand(makeCommandCSTR, projectName, bindir,
  1463. extraOptions, target,
  1464. config, false, fast);
  1465. if(output)
  1466. {
  1467. *output += "\nRun Build Command:";
  1468. *output += makeCommand;
  1469. *output += "\n";
  1470. }
  1471. std::vector<cmStdString> command =
  1472. cmSystemTools::ParseArguments(makeCommand.c_str());
  1473. for(std::vector<std::string>::const_iterator ni = nativeOptions.begin();
  1474. ni != nativeOptions.end(); ++ni)
  1475. {
  1476. command.push_back(*ni);
  1477. }
  1478. if (!cmSystemTools::RunSingleCommand(command, outputPtr,
  1479. &retVal, 0, outputflag, timeout))
  1480. {
  1481. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1482. cmSystemTools::Error
  1483. ("Generator: execution of make failed. Make command was: ",
  1484. makeCommand.c_str());
  1485. if (output)
  1486. {
  1487. *output += *outputPtr;
  1488. *output += "\nGenerator: execution of make failed. Make command was: "
  1489. + makeCommand + "\n";
  1490. }
  1491. // return to the original directory
  1492. cmSystemTools::ChangeDirectory(cwd.c_str());
  1493. return 1;
  1494. }
  1495. if (output)
  1496. {
  1497. *output += *outputPtr;
  1498. }
  1499. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1500. // The SGI MipsPro 7.3 compiler does not return an error code when
  1501. // the source has a #error in it! This is a work-around for such
  1502. // compilers.
  1503. if((retVal == 0) && (output->find("#error") != std::string::npos))
  1504. {
  1505. retVal = 1;
  1506. }
  1507. cmSystemTools::ChangeDirectory(cwd.c_str());
  1508. return retVal;
  1509. }
  1510. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  1511. {
  1512. this->LocalGenerators.push_back(lg);
  1513. // update progress
  1514. // estimate how many lg there will be
  1515. const char *numGenC =
  1516. this->CMakeInstance->GetCacheManager()->GetCacheValue
  1517. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  1518. if (!numGenC)
  1519. {
  1520. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  1521. // we are in the first time progress and we have no
  1522. // idea how long it will be. So, just move half way
  1523. // there each time, and don't go over 95%
  1524. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  1525. if(this->FirstTimeProgress > 0.95f)
  1526. {
  1527. this->FirstTimeProgress = 0.95f;
  1528. }
  1529. this->CMakeInstance->UpdateProgress("Configuring",
  1530. this->FirstTimeProgress);
  1531. return;
  1532. }
  1533. int numGen = atoi(numGenC);
  1534. float prog = 0.9f*static_cast<float>(this->LocalGenerators.size())/
  1535. static_cast<float>(numGen);
  1536. if (prog > 0.9f)
  1537. {
  1538. prog = 0.9f;
  1539. }
  1540. this->CMakeInstance->UpdateProgress("Configuring", prog);
  1541. }
  1542. void cmGlobalGenerator::AddInstallComponent(const char* component)
  1543. {
  1544. if(component && *component)
  1545. {
  1546. this->InstallComponents.insert(component);
  1547. }
  1548. }
  1549. void cmGlobalGenerator::EnableInstallTarget()
  1550. {
  1551. this->InstallTargetEnabled = true;
  1552. }
  1553. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  1554. {
  1555. cmLocalGenerator *lg = new cmLocalGenerator;
  1556. lg->SetGlobalGenerator(this);
  1557. return lg;
  1558. }
  1559. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen,
  1560. cmMakefile* mf)
  1561. {
  1562. this->SetConfiguredFilesPath(gen);
  1563. this->TryCompileOuterMakefile = mf;
  1564. const char* make =
  1565. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  1566. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  1567. "make program",
  1568. cmCacheManager::FILEPATH);
  1569. // copy the enabled languages
  1570. this->LanguageEnabled = gen->LanguageEnabled;
  1571. this->LanguagesReady = gen->LanguagesReady;
  1572. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  1573. this->IgnoreExtensions = gen->IgnoreExtensions;
  1574. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  1575. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  1576. this->OutputExtensions = gen->OutputExtensions;
  1577. }
  1578. //----------------------------------------------------------------------------
  1579. void cmGlobalGenerator::SetConfiguredFilesPath(cmGlobalGenerator* gen)
  1580. {
  1581. if(!gen->ConfiguredFilesPath.empty())
  1582. {
  1583. this->ConfiguredFilesPath = gen->ConfiguredFilesPath;
  1584. }
  1585. else
  1586. {
  1587. this->ConfiguredFilesPath = gen->CMakeInstance->GetHomeOutputDirectory();
  1588. this->ConfiguredFilesPath += cmake::GetCMakeFilesDirectory();
  1589. }
  1590. }
  1591. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1592. cmLocalGenerator* gen)
  1593. {
  1594. if(!gen || gen == root)
  1595. {
  1596. // No directory excludes itself.
  1597. return false;
  1598. }
  1599. if(gen->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1600. {
  1601. // This directory is excluded from its parent.
  1602. return true;
  1603. }
  1604. // This directory is included in its parent. Check whether the
  1605. // parent is excluded.
  1606. return this->IsExcluded(root, gen->GetParent());
  1607. }
  1608. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1609. cmTarget& target)
  1610. {
  1611. if(target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1612. {
  1613. // This target is excluded from its directory.
  1614. return true;
  1615. }
  1616. else
  1617. {
  1618. // This target is included in its directory. Check whether the
  1619. // directory is excluded.
  1620. return this->IsExcluded(root, target.GetMakefile()->GetLocalGenerator());
  1621. }
  1622. }
  1623. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  1624. {
  1625. for(std::map<cmStdString, bool>::iterator i =
  1626. this->LanguageEnabled.begin(); i != this->LanguageEnabled.end(); ++i)
  1627. {
  1628. lang.push_back(i->first);
  1629. }
  1630. }
  1631. int cmGlobalGenerator::GetLinkerPreference(const char* lang)
  1632. {
  1633. std::map<cmStdString, int>::const_iterator it =
  1634. this->LanguageToLinkerPreference.find(lang);
  1635. if (it != this->LanguageToLinkerPreference.end())
  1636. {
  1637. return it->second;
  1638. }
  1639. return 0;
  1640. }
  1641. void cmGlobalGenerator::FillProjectMap()
  1642. {
  1643. this->ProjectMap.clear(); // make sure we start with a clean map
  1644. unsigned int i;
  1645. for(i = 0; i < this->LocalGenerators.size(); ++i)
  1646. {
  1647. // for each local generator add all projects
  1648. cmLocalGenerator *lg = this->LocalGenerators[i];
  1649. std::string name;
  1650. do
  1651. {
  1652. if (name != lg->GetMakefile()->GetProjectName())
  1653. {
  1654. name = lg->GetMakefile()->GetProjectName();
  1655. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  1656. }
  1657. lg = lg->GetParent();
  1658. }
  1659. while (lg);
  1660. }
  1661. }
  1662. // Build a map that contains a the set of targets used by each local
  1663. // generator directory level.
  1664. void cmGlobalGenerator::FillLocalGeneratorToTargetMap()
  1665. {
  1666. this->LocalGeneratorToTargetMap.clear();
  1667. // Loop over all targets in all local generators.
  1668. for(std::vector<cmLocalGenerator*>::const_iterator
  1669. lgi = this->LocalGenerators.begin();
  1670. lgi != this->LocalGenerators.end(); ++lgi)
  1671. {
  1672. cmLocalGenerator* lg = *lgi;
  1673. cmMakefile* mf = lg->GetMakefile();
  1674. cmTargets& targets = mf->GetTargets();
  1675. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  1676. {
  1677. cmTarget& target = t->second;
  1678. // Consider the directory containing the target and all its
  1679. // parents until something excludes the target.
  1680. for(cmLocalGenerator* clg = lg; clg && !this->IsExcluded(clg, target);
  1681. clg = clg->GetParent())
  1682. {
  1683. // This local generator includes the target.
  1684. std::set<cmTarget*>& targetSet =
  1685. this->LocalGeneratorToTargetMap[clg];
  1686. targetSet.insert(&target);
  1687. // Add dependencies of the included target. An excluded
  1688. // target may still be included if it is a dependency of a
  1689. // non-excluded target.
  1690. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  1691. for(TargetDependSet::const_iterator ti = tgtdeps.begin();
  1692. ti != tgtdeps.end(); ++ti)
  1693. {
  1694. targetSet.insert(*ti);
  1695. }
  1696. }
  1697. }
  1698. }
  1699. }
  1700. ///! Find a local generator by its startdirectory
  1701. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(const char* start_dir)
  1702. {
  1703. std::vector<cmLocalGenerator*>* gens = &this->LocalGenerators;
  1704. for(unsigned int i = 0; i < gens->size(); ++i)
  1705. {
  1706. std::string sd = (*gens)[i]->GetMakefile()->GetStartDirectory();
  1707. if (sd == start_dir)
  1708. {
  1709. return (*gens)[i];
  1710. }
  1711. }
  1712. return 0;
  1713. }
  1714. //----------------------------------------------------------------------------
  1715. void cmGlobalGenerator::AddAlias(const char *name, cmTarget *tgt)
  1716. {
  1717. this->AliasTargets[name] = tgt;
  1718. }
  1719. //----------------------------------------------------------------------------
  1720. bool cmGlobalGenerator::IsAlias(const char *name)
  1721. {
  1722. return this->AliasTargets.find(name) != this->AliasTargets.end();
  1723. }
  1724. //----------------------------------------------------------------------------
  1725. cmTarget*
  1726. cmGlobalGenerator::FindTarget(const char* project, const char* name,
  1727. bool excludeAliases)
  1728. {
  1729. // if project specific
  1730. if(project)
  1731. {
  1732. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  1733. for(unsigned int i = 0; i < gens->size(); ++i)
  1734. {
  1735. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name,
  1736. excludeAliases);
  1737. if(ret)
  1738. {
  1739. return ret;
  1740. }
  1741. }
  1742. }
  1743. // if all projects/directories
  1744. else
  1745. {
  1746. if (!excludeAliases)
  1747. {
  1748. std::map<cmStdString, cmTarget*>::iterator ai
  1749. = this->AliasTargets.find(name);
  1750. if (ai != this->AliasTargets.end())
  1751. {
  1752. return ai->second;
  1753. }
  1754. }
  1755. std::map<cmStdString,cmTarget *>::iterator i =
  1756. this->TotalTargets.find ( name );
  1757. if ( i != this->TotalTargets.end() )
  1758. {
  1759. return i->second;
  1760. }
  1761. i = this->ImportedTargets.find(name);
  1762. if ( i != this->ImportedTargets.end() )
  1763. {
  1764. return i->second;
  1765. }
  1766. }
  1767. return 0;
  1768. }
  1769. //----------------------------------------------------------------------------
  1770. bool cmGlobalGenerator::NameResolvesToFramework(const std::string& libname)
  1771. {
  1772. if(cmSystemTools::IsPathToFramework(libname.c_str()))
  1773. {
  1774. return true;
  1775. }
  1776. if(cmTarget* tgt = this->FindTarget(0, libname.c_str()))
  1777. {
  1778. if(tgt->IsFrameworkOnApple())
  1779. {
  1780. return true;
  1781. }
  1782. }
  1783. return false;
  1784. }
  1785. //----------------------------------------------------------------------------
  1786. inline std::string removeQuotes(const std::string& s)
  1787. {
  1788. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1789. {
  1790. return s.substr(1, s.size()-2);
  1791. }
  1792. return s;
  1793. }
  1794. void cmGlobalGenerator::SetCMakeInstance(cmake* cm)
  1795. {
  1796. // Store a pointer to the cmake object instance.
  1797. this->CMakeInstance = cm;
  1798. }
  1799. void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
  1800. {
  1801. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  1802. const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
  1803. const char* cmakeCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1804. // CPack
  1805. std::string workingDir = mf->GetStartOutputDirectory();
  1806. cmCustomCommandLines cpackCommandLines;
  1807. std::vector<std::string> depends;
  1808. cmCustomCommandLine singleLine;
  1809. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1810. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1811. {
  1812. singleLine.push_back("-C");
  1813. singleLine.push_back(cmakeCfgIntDir);
  1814. }
  1815. singleLine.push_back("--config");
  1816. std::string configFile = mf->GetStartOutputDirectory();;
  1817. configFile += "/CPackConfig.cmake";
  1818. std::string relConfigFile = "./CPackConfig.cmake";
  1819. singleLine.push_back(relConfigFile);
  1820. cpackCommandLines.push_back(singleLine);
  1821. if ( this->GetPreinstallTargetName() )
  1822. {
  1823. depends.push_back(this->GetPreinstallTargetName());
  1824. }
  1825. else
  1826. {
  1827. const char* noPackageAll =
  1828. mf->GetDefinition("CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY");
  1829. if(!noPackageAll || cmSystemTools::IsOff(noPackageAll))
  1830. {
  1831. depends.push_back(this->GetAllTargetName());
  1832. }
  1833. }
  1834. if(cmSystemTools::FileExists(configFile.c_str()))
  1835. {
  1836. (*targets)[this->GetPackageTargetName()]
  1837. = this->CreateGlobalTarget(this->GetPackageTargetName(),
  1838. "Run CPack packaging tool...",
  1839. &cpackCommandLines, depends,
  1840. workingDir.c_str());
  1841. }
  1842. // CPack source
  1843. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1844. if ( packageSourceTargetName )
  1845. {
  1846. cpackCommandLines.erase(cpackCommandLines.begin(),
  1847. cpackCommandLines.end());
  1848. singleLine.erase(singleLine.begin(), singleLine.end());
  1849. depends.erase(depends.begin(), depends.end());
  1850. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1851. singleLine.push_back("--config");
  1852. configFile = mf->GetStartOutputDirectory();;
  1853. configFile += "/CPackSourceConfig.cmake";
  1854. relConfigFile = "./CPackSourceConfig.cmake";
  1855. singleLine.push_back(relConfigFile);
  1856. if(cmSystemTools::FileExists(configFile.c_str()))
  1857. {
  1858. singleLine.push_back(configFile);
  1859. cpackCommandLines.push_back(singleLine);
  1860. (*targets)[packageSourceTargetName]
  1861. = this->CreateGlobalTarget(packageSourceTargetName,
  1862. "Run CPack packaging tool for source...",
  1863. &cpackCommandLines, depends,
  1864. workingDir.c_str()
  1865. );
  1866. }
  1867. }
  1868. // Test
  1869. if(mf->IsOn("CMAKE_TESTING_ENABLED"))
  1870. {
  1871. cpackCommandLines.erase(cpackCommandLines.begin(),
  1872. cpackCommandLines.end());
  1873. singleLine.erase(singleLine.begin(), singleLine.end());
  1874. depends.erase(depends.begin(), depends.end());
  1875. singleLine.push_back(this->GetCMakeInstance()->GetCTestCommand());
  1876. singleLine.push_back("--force-new-ctest-process");
  1877. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1878. {
  1879. singleLine.push_back("-C");
  1880. singleLine.push_back(cmakeCfgIntDir);
  1881. }
  1882. else // TODO: This is a hack. Should be something to do with the generator
  1883. {
  1884. singleLine.push_back("$(ARGS)");
  1885. }
  1886. cpackCommandLines.push_back(singleLine);
  1887. (*targets)[this->GetTestTargetName()]
  1888. = this->CreateGlobalTarget(this->GetTestTargetName(),
  1889. "Running tests...", &cpackCommandLines, depends, 0);
  1890. }
  1891. //Edit Cache
  1892. const char* editCacheTargetName = this->GetEditCacheTargetName();
  1893. if ( editCacheTargetName )
  1894. {
  1895. cpackCommandLines.erase(cpackCommandLines.begin(),
  1896. cpackCommandLines.end());
  1897. singleLine.erase(singleLine.begin(), singleLine.end());
  1898. depends.erase(depends.begin(), depends.end());
  1899. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1900. // Otherwise default to the interactive command-line interface.
  1901. if(mf->GetDefinition("CMAKE_EDIT_COMMAND"))
  1902. {
  1903. singleLine.push_back(mf->GetDefinition("CMAKE_EDIT_COMMAND"));
  1904. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1905. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1906. cpackCommandLines.push_back(singleLine);
  1907. (*targets)[editCacheTargetName] =
  1908. this->CreateGlobalTarget(
  1909. editCacheTargetName, "Running CMake cache editor...",
  1910. &cpackCommandLines, depends, 0);
  1911. }
  1912. else
  1913. {
  1914. singleLine.push_back(cmakeCommand);
  1915. singleLine.push_back("-i");
  1916. singleLine.push_back(".");
  1917. cpackCommandLines.push_back(singleLine);
  1918. (*targets)[editCacheTargetName] =
  1919. this->CreateGlobalTarget(
  1920. editCacheTargetName,
  1921. "Running interactive CMake command-line interface...",
  1922. &cpackCommandLines, depends, 0);
  1923. }
  1924. }
  1925. //Rebuild Cache
  1926. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  1927. if ( rebuildCacheTargetName )
  1928. {
  1929. cpackCommandLines.erase(cpackCommandLines.begin(),
  1930. cpackCommandLines.end());
  1931. singleLine.erase(singleLine.begin(), singleLine.end());
  1932. depends.erase(depends.begin(), depends.end());
  1933. singleLine.push_back(cmakeCommand);
  1934. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1935. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1936. cpackCommandLines.push_back(singleLine);
  1937. (*targets)[rebuildCacheTargetName] =
  1938. this->CreateGlobalTarget(
  1939. rebuildCacheTargetName, "Running CMake to regenerate build system...",
  1940. &cpackCommandLines, depends, 0);
  1941. }
  1942. //Install
  1943. if(this->InstallTargetEnabled)
  1944. {
  1945. if(!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.')
  1946. {
  1947. std::set<cmStdString>* componentsSet = &this->InstallComponents;
  1948. cpackCommandLines.erase(cpackCommandLines.begin(),
  1949. cpackCommandLines.end());
  1950. depends.erase(depends.begin(), depends.end());
  1951. cmOStringStream ostr;
  1952. if ( componentsSet->size() > 0 )
  1953. {
  1954. ostr << "Available install components are:";
  1955. std::set<cmStdString>::iterator it;
  1956. for (
  1957. it = componentsSet->begin();
  1958. it != componentsSet->end();
  1959. ++ it )
  1960. {
  1961. ostr << " \"" << it->c_str() << "\"";
  1962. }
  1963. }
  1964. else
  1965. {
  1966. ostr << "Only default component available";
  1967. }
  1968. singleLine.push_back(ostr.str().c_str());
  1969. (*targets)["list_install_components"]
  1970. = this->CreateGlobalTarget("list_install_components",
  1971. ostr.str().c_str(),
  1972. &cpackCommandLines, depends, 0);
  1973. }
  1974. std::string cmd = cmakeCommand;
  1975. cpackCommandLines.erase(cpackCommandLines.begin(),
  1976. cpackCommandLines.end());
  1977. singleLine.erase(singleLine.begin(), singleLine.end());
  1978. depends.erase(depends.begin(), depends.end());
  1979. if ( this->GetPreinstallTargetName() )
  1980. {
  1981. depends.push_back(this->GetPreinstallTargetName());
  1982. }
  1983. else
  1984. {
  1985. const char* noall =
  1986. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1987. if(!noall || cmSystemTools::IsOff(noall))
  1988. {
  1989. depends.push_back(this->GetAllTargetName());
  1990. }
  1991. }
  1992. if(mf->GetDefinition("CMake_BINARY_DIR"))
  1993. {
  1994. // We are building CMake itself. We cannot use the original
  1995. // executable to install over itself. The generator will
  1996. // automatically convert this name to the build-time location.
  1997. cmd = "cmake";
  1998. }
  1999. singleLine.push_back(cmd.c_str());
  2000. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  2001. {
  2002. std::string cfgArg = "-DBUILD_TYPE=";
  2003. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  2004. singleLine.push_back(cfgArg);
  2005. }
  2006. singleLine.push_back("-P");
  2007. singleLine.push_back("cmake_install.cmake");
  2008. cpackCommandLines.push_back(singleLine);
  2009. (*targets)[this->GetInstallTargetName()] =
  2010. this->CreateGlobalTarget(
  2011. this->GetInstallTargetName(), "Install the project...",
  2012. &cpackCommandLines, depends, 0);
  2013. // install_local
  2014. if(const char* install_local = this->GetInstallLocalTargetName())
  2015. {
  2016. cmCustomCommandLine localCmdLine = singleLine;
  2017. localCmdLine.insert(localCmdLine.begin()+1,
  2018. "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  2019. cpackCommandLines.erase(cpackCommandLines.begin(),
  2020. cpackCommandLines.end());
  2021. cpackCommandLines.push_back(localCmdLine);
  2022. (*targets)[install_local] =
  2023. this->CreateGlobalTarget(
  2024. install_local, "Installing only the local directory...",
  2025. &cpackCommandLines, depends, 0);
  2026. }
  2027. // install_strip
  2028. const char* install_strip = this->GetInstallStripTargetName();
  2029. if((install_strip !=0) && (mf->IsSet("CMAKE_STRIP")))
  2030. {
  2031. cmCustomCommandLine stripCmdLine = singleLine;
  2032. stripCmdLine.insert(stripCmdLine.begin()+1,"-DCMAKE_INSTALL_DO_STRIP=1");
  2033. cpackCommandLines.erase(cpackCommandLines.begin(),
  2034. cpackCommandLines.end());
  2035. cpackCommandLines.push_back(stripCmdLine);
  2036. (*targets)[install_strip] =
  2037. this->CreateGlobalTarget(
  2038. install_strip, "Installing the project stripped...",
  2039. &cpackCommandLines, depends, 0);
  2040. }
  2041. }
  2042. }
  2043. //----------------------------------------------------------------------------
  2044. const char* cmGlobalGenerator::GetPredefinedTargetsFolder()
  2045. {
  2046. const char* prop =
  2047. this->GetCMakeInstance()->GetProperty("PREDEFINED_TARGETS_FOLDER");
  2048. if (prop)
  2049. {
  2050. return prop;
  2051. }
  2052. return "CMakePredefinedTargets";
  2053. }
  2054. //----------------------------------------------------------------------------
  2055. bool cmGlobalGenerator::UseFolderProperty()
  2056. {
  2057. const char* prop = this->GetCMakeInstance()->GetProperty("USE_FOLDERS");
  2058. // If this property is defined, let the setter turn this on or off...
  2059. //
  2060. if (prop)
  2061. {
  2062. return cmSystemTools::IsOn(prop);
  2063. }
  2064. // By default, this feature is OFF, since it is not supported in the
  2065. // Visual Studio Express editions until VS11:
  2066. //
  2067. return false;
  2068. }
  2069. //----------------------------------------------------------------------------
  2070. void cmGlobalGenerator::EnableMinGWLanguage(cmMakefile *mf)
  2071. {
  2072. this->FindMakeProgram(mf);
  2073. std::string makeProgram = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  2074. std::vector<std::string> locations;
  2075. locations.push_back(cmSystemTools::GetProgramPath(makeProgram.c_str()));
  2076. locations.push_back("/mingw/bin");
  2077. locations.push_back("c:/mingw/bin");
  2078. std::string tgcc = cmSystemTools::FindProgram("gcc", locations);
  2079. std::string gcc = "gcc.exe";
  2080. if(tgcc.size())
  2081. {
  2082. gcc = tgcc;
  2083. }
  2084. std::string tgxx = cmSystemTools::FindProgram("g++", locations);
  2085. std::string gxx = "g++.exe";
  2086. if(tgxx.size())
  2087. {
  2088. gxx = tgxx;
  2089. }
  2090. std::string trc = cmSystemTools::FindProgram("windres", locations);
  2091. std::string rc = "windres.exe";
  2092. if(trc.size())
  2093. {
  2094. rc = trc;
  2095. }
  2096. mf->AddDefinition("CMAKE_GENERATOR_CC", gcc.c_str());
  2097. mf->AddDefinition("CMAKE_GENERATOR_CXX", gxx.c_str());
  2098. mf->AddDefinition("CMAKE_GENERATOR_RC", rc.c_str());
  2099. }
  2100. //----------------------------------------------------------------------------
  2101. cmTarget cmGlobalGenerator::CreateGlobalTarget(
  2102. const char* name, const char* message,
  2103. const cmCustomCommandLines* commandLines,
  2104. std::vector<std::string> depends,
  2105. const char* workingDirectory)
  2106. {
  2107. // Package
  2108. cmTarget target;
  2109. target.GetProperties().SetCMakeInstance(this->CMakeInstance);
  2110. target.SetType(cmTarget::GLOBAL_TARGET, name);
  2111. target.SetProperty("EXCLUDE_FROM_ALL","TRUE");
  2112. std::vector<std::string> no_outputs;
  2113. std::vector<std::string> no_depends;
  2114. // Store the custom command in the target.
  2115. cmCustomCommand cc(0, no_outputs, no_depends, *commandLines, 0,
  2116. workingDirectory);
  2117. target.GetPostBuildCommands().push_back(cc);
  2118. target.SetProperty("EchoString", message);
  2119. std::vector<std::string>::iterator dit;
  2120. for ( dit = depends.begin(); dit != depends.end(); ++ dit )
  2121. {
  2122. target.AddUtility(dit->c_str());
  2123. }
  2124. // Organize in the "predefined targets" folder:
  2125. //
  2126. if (this->UseFolderProperty())
  2127. {
  2128. target.SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  2129. }
  2130. return target;
  2131. }
  2132. //----------------------------------------------------------------------------
  2133. std::string
  2134. cmGlobalGenerator::GenerateRuleFile(std::string const& output) const
  2135. {
  2136. std::string ruleFile = output;
  2137. ruleFile += ".rule";
  2138. const char* dir = this->GetCMakeCFGIntDir();
  2139. if(dir && dir[0] == '$')
  2140. {
  2141. cmSystemTools::ReplaceString(ruleFile, dir,
  2142. cmake::GetCMakeFilesDirectory());
  2143. }
  2144. return ruleFile;
  2145. }
  2146. //----------------------------------------------------------------------------
  2147. std::string cmGlobalGenerator::GetSharedLibFlagsForLanguage(
  2148. std::string const& l)
  2149. {
  2150. if(this->LanguageToOriginalSharedLibFlags.count(l) > 0)
  2151. {
  2152. return this->LanguageToOriginalSharedLibFlags[l];
  2153. }
  2154. return "";
  2155. }
  2156. //----------------------------------------------------------------------------
  2157. void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
  2158. const char*, std::string&)
  2159. {
  2160. // Subclasses that support multiple configurations should implement
  2161. // this method to append the subdirectory for the given build
  2162. // configuration.
  2163. }
  2164. //----------------------------------------------------------------------------
  2165. cmGlobalGenerator::TargetDependSet const&
  2166. cmGlobalGenerator::GetTargetDirectDepends(cmTarget & target)
  2167. {
  2168. return this->TargetDependencies[&target];
  2169. }
  2170. void cmGlobalGenerator::AddTarget(cmTarget* t)
  2171. {
  2172. if(t->IsImported())
  2173. {
  2174. this->ImportedTargets[t->GetName()] = t;
  2175. }
  2176. else
  2177. {
  2178. this->TotalTargets[t->GetName()] = t;
  2179. }
  2180. }
  2181. void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
  2182. cmExternalMakefileProjectGenerator *extraGenerator)
  2183. {
  2184. this->ExtraGenerator = extraGenerator;
  2185. if (this->ExtraGenerator!=0)
  2186. {
  2187. this->ExtraGenerator->SetGlobalGenerator(this);
  2188. }
  2189. }
  2190. const char* cmGlobalGenerator::GetExtraGeneratorName() const
  2191. {
  2192. return this->ExtraGenerator==0 ? 0 : this->ExtraGenerator->GetName();
  2193. }
  2194. void cmGlobalGenerator::FileReplacedDuringGenerate(const std::string& filename)
  2195. {
  2196. this->FilesReplacedDuringGenerate.push_back(filename);
  2197. }
  2198. void
  2199. cmGlobalGenerator
  2200. ::GetFilesReplacedDuringGenerate(std::vector<std::string>& filenames)
  2201. {
  2202. filenames.clear();
  2203. std::copy(
  2204. this->FilesReplacedDuringGenerate.begin(),
  2205. this->FilesReplacedDuringGenerate.end(),
  2206. std::back_inserter(filenames));
  2207. }
  2208. //----------------------------------------------------------------------------
  2209. void cmGlobalGenerator::GetTargetSets(TargetDependSet& projectTargets,
  2210. TargetDependSet& originalTargets,
  2211. cmLocalGenerator* root,
  2212. GeneratorVector const& generators)
  2213. {
  2214. // loop over all local generators
  2215. for(std::vector<cmLocalGenerator*>::const_iterator i = generators.begin();
  2216. i != generators.end(); ++i)
  2217. {
  2218. // check to make sure generator is not excluded
  2219. if(this->IsExcluded(root, *i))
  2220. {
  2221. continue;
  2222. }
  2223. cmMakefile* mf = (*i)->GetMakefile();
  2224. // Get the targets in the makefile
  2225. cmTargets &tgts = mf->GetTargets();
  2226. // loop over all the targets
  2227. for (cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2228. {
  2229. cmTarget* target = &l->second;
  2230. if(this->IsRootOnlyTarget(target) &&
  2231. target->GetMakefile() != root->GetMakefile())
  2232. {
  2233. continue;
  2234. }
  2235. // put the target in the set of original targets
  2236. originalTargets.insert(target);
  2237. // Get the set of targets that depend on target
  2238. this->AddTargetDepends(target, projectTargets);
  2239. }
  2240. }
  2241. }
  2242. //----------------------------------------------------------------------------
  2243. bool cmGlobalGenerator::IsRootOnlyTarget(cmTarget* target)
  2244. {
  2245. return (target->GetType() == cmTarget::GLOBAL_TARGET ||
  2246. strcmp(target->GetName(), this->GetAllTargetName()) == 0);
  2247. }
  2248. //----------------------------------------------------------------------------
  2249. void cmGlobalGenerator::AddTargetDepends(cmTarget* target,
  2250. TargetDependSet& projectTargets)
  2251. {
  2252. // add the target itself
  2253. if(projectTargets.insert(target).second)
  2254. {
  2255. // This is the first time we have encountered the target.
  2256. // Recursively follow its dependencies.
  2257. TargetDependSet const& ts = this->GetTargetDirectDepends(*target);
  2258. for(TargetDependSet::const_iterator i = ts.begin(); i != ts.end(); ++i)
  2259. {
  2260. cmTarget* dtarget = *i;
  2261. this->AddTargetDepends(dtarget, projectTargets);
  2262. }
  2263. }
  2264. }
  2265. //----------------------------------------------------------------------------
  2266. void cmGlobalGenerator::AddToManifest(const char* config,
  2267. std::string const& f)
  2268. {
  2269. // Add to the main manifest for this configuration.
  2270. this->TargetManifest[config].insert(f);
  2271. // Add to the content listing for the file's directory.
  2272. std::string dir = cmSystemTools::GetFilenamePath(f);
  2273. std::string file = cmSystemTools::GetFilenameName(f);
  2274. this->DirectoryContentMap[dir].insert(file);
  2275. }
  2276. //----------------------------------------------------------------------------
  2277. std::set<cmStdString> const&
  2278. cmGlobalGenerator::GetDirectoryContent(std::string const& dir, bool needDisk)
  2279. {
  2280. DirectoryContent& dc = this->DirectoryContentMap[dir];
  2281. if(needDisk && !dc.LoadedFromDisk)
  2282. {
  2283. // Load the directory content from disk.
  2284. cmsys::Directory d;
  2285. if(d.Load(dir.c_str()))
  2286. {
  2287. unsigned long n = d.GetNumberOfFiles();
  2288. for(unsigned long i = 0; i < n; ++i)
  2289. {
  2290. const char* f = d.GetFile(i);
  2291. if(strcmp(f, ".") != 0 && strcmp(f, "..") != 0)
  2292. {
  2293. dc.insert(f);
  2294. }
  2295. }
  2296. }
  2297. dc.LoadedFromDisk = true;
  2298. }
  2299. return dc;
  2300. }
  2301. //----------------------------------------------------------------------------
  2302. void
  2303. cmGlobalGenerator::AddRuleHash(const std::vector<std::string>& outputs,
  2304. std::string const& content)
  2305. {
  2306. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2307. // Ignore if there are no outputs.
  2308. if(outputs.empty())
  2309. {
  2310. return;
  2311. }
  2312. // Compute a hash of the rule.
  2313. RuleHash hash;
  2314. {
  2315. unsigned char const* data =
  2316. reinterpret_cast<unsigned char const*>(content.c_str());
  2317. int length = static_cast<int>(content.length());
  2318. cmsysMD5* sum = cmsysMD5_New();
  2319. cmsysMD5_Initialize(sum);
  2320. cmsysMD5_Append(sum, data, length);
  2321. cmsysMD5_FinalizeHex(sum, hash.Data);
  2322. cmsysMD5_Delete(sum);
  2323. }
  2324. // Shorten the output name (in expected use case).
  2325. cmLocalGenerator* lg = this->GetLocalGenerators()[0];
  2326. std::string fname = lg->Convert(outputs[0].c_str(),
  2327. cmLocalGenerator::HOME_OUTPUT);
  2328. // Associate the hash with this output.
  2329. this->RuleHashes[fname] = hash;
  2330. #else
  2331. (void)outputs;
  2332. (void)content;
  2333. #endif
  2334. }
  2335. //----------------------------------------------------------------------------
  2336. void cmGlobalGenerator::CheckRuleHashes()
  2337. {
  2338. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2339. std::string home = this->GetCMakeInstance()->GetHomeOutputDirectory();
  2340. std::string pfile = home;
  2341. pfile += this->GetCMakeInstance()->GetCMakeFilesDirectory();
  2342. pfile += "/CMakeRuleHashes.txt";
  2343. this->CheckRuleHashes(pfile, home);
  2344. this->WriteRuleHashes(pfile);
  2345. #endif
  2346. }
  2347. //----------------------------------------------------------------------------
  2348. void cmGlobalGenerator::CheckRuleHashes(std::string const& pfile,
  2349. std::string const& home)
  2350. {
  2351. #if defined(_WIN32) || defined(__CYGWIN__)
  2352. std::ifstream fin(pfile.c_str(), std::ios::in | std::ios::binary);
  2353. #else
  2354. std::ifstream fin(pfile.c_str(), std::ios::in);
  2355. #endif
  2356. if(!fin)
  2357. {
  2358. return;
  2359. }
  2360. std::string line;
  2361. std::string fname;
  2362. while(cmSystemTools::GetLineFromStream(fin, line))
  2363. {
  2364. // Line format is a 32-byte hex string followed by a space
  2365. // followed by a file name (with no escaping).
  2366. // Skip blank and comment lines.
  2367. if(line.size() < 34 || line[0] == '#')
  2368. {
  2369. continue;
  2370. }
  2371. // Get the filename.
  2372. fname = line.substr(33, line.npos);
  2373. // Look for a hash for this file's rule.
  2374. std::map<cmStdString, RuleHash>::const_iterator rhi =
  2375. this->RuleHashes.find(fname);
  2376. if(rhi != this->RuleHashes.end())
  2377. {
  2378. // Compare the rule hash in the file to that we were given.
  2379. if(strncmp(line.c_str(), rhi->second.Data, 32) != 0)
  2380. {
  2381. // The rule has changed. Delete the output so it will be
  2382. // built again.
  2383. fname = cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  2384. cmSystemTools::RemoveFile(fname.c_str());
  2385. }
  2386. }
  2387. else
  2388. {
  2389. // We have no hash for a rule previously listed. This may be a
  2390. // case where a user has turned off a build option and might
  2391. // want to turn it back on later, so do not delete the file.
  2392. // Instead, we keep the rule hash as long as the file exists so
  2393. // that if the feature is turned back on and the rule has
  2394. // changed the file is still rebuilt.
  2395. std::string fpath =
  2396. cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  2397. if(cmSystemTools::FileExists(fpath.c_str()))
  2398. {
  2399. RuleHash hash;
  2400. strncpy(hash.Data, line.c_str(), 32);
  2401. this->RuleHashes[fname] = hash;
  2402. }
  2403. }
  2404. }
  2405. }
  2406. //----------------------------------------------------------------------------
  2407. void cmGlobalGenerator::WriteRuleHashes(std::string const& pfile)
  2408. {
  2409. // Now generate a new persistence file with the current hashes.
  2410. if(this->RuleHashes.empty())
  2411. {
  2412. cmSystemTools::RemoveFile(pfile.c_str());
  2413. }
  2414. else
  2415. {
  2416. cmGeneratedFileStream fout(pfile.c_str());
  2417. fout << "# Hashes of file build rules.\n";
  2418. for(std::map<cmStdString, RuleHash>::const_iterator
  2419. rhi = this->RuleHashes.begin(); rhi != this->RuleHashes.end(); ++rhi)
  2420. {
  2421. fout.write(rhi->second.Data, 32);
  2422. fout << " " << rhi->first << "\n";
  2423. }
  2424. }
  2425. }
  2426. //----------------------------------------------------------------------------
  2427. void cmGlobalGenerator::WriteSummary()
  2428. {
  2429. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  2430. // Record all target directories in a central location.
  2431. std::string fname = mf->GetHomeOutputDirectory();
  2432. fname += cmake::GetCMakeFilesDirectory();
  2433. fname += "/TargetDirectories.txt";
  2434. cmGeneratedFileStream fout(fname.c_str());
  2435. // Generate summary information files for each target.
  2436. std::string dir;
  2437. for(std::map<cmStdString,cmTarget *>::const_iterator ti =
  2438. this->TotalTargets.begin(); ti != this->TotalTargets.end(); ++ti)
  2439. {
  2440. if ((ti->second)->GetType() == cmTarget::INTERFACE_LIBRARY)
  2441. {
  2442. continue;
  2443. }
  2444. this->WriteSummary(ti->second);
  2445. fout << ti->second->GetSupportDirectory() << "\n";
  2446. }
  2447. }
  2448. //----------------------------------------------------------------------------
  2449. void cmGlobalGenerator::WriteSummary(cmTarget* target)
  2450. {
  2451. // Place the labels file in a per-target support directory.
  2452. std::string dir = target->GetSupportDirectory();
  2453. std::string file = dir;
  2454. file += "/Labels.txt";
  2455. // Check whether labels are enabled for this target.
  2456. if(const char* value = target->GetProperty("LABELS"))
  2457. {
  2458. cmSystemTools::MakeDirectory(dir.c_str());
  2459. cmGeneratedFileStream fout(file.c_str());
  2460. // List the target-wide labels. All sources in the target get
  2461. // these labels.
  2462. std::vector<std::string> labels;
  2463. cmSystemTools::ExpandListArgument(value, labels);
  2464. if(!labels.empty())
  2465. {
  2466. fout << "# Target labels\n";
  2467. for(std::vector<std::string>::const_iterator li = labels.begin();
  2468. li != labels.end(); ++li)
  2469. {
  2470. fout << " " << *li << "\n";
  2471. }
  2472. }
  2473. // List the source files with any per-source labels.
  2474. fout << "# Source files and their labels\n";
  2475. std::vector<cmSourceFile*> const& sources = target->GetSourceFiles();
  2476. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  2477. si != sources.end(); ++si)
  2478. {
  2479. cmSourceFile* sf = *si;
  2480. fout << sf->GetFullPath() << "\n";
  2481. if(const char* svalue = sf->GetProperty("LABELS"))
  2482. {
  2483. labels.clear();
  2484. cmSystemTools::ExpandListArgument(svalue, labels);
  2485. for(std::vector<std::string>::const_iterator li = labels.begin();
  2486. li != labels.end(); ++li)
  2487. {
  2488. fout << " " << *li << "\n";
  2489. }
  2490. }
  2491. }
  2492. }
  2493. else
  2494. {
  2495. cmSystemTools::RemoveFile(file.c_str());
  2496. }
  2497. }
  2498. //----------------------------------------------------------------------------
  2499. // static
  2500. std::string cmGlobalGenerator::EscapeJSON(const std::string& s) {
  2501. std::string result;
  2502. for (std::string::size_type i = 0; i < s.size(); ++i) {
  2503. if (s[i] == '"' || s[i] == '\\') {
  2504. result += '\\';
  2505. }
  2506. result += s[i];
  2507. }
  2508. return result;
  2509. }
  2510. //----------------------------------------------------------------------------
  2511. void cmGlobalGenerator::AddEvaluationFile(const std::string &inputFile,
  2512. cmsys::auto_ptr<cmCompiledGeneratorExpression> outputExpr,
  2513. cmMakefile *makefile,
  2514. cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
  2515. bool inputIsContent)
  2516. {
  2517. this->EvaluationFiles.push_back(
  2518. new cmGeneratorExpressionEvaluationFile(inputFile, outputExpr,
  2519. makefile, condition,
  2520. inputIsContent));
  2521. }
  2522. //----------------------------------------------------------------------------
  2523. void cmGlobalGenerator::ProcessEvaluationFiles()
  2524. {
  2525. std::set<std::string> generatedFiles;
  2526. for(std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator
  2527. li = this->EvaluationFiles.begin();
  2528. li != this->EvaluationFiles.end();
  2529. ++li)
  2530. {
  2531. (*li)->Generate();
  2532. if (cmSystemTools::GetFatalErrorOccured())
  2533. {
  2534. return;
  2535. }
  2536. std::vector<std::string> files = (*li)->GetFiles();
  2537. for(std::vector<std::string>::const_iterator fi = files.begin();
  2538. fi != files.end(); ++fi)
  2539. {
  2540. if (!generatedFiles.insert(*fi).second)
  2541. {
  2542. cmSystemTools::Error("File to be generated by multiple different "
  2543. "commands: ", fi->c_str());
  2544. return;
  2545. }
  2546. }
  2547. }
  2548. }