cmGlobalGenerator.cxx 97 KB

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