cmGlobalGenerator.cxx 91 KB

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