cmGlobalGenerator.cxx 99 KB

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