cmLocalGenerator.cxx 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381
  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 "cmLocalGenerator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandGenerator.h"
  8. #include "cmCustomCommandLines.h"
  9. #include "cmGeneratedFileStream.h"
  10. #include "cmGeneratorExpression.h"
  11. #include "cmGeneratorExpressionEvaluationFile.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmInstallGenerator.h"
  15. #include "cmInstallScriptGenerator.h"
  16. #include "cmInstallTargetGenerator.h"
  17. #include "cmLinkLineComputer.h"
  18. #include "cmLinkLineDeviceComputer.h"
  19. #include "cmMakefile.h"
  20. #include "cmRulePlaceholderExpander.h"
  21. #include "cmSourceFile.h"
  22. #include "cmSourceFileLocation.h"
  23. #include "cmSourceFileLocationKind.h"
  24. #include "cmState.h"
  25. #include "cmStateDirectory.h"
  26. #include "cmStateTypes.h"
  27. #include "cmStringAlgorithms.h"
  28. #include "cmSystemTools.h"
  29. #include "cmTarget.h"
  30. #include "cmTestGenerator.h"
  31. #include "cmVersion.h"
  32. #include "cm_string_view.hxx"
  33. #include "cmake.h"
  34. #include "cmsys/RegularExpression.hxx"
  35. #if !defined(CMAKE_BOOTSTRAP)
  36. # define CM_LG_ENCODE_OBJECT_NAMES
  37. # include "cmCryptoHash.h"
  38. #endif
  39. #include <algorithm>
  40. #include <cassert>
  41. #include <cstdio>
  42. #include <cstdlib>
  43. #include <cstring>
  44. #include <functional>
  45. #include <initializer_list>
  46. #include <iterator>
  47. #include <memory>
  48. #include <sstream>
  49. #include <unordered_set>
  50. #include <utility>
  51. #include <vector>
  52. #if defined(__HAIKU__)
  53. # include <FindDirectory.h>
  54. # include <StorageDefs.h>
  55. #endif
  56. // List of variables that are replaced when
  57. // rules are expanced. These variables are
  58. // replaced in the form <var> with GetSafeDefinition(var).
  59. // ${LANG} is replaced in the variable first with all enabled
  60. // languages.
  61. static auto ruleReplaceVars = { "CMAKE_${LANG}_COMPILER",
  62. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  63. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  64. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  65. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  66. "CMAKE_${LANG}_LINK_FLAGS",
  67. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  68. "CMAKE_${LANG}_ARCHIVE",
  69. "CMAKE_AR",
  70. "CMAKE_CURRENT_SOURCE_DIR",
  71. "CMAKE_CURRENT_BINARY_DIR",
  72. "CMAKE_RANLIB",
  73. "CMAKE_LINKER",
  74. "CMAKE_MT",
  75. "CMAKE_CUDA_HOST_COMPILER",
  76. "CMAKE_CUDA_HOST_LINK_LAUNCHER",
  77. "CMAKE_CL_SHOWINCLUDES_PREFIX" };
  78. cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile)
  79. : cmOutputConverter(makefile->GetStateSnapshot())
  80. , StateSnapshot(makefile->GetStateSnapshot())
  81. , DirectoryBacktrace(makefile->GetBacktrace())
  82. {
  83. this->GlobalGenerator = gg;
  84. this->Makefile = makefile;
  85. this->AliasTargets = makefile->GetAliasTargets();
  86. this->EmitUniversalBinaryFlags = true;
  87. this->BackwardsCompatibility = 0;
  88. this->BackwardsCompatibilityFinal = false;
  89. this->ComputeObjectMaxPath();
  90. // Canonicalize entries of the CPATH environment variable the same
  91. // way detection of CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES does.
  92. {
  93. std::vector<std::string> cpath;
  94. cmSystemTools::GetPath(cpath, "CPATH");
  95. for (std::string& cp : cpath) {
  96. if (cmSystemTools::FileIsFullPath(cp)) {
  97. cp = cmSystemTools::CollapseFullPath(cp);
  98. this->EnvCPATH.emplace(std::move(cp));
  99. }
  100. }
  101. }
  102. std::vector<std::string> enabledLanguages =
  103. this->GetState()->GetEnabledLanguages();
  104. if (const char* sysrootCompile =
  105. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  106. this->CompilerSysroot = sysrootCompile;
  107. } else {
  108. this->CompilerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  109. }
  110. if (const char* sysrootLink =
  111. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  112. this->LinkerSysroot = sysrootLink;
  113. } else {
  114. this->LinkerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  115. }
  116. for (std::string const& lang : enabledLanguages) {
  117. if (lang == "NONE") {
  118. continue;
  119. }
  120. this->Compilers["CMAKE_" + lang + "_COMPILER"] = lang;
  121. this->VariableMappings["CMAKE_" + lang + "_COMPILER"] =
  122. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER");
  123. std::string const& compilerArg1 = "CMAKE_" + lang + "_COMPILER_ARG1";
  124. std::string const& compilerTarget = "CMAKE_" + lang + "_COMPILER_TARGET";
  125. std::string const& compilerOptionTarget =
  126. "CMAKE_" + lang + "_COMPILE_OPTIONS_TARGET";
  127. std::string const& compilerExternalToolchain =
  128. "CMAKE_" + lang + "_COMPILER_EXTERNAL_TOOLCHAIN";
  129. std::string const& compilerOptionExternalToolchain =
  130. "CMAKE_" + lang + "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN";
  131. std::string const& compilerOptionSysroot =
  132. "CMAKE_" + lang + "_COMPILE_OPTIONS_SYSROOT";
  133. this->VariableMappings[compilerArg1] =
  134. this->Makefile->GetSafeDefinition(compilerArg1);
  135. this->VariableMappings[compilerTarget] =
  136. this->Makefile->GetSafeDefinition(compilerTarget);
  137. this->VariableMappings[compilerOptionTarget] =
  138. this->Makefile->GetSafeDefinition(compilerOptionTarget);
  139. this->VariableMappings[compilerExternalToolchain] =
  140. this->Makefile->GetSafeDefinition(compilerExternalToolchain);
  141. this->VariableMappings[compilerOptionExternalToolchain] =
  142. this->Makefile->GetSafeDefinition(compilerOptionExternalToolchain);
  143. this->VariableMappings[compilerOptionSysroot] =
  144. this->Makefile->GetSafeDefinition(compilerOptionSysroot);
  145. for (std::string replaceVar : ruleReplaceVars) {
  146. if (replaceVar.find("${LANG}") != std::string::npos) {
  147. cmSystemTools::ReplaceString(replaceVar, "${LANG}", lang);
  148. }
  149. this->VariableMappings[replaceVar] =
  150. this->Makefile->GetSafeDefinition(replaceVar);
  151. }
  152. }
  153. }
  154. cmRulePlaceholderExpander* cmLocalGenerator::CreateRulePlaceholderExpander()
  155. const
  156. {
  157. return new cmRulePlaceholderExpander(this->Compilers, this->VariableMappings,
  158. this->CompilerSysroot,
  159. this->LinkerSysroot);
  160. }
  161. cmLocalGenerator::~cmLocalGenerator()
  162. {
  163. cmDeleteAll(this->GeneratorTargets);
  164. cmDeleteAll(this->OwnedImportedGeneratorTargets);
  165. }
  166. void cmLocalGenerator::IssueMessage(MessageType t,
  167. std::string const& text) const
  168. {
  169. this->GetCMakeInstance()->IssueMessage(t, text, this->DirectoryBacktrace);
  170. }
  171. void cmLocalGenerator::ComputeObjectMaxPath()
  172. {
  173. // Choose a maximum object file name length.
  174. #if defined(_WIN32) || defined(__CYGWIN__)
  175. this->ObjectPathMax = 250;
  176. #else
  177. this->ObjectPathMax = 1000;
  178. #endif
  179. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  180. if (plen && *plen) {
  181. unsigned int pmax;
  182. if (sscanf(plen, "%u", &pmax) == 1) {
  183. if (pmax >= 128) {
  184. this->ObjectPathMax = pmax;
  185. } else {
  186. std::ostringstream w;
  187. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  188. << ", which is less than the minimum of 128. "
  189. << "The value will be ignored.";
  190. this->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  191. }
  192. } else {
  193. std::ostringstream w;
  194. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  195. << "\", which fails to parse as a positive integer. "
  196. << "The value will be ignored.";
  197. this->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  198. }
  199. }
  200. this->ObjectMaxPathViolations.clear();
  201. }
  202. static void MoveSystemIncludesToEnd(std::vector<std::string>& includeDirs,
  203. const std::string& config,
  204. const std::string& lang,
  205. const cmGeneratorTarget* target)
  206. {
  207. if (!target) {
  208. return;
  209. }
  210. std::stable_sort(
  211. includeDirs.begin(), includeDirs.end(),
  212. [&target, &config, &lang](std::string const& a, std::string const& b) {
  213. return !target->IsSystemIncludeDirectory(a, config, lang) &&
  214. target->IsSystemIncludeDirectory(b, config, lang);
  215. });
  216. }
  217. static void MoveSystemIncludesToEnd(std::vector<BT<std::string>>& includeDirs,
  218. const std::string& config,
  219. const std::string& lang,
  220. const cmGeneratorTarget* target)
  221. {
  222. if (!target) {
  223. return;
  224. }
  225. std::stable_sort(includeDirs.begin(), includeDirs.end(),
  226. [target, &config, &lang](BT<std::string> const& a,
  227. BT<std::string> const& b) {
  228. return !target->IsSystemIncludeDirectory(a.Value, config,
  229. lang) &&
  230. target->IsSystemIncludeDirectory(b.Value, config, lang);
  231. });
  232. }
  233. void cmLocalGenerator::TraceDependencies()
  234. {
  235. std::vector<std::string> const& configs =
  236. this->Makefile->GetGeneratorConfigs();
  237. for (std::string const& c : configs) {
  238. this->GlobalGenerator->CreateEvaluationSourceFiles(c);
  239. }
  240. // Generate the rule files for each target.
  241. const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
  242. for (cmGeneratorTarget* target : targets) {
  243. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  244. continue;
  245. }
  246. target->TraceDependencies();
  247. }
  248. }
  249. void cmLocalGenerator::GenerateTestFiles()
  250. {
  251. if (!this->Makefile->IsOn("CMAKE_TESTING_ENABLED")) {
  252. return;
  253. }
  254. // Compute the set of configurations.
  255. std::vector<std::string> configurationTypes;
  256. const std::string& config =
  257. this->Makefile->GetConfigurations(configurationTypes, false);
  258. std::string file =
  259. cmStrCat(this->StateSnapshot.GetDirectory().GetCurrentBinary(),
  260. "/CTestTestfile.cmake");
  261. cmGeneratedFileStream fout(file);
  262. fout.SetCopyIfDifferent(true);
  263. fout << "# CMake generated Testfile for " << std::endl
  264. << "# Source directory: "
  265. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  266. << "# Build directory: "
  267. << this->StateSnapshot.GetDirectory().GetCurrentBinary() << std::endl
  268. << "# " << std::endl
  269. << "# This file includes the relevant testing commands "
  270. << "required for " << std::endl
  271. << "# testing this directory and lists subdirectories to "
  272. << "be tested as well." << std::endl;
  273. const char* testIncludeFile =
  274. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  275. if (testIncludeFile) {
  276. fout << "include(\"" << testIncludeFile << "\")" << std::endl;
  277. }
  278. const char* testIncludeFiles =
  279. this->Makefile->GetProperty("TEST_INCLUDE_FILES");
  280. if (testIncludeFiles) {
  281. std::vector<std::string> includesList = cmExpandedList(testIncludeFiles);
  282. for (std::string const& i : includesList) {
  283. fout << "include(\"" << i << "\")" << std::endl;
  284. }
  285. }
  286. // Ask each test generator to write its code.
  287. std::vector<cmTestGenerator*> const& testers =
  288. this->Makefile->GetTestGenerators();
  289. for (cmTestGenerator* tester : testers) {
  290. tester->Compute(this);
  291. tester->Generate(fout, config, configurationTypes);
  292. }
  293. using vec_t = std::vector<cmStateSnapshot>;
  294. vec_t const& children = this->Makefile->GetStateSnapshot().GetChildren();
  295. std::string parentBinDir = this->GetCurrentBinaryDirectory();
  296. for (cmStateSnapshot const& i : children) {
  297. // TODO: Use add_subdirectory instead?
  298. std::string outP = i.GetDirectory().GetCurrentBinary();
  299. outP = this->MaybeConvertToRelativePath(parentBinDir, outP);
  300. outP = cmOutputConverter::EscapeForCMake(outP);
  301. fout << "subdirs(" << outP << ")" << std::endl;
  302. }
  303. // Add directory labels property
  304. const char* directoryLabels =
  305. this->Makefile->GetDefinition("CMAKE_DIRECTORY_LABELS");
  306. const char* labels = this->Makefile->GetProperty("LABELS");
  307. if (labels || directoryLabels) {
  308. fout << "set_directory_properties(PROPERTIES LABELS ";
  309. if (labels) {
  310. fout << cmOutputConverter::EscapeForCMake(labels);
  311. }
  312. if (labels && directoryLabels) {
  313. fout << ";";
  314. }
  315. if (directoryLabels) {
  316. fout << cmOutputConverter::EscapeForCMake(directoryLabels);
  317. }
  318. fout << ")" << std::endl;
  319. }
  320. }
  321. void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
  322. {
  323. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  324. this->Makefile->GetEvaluationFiles();
  325. for (cmGeneratorExpressionEvaluationFile* geef : ef) {
  326. geef->CreateOutputFile(this, config);
  327. }
  328. }
  329. void cmLocalGenerator::ProcessEvaluationFiles(
  330. std::vector<std::string>& generatedFiles)
  331. {
  332. for (cmGeneratorExpressionEvaluationFile* geef :
  333. this->Makefile->GetEvaluationFiles()) {
  334. geef->Generate(this);
  335. if (cmSystemTools::GetFatalErrorOccured()) {
  336. return;
  337. }
  338. std::vector<std::string> files = geef->GetFiles();
  339. std::sort(files.begin(), files.end());
  340. std::vector<std::string> intersection;
  341. std::set_intersection(files.begin(), files.end(), generatedFiles.begin(),
  342. generatedFiles.end(),
  343. std::back_inserter(intersection));
  344. if (!intersection.empty()) {
  345. cmSystemTools::Error("Files to be generated by multiple different "
  346. "commands: " +
  347. cmWrap('"', intersection, '"', " "));
  348. return;
  349. }
  350. cmAppend(generatedFiles, files);
  351. std::inplace_merge(generatedFiles.begin(),
  352. generatedFiles.end() - files.size(),
  353. generatedFiles.end());
  354. }
  355. }
  356. void cmLocalGenerator::GenerateInstallRules()
  357. {
  358. // Compute the install prefix.
  359. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  360. #if defined(_WIN32) && !defined(__CYGWIN__)
  361. std::string prefix_win32;
  362. if (!prefix) {
  363. if (!cmSystemTools::GetEnv("SystemDrive", prefix_win32)) {
  364. prefix_win32 = "C:";
  365. }
  366. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  367. if (project_name && project_name[0]) {
  368. prefix_win32 += "/Program Files/";
  369. prefix_win32 += project_name;
  370. } else {
  371. prefix_win32 += "/InstalledCMakeProject";
  372. }
  373. prefix = prefix_win32.c_str();
  374. }
  375. #elif defined(__HAIKU__)
  376. char dir[B_PATH_NAME_LENGTH];
  377. if (!prefix) {
  378. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  379. B_OK) {
  380. prefix = dir;
  381. } else {
  382. prefix = "/boot/system";
  383. }
  384. }
  385. #else
  386. if (!prefix) {
  387. prefix = "/usr/local";
  388. }
  389. #endif
  390. if (const char* stagingPrefix =
  391. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) {
  392. prefix = stagingPrefix;
  393. }
  394. // Compute the set of configurations.
  395. std::vector<std::string> configurationTypes;
  396. const std::string& config =
  397. this->Makefile->GetConfigurations(configurationTypes, false);
  398. // Choose a default install configuration.
  399. std::string default_config = config;
  400. const char* default_order[] = { "RELEASE", "MINSIZEREL", "RELWITHDEBINFO",
  401. "DEBUG", nullptr };
  402. for (const char** c = default_order; *c && default_config.empty(); ++c) {
  403. for (std::string const& configurationType : configurationTypes) {
  404. if (cmSystemTools::UpperCase(configurationType) == *c) {
  405. default_config = configurationType;
  406. }
  407. }
  408. }
  409. if (default_config.empty() && !configurationTypes.empty()) {
  410. default_config = configurationTypes[0];
  411. }
  412. // Create the install script file.
  413. std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  414. std::string homedir = this->GetState()->GetBinaryDirectory();
  415. int toplevel_install = 0;
  416. if (file == homedir) {
  417. toplevel_install = 1;
  418. }
  419. file += "/cmake_install.cmake";
  420. cmGeneratedFileStream fout(file);
  421. fout.SetCopyIfDifferent(true);
  422. // Write the header.
  423. fout << "# Install script for directory: "
  424. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  425. << std::endl;
  426. fout << "# Set the install prefix" << std::endl
  427. << "if(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  428. << " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  429. << "endif()" << std::endl
  430. << R"(string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX )"
  431. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  432. << std::endl;
  433. // Write support code for generating per-configuration install rules.
  434. /* clang-format off */
  435. fout <<
  436. "# Set the install configuration name.\n"
  437. "if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  438. " if(BUILD_TYPE)\n"
  439. " string(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  440. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  441. " else()\n"
  442. " set(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  443. " endif()\n"
  444. " message(STATUS \"Install configuration: "
  445. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  446. "endif()\n"
  447. "\n";
  448. /* clang-format on */
  449. // Write support code for dealing with component-specific installs.
  450. /* clang-format off */
  451. fout <<
  452. "# Set the component getting installed.\n"
  453. "if(NOT CMAKE_INSTALL_COMPONENT)\n"
  454. " if(COMPONENT)\n"
  455. " message(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  456. " set(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  457. " else()\n"
  458. " set(CMAKE_INSTALL_COMPONENT)\n"
  459. " endif()\n"
  460. "endif()\n"
  461. "\n";
  462. /* clang-format on */
  463. // Copy user-specified install options to the install code.
  464. if (const char* so_no_exe =
  465. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE")) {
  466. /* clang-format off */
  467. fout <<
  468. "# Install shared libraries without execute permission?\n"
  469. "if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  470. " set(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  471. "endif()\n"
  472. "\n";
  473. /* clang-format on */
  474. }
  475. // Copy cmake cross compile state to install code.
  476. if (const char* crosscompiling =
  477. this->Makefile->GetDefinition("CMAKE_CROSSCOMPILING")) {
  478. /* clang-format off */
  479. fout <<
  480. "# Is this installation the result of a crosscompile?\n"
  481. "if(NOT DEFINED CMAKE_CROSSCOMPILING)\n"
  482. " set(CMAKE_CROSSCOMPILING \"" << crosscompiling << "\")\n"
  483. "endif()\n"
  484. "\n";
  485. /* clang-format on */
  486. }
  487. // Write default directory permissions.
  488. if (const char* defaultDirPermissions = this->Makefile->GetDefinition(
  489. "CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS")) {
  490. /* clang-format off */
  491. fout <<
  492. "# Set default install directory permissions.\n"
  493. "if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS)\n"
  494. " set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS \""
  495. << defaultDirPermissions << "\")\n"
  496. "endif()\n"
  497. "\n";
  498. /* clang-format on */
  499. }
  500. // Ask each install generator to write its code.
  501. cmPolicies::PolicyStatus status = this->GetPolicyStatus(cmPolicies::CMP0082);
  502. std::vector<cmInstallGenerator*> const& installers =
  503. this->Makefile->GetInstallGenerators();
  504. bool haveSubdirectoryInstall = false;
  505. bool haveInstallAfterSubdirectory = false;
  506. if (status == cmPolicies::WARN) {
  507. for (cmInstallGenerator* installer : installers) {
  508. installer->CheckCMP0082(haveSubdirectoryInstall,
  509. haveInstallAfterSubdirectory);
  510. installer->Generate(fout, config, configurationTypes);
  511. }
  512. } else {
  513. for (cmInstallGenerator* installer : installers) {
  514. installer->Generate(fout, config, configurationTypes);
  515. }
  516. }
  517. // Write rules from old-style specification stored in targets.
  518. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  519. // Include install scripts from subdirectories.
  520. switch (status) {
  521. case cmPolicies::WARN:
  522. if (haveInstallAfterSubdirectory &&
  523. this->Makefile->PolicyOptionalWarningEnabled(
  524. "CMAKE_POLICY_WARNING_CMP0082")) {
  525. std::ostringstream e;
  526. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0082) << "\n";
  527. this->IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  528. }
  529. CM_FALLTHROUGH;
  530. case cmPolicies::OLD: {
  531. std::vector<cmStateSnapshot> children =
  532. this->Makefile->GetStateSnapshot().GetChildren();
  533. if (!children.empty()) {
  534. fout << "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  535. fout << " # Include the install script for each subdirectory.\n";
  536. for (cmStateSnapshot const& c : children) {
  537. if (!c.GetDirectory().GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  538. std::string odir = c.GetDirectory().GetCurrentBinary();
  539. cmSystemTools::ConvertToUnixSlashes(odir);
  540. fout << " include(\"" << odir << "/cmake_install.cmake\")"
  541. << std::endl;
  542. }
  543. }
  544. fout << "\n";
  545. fout << "endif()\n\n";
  546. }
  547. } break;
  548. case cmPolicies::REQUIRED_IF_USED:
  549. case cmPolicies::REQUIRED_ALWAYS:
  550. case cmPolicies::NEW:
  551. // NEW behavior is handled in
  552. // cmInstallSubdirectoryGenerator::GenerateScript()
  553. break;
  554. }
  555. // Record the install manifest.
  556. if (toplevel_install) {
  557. /* clang-format off */
  558. fout <<
  559. "if(CMAKE_INSTALL_COMPONENT)\n"
  560. " set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  561. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  562. "else()\n"
  563. " set(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  564. "endif()\n"
  565. "\n"
  566. "string(REPLACE \";\" \"\\n\" CMAKE_INSTALL_MANIFEST_CONTENT\n"
  567. " \"${CMAKE_INSTALL_MANIFEST_FILES}\")\n"
  568. "file(WRITE \"" << homedir << "/${CMAKE_INSTALL_MANIFEST}\"\n"
  569. " \"${CMAKE_INSTALL_MANIFEST_CONTENT}\")\n";
  570. /* clang-format on */
  571. }
  572. }
  573. void cmLocalGenerator::AddGeneratorTarget(cmGeneratorTarget* gt)
  574. {
  575. this->GeneratorTargets.push_back(gt);
  576. this->GeneratorTargetSearchIndex.emplace(gt->GetName(), gt);
  577. this->GlobalGenerator->IndexGeneratorTarget(gt);
  578. }
  579. void cmLocalGenerator::AddImportedGeneratorTarget(cmGeneratorTarget* gt)
  580. {
  581. this->ImportedGeneratorTargets.emplace(gt->GetName(), gt);
  582. this->GlobalGenerator->IndexGeneratorTarget(gt);
  583. }
  584. void cmLocalGenerator::AddOwnedImportedGeneratorTarget(cmGeneratorTarget* gt)
  585. {
  586. this->OwnedImportedGeneratorTargets.push_back(gt);
  587. }
  588. cmGeneratorTarget* cmLocalGenerator::FindLocalNonAliasGeneratorTarget(
  589. const std::string& name) const
  590. {
  591. auto ti = this->GeneratorTargetSearchIndex.find(name);
  592. if (ti != this->GeneratorTargetSearchIndex.end()) {
  593. return ti->second;
  594. }
  595. return nullptr;
  596. }
  597. void cmLocalGenerator::ComputeTargetManifest()
  598. {
  599. // Collect the set of configuration types.
  600. std::vector<std::string> configNames;
  601. this->Makefile->GetConfigurations(configNames);
  602. if (configNames.empty()) {
  603. configNames.emplace_back();
  604. }
  605. // Add our targets to the manifest for each configuration.
  606. const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
  607. for (cmGeneratorTarget* target : targets) {
  608. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  609. continue;
  610. }
  611. for (std::string const& c : configNames) {
  612. target->ComputeTargetManifest(c);
  613. }
  614. }
  615. }
  616. bool cmLocalGenerator::ComputeTargetCompileFeatures()
  617. {
  618. // Collect the set of configuration types.
  619. std::vector<std::string> configNames;
  620. this->Makefile->GetConfigurations(configNames);
  621. if (configNames.empty()) {
  622. configNames.emplace_back();
  623. }
  624. // Process compile features of all targets.
  625. const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
  626. for (cmGeneratorTarget* target : targets) {
  627. for (std::string const& c : configNames) {
  628. if (!target->ComputeCompileFeatures(c)) {
  629. return false;
  630. }
  631. }
  632. }
  633. return true;
  634. }
  635. bool cmLocalGenerator::IsRootMakefile() const
  636. {
  637. return !this->StateSnapshot.GetBuildsystemDirectoryParent().IsValid();
  638. }
  639. cmState* cmLocalGenerator::GetState() const
  640. {
  641. return this->GlobalGenerator->GetCMakeInstance()->GetState();
  642. }
  643. cmStateSnapshot cmLocalGenerator::GetStateSnapshot() const
  644. {
  645. return this->Makefile->GetStateSnapshot();
  646. }
  647. const char* cmLocalGenerator::GetRuleLauncher(cmGeneratorTarget* target,
  648. const std::string& prop)
  649. {
  650. if (target) {
  651. return target->GetProperty(prop);
  652. }
  653. return this->Makefile->GetProperty(prop);
  654. }
  655. std::string cmLocalGenerator::ConvertToIncludeReference(
  656. std::string const& path, OutputFormat format, bool forceFullPaths)
  657. {
  658. static_cast<void>(forceFullPaths);
  659. return this->ConvertToOutputForExisting(path, format);
  660. }
  661. std::string cmLocalGenerator::GetIncludeFlags(
  662. const std::vector<std::string>& includeDirs, cmGeneratorTarget* target,
  663. const std::string& lang, bool forceFullPaths, bool forResponseFile,
  664. const std::string& config)
  665. {
  666. if (lang.empty()) {
  667. return "";
  668. }
  669. std::vector<std::string> includes = includeDirs;
  670. MoveSystemIncludesToEnd(includes, config, lang, target);
  671. OutputFormat shellFormat = forResponseFile ? RESPONSE : SHELL;
  672. std::ostringstream includeFlags;
  673. std::string const& includeFlag =
  674. this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_INCLUDE_FLAG_", lang));
  675. const char* sep =
  676. this->Makefile->GetDefinition(cmStrCat("CMAKE_INCLUDE_FLAG_SEP_", lang));
  677. bool quotePaths = false;
  678. if (this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS")) {
  679. quotePaths = true;
  680. }
  681. bool repeatFlag = true;
  682. // should the include flag be repeated like ie. -IA -IB
  683. if (!sep) {
  684. sep = " ";
  685. } else {
  686. // if there is a separator then the flag is not repeated but is only
  687. // given once i.e. -classpath a:b:c
  688. repeatFlag = false;
  689. }
  690. // Support special system include flag if it is available and the
  691. // normal flag is repeated for each directory.
  692. const char* sysIncludeFlag = nullptr;
  693. if (repeatFlag) {
  694. sysIncludeFlag = this->Makefile->GetDefinition(
  695. cmStrCat("CMAKE_INCLUDE_SYSTEM_FLAG_", lang));
  696. }
  697. const char* fwSearchFlag = this->Makefile->GetDefinition(
  698. cmStrCat("CMAKE_", lang, "_FRAMEWORK_SEARCH_FLAG"));
  699. const char* sysFwSearchFlag = this->Makefile->GetDefinition(
  700. cmStrCat("CMAKE_", lang, "_SYSTEM_FRAMEWORK_SEARCH_FLAG"));
  701. bool flagUsed = false;
  702. std::set<std::string> emitted;
  703. #ifdef __APPLE__
  704. emitted.insert("/System/Library/Frameworks");
  705. #endif
  706. for (std::string const& i : includes) {
  707. if (fwSearchFlag && *fwSearchFlag && this->Makefile->IsOn("APPLE") &&
  708. cmSystemTools::IsPathToFramework(i)) {
  709. std::string const frameworkDir =
  710. cmSystemTools::CollapseFullPath(cmStrCat(i, "/../"));
  711. if (emitted.insert(frameworkDir).second) {
  712. if (sysFwSearchFlag && target &&
  713. target->IsSystemIncludeDirectory(i, config, lang)) {
  714. includeFlags << sysFwSearchFlag;
  715. } else {
  716. includeFlags << fwSearchFlag;
  717. }
  718. includeFlags << this->ConvertToOutputFormat(frameworkDir, shellFormat)
  719. << " ";
  720. }
  721. continue;
  722. }
  723. if (!flagUsed || repeatFlag) {
  724. if (sysIncludeFlag && target &&
  725. target->IsSystemIncludeDirectory(i, config, lang)) {
  726. includeFlags << sysIncludeFlag;
  727. } else {
  728. includeFlags << includeFlag;
  729. }
  730. flagUsed = true;
  731. }
  732. std::string includePath =
  733. this->ConvertToIncludeReference(i, shellFormat, forceFullPaths);
  734. if (quotePaths && !includePath.empty() && includePath.front() != '\"') {
  735. includeFlags << "\"";
  736. }
  737. includeFlags << includePath;
  738. if (quotePaths && !includePath.empty() && includePath.front() != '\"') {
  739. includeFlags << "\"";
  740. }
  741. includeFlags << sep;
  742. }
  743. std::string flags = includeFlags.str();
  744. // remove trailing separators
  745. if ((sep[0] != ' ') && !flags.empty() && flags.back() == sep[0]) {
  746. flags.back() = ' ';
  747. }
  748. return flags;
  749. }
  750. void cmLocalGenerator::AddCompileOptions(std::string& flags,
  751. cmGeneratorTarget* target,
  752. const std::string& lang,
  753. const std::string& config)
  754. {
  755. std::vector<BT<std::string>> tmpFlags;
  756. this->AddCompileOptions(tmpFlags, target, lang, config);
  757. this->AppendFlags(flags, tmpFlags);
  758. }
  759. void cmLocalGenerator::AddCompileOptions(std::vector<BT<std::string>>& flags,
  760. cmGeneratorTarget* target,
  761. const std::string& lang,
  762. const std::string& config)
  763. {
  764. std::string langFlagRegexVar = std::string("CMAKE_") + lang + "_FLAG_REGEX";
  765. if (const char* langFlagRegexStr =
  766. this->Makefile->GetDefinition(langFlagRegexVar)) {
  767. // Filter flags acceptable to this language.
  768. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  769. std::vector<std::string> opts;
  770. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  771. // Re-escape these flags since COMPILE_FLAGS were already parsed
  772. // as a command line above.
  773. std::string compileOpts;
  774. this->AppendCompileOptions(compileOpts, opts, langFlagRegexStr);
  775. if (!compileOpts.empty()) {
  776. flags.emplace_back(std::move(compileOpts));
  777. }
  778. }
  779. std::vector<BT<std::string>> targetCompileOpts =
  780. target->GetCompileOptions(config, lang);
  781. // COMPILE_OPTIONS are escaped.
  782. this->AppendCompileOptions(flags, targetCompileOpts, langFlagRegexStr);
  783. } else {
  784. // Use all flags.
  785. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  786. // COMPILE_FLAGS are not escaped for historical reasons.
  787. std::string compileFlags;
  788. this->AppendFlags(compileFlags, targetFlags);
  789. if (!compileFlags.empty()) {
  790. flags.emplace_back(std::move(compileFlags));
  791. }
  792. }
  793. std::vector<BT<std::string>> targetCompileOpts =
  794. target->GetCompileOptions(config, lang);
  795. // COMPILE_OPTIONS are escaped.
  796. this->AppendCompileOptions(flags, targetCompileOpts);
  797. }
  798. for (auto const& it : target->GetMaxLanguageStandards()) {
  799. const char* standard = target->GetProperty(it.first + "_STANDARD");
  800. if (!standard) {
  801. continue;
  802. }
  803. if (this->Makefile->IsLaterStandard(it.first, standard, it.second)) {
  804. std::ostringstream e;
  805. e << "The COMPILE_FEATURES property of target \"" << target->GetName()
  806. << "\" was evaluated when computing the link "
  807. "implementation, and the \""
  808. << it.first << "_STANDARD\" was \"" << it.second
  809. << "\" for that computation. Computing the "
  810. "COMPILE_FEATURES based on the link implementation resulted in a "
  811. "higher \""
  812. << it.first << "_STANDARD\" \"" << standard
  813. << "\". "
  814. "This is not permitted. The COMPILE_FEATURES may not both depend "
  815. "on "
  816. "and be depended on by the link implementation."
  817. << std::endl;
  818. this->IssueMessage(MessageType::FATAL_ERROR, e.str());
  819. return;
  820. }
  821. }
  822. std::string compReqFlag;
  823. this->AddCompilerRequirementFlag(compReqFlag, target, lang);
  824. if (!compReqFlag.empty()) {
  825. flags.emplace_back(std::move(compReqFlag));
  826. }
  827. // Add compile flag for the MSVC compiler only.
  828. cmMakefile* mf = this->GetMakefile();
  829. if (const char* jmc =
  830. mf->GetDefinition("CMAKE_" + lang + "_COMPILE_OPTIONS_JMC")) {
  831. // Handle Just My Code debugging flags, /JMC.
  832. // If the target is a Managed C++ one, /JMC is not compatible.
  833. if (target->GetManagedType(config) !=
  834. cmGeneratorTarget::ManagedType::Managed) {
  835. // add /JMC flags if target property VS_JUST_MY_CODE_DEBUGGING is set
  836. // to ON
  837. if (char const* jmcExprGen =
  838. target->GetProperty("VS_JUST_MY_CODE_DEBUGGING")) {
  839. cmGeneratorExpression ge;
  840. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  841. ge.Parse(jmcExprGen);
  842. std::string isJMCEnabled = cge->Evaluate(this, config);
  843. if (cmIsOn(isJMCEnabled)) {
  844. std::vector<std::string> optVec = cmExpandedList(jmc);
  845. std::string jmcFlags;
  846. this->AppendCompileOptions(jmcFlags, optVec);
  847. if (!jmcFlags.empty()) {
  848. flags.emplace_back(std::move(jmcFlags));
  849. }
  850. }
  851. }
  852. }
  853. }
  854. }
  855. std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectoriesImplicit(
  856. cmGeneratorTarget const* target, std::string const& lang,
  857. std::string const& config, bool stripImplicitDirs,
  858. bool appendAllImplicitDirs) const
  859. {
  860. std::vector<BT<std::string>> result;
  861. // Do not repeat an include path.
  862. std::set<std::string> emitted;
  863. auto emitDir = [&result, &emitted](std::string const& dir) {
  864. if (emitted.insert(dir).second) {
  865. result.emplace_back(dir);
  866. }
  867. };
  868. auto emitBT = [&result, &emitted](BT<std::string> const& dir) {
  869. if (emitted.insert(dir.Value).second) {
  870. result.emplace_back(dir);
  871. }
  872. };
  873. // When automatic include directories are requested for a build then
  874. // include the source and binary directories at the beginning of the
  875. // include path to approximate include file behavior for an
  876. // in-source build. This does not account for the case of a source
  877. // file in a subdirectory of the current source directory but we
  878. // cannot fix this because not all native build tools support
  879. // per-source-file include paths.
  880. if (this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR")) {
  881. // Current binary directory
  882. emitDir(this->StateSnapshot.GetDirectory().GetCurrentBinary());
  883. // Current source directory
  884. emitDir(this->StateSnapshot.GetDirectory().GetCurrentSource());
  885. }
  886. if (!target) {
  887. return result;
  888. }
  889. // Standard include directories to be added unconditionally at the end.
  890. // These are intended to simulate additional implicit include directories.
  891. std::vector<std::string> userStandardDirs;
  892. {
  893. std::string const value = this->Makefile->GetSafeDefinition(
  894. cmStrCat("CMAKE_", lang, "_STANDARD_INCLUDE_DIRECTORIES"));
  895. cmExpandList(value, userStandardDirs);
  896. for (std::string& usd : userStandardDirs) {
  897. cmSystemTools::ConvertToUnixSlashes(usd);
  898. }
  899. }
  900. // Implicit include directories
  901. std::vector<std::string> implicitDirs;
  902. std::set<std::string> implicitSet;
  903. // Include directories to be excluded as if they were implicit.
  904. std::set<std::string> implicitExclude;
  905. {
  906. // Raw list of implicit include directories
  907. // Start with "standard" directories that we unconditionally add below.
  908. std::vector<std::string> impDirVec = userStandardDirs;
  909. // Load implicit include directories for this language.
  910. // We ignore this for Fortran because:
  911. // * There are no standard library headers to avoid overriding.
  912. // * Compilers like gfortran do not search their own implicit include
  913. // directories for modules ('.mod' files).
  914. if (lang != "Fortran") {
  915. const char* value = this->Makefile->GetDefinition(
  916. cmStrCat("CMAKE_", lang, "_IMPLICIT_INCLUDE_DIRECTORIES"));
  917. if (value != nullptr) {
  918. size_t const impDirVecOldSize = impDirVec.size();
  919. cmExpandList(value, impDirVec);
  920. // FIXME: Use cmRange with 'advance()' when it supports non-const.
  921. for (size_t i = impDirVecOldSize; i < impDirVec.size(); ++i) {
  922. cmSystemTools::ConvertToUnixSlashes(impDirVec[i]);
  923. }
  924. }
  925. }
  926. // The Platform/UnixPaths module used to hard-code /usr/include for C, CXX,
  927. // and CUDA in CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES, but those
  928. // variables are now computed. On macOS the /usr/include directory is
  929. // inside the platform SDK so the computed value does not contain it
  930. // directly. In this case adding -I/usr/include can hide SDK headers so we
  931. // must still exclude it.
  932. if ((lang == "C" || lang == "CXX" || lang == "CUDA") &&
  933. !cmContains(impDirVec, "/usr/include") &&
  934. std::find_if(impDirVec.begin(), impDirVec.end(),
  935. [](std::string const& d) {
  936. return cmHasLiteralSuffix(d, "/usr/include");
  937. }) != impDirVec.end()) {
  938. // Only exclude this hard coded path for backwards compatibility.
  939. implicitExclude.emplace("/usr/include");
  940. }
  941. for (std::string const& i : impDirVec) {
  942. if (implicitSet.insert(cmSystemTools::GetRealPath(i)).second) {
  943. implicitDirs.emplace_back(i);
  944. }
  945. }
  946. }
  947. // Checks if this is not an excluded (implicit) include directory.
  948. auto notExcluded = [this, &implicitSet, &implicitExclude,
  949. &lang](std::string const& dir) {
  950. return (
  951. // Do not exclude directories that are not in an excluded set.
  952. ((!cmContains(implicitSet, cmSystemTools::GetRealPath(dir))) &&
  953. (!cmContains(implicitExclude, dir)))
  954. // Do not exclude entries of the CPATH environment variable even though
  955. // they are implicitly searched by the compiler. They are meant to be
  956. // user-specified directories that can be re-ordered or converted to
  957. // -isystem without breaking real compiler builtin headers.
  958. || ((lang == "C" || lang == "CXX") && cmContains(this->EnvCPATH, dir)));
  959. };
  960. // Get the target-specific include directories.
  961. std::vector<BT<std::string>> userDirs =
  962. target->GetIncludeDirectories(config, lang);
  963. // Support putting all the in-project include directories first if
  964. // it is requested by the project.
  965. if (this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE")) {
  966. std::string const& topSourceDir = this->GetState()->GetSourceDirectory();
  967. std::string const& topBinaryDir = this->GetState()->GetBinaryDirectory();
  968. for (BT<std::string> const& udr : userDirs) {
  969. // Emit this directory only if it is a subdirectory of the
  970. // top-level source or binary tree.
  971. if (cmSystemTools::ComparePath(udr.Value, topSourceDir) ||
  972. cmSystemTools::ComparePath(udr.Value, topBinaryDir) ||
  973. cmSystemTools::IsSubDirectory(udr.Value, topSourceDir) ||
  974. cmSystemTools::IsSubDirectory(udr.Value, topBinaryDir)) {
  975. if (notExcluded(udr.Value)) {
  976. emitBT(udr);
  977. }
  978. }
  979. }
  980. }
  981. // Emit remaining non implicit user direcories.
  982. for (BT<std::string> const& udr : userDirs) {
  983. if (notExcluded(udr.Value)) {
  984. emitBT(udr);
  985. }
  986. }
  987. // Sort result
  988. MoveSystemIncludesToEnd(result, config, lang, target);
  989. // Append standard include directories for this language.
  990. userDirs.reserve(userDirs.size() + userStandardDirs.size());
  991. for (std::string& usd : userStandardDirs) {
  992. emitDir(usd);
  993. userDirs.emplace_back(std::move(usd));
  994. }
  995. // Append compiler implicit include directories
  996. if (!stripImplicitDirs) {
  997. // Append implicit directories that were requested by the user only
  998. for (BT<std::string> const& udr : userDirs) {
  999. if (cmContains(implicitSet, cmSystemTools::GetRealPath(udr.Value))) {
  1000. emitBT(udr);
  1001. }
  1002. }
  1003. // Append remaining implicit directories (on demand)
  1004. if (appendAllImplicitDirs) {
  1005. for (std::string& imd : implicitDirs) {
  1006. emitDir(imd);
  1007. }
  1008. }
  1009. }
  1010. return result;
  1011. }
  1012. void cmLocalGenerator::GetIncludeDirectoriesImplicit(
  1013. std::vector<std::string>& dirs, cmGeneratorTarget const* target,
  1014. const std::string& lang, const std::string& config, bool stripImplicitDirs,
  1015. bool appendAllImplicitDirs) const
  1016. {
  1017. std::vector<BT<std::string>> tmp = this->GetIncludeDirectoriesImplicit(
  1018. target, lang, config, stripImplicitDirs, appendAllImplicitDirs);
  1019. dirs.reserve(dirs.size() + tmp.size());
  1020. for (BT<std::string>& v : tmp) {
  1021. dirs.emplace_back(std::move(v.Value));
  1022. }
  1023. }
  1024. std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectories(
  1025. cmGeneratorTarget const* target, std::string const& lang,
  1026. std::string const& config) const
  1027. {
  1028. return this->GetIncludeDirectoriesImplicit(target, lang, config);
  1029. }
  1030. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1031. cmGeneratorTarget const* target,
  1032. const std::string& lang,
  1033. const std::string& config) const
  1034. {
  1035. this->GetIncludeDirectoriesImplicit(dirs, target, lang, config);
  1036. }
  1037. void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
  1038. std::string const& config,
  1039. std::string const& linkLanguage,
  1040. cmGeneratorTarget* target)
  1041. {
  1042. std::vector<BT<std::string>> tmpFlags =
  1043. this->GetStaticLibraryFlags(config, linkLanguage, target);
  1044. this->AppendFlags(flags, tmpFlags);
  1045. }
  1046. std::vector<BT<std::string>> cmLocalGenerator::GetStaticLibraryFlags(
  1047. std::string const& config, std::string const& linkLanguage,
  1048. cmGeneratorTarget* target)
  1049. {
  1050. std::vector<BT<std::string>> flags;
  1051. if (linkLanguage != "Swift") {
  1052. std::string staticLibFlags;
  1053. this->AppendFlags(
  1054. staticLibFlags,
  1055. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS"));
  1056. if (!config.empty()) {
  1057. std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config;
  1058. this->AppendFlags(staticLibFlags,
  1059. this->Makefile->GetSafeDefinition(name));
  1060. }
  1061. if (!staticLibFlags.empty()) {
  1062. flags.emplace_back(std::move(staticLibFlags));
  1063. }
  1064. }
  1065. std::string staticLibFlags;
  1066. this->AppendFlags(staticLibFlags,
  1067. target->GetSafeProperty("STATIC_LIBRARY_FLAGS"));
  1068. if (!config.empty()) {
  1069. std::string name = "STATIC_LIBRARY_FLAGS_" + config;
  1070. this->AppendFlags(staticLibFlags, target->GetSafeProperty(name));
  1071. }
  1072. if (!staticLibFlags.empty()) {
  1073. flags.emplace_back(std::move(staticLibFlags));
  1074. }
  1075. std::vector<BT<std::string>> staticLibOpts =
  1076. target->GetStaticLibraryLinkOptions(config, linkLanguage);
  1077. // STATIC_LIBRARY_OPTIONS are escaped.
  1078. this->AppendCompileOptions(flags, staticLibOpts);
  1079. return flags;
  1080. }
  1081. void cmLocalGenerator::GetTargetFlags(
  1082. cmLinkLineComputer* linkLineComputer, const std::string& config,
  1083. std::string& linkLibs, std::string& flags, std::string& linkFlags,
  1084. std::string& frameworkPath, std::string& linkPath, cmGeneratorTarget* target)
  1085. {
  1086. std::vector<BT<std::string>> tmpLinkFlags;
  1087. this->GetTargetFlags(linkLineComputer, config, linkLibs, flags, tmpLinkFlags,
  1088. frameworkPath, linkPath, target);
  1089. this->AppendFlags(linkFlags, tmpLinkFlags);
  1090. }
  1091. void cmLocalGenerator::GetTargetFlags(
  1092. cmLinkLineComputer* linkLineComputer, const std::string& config,
  1093. std::string& linkLibs, std::string& flags,
  1094. std::vector<BT<std::string>>& linkFlags, std::string& frameworkPath,
  1095. std::string& linkPath, cmGeneratorTarget* target)
  1096. {
  1097. const std::string buildType = cmSystemTools::UpperCase(config);
  1098. cmComputeLinkInformation* pcli = target->GetLinkInformation(config);
  1099. const char* libraryLinkVariable =
  1100. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1101. const std::string linkLanguage =
  1102. linkLineComputer->GetLinkerLanguage(target, buildType);
  1103. switch (target->GetType()) {
  1104. case cmStateEnums::STATIC_LIBRARY:
  1105. linkFlags = this->GetStaticLibraryFlags(buildType, linkLanguage, target);
  1106. if (pcli && dynamic_cast<cmLinkLineDeviceComputer*>(linkLineComputer)) {
  1107. // Compute the required cuda device link libraries when
  1108. // resolving cuda device symbols
  1109. this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  1110. frameworkPath, linkPath);
  1111. }
  1112. break;
  1113. case cmStateEnums::MODULE_LIBRARY:
  1114. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1115. CM_FALLTHROUGH;
  1116. case cmStateEnums::SHARED_LIBRARY: {
  1117. std::string sharedLibFlags;
  1118. if (linkLanguage != "Swift") {
  1119. sharedLibFlags = cmStrCat(
  1120. this->Makefile->GetSafeDefinition(libraryLinkVariable), ' ');
  1121. if (!buildType.empty()) {
  1122. std::string build = cmStrCat(libraryLinkVariable, '_', buildType);
  1123. sharedLibFlags += this->Makefile->GetSafeDefinition(build);
  1124. sharedLibFlags += " ";
  1125. }
  1126. if (this->Makefile->IsOn("WIN32") &&
  1127. !(this->Makefile->IsOn("CYGWIN") ||
  1128. this->Makefile->IsOn("MINGW"))) {
  1129. std::vector<cmSourceFile*> sources;
  1130. target->GetSourceFiles(sources, buildType);
  1131. std::string defFlag =
  1132. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1133. for (cmSourceFile* sf : sources) {
  1134. if (sf->GetExtension() == "def") {
  1135. sharedLibFlags += defFlag;
  1136. sharedLibFlags += this->ConvertToOutputFormat(
  1137. cmSystemTools::CollapseFullPath(sf->ResolveFullPath()), SHELL);
  1138. sharedLibFlags += " ";
  1139. }
  1140. }
  1141. }
  1142. }
  1143. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1144. if (targetLinkFlags) {
  1145. sharedLibFlags += targetLinkFlags;
  1146. sharedLibFlags += " ";
  1147. }
  1148. if (!buildType.empty()) {
  1149. targetLinkFlags =
  1150. target->GetProperty(cmStrCat("LINK_FLAGS_", buildType));
  1151. if (targetLinkFlags) {
  1152. sharedLibFlags += targetLinkFlags;
  1153. sharedLibFlags += " ";
  1154. }
  1155. }
  1156. if (!sharedLibFlags.empty()) {
  1157. linkFlags.emplace_back(std::move(sharedLibFlags));
  1158. }
  1159. std::vector<BT<std::string>> linkOpts =
  1160. target->GetLinkOptions(config, linkLanguage);
  1161. // LINK_OPTIONS are escaped.
  1162. this->AppendCompileOptions(linkFlags, linkOpts);
  1163. if (pcli) {
  1164. this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  1165. frameworkPath, linkPath);
  1166. }
  1167. } break;
  1168. case cmStateEnums::EXECUTABLE: {
  1169. std::string exeFlags;
  1170. if (linkLanguage != "Swift") {
  1171. exeFlags = this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1172. exeFlags += " ";
  1173. if (!buildType.empty()) {
  1174. exeFlags += this->Makefile->GetSafeDefinition(
  1175. cmStrCat("CMAKE_EXE_LINKER_FLAGS_", buildType));
  1176. exeFlags += " ";
  1177. }
  1178. if (linkLanguage.empty()) {
  1179. cmSystemTools::Error(
  1180. "CMake can not determine linker language for target: " +
  1181. target->GetName());
  1182. return;
  1183. }
  1184. if (target->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  1185. exeFlags +=
  1186. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1187. exeFlags += " ";
  1188. } else {
  1189. exeFlags +=
  1190. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1191. exeFlags += " ";
  1192. }
  1193. if (target->IsExecutableWithExports()) {
  1194. exeFlags += this->Makefile->GetSafeDefinition(
  1195. cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG"));
  1196. exeFlags += " ";
  1197. }
  1198. }
  1199. this->AddLanguageFlagsForLinking(flags, target, linkLanguage, buildType);
  1200. if (pcli) {
  1201. this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  1202. frameworkPath, linkPath);
  1203. }
  1204. if (cmIsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS"))) {
  1205. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") +
  1206. linkLanguage + std::string("_FLAGS");
  1207. exeFlags += this->Makefile->GetSafeDefinition(sFlagVar);
  1208. exeFlags += " ";
  1209. }
  1210. std::string cmp0065Flags =
  1211. this->GetLinkLibsCMP0065(linkLanguage, *target);
  1212. if (!cmp0065Flags.empty()) {
  1213. exeFlags += cmp0065Flags;
  1214. exeFlags += " ";
  1215. }
  1216. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1217. if (targetLinkFlags) {
  1218. exeFlags += targetLinkFlags;
  1219. exeFlags += " ";
  1220. }
  1221. if (!buildType.empty()) {
  1222. targetLinkFlags =
  1223. target->GetProperty(cmStrCat("LINK_FLAGS_", buildType));
  1224. if (targetLinkFlags) {
  1225. exeFlags += targetLinkFlags;
  1226. exeFlags += " ";
  1227. }
  1228. }
  1229. if (!exeFlags.empty()) {
  1230. linkFlags.emplace_back(std::move(exeFlags));
  1231. }
  1232. std::vector<BT<std::string>> linkOpts =
  1233. target->GetLinkOptions(config, linkLanguage);
  1234. // LINK_OPTIONS are escaped.
  1235. this->AppendCompileOptions(linkFlags, linkOpts);
  1236. } break;
  1237. default:
  1238. break;
  1239. }
  1240. std::string extraLinkFlags;
  1241. this->AppendPositionIndependentLinkerFlags(extraLinkFlags, target, config,
  1242. linkLanguage);
  1243. this->AppendIPOLinkerFlags(extraLinkFlags, target, config, linkLanguage);
  1244. if (!extraLinkFlags.empty()) {
  1245. linkFlags.emplace_back(std::move(extraLinkFlags));
  1246. }
  1247. }
  1248. void cmLocalGenerator::GetTargetCompileFlags(cmGeneratorTarget* target,
  1249. std::string const& config,
  1250. std::string const& lang,
  1251. std::string& flags)
  1252. {
  1253. std::vector<BT<std::string>> tmpFlags =
  1254. this->GetTargetCompileFlags(target, config, lang);
  1255. this->AppendFlags(flags, tmpFlags);
  1256. }
  1257. std::vector<BT<std::string>> cmLocalGenerator::GetTargetCompileFlags(
  1258. cmGeneratorTarget* target, std::string const& config,
  1259. std::string const& lang)
  1260. {
  1261. std::vector<BT<std::string>> flags;
  1262. std::string compileFlags;
  1263. cmMakefile* mf = this->GetMakefile();
  1264. // Add language-specific flags.
  1265. this->AddLanguageFlags(compileFlags, target, lang, config);
  1266. if (target->IsIPOEnabled(lang, config)) {
  1267. this->AppendFeatureOptions(compileFlags, lang, "IPO");
  1268. }
  1269. this->AddArchitectureFlags(compileFlags, target, lang, config);
  1270. if (lang == "Fortran") {
  1271. this->AppendFlags(compileFlags,
  1272. this->GetTargetFortranFlags(target, config));
  1273. }
  1274. this->AddCMP0018Flags(compileFlags, target, lang, config);
  1275. this->AddVisibilityPresetFlags(compileFlags, target, lang);
  1276. this->AppendFlags(compileFlags, mf->GetDefineFlags());
  1277. this->AppendFlags(compileFlags,
  1278. this->GetFrameworkFlags(lang, config, target));
  1279. if (!compileFlags.empty()) {
  1280. flags.emplace_back(std::move(compileFlags));
  1281. }
  1282. this->AddCompileOptions(flags, target, lang, config);
  1283. return flags;
  1284. }
  1285. static std::string GetFrameworkFlags(const std::string& lang,
  1286. const std::string& config,
  1287. cmGeneratorTarget* target)
  1288. {
  1289. cmLocalGenerator* lg = target->GetLocalGenerator();
  1290. cmMakefile* mf = lg->GetMakefile();
  1291. if (!mf->IsOn("APPLE")) {
  1292. return std::string();
  1293. }
  1294. std::string fwSearchFlagVar = "CMAKE_" + lang + "_FRAMEWORK_SEARCH_FLAG";
  1295. const char* fwSearchFlag = mf->GetDefinition(fwSearchFlagVar);
  1296. if (!(fwSearchFlag && *fwSearchFlag)) {
  1297. return std::string();
  1298. }
  1299. std::set<std::string> emitted;
  1300. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1301. emitted.insert("/System/Library/Frameworks");
  1302. #endif
  1303. std::vector<std::string> includes;
  1304. lg->GetIncludeDirectories(includes, target, "C", config);
  1305. // check all include directories for frameworks as this
  1306. // will already have added a -F for the framework
  1307. for (std::string const& include : includes) {
  1308. if (lg->GetGlobalGenerator()->NameResolvesToFramework(include)) {
  1309. std::string frameworkDir = cmStrCat(include, "/../");
  1310. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1311. emitted.insert(frameworkDir);
  1312. }
  1313. }
  1314. std::string flags;
  1315. if (cmComputeLinkInformation* cli = target->GetLinkInformation(config)) {
  1316. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1317. for (std::string const& framework : frameworks) {
  1318. if (emitted.insert(framework).second) {
  1319. flags += fwSearchFlag;
  1320. flags +=
  1321. lg->ConvertToOutputFormat(framework, cmOutputConverter::SHELL);
  1322. flags += " ";
  1323. }
  1324. }
  1325. }
  1326. return flags;
  1327. }
  1328. std::string cmLocalGenerator::GetFrameworkFlags(std::string const& l,
  1329. std::string const& config,
  1330. cmGeneratorTarget* target)
  1331. {
  1332. return ::GetFrameworkFlags(l, config, target);
  1333. }
  1334. void cmLocalGenerator::GetTargetDefines(cmGeneratorTarget const* target,
  1335. std::string const& config,
  1336. std::string const& lang,
  1337. std::set<std::string>& defines) const
  1338. {
  1339. std::set<BT<std::string>> tmp = this->GetTargetDefines(target, config, lang);
  1340. for (BT<std::string> const& v : tmp) {
  1341. defines.emplace(v.Value);
  1342. }
  1343. }
  1344. std::set<BT<std::string>> cmLocalGenerator::GetTargetDefines(
  1345. cmGeneratorTarget const* target, std::string const& config,
  1346. std::string const& lang) const
  1347. {
  1348. std::set<BT<std::string>> defines;
  1349. // Add the export symbol definition for shared library objects.
  1350. if (const std::string* exportMacro = target->GetExportMacro()) {
  1351. this->AppendDefines(defines, *exportMacro);
  1352. }
  1353. // Add preprocessor definitions for this target and configuration.
  1354. std::vector<BT<std::string>> targetDefines =
  1355. target->GetCompileDefinitions(config, lang);
  1356. this->AppendDefines(defines, targetDefines);
  1357. return defines;
  1358. }
  1359. std::string cmLocalGenerator::GetTargetFortranFlags(
  1360. cmGeneratorTarget const* /*unused*/, std::string const& /*unused*/)
  1361. {
  1362. // Implemented by specific generators that override this.
  1363. return std::string();
  1364. }
  1365. /**
  1366. * Output the linking rules on a command line. For executables,
  1367. * targetLibrary should be a NULL pointer. For libraries, it should point
  1368. * to the name of the library. This will not link a library against itself.
  1369. */
  1370. void cmLocalGenerator::OutputLinkLibraries(
  1371. cmComputeLinkInformation* pcli, cmLinkLineComputer* linkLineComputer,
  1372. std::string& linkLibraries, std::string& frameworkPath,
  1373. std::string& linkPath)
  1374. {
  1375. cmComputeLinkInformation& cli = *pcli;
  1376. std::string linkLanguage = cli.GetLinkLanguage();
  1377. std::string libPathFlag;
  1378. if (const char* value = this->Makefile->GetDefinition(
  1379. "CMAKE_" + cli.GetLinkLanguage() + "_LIBRARY_PATH_FLAG")) {
  1380. libPathFlag = value;
  1381. } else {
  1382. libPathFlag =
  1383. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1384. }
  1385. std::string libPathTerminator;
  1386. if (const char* value = this->Makefile->GetDefinition(
  1387. "CMAKE_" + cli.GetLinkLanguage() + "_LIBRARY_PATH_TERMINATOR")) {
  1388. libPathTerminator = value;
  1389. } else {
  1390. libPathTerminator =
  1391. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1392. }
  1393. // Add standard libraries for this language.
  1394. std::string stdLibString = this->Makefile->GetSafeDefinition(
  1395. cmStrCat("CMAKE_", cli.GetLinkLanguage(), "_STANDARD_LIBRARIES"));
  1396. // Append the framework search path flags.
  1397. std::string fwSearchFlag = this->Makefile->GetSafeDefinition(
  1398. cmStrCat("CMAKE_", linkLanguage, "_FRAMEWORK_SEARCH_FLAG"));
  1399. frameworkPath = linkLineComputer->ComputeFrameworkPath(cli, fwSearchFlag);
  1400. linkPath =
  1401. linkLineComputer->ComputeLinkPath(cli, libPathFlag, libPathTerminator);
  1402. linkLibraries = linkLineComputer->ComputeLinkLibraries(cli, stdLibString);
  1403. }
  1404. std::string cmLocalGenerator::GetLinkLibsCMP0065(
  1405. std::string const& linkLanguage, cmGeneratorTarget& tgt) const
  1406. {
  1407. std::string linkFlags;
  1408. // Flags to link an executable to shared libraries.
  1409. if (tgt.GetType() == cmStateEnums::EXECUTABLE &&
  1410. this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
  1411. "TARGET_SUPPORTS_SHARED_LIBS")) {
  1412. bool add_shlib_flags = false;
  1413. switch (tgt.GetPolicyStatusCMP0065()) {
  1414. case cmPolicies::WARN:
  1415. if (!tgt.GetPropertyAsBool("ENABLE_EXPORTS") &&
  1416. this->Makefile->PolicyOptionalWarningEnabled(
  1417. "CMAKE_POLICY_WARNING_CMP0065")) {
  1418. std::ostringstream w;
  1419. /* clang-format off */
  1420. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n"
  1421. "For compatibility with older versions of CMake, "
  1422. "additional flags may be added to export symbols on all "
  1423. "executables regardless of their ENABLE_EXPORTS property.";
  1424. /* clang-format on */
  1425. this->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  1426. }
  1427. CM_FALLTHROUGH;
  1428. case cmPolicies::OLD:
  1429. // OLD behavior is to always add the flags, except on AIX where
  1430. // we compute symbol exports if ENABLE_EXPORTS is on.
  1431. add_shlib_flags =
  1432. !(tgt.Target->IsAIX() && tgt.GetPropertyAsBool("ENABLE_EXPORTS"));
  1433. break;
  1434. case cmPolicies::REQUIRED_IF_USED:
  1435. case cmPolicies::REQUIRED_ALWAYS:
  1436. this->IssueMessage(
  1437. MessageType::FATAL_ERROR,
  1438. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0065));
  1439. CM_FALLTHROUGH;
  1440. case cmPolicies::NEW:
  1441. // NEW behavior is to only add the flags if ENABLE_EXPORTS is on,
  1442. // except on AIX where we compute symbol exports.
  1443. add_shlib_flags =
  1444. !tgt.Target->IsAIX() && tgt.GetPropertyAsBool("ENABLE_EXPORTS");
  1445. break;
  1446. }
  1447. if (add_shlib_flags) {
  1448. linkFlags = this->Makefile->GetSafeDefinition(
  1449. cmStrCat("CMAKE_SHARED_LIBRARY_LINK_", linkLanguage, "_FLAGS"));
  1450. }
  1451. }
  1452. return linkFlags;
  1453. }
  1454. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1455. cmGeneratorTarget const* target,
  1456. const std::string& lang,
  1457. const std::string& config)
  1458. {
  1459. // Only add macOS specific flags on Darwin platforms (macOS and iOS):
  1460. if (this->Makefile->IsOn("APPLE") && this->EmitUniversalBinaryFlags) {
  1461. std::vector<std::string> archs;
  1462. target->GetAppleArchs(config, archs);
  1463. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1464. if (sysroot && sysroot[0] == '/' && !sysroot[1]) {
  1465. sysroot = nullptr;
  1466. }
  1467. std::string sysrootFlagVar =
  1468. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1469. const char* sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
  1470. const char* deploymentTarget =
  1471. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1472. std::string deploymentTargetFlagVar =
  1473. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1474. const char* deploymentTargetFlag =
  1475. this->Makefile->GetDefinition(deploymentTargetFlagVar);
  1476. if (!archs.empty() && !lang.empty() &&
  1477. (lang[0] == 'C' || lang[0] == 'F')) {
  1478. for (std::string const& arch : archs) {
  1479. flags += " -arch ";
  1480. flags += arch;
  1481. }
  1482. }
  1483. if (sysrootFlag && *sysrootFlag && sysroot && *sysroot) {
  1484. flags += " ";
  1485. flags += sysrootFlag;
  1486. flags += " ";
  1487. flags += this->ConvertToOutputFormat(sysroot, SHELL);
  1488. }
  1489. if (deploymentTargetFlag && *deploymentTargetFlag && deploymentTarget &&
  1490. *deploymentTarget) {
  1491. flags += " ";
  1492. flags += deploymentTargetFlag;
  1493. flags += deploymentTarget;
  1494. }
  1495. }
  1496. }
  1497. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1498. cmGeneratorTarget const* target,
  1499. const std::string& lang,
  1500. const std::string& config)
  1501. {
  1502. // Add language-specific flags.
  1503. this->AddConfigVariableFlags(flags, cmStrCat("CMAKE_", lang, "_FLAGS"),
  1504. config);
  1505. if (lang == "Swift") {
  1506. if (const char* v = target->GetProperty("Swift_LANGUAGE_VERSION")) {
  1507. if (cmSystemTools::VersionCompare(
  1508. cmSystemTools::OP_GREATER_EQUAL,
  1509. this->Makefile->GetDefinition("CMAKE_Swift_COMPILER_VERSION"),
  1510. "4.2")) {
  1511. this->AppendFlags(flags, "-swift-version " + std::string(v));
  1512. }
  1513. }
  1514. }
  1515. // Add MSVC runtime library flags. This is activated by the presence
  1516. // of a default selection whether or not it is overridden by a property.
  1517. const char* msvcRuntimeLibraryDefault =
  1518. this->Makefile->GetDefinition("CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT");
  1519. if (msvcRuntimeLibraryDefault && *msvcRuntimeLibraryDefault) {
  1520. const char* msvcRuntimeLibraryValue =
  1521. target->GetProperty("MSVC_RUNTIME_LIBRARY");
  1522. if (!msvcRuntimeLibraryValue) {
  1523. msvcRuntimeLibraryValue = msvcRuntimeLibraryDefault;
  1524. }
  1525. cmGeneratorExpression ge;
  1526. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  1527. ge.Parse(msvcRuntimeLibraryValue);
  1528. std::string const msvcRuntimeLibrary =
  1529. cge->Evaluate(this, config, false, target);
  1530. if (!msvcRuntimeLibrary.empty()) {
  1531. if (const char* msvcRuntimeLibraryOptions =
  1532. this->Makefile->GetDefinition(
  1533. "CMAKE_" + lang + "_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_" +
  1534. msvcRuntimeLibrary)) {
  1535. this->AppendCompileOptions(flags, msvcRuntimeLibraryOptions);
  1536. } else if ((this->Makefile->GetSafeDefinition(
  1537. "CMAKE_" + lang + "_COMPILER_ID") == "MSVC" ||
  1538. this->Makefile->GetSafeDefinition(
  1539. "CMAKE_" + lang + "_SIMULATE_ID") == "MSVC") &&
  1540. !cmSystemTools::GetErrorOccuredFlag()) {
  1541. // The compiler uses the MSVC ABI so it needs a known runtime library.
  1542. this->IssueMessage(MessageType::FATAL_ERROR,
  1543. "MSVC_RUNTIME_LIBRARY value '" +
  1544. msvcRuntimeLibrary + "' not known for this " +
  1545. lang + " compiler.");
  1546. }
  1547. }
  1548. }
  1549. }
  1550. void cmLocalGenerator::AddLanguageFlagsForLinking(
  1551. std::string& flags, cmGeneratorTarget const* target, const std::string& lang,
  1552. const std::string& config)
  1553. {
  1554. if (this->Makefile->IsOn("CMAKE_" + lang +
  1555. "_LINK_WITH_STANDARD_COMPILE_OPTION")) {
  1556. // This toolchain requires use of the language standard flag
  1557. // when linking in order to use the matching standard library.
  1558. // FIXME: If CMake gains an abstraction for standard library
  1559. // selection, this will have to be reconciled with it.
  1560. this->AddCompilerRequirementFlag(flags, target, lang);
  1561. }
  1562. this->AddLanguageFlags(flags, target, lang, config);
  1563. if (target->IsIPOEnabled(lang, config)) {
  1564. this->AppendFeatureOptions(flags, lang, "IPO");
  1565. }
  1566. }
  1567. cmGeneratorTarget* cmLocalGenerator::FindGeneratorTargetToUse(
  1568. const std::string& name) const
  1569. {
  1570. auto imported = this->ImportedGeneratorTargets.find(name);
  1571. if (imported != this->ImportedGeneratorTargets.end()) {
  1572. return imported->second;
  1573. }
  1574. if (cmGeneratorTarget* t = this->FindLocalNonAliasGeneratorTarget(name)) {
  1575. return t;
  1576. }
  1577. return this->GetGlobalGenerator()->FindGeneratorTarget(name);
  1578. }
  1579. bool cmLocalGenerator::GetRealDependency(const std::string& inName,
  1580. const std::string& config,
  1581. std::string& dep)
  1582. {
  1583. // Older CMake code may specify the dependency using the target
  1584. // output file rather than the target name. Such code would have
  1585. // been written before there was support for target properties that
  1586. // modify the name so stripping down to just the file name should
  1587. // produce the target name in this case.
  1588. std::string name = cmSystemTools::GetFilenameName(inName);
  1589. // If the input name is the empty string, there is no real
  1590. // dependency. Short-circuit the other checks:
  1591. if (name.empty()) {
  1592. return false;
  1593. }
  1594. if (cmSystemTools::GetFilenameLastExtension(name) == ".exe") {
  1595. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1596. }
  1597. // Look for a CMake target with the given name.
  1598. if (cmGeneratorTarget* target = this->FindGeneratorTargetToUse(name)) {
  1599. // make sure it is not just a coincidence that the target name
  1600. // found is part of the inName
  1601. if (cmSystemTools::FileIsFullPath(inName)) {
  1602. std::string tLocation;
  1603. if (target->GetType() >= cmStateEnums::EXECUTABLE &&
  1604. target->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  1605. tLocation = target->GetLocation(config);
  1606. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1607. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  1608. }
  1609. std::string depLocation =
  1610. cmSystemTools::GetFilenamePath(std::string(inName));
  1611. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  1612. if (depLocation != tLocation) {
  1613. // it is a full path to a depend that has the same name
  1614. // as a target but is in a different location so do not use
  1615. // the target as the depend
  1616. dep = inName;
  1617. return true;
  1618. }
  1619. }
  1620. switch (target->GetType()) {
  1621. case cmStateEnums::EXECUTABLE:
  1622. case cmStateEnums::STATIC_LIBRARY:
  1623. case cmStateEnums::SHARED_LIBRARY:
  1624. case cmStateEnums::MODULE_LIBRARY:
  1625. case cmStateEnums::UNKNOWN_LIBRARY:
  1626. dep = target->GetLocation(config);
  1627. return true;
  1628. case cmStateEnums::OBJECT_LIBRARY:
  1629. // An object library has no single file on which to depend.
  1630. // This was listed to get the target-level dependency.
  1631. return false;
  1632. case cmStateEnums::INTERFACE_LIBRARY:
  1633. // An interface library has no file on which to depend.
  1634. // This was listed to get the target-level dependency.
  1635. return false;
  1636. case cmStateEnums::UTILITY:
  1637. case cmStateEnums::GLOBAL_TARGET:
  1638. // A utility target has no file on which to depend. This was listed
  1639. // only to get the target-level dependency.
  1640. return false;
  1641. }
  1642. }
  1643. // The name was not that of a CMake target. It must name a file.
  1644. if (cmSystemTools::FileIsFullPath(inName)) {
  1645. // This is a full path. Return it as given.
  1646. dep = inName;
  1647. return true;
  1648. }
  1649. // Check for a source file in this directory that matches the
  1650. // dependency.
  1651. if (cmSourceFile* sf = this->Makefile->GetSource(inName)) {
  1652. dep = sf->ResolveFullPath();
  1653. return true;
  1654. }
  1655. // Treat the name as relative to the source directory in which it
  1656. // was given.
  1657. dep = cmStrCat(this->StateSnapshot.GetDirectory().GetCurrentSource(), '/',
  1658. inName);
  1659. return true;
  1660. }
  1661. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1662. const std::string& lang, bool shared)
  1663. {
  1664. std::string flagsVar;
  1665. // Add flags for dealing with shared libraries for this language.
  1666. if (shared) {
  1667. this->AppendFlags(flags,
  1668. this->Makefile->GetSafeDefinition(
  1669. cmStrCat("CMAKE_SHARED_LIBRARY_", lang, "_FLAGS")));
  1670. }
  1671. }
  1672. void cmLocalGenerator::AddCompilerRequirementFlag(
  1673. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1674. {
  1675. if (lang.empty()) {
  1676. return;
  1677. }
  1678. const char* defaultStd =
  1679. this->Makefile->GetDefinition("CMAKE_" + lang + "_STANDARD_DEFAULT");
  1680. if (!defaultStd || !*defaultStd) {
  1681. // This compiler has no notion of language standard levels.
  1682. return;
  1683. }
  1684. std::string extProp = lang + "_EXTENSIONS";
  1685. bool ext = true;
  1686. if (const char* extPropValue = target->GetProperty(extProp)) {
  1687. if (cmIsOff(extPropValue)) {
  1688. ext = false;
  1689. }
  1690. }
  1691. std::string stdProp = lang + "_STANDARD";
  1692. const char* standardProp = target->GetProperty(stdProp);
  1693. if (!standardProp) {
  1694. if (ext) {
  1695. // No language standard is specified and extensions are not disabled.
  1696. // Check if this compiler needs a flag to enable extensions.
  1697. std::string const option_flag =
  1698. "CMAKE_" + lang + "_EXTENSION_COMPILE_OPTION";
  1699. if (const char* opt =
  1700. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1701. std::vector<std::string> optVec = cmExpandedList(opt);
  1702. for (std::string const& i : optVec) {
  1703. this->AppendFlagEscape(flags, i);
  1704. }
  1705. }
  1706. }
  1707. return;
  1708. }
  1709. std::string const type = ext ? "EXTENSION" : "STANDARD";
  1710. if (target->GetPropertyAsBool(lang + "_STANDARD_REQUIRED")) {
  1711. std::string option_flag =
  1712. "CMAKE_" + lang + standardProp + "_" + type + "_COMPILE_OPTION";
  1713. const char* opt =
  1714. target->Target->GetMakefile()->GetDefinition(option_flag);
  1715. if (!opt) {
  1716. std::ostringstream e;
  1717. e << "Target \"" << target->GetName()
  1718. << "\" requires the language "
  1719. "dialect \""
  1720. << lang << standardProp << "\" "
  1721. << (ext ? "(with compiler extensions)" : "")
  1722. << ", but CMake "
  1723. "does not know the compile flags to use to enable it.";
  1724. this->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1725. } else {
  1726. std::vector<std::string> optVec = cmExpandedList(opt);
  1727. for (std::string const& i : optVec) {
  1728. this->AppendFlagEscape(flags, i);
  1729. }
  1730. }
  1731. return;
  1732. }
  1733. static std::map<std::string, std::vector<std::string>> langStdMap;
  1734. if (langStdMap.empty()) {
  1735. // Maintain sorted order, most recent first.
  1736. langStdMap["CXX"].emplace_back("20");
  1737. langStdMap["CXX"].emplace_back("17");
  1738. langStdMap["CXX"].emplace_back("14");
  1739. langStdMap["CXX"].emplace_back("11");
  1740. langStdMap["CXX"].emplace_back("98");
  1741. langStdMap["C"].emplace_back("11");
  1742. langStdMap["C"].emplace_back("99");
  1743. langStdMap["C"].emplace_back("90");
  1744. langStdMap["CUDA"].emplace_back("14");
  1745. langStdMap["CUDA"].emplace_back("11");
  1746. langStdMap["CUDA"].emplace_back("98");
  1747. }
  1748. std::string standard(standardProp);
  1749. std::vector<std::string>& stds = langStdMap[lang];
  1750. auto stdIt = std::find(stds.begin(), stds.end(), standard);
  1751. if (stdIt == stds.end()) {
  1752. std::string e =
  1753. lang + "_STANDARD is set to invalid value '" + standard + "'";
  1754. this->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  1755. MessageType::FATAL_ERROR, e, target->GetBacktrace());
  1756. return;
  1757. }
  1758. auto defaultStdIt = std::find(stds.begin(), stds.end(), defaultStd);
  1759. if (defaultStdIt == stds.end()) {
  1760. std::string e = "CMAKE_" + lang +
  1761. "_STANDARD_DEFAULT is set to invalid value '" + std::string(defaultStd) +
  1762. "'";
  1763. this->IssueMessage(MessageType::INTERNAL_ERROR, e);
  1764. return;
  1765. }
  1766. // If the standard requested is older than the compiler's default
  1767. // then we need to use a flag to change it. The comparison is
  1768. // greater-or-equal because the standards are stored in backward
  1769. // chronological order.
  1770. if (stdIt >= defaultStdIt) {
  1771. std::string option_flag =
  1772. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1773. std::string const& opt =
  1774. target->Target->GetMakefile()->GetRequiredDefinition(option_flag);
  1775. std::vector<std::string> optVec = cmExpandedList(opt);
  1776. for (std::string const& i : optVec) {
  1777. this->AppendFlagEscape(flags, i);
  1778. }
  1779. return;
  1780. }
  1781. // The standard requested is at least as new as the compiler's default,
  1782. // and the standard request is not required. Decay to the newest standard
  1783. // for which a flag is defined.
  1784. for (; stdIt < defaultStdIt; ++stdIt) {
  1785. std::string option_flag =
  1786. cmStrCat("CMAKE_", lang, *stdIt, "_", type, "_COMPILE_OPTION");
  1787. if (const char* opt =
  1788. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1789. std::vector<std::string> optVec = cmExpandedList(opt);
  1790. for (std::string const& i : optVec) {
  1791. this->AppendFlagEscape(flags, i);
  1792. }
  1793. return;
  1794. }
  1795. }
  1796. }
  1797. static void AddVisibilityCompileOption(std::string& flags,
  1798. cmGeneratorTarget const* target,
  1799. cmLocalGenerator* lg,
  1800. const std::string& lang,
  1801. std::string* warnCMP0063)
  1802. {
  1803. std::string compileOption = "CMAKE_" + lang + "_COMPILE_OPTIONS_VISIBILITY";
  1804. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1805. if (!opt) {
  1806. return;
  1807. }
  1808. std::string flagDefine = lang + "_VISIBILITY_PRESET";
  1809. const char* prop = target->GetProperty(flagDefine);
  1810. if (!prop) {
  1811. return;
  1812. }
  1813. if (warnCMP0063) {
  1814. *warnCMP0063 += " " + flagDefine + "\n";
  1815. return;
  1816. }
  1817. if (strcmp(prop, "hidden") != 0 && strcmp(prop, "default") != 0 &&
  1818. strcmp(prop, "protected") != 0 && strcmp(prop, "internal") != 0) {
  1819. std::ostringstream e;
  1820. e << "Target " << target->GetName() << " uses unsupported value \"" << prop
  1821. << "\" for " << flagDefine << "."
  1822. << " The supported values are: default, hidden, protected, and "
  1823. "internal.";
  1824. cmSystemTools::Error(e.str());
  1825. return;
  1826. }
  1827. std::string option = std::string(opt) + prop;
  1828. lg->AppendFlags(flags, option);
  1829. }
  1830. static void AddInlineVisibilityCompileOption(std::string& flags,
  1831. cmGeneratorTarget const* target,
  1832. cmLocalGenerator* lg,
  1833. std::string* warnCMP0063)
  1834. {
  1835. std::string compileOption =
  1836. "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1837. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1838. if (!opt) {
  1839. return;
  1840. }
  1841. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1842. if (!prop) {
  1843. return;
  1844. }
  1845. if (warnCMP0063) {
  1846. *warnCMP0063 += " VISIBILITY_INLINES_HIDDEN\n";
  1847. return;
  1848. }
  1849. lg->AppendFlags(flags, opt);
  1850. }
  1851. void cmLocalGenerator::AddVisibilityPresetFlags(
  1852. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1853. {
  1854. if (lang.empty()) {
  1855. return;
  1856. }
  1857. std::string warnCMP0063;
  1858. std::string* pWarnCMP0063 = nullptr;
  1859. if (target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1860. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1861. !target->IsExecutableWithExports()) {
  1862. switch (target->GetPolicyStatusCMP0063()) {
  1863. case cmPolicies::OLD:
  1864. return;
  1865. case cmPolicies::WARN:
  1866. pWarnCMP0063 = &warnCMP0063;
  1867. break;
  1868. default:
  1869. break;
  1870. }
  1871. }
  1872. AddVisibilityCompileOption(flags, target, this, lang, pWarnCMP0063);
  1873. if (lang == "CXX") {
  1874. AddInlineVisibilityCompileOption(flags, target, this, pWarnCMP0063);
  1875. }
  1876. if (!warnCMP0063.empty() && this->WarnCMP0063.insert(target).second) {
  1877. std::ostringstream w;
  1878. /* clang-format off */
  1879. w <<
  1880. cmPolicies::GetPolicyWarning(cmPolicies::CMP0063) << "\n"
  1881. "Target \"" << target->GetName() << "\" of "
  1882. "type \"" << cmState::GetTargetTypeName(target->GetType()) << "\" "
  1883. "has the following visibility properties set for " << lang << ":\n" <<
  1884. warnCMP0063 <<
  1885. "For compatibility CMake is not honoring them for this target.";
  1886. /* clang-format on */
  1887. target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1888. MessageType::AUTHOR_WARNING, w.str(), target->GetBacktrace());
  1889. }
  1890. }
  1891. void cmLocalGenerator::AddCMP0018Flags(std::string& flags,
  1892. cmGeneratorTarget const* target,
  1893. std::string const& lang,
  1894. const std::string& config)
  1895. {
  1896. int targetType = target->GetType();
  1897. bool shared = ((targetType == cmStateEnums::SHARED_LIBRARY) ||
  1898. (targetType == cmStateEnums::MODULE_LIBRARY));
  1899. if (this->GetShouldUseOldFlags(shared, lang)) {
  1900. this->AddSharedFlags(flags, lang, shared);
  1901. } else {
  1902. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1903. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE")) {
  1904. this->AddPositionIndependentFlags(flags, lang, targetType);
  1905. }
  1906. return;
  1907. }
  1908. if (target->GetLinkInterfaceDependentBoolProperty(
  1909. "POSITION_INDEPENDENT_CODE", config)) {
  1910. this->AddPositionIndependentFlags(flags, lang, targetType);
  1911. }
  1912. if (shared) {
  1913. this->AppendFeatureOptions(flags, lang, "DLL");
  1914. }
  1915. }
  1916. }
  1917. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  1918. const std::string& lang) const
  1919. {
  1920. std::string originalFlags =
  1921. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  1922. if (shared) {
  1923. std::string flagsVar = cmStrCat("CMAKE_SHARED_LIBRARY_", lang, "_FLAGS");
  1924. std::string const& flags = this->Makefile->GetSafeDefinition(flagsVar);
  1925. if (flags != originalFlags) {
  1926. switch (this->GetPolicyStatus(cmPolicies::CMP0018)) {
  1927. case cmPolicies::WARN: {
  1928. std::ostringstream e;
  1929. e << "Variable " << flagsVar
  1930. << " has been modified. CMake "
  1931. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  1932. "shared libraries and will use the "
  1933. << flagsVar
  1934. << " variable "
  1935. "instead. This may cause errors if the original content of "
  1936. << flagsVar << " was removed.\n"
  1937. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0018);
  1938. this->IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  1939. CM_FALLTHROUGH;
  1940. }
  1941. case cmPolicies::OLD:
  1942. return true;
  1943. case cmPolicies::REQUIRED_IF_USED:
  1944. case cmPolicies::REQUIRED_ALWAYS:
  1945. case cmPolicies::NEW:
  1946. return false;
  1947. }
  1948. }
  1949. }
  1950. return false;
  1951. }
  1952. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  1953. std::string const& lang,
  1954. int targetType)
  1955. {
  1956. std::string picFlags;
  1957. if (targetType == cmStateEnums::EXECUTABLE) {
  1958. picFlags = this->Makefile->GetSafeDefinition(
  1959. cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_PIE"));
  1960. }
  1961. if (picFlags.empty()) {
  1962. picFlags = this->Makefile->GetSafeDefinition(
  1963. cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_PIC"));
  1964. }
  1965. if (!picFlags.empty()) {
  1966. std::vector<std::string> options = cmExpandedList(picFlags);
  1967. for (std::string const& o : options) {
  1968. this->AppendFlagEscape(flags, o);
  1969. }
  1970. }
  1971. }
  1972. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1973. const std::string& var,
  1974. const std::string& config)
  1975. {
  1976. // Add the flags from the variable itself.
  1977. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(var));
  1978. // Add the flags from the build-type specific variable.
  1979. if (!config.empty()) {
  1980. const std::string flagsVar =
  1981. cmStrCat(var, '_', cmSystemTools::UpperCase(config));
  1982. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(flagsVar));
  1983. }
  1984. }
  1985. void cmLocalGenerator::AppendFlags(std::string& flags,
  1986. const std::string& newFlags) const
  1987. {
  1988. if (!newFlags.empty()) {
  1989. if (!flags.empty()) {
  1990. flags += " ";
  1991. }
  1992. flags += newFlags;
  1993. }
  1994. }
  1995. void cmLocalGenerator::AppendFlags(
  1996. std::string& flags, const std::vector<BT<std::string>>& newFlags) const
  1997. {
  1998. for (BT<std::string> const& flag : newFlags) {
  1999. this->AppendFlags(flags, flag.Value);
  2000. }
  2001. }
  2002. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  2003. const std::string& rawFlag) const
  2004. {
  2005. this->AppendFlags(flags, this->EscapeForShell(rawFlag));
  2006. }
  2007. void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target,
  2008. const std::string& config)
  2009. {
  2010. const std::string lang = target->GetLinkerLanguage(config);
  2011. const std::string buildType = cmSystemTools::UpperCase(config);
  2012. const std::string pchSource = target->GetPchSource(config, lang);
  2013. const std::string pchHeader = target->GetPchHeader(config, lang);
  2014. if (pchSource.empty() || pchHeader.empty()) {
  2015. return;
  2016. }
  2017. const std::string createOptVar =
  2018. cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_CREATE_PCH");
  2019. std::string createOptionList =
  2020. this->Makefile->GetSafeDefinition(createOptVar);
  2021. const std::string useOptVar =
  2022. cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_USE_PCH");
  2023. std::string useOptionList = this->Makefile->GetSafeDefinition(useOptVar);
  2024. const std::string pchExtension =
  2025. this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
  2026. if (createOptionList.empty() || useOptionList.empty() ||
  2027. pchExtension.empty()) {
  2028. return;
  2029. }
  2030. const char* pchReuseFrom =
  2031. target->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  2032. auto pch_sf = this->Makefile->GetOrCreateSource(
  2033. pchSource, false, cmSourceFileLocationKind::Known);
  2034. std::string pchFile = pchHeader;
  2035. if (!this->GetGlobalGenerator()->IsXcode()) {
  2036. if (!pchReuseFrom) {
  2037. target->AddSource(pchSource, true);
  2038. }
  2039. // Exclude the pch files from linking
  2040. if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  2041. auto replaceExtension = [](const std::string& str,
  2042. const std::string& ext) -> std::string {
  2043. auto dot_pos = str.rfind('.');
  2044. std::string result;
  2045. if (dot_pos != std::string::npos) {
  2046. result = str.substr(0, dot_pos);
  2047. }
  2048. result += ext;
  2049. return result;
  2050. };
  2051. if (!pchReuseFrom) {
  2052. std::string pchSourceObj = target->GetPchFileObject(config, lang);
  2053. pchFile = replaceExtension(pchSourceObj, pchExtension);
  2054. pch_sf->SetProperty("OBJECT_OUTPUTS", pchFile.c_str());
  2055. } else {
  2056. auto reuseTarget =
  2057. this->GlobalGenerator->FindGeneratorTarget(pchReuseFrom);
  2058. if (this->Makefile->IsOn("CMAKE_PCH_COPY_COMPILE_PDB")) {
  2059. const std::string pdb_prefix =
  2060. this->GetGlobalGenerator()->IsMultiConfig()
  2061. ? cmStrCat(this->GlobalGenerator->GetCMakeCFGIntDir(), "/")
  2062. : "";
  2063. const std::string target_compile_pdb_dir =
  2064. cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  2065. "/", target->GetName(), ".dir/");
  2066. const std::string copy_script =
  2067. cmStrCat(target_compile_pdb_dir, "copy_idb_pdb.cmake");
  2068. cmGeneratedFileStream file(copy_script);
  2069. file << "# CMake generated file\n";
  2070. for (auto extension : { ".pdb", ".idb" }) {
  2071. const std::string from_file = cmStrCat(
  2072. reuseTarget->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  2073. "/", pchReuseFrom, ".dir/${PDB_PREFIX}", pchReuseFrom,
  2074. extension);
  2075. const std::string to_dir = cmStrCat(
  2076. target->GetLocalGenerator()->GetCurrentBinaryDirectory(), "/",
  2077. target->GetName(), ".dir/${PDB_PREFIX}");
  2078. file << "if (EXISTS \"" << from_file << "\")\n";
  2079. file << " file(COPY \"" << from_file << "\""
  2080. << " DESTINATION \"" << to_dir << "\")\n";
  2081. file << "endif()\n";
  2082. }
  2083. cmCustomCommandLines commandLines;
  2084. cmCustomCommandLine currentLine;
  2085. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  2086. currentLine.push_back(cmStrCat("-DPDB_PREFIX=", pdb_prefix));
  2087. currentLine.push_back("-P");
  2088. currentLine.push_back(copy_script);
  2089. commandLines.push_back(std::move(currentLine));
  2090. const std::string no_main_dependency;
  2091. const std::vector<std::string> no_deps;
  2092. const char* no_message = "";
  2093. const char* no_current_dir = nullptr;
  2094. std::vector<std::string> no_byproducts;
  2095. std::vector<std::string> outputs;
  2096. outputs.push_back(cmStrCat(target_compile_pdb_dir, pdb_prefix,
  2097. pchReuseFrom, ".pdb"));
  2098. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  2099. this->Makefile->AddCustomCommandToTarget(
  2100. target->GetName(), outputs, no_deps, commandLines,
  2101. cmTarget::PRE_BUILD, no_message, no_current_dir);
  2102. } else {
  2103. cmImplicitDependsList no_implicit_depends;
  2104. cmSourceFile* copy_rule = this->Makefile->AddCustomCommandToOutput(
  2105. outputs, no_byproducts, no_deps, no_main_dependency,
  2106. no_implicit_depends, commandLines, no_message, no_current_dir);
  2107. if (copy_rule) {
  2108. target->AddSource(copy_rule->ResolveFullPath());
  2109. }
  2110. }
  2111. target->Target->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY",
  2112. target_compile_pdb_dir.c_str());
  2113. }
  2114. std::string pchSourceObj = reuseTarget->GetPchFileObject(config, lang);
  2115. // Link to the pch object file
  2116. target->Target->SetProperty(
  2117. "LINK_FLAGS",
  2118. this->ConvertToOutputFormat(pchSourceObj, SHELL).c_str());
  2119. pchFile = replaceExtension(pchSourceObj, pchExtension);
  2120. }
  2121. } else {
  2122. pchFile += pchExtension;
  2123. pch_sf->SetProperty("PCH_EXTENSION", pchExtension.c_str());
  2124. }
  2125. // Add pchHeader to source files, which will
  2126. // be grouped as "Precompile Header File"
  2127. auto pchHeader_sf = this->Makefile->GetOrCreateSource(
  2128. pchHeader, false, cmSourceFileLocationKind::Known);
  2129. std::string err;
  2130. pchHeader_sf->ResolveFullPath(&err);
  2131. target->AddSource(pchHeader);
  2132. for (auto& str : { std::ref(useOptionList), std::ref(createOptionList) }) {
  2133. cmSystemTools::ReplaceString(str, "<PCH_HEADER>", pchHeader);
  2134. cmSystemTools::ReplaceString(str, "<PCH_FILE>", pchFile);
  2135. }
  2136. }
  2137. pch_sf->SetProperty("COMPILE_OPTIONS", createOptionList.c_str());
  2138. std::vector<cmSourceFile*> sources;
  2139. target->GetSourceFiles(sources, buildType);
  2140. for (cmSourceFile* sf : sources) {
  2141. if (pch_sf == sf || sf->GetLanguage() != lang) {
  2142. continue;
  2143. }
  2144. if (sf->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
  2145. if (this->GetGlobalGenerator()->IsXcode()) {
  2146. sf->SetProperty("COMPILE_DEFINITIONS",
  2147. "CMAKE_SKIP_PRECOMPILE_HEADERS");
  2148. }
  2149. continue;
  2150. }
  2151. if (!this->GetGlobalGenerator()->IsXcode()) {
  2152. sf->AppendProperty("OBJECT_DEPENDS", pchFile.c_str());
  2153. sf->AppendProperty("OBJECT_DEPENDS", pchHeader.c_str());
  2154. sf->SetProperty("COMPILE_OPTIONS", useOptionList.c_str());
  2155. }
  2156. }
  2157. }
  2158. void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target,
  2159. const std::string& config)
  2160. {
  2161. if (!target->GetPropertyAsBool("UNITY_BUILD")) {
  2162. return;
  2163. }
  2164. const std::string buildType = cmSystemTools::UpperCase(config);
  2165. std::string filename_base =
  2166. cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles/",
  2167. target->GetName(), ".dir/Unity/");
  2168. std::vector<cmSourceFile*> sources;
  2169. target->GetSourceFiles(sources, buildType);
  2170. auto batchSizeString = target->GetProperty("UNITY_BUILD_BATCH_SIZE");
  2171. const size_t unityBatchSize =
  2172. static_cast<size_t>(std::atoi(batchSizeString));
  2173. auto beforeInclude = target->GetProperty("UNITY_BUILD_CODE_BEFORE_INCLUDE");
  2174. auto afterInclude = target->GetProperty("UNITY_BUILD_CODE_AFTER_INCLUDE");
  2175. for (std::string lang : { "C", "CXX" }) {
  2176. std::vector<cmSourceFile*> filtered_sources;
  2177. std::copy_if(sources.begin(), sources.end(),
  2178. std::back_inserter(filtered_sources), [&](cmSourceFile* sf) {
  2179. return sf->GetLanguage() == lang &&
  2180. !sf->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION") &&
  2181. !sf->GetPropertyAsBool("GENERATED") &&
  2182. !sf->GetProperty("COMPILE_OPTIONS") &&
  2183. !sf->GetProperty("COMPILE_DEFINITIONS") &&
  2184. !sf->GetProperty("COMPILE_FLAGS") &&
  2185. !sf->GetProperty("INCLUDE_DIRECTORIES");
  2186. });
  2187. size_t batchSize = unityBatchSize;
  2188. if (unityBatchSize == 0) {
  2189. batchSize = filtered_sources.size();
  2190. }
  2191. for (size_t itemsLeft = filtered_sources.size(), chunk = batchSize,
  2192. batch = 0;
  2193. itemsLeft > 0; itemsLeft -= chunk, ++batch) {
  2194. chunk = std::min(itemsLeft, batchSize);
  2195. std::string filename = cmStrCat(filename_base, "unity_", batch,
  2196. (lang == "C") ? ".c" : ".cxx");
  2197. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  2198. {
  2199. size_t begin = batch * batchSize;
  2200. size_t end = begin + chunk;
  2201. cmGeneratedFileStream file(
  2202. filename_tmp, false,
  2203. this->GetGlobalGenerator()->GetMakefileEncoding());
  2204. file << "/* generated by CMake */\n\n";
  2205. for (; begin != end; ++begin) {
  2206. cmSourceFile* sf = filtered_sources[begin];
  2207. // Only in Visual Studio generator we keep the source files
  2208. // for explicit processing. For the rest the source files will
  2209. // not be included in the project.
  2210. if (!this->GetGlobalGenerator()->IsMultiConfig() ||
  2211. this->GetGlobalGenerator()->IsXcode()) {
  2212. sf->SetProperty("HEADER_FILE_ONLY", "ON");
  2213. }
  2214. sf->SetProperty("UNITY_SOURCE_FILE", filename.c_str());
  2215. if (beforeInclude) {
  2216. file << beforeInclude << "\n";
  2217. }
  2218. file << "#include \"" << sf->GetFullPath() << "\"\n";
  2219. if (afterInclude) {
  2220. file << afterInclude << "\n";
  2221. }
  2222. }
  2223. }
  2224. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  2225. target->AddSource(filename, true);
  2226. auto unity = this->Makefile->GetOrCreateSource(filename);
  2227. unity->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "ON");
  2228. unity->SetProperty("UNITY_SOURCE_FILE", filename.c_str());
  2229. }
  2230. }
  2231. }
  2232. void cmLocalGenerator::AppendIPOLinkerFlags(std::string& flags,
  2233. cmGeneratorTarget* target,
  2234. const std::string& config,
  2235. const std::string& lang)
  2236. {
  2237. if (!target->IsIPOEnabled(lang, config)) {
  2238. return;
  2239. }
  2240. switch (target->GetType()) {
  2241. case cmStateEnums::EXECUTABLE:
  2242. case cmStateEnums::SHARED_LIBRARY:
  2243. case cmStateEnums::MODULE_LIBRARY:
  2244. break;
  2245. default:
  2246. return;
  2247. }
  2248. const std::string name = "CMAKE_" + lang + "_LINK_OPTIONS_IPO";
  2249. const char* rawFlagsList = this->Makefile->GetDefinition(name);
  2250. if (rawFlagsList == nullptr) {
  2251. return;
  2252. }
  2253. std::vector<std::string> flagsList = cmExpandedList(rawFlagsList);
  2254. for (std::string const& o : flagsList) {
  2255. this->AppendFlagEscape(flags, o);
  2256. }
  2257. }
  2258. void cmLocalGenerator::AppendPositionIndependentLinkerFlags(
  2259. std::string& flags, cmGeneratorTarget* target, const std::string& config,
  2260. const std::string& lang)
  2261. {
  2262. // For now, only EXECUTABLE is concerned
  2263. if (target->GetType() != cmStateEnums::EXECUTABLE) {
  2264. return;
  2265. }
  2266. const char* PICValue = target->GetLinkPIEProperty(config);
  2267. if (PICValue == nullptr) {
  2268. // POSITION_INDEPENDENT_CODE is not set
  2269. return;
  2270. }
  2271. const std::string mode = cmIsOn(PICValue) ? "PIE" : "NO_PIE";
  2272. std::string supported = "CMAKE_" + lang + "_LINK_" + mode + "_SUPPORTED";
  2273. if (cmIsOff(this->Makefile->GetDefinition(supported))) {
  2274. return;
  2275. }
  2276. std::string name = "CMAKE_" + lang + "_LINK_OPTIONS_" + mode;
  2277. auto pieFlags = this->Makefile->GetSafeDefinition(name);
  2278. if (pieFlags.empty()) {
  2279. return;
  2280. }
  2281. std::vector<std::string> flagsList = cmExpandedList(pieFlags);
  2282. for (const auto& flag : flagsList) {
  2283. this->AppendFlagEscape(flags, flag);
  2284. }
  2285. }
  2286. void cmLocalGenerator::AppendCompileOptions(std::string& options,
  2287. const char* options_list,
  2288. const char* regex) const
  2289. {
  2290. // Short-circuit if there are no options.
  2291. if (!options_list) {
  2292. return;
  2293. }
  2294. // Expand the list of options.
  2295. std::vector<std::string> options_vec = cmExpandedList(options_list);
  2296. this->AppendCompileOptions(options, options_vec, regex);
  2297. }
  2298. void cmLocalGenerator::AppendCompileOptions(
  2299. std::string& options, const std::vector<std::string>& options_vec,
  2300. const char* regex) const
  2301. {
  2302. if (regex != nullptr) {
  2303. // Filter flags upon specified reges.
  2304. cmsys::RegularExpression r(regex);
  2305. for (std::string const& opt : options_vec) {
  2306. if (r.find(opt)) {
  2307. this->AppendFlagEscape(options, opt);
  2308. }
  2309. }
  2310. } else {
  2311. for (std::string const& opt : options_vec) {
  2312. this->AppendFlagEscape(options, opt);
  2313. }
  2314. }
  2315. }
  2316. void cmLocalGenerator::AppendCompileOptions(
  2317. std::vector<BT<std::string>>& options,
  2318. const std::vector<BT<std::string>>& options_vec, const char* regex) const
  2319. {
  2320. if (regex != nullptr) {
  2321. // Filter flags upon specified regular expressions.
  2322. cmsys::RegularExpression r(regex);
  2323. for (BT<std::string> const& opt : options_vec) {
  2324. if (r.find(opt.Value)) {
  2325. std::string flag;
  2326. this->AppendFlagEscape(flag, opt.Value);
  2327. options.emplace_back(std::move(flag), opt.Backtrace);
  2328. }
  2329. }
  2330. } else {
  2331. for (BT<std::string> const& opt : options_vec) {
  2332. std::string flag;
  2333. this->AppendFlagEscape(flag, opt.Value);
  2334. options.emplace_back(std::move(flag), opt.Backtrace);
  2335. }
  2336. }
  2337. }
  2338. void cmLocalGenerator::AppendIncludeDirectories(
  2339. std::vector<std::string>& includes, const char* includes_list,
  2340. const cmSourceFile& sourceFile) const
  2341. {
  2342. // Short-circuit if there are no includes.
  2343. if (!includes_list) {
  2344. return;
  2345. }
  2346. // Expand the list of includes.
  2347. std::vector<std::string> includes_vec = cmExpandedList(includes_list);
  2348. this->AppendIncludeDirectories(includes, includes_vec, sourceFile);
  2349. }
  2350. void cmLocalGenerator::AppendIncludeDirectories(
  2351. std::vector<std::string>& includes,
  2352. const std::vector<std::string>& includes_vec,
  2353. const cmSourceFile& sourceFile) const
  2354. {
  2355. std::unordered_set<std::string> uniqueIncludes;
  2356. for (const std::string& include : includes_vec) {
  2357. if (!cmSystemTools::FileIsFullPath(include)) {
  2358. std::ostringstream e;
  2359. e << "Found relative path while evaluating include directories of "
  2360. "\""
  2361. << sourceFile.GetLocation().GetName() << "\":\n \"" << include
  2362. << "\"\n";
  2363. this->IssueMessage(MessageType::FATAL_ERROR, e.str());
  2364. return;
  2365. }
  2366. std::string inc = include;
  2367. if (!cmIsOff(inc)) {
  2368. cmSystemTools::ConvertToUnixSlashes(inc);
  2369. }
  2370. if (uniqueIncludes.insert(inc).second) {
  2371. includes.push_back(std::move(inc));
  2372. }
  2373. }
  2374. }
  2375. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2376. std::string const& defines_list) const
  2377. {
  2378. std::set<BT<std::string>> tmp;
  2379. this->AppendDefines(tmp, ExpandListWithBacktrace(defines_list));
  2380. for (BT<std::string> const& i : tmp) {
  2381. defines.emplace(i.Value);
  2382. }
  2383. }
  2384. void cmLocalGenerator::AppendDefines(std::set<BT<std::string>>& defines,
  2385. std::string const& defines_list) const
  2386. {
  2387. // Short-circuit if there are no definitions.
  2388. if (defines_list.empty()) {
  2389. return;
  2390. }
  2391. // Expand the list of definitions.
  2392. this->AppendDefines(defines, ExpandListWithBacktrace(defines_list));
  2393. }
  2394. void cmLocalGenerator::AppendDefines(
  2395. std::set<BT<std::string>>& defines,
  2396. const std::vector<BT<std::string>>& defines_vec) const
  2397. {
  2398. for (BT<std::string> const& d : defines_vec) {
  2399. // Skip unsupported definitions.
  2400. if (!this->CheckDefinition(d.Value)) {
  2401. continue;
  2402. }
  2403. defines.insert(d);
  2404. }
  2405. }
  2406. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  2407. std::string& definesString,
  2408. const std::string& lang)
  2409. {
  2410. // Lookup the define flag for the current language.
  2411. std::string dflag = "-D";
  2412. if (!lang.empty()) {
  2413. const char* df =
  2414. this->Makefile->GetDefinition(cmStrCat("CMAKE_", lang, "_DEFINE_FLAG"));
  2415. if (df && *df) {
  2416. dflag = df;
  2417. }
  2418. }
  2419. const char* itemSeparator = definesString.empty() ? "" : " ";
  2420. for (std::string const& define : defines) {
  2421. // Append the definition with proper escaping.
  2422. std::string def = dflag;
  2423. if (this->GetState()->UseWatcomWMake()) {
  2424. // The Watcom compiler does its own command line parsing instead
  2425. // of using the windows shell rules. Definitions are one of
  2426. // -DNAME
  2427. // -DNAME=<cpp-token>
  2428. // -DNAME="c-string with spaces and other characters(?@#$)"
  2429. //
  2430. // Watcom will properly parse each of these cases from the
  2431. // command line without any escapes. However we still have to
  2432. // get the '$' and '#' characters through WMake as '$$' and
  2433. // '$#'.
  2434. for (const char* c = define.c_str(); *c; ++c) {
  2435. if (*c == '$' || *c == '#') {
  2436. def += '$';
  2437. }
  2438. def += *c;
  2439. }
  2440. } else {
  2441. // Make the definition appear properly on the command line. Use
  2442. // -DNAME="value" instead of -D"NAME=value" for historical reasons.
  2443. std::string::size_type eq = define.find('=');
  2444. def += define.substr(0, eq);
  2445. if (eq != std::string::npos) {
  2446. def += "=";
  2447. def += this->EscapeForShell(define.substr(eq + 1), true);
  2448. }
  2449. }
  2450. definesString += itemSeparator;
  2451. itemSeparator = " ";
  2452. definesString += def;
  2453. }
  2454. }
  2455. void cmLocalGenerator::AppendFeatureOptions(std::string& flags,
  2456. const std::string& lang,
  2457. const char* feature)
  2458. {
  2459. const char* optionList = this->Makefile->GetDefinition(
  2460. cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_", feature));
  2461. if (optionList != nullptr) {
  2462. std::vector<std::string> options = cmExpandedList(optionList);
  2463. for (std::string const& o : options) {
  2464. this->AppendFlagEscape(flags, o);
  2465. }
  2466. }
  2467. }
  2468. const char* cmLocalGenerator::GetFeature(const std::string& feature,
  2469. const std::string& config)
  2470. {
  2471. std::string featureName = feature;
  2472. // TODO: Define accumulation policy for features (prepend, append, replace).
  2473. // Currently we always replace.
  2474. if (!config.empty()) {
  2475. featureName += "_";
  2476. featureName += cmSystemTools::UpperCase(config);
  2477. }
  2478. cmStateSnapshot snp = this->StateSnapshot;
  2479. while (snp.IsValid()) {
  2480. if (const char* value = snp.GetDirectory().GetProperty(featureName)) {
  2481. return value;
  2482. }
  2483. snp = snp.GetBuildsystemDirectoryParent();
  2484. }
  2485. return nullptr;
  2486. }
  2487. std::string cmLocalGenerator::GetProjectName() const
  2488. {
  2489. return this->StateSnapshot.GetProjectName();
  2490. }
  2491. std::string cmLocalGenerator::ConstructComment(
  2492. cmCustomCommandGenerator const& ccg, const char* default_comment)
  2493. {
  2494. // Check for a comment provided with the command.
  2495. if (ccg.GetComment()) {
  2496. return ccg.GetComment();
  2497. }
  2498. // Construct a reasonable default comment if possible.
  2499. if (!ccg.GetOutputs().empty()) {
  2500. std::string comment;
  2501. comment = "Generating ";
  2502. const char* sep = "";
  2503. std::string currentBinaryDir = this->GetCurrentBinaryDirectory();
  2504. for (std::string const& o : ccg.GetOutputs()) {
  2505. comment += sep;
  2506. comment += this->MaybeConvertToRelativePath(currentBinaryDir, o);
  2507. sep = ", ";
  2508. }
  2509. return comment;
  2510. }
  2511. // Otherwise use the provided default.
  2512. return default_comment;
  2513. }
  2514. class cmInstallTargetGeneratorLocal : public cmInstallTargetGenerator
  2515. {
  2516. public:
  2517. cmInstallTargetGeneratorLocal(cmLocalGenerator* lg, std::string const& t,
  2518. const char* dest, bool implib)
  2519. : cmInstallTargetGenerator(
  2520. t, dest, implib, "", std::vector<std::string>(), "Unspecified",
  2521. cmInstallGenerator::SelectMessageLevel(lg->GetMakefile()), false,
  2522. false)
  2523. {
  2524. this->Compute(lg);
  2525. }
  2526. };
  2527. void cmLocalGenerator::GenerateTargetInstallRules(
  2528. std::ostream& os, const std::string& config,
  2529. std::vector<std::string> const& configurationTypes)
  2530. {
  2531. // Convert the old-style install specification from each target to
  2532. // an install generator and run it.
  2533. const std::vector<cmGeneratorTarget*>& tgts = this->GetGeneratorTargets();
  2534. for (cmGeneratorTarget* l : tgts) {
  2535. if (l->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2536. continue;
  2537. }
  2538. // Include the user-specified pre-install script for this target.
  2539. if (const char* preinstall = l->GetProperty("PRE_INSTALL_SCRIPT")) {
  2540. cmInstallScriptGenerator g(preinstall, false, nullptr, false);
  2541. g.Generate(os, config, configurationTypes);
  2542. }
  2543. // Install this target if a destination is given.
  2544. if (!l->Target->GetInstallPath().empty()) {
  2545. // Compute the full install destination. Note that converting
  2546. // to unix slashes also removes any trailing slash.
  2547. // We also skip over the leading slash given by the user.
  2548. std::string destination = l->Target->GetInstallPath().substr(1);
  2549. cmSystemTools::ConvertToUnixSlashes(destination);
  2550. if (destination.empty()) {
  2551. destination = ".";
  2552. }
  2553. // Generate the proper install generator for this target type.
  2554. switch (l->GetType()) {
  2555. case cmStateEnums::EXECUTABLE:
  2556. case cmStateEnums::STATIC_LIBRARY:
  2557. case cmStateEnums::MODULE_LIBRARY: {
  2558. // Use a target install generator.
  2559. cmInstallTargetGeneratorLocal g(this, l->GetName(),
  2560. destination.c_str(), false);
  2561. g.Generate(os, config, configurationTypes);
  2562. } break;
  2563. case cmStateEnums::SHARED_LIBRARY: {
  2564. #if defined(_WIN32) || defined(__CYGWIN__)
  2565. // Special code to handle DLL. Install the import library
  2566. // to the normal destination and the DLL to the runtime
  2567. // destination.
  2568. cmInstallTargetGeneratorLocal g1(this, l->GetName(),
  2569. destination.c_str(), true);
  2570. g1.Generate(os, config, configurationTypes);
  2571. // We also skip over the leading slash given by the user.
  2572. destination = l->Target->GetRuntimeInstallPath().substr(1);
  2573. cmSystemTools::ConvertToUnixSlashes(destination);
  2574. cmInstallTargetGeneratorLocal g2(this, l->GetName(),
  2575. destination.c_str(), false);
  2576. g2.Generate(os, config, configurationTypes);
  2577. #else
  2578. // Use a target install generator.
  2579. cmInstallTargetGeneratorLocal g(this, l->GetName(),
  2580. destination.c_str(), false);
  2581. g.Generate(os, config, configurationTypes);
  2582. #endif
  2583. } break;
  2584. default:
  2585. break;
  2586. }
  2587. }
  2588. // Include the user-specified post-install script for this target.
  2589. if (const char* postinstall = l->GetProperty("POST_INSTALL_SCRIPT")) {
  2590. cmInstallScriptGenerator g(postinstall, false, nullptr, false);
  2591. g.Generate(os, config, configurationTypes);
  2592. }
  2593. }
  2594. }
  2595. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2596. static bool cmLocalGeneratorShortenObjectName(std::string& objName,
  2597. std::string::size_type max_len)
  2598. {
  2599. // Replace the beginning of the path portion of the object name with
  2600. // its own md5 sum.
  2601. std::string::size_type pos =
  2602. objName.find('/', objName.size() - max_len + 32);
  2603. if (pos != std::string::npos) {
  2604. cmCryptoHash md5(cmCryptoHash::AlgoMD5);
  2605. std::string md5name = cmStrCat(md5.HashString(objName.substr(0, pos)),
  2606. cm::string_view(objName).substr(pos));
  2607. objName = md5name;
  2608. // The object name is now short enough.
  2609. return true;
  2610. }
  2611. // The object name could not be shortened enough.
  2612. return false;
  2613. }
  2614. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2615. std::string::size_type dir_len,
  2616. std::string::size_type max_total_len)
  2617. {
  2618. // Enforce the maximum file name length if possible.
  2619. std::string::size_type max_obj_len = max_total_len;
  2620. if (dir_len < max_total_len) {
  2621. max_obj_len = max_total_len - dir_len;
  2622. if (objName.size() > max_obj_len) {
  2623. // The current object file name is too long. Try to shorten it.
  2624. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2625. }
  2626. // The object file name is short enough.
  2627. return true;
  2628. }
  2629. // The build directory in which the object will be stored is
  2630. // already too deep.
  2631. return false;
  2632. }
  2633. #endif
  2634. std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
  2635. const std::string& sin, std::string const& dir_max)
  2636. {
  2637. // Look for an existing mapped name for this object file.
  2638. auto it = this->UniqueObjectNamesMap.find(sin);
  2639. // If no entry exists create one.
  2640. if (it == this->UniqueObjectNamesMap.end()) {
  2641. // Start with the original name.
  2642. std::string ssin = sin;
  2643. // Avoid full paths by removing leading slashes.
  2644. ssin.erase(0, ssin.find_first_not_of('/'));
  2645. // Avoid full paths by removing colons.
  2646. std::replace(ssin.begin(), ssin.end(), ':', '_');
  2647. // Avoid relative paths that go up the tree.
  2648. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2649. // Avoid spaces.
  2650. std::replace(ssin.begin(), ssin.end(), ' ', '_');
  2651. // Mangle the name if necessary.
  2652. if (this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES")) {
  2653. bool done;
  2654. int cc = 0;
  2655. char rpstr[100];
  2656. sprintf(rpstr, "_p_");
  2657. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2658. std::string sssin = sin;
  2659. do {
  2660. done = true;
  2661. for (it = this->UniqueObjectNamesMap.begin();
  2662. it != this->UniqueObjectNamesMap.end(); ++it) {
  2663. if (it->second == ssin) {
  2664. done = false;
  2665. }
  2666. }
  2667. if (done) {
  2668. break;
  2669. }
  2670. sssin = ssin;
  2671. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2672. sprintf(rpstr, "_p%d_", cc++);
  2673. } while (!done);
  2674. }
  2675. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2676. if (!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2677. this->ObjectPathMax)) {
  2678. // Warn if this is the first time the path has been seen.
  2679. if (this->ObjectMaxPathViolations.insert(dir_max).second) {
  2680. std::ostringstream m;
  2681. /* clang-format off */
  2682. m << "The object file directory\n"
  2683. << " " << dir_max << "\n"
  2684. << "has " << dir_max.size() << " characters. "
  2685. << "The maximum full path to an object file is "
  2686. << this->ObjectPathMax << " characters "
  2687. << "(see CMAKE_OBJECT_PATH_MAX). "
  2688. << "Object file\n"
  2689. << " " << ssin << "\n"
  2690. << "cannot be safely placed under this directory. "
  2691. << "The build may not work correctly.";
  2692. /* clang-format on */
  2693. this->IssueMessage(MessageType::WARNING, m.str());
  2694. }
  2695. }
  2696. #else
  2697. (void)dir_max;
  2698. #endif
  2699. // Insert the newly mapped object file name.
  2700. std::map<std::string, std::string>::value_type e(sin, ssin);
  2701. it = this->UniqueObjectNamesMap.insert(e).first;
  2702. }
  2703. // Return the map entry.
  2704. return it->second;
  2705. }
  2706. void cmLocalGenerator::ComputeObjectFilenames(
  2707. std::map<cmSourceFile const*, std::string>& /*unused*/,
  2708. cmGeneratorTarget const* /*unused*/)
  2709. {
  2710. }
  2711. bool cmLocalGenerator::IsWindowsShell() const
  2712. {
  2713. return this->GetState()->UseWindowsShell();
  2714. }
  2715. bool cmLocalGenerator::IsWatcomWMake() const
  2716. {
  2717. return this->GetState()->UseWatcomWMake();
  2718. }
  2719. bool cmLocalGenerator::IsMinGWMake() const
  2720. {
  2721. return this->GetState()->UseMinGWMake();
  2722. }
  2723. bool cmLocalGenerator::IsNMake() const
  2724. {
  2725. return this->GetState()->UseNMake();
  2726. }
  2727. std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
  2728. const cmSourceFile& source, std::string const& dir_max,
  2729. bool* hasSourceExtension, char const* customOutputExtension)
  2730. {
  2731. // Construct the object file name using the full path to the source
  2732. // file which is its only unique identification.
  2733. std::string const& fullPath = source.GetFullPath();
  2734. // Try referencing the source relative to the source tree.
  2735. std::string relFromSource = this->MaybeConvertToRelativePath(
  2736. this->GetCurrentSourceDirectory(), fullPath);
  2737. assert(!relFromSource.empty());
  2738. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource);
  2739. bool subSource = relSource && relFromSource[0] != '.';
  2740. // Try referencing the source relative to the binary tree.
  2741. std::string relFromBinary = this->MaybeConvertToRelativePath(
  2742. this->GetCurrentBinaryDirectory(), fullPath);
  2743. assert(!relFromBinary.empty());
  2744. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary);
  2745. bool subBinary = relBinary && relFromBinary[0] != '.';
  2746. // Select a nice-looking reference to the source file to construct
  2747. // the object file name.
  2748. std::string objectName;
  2749. if ((relSource && !relBinary) || (subSource && !subBinary)) {
  2750. objectName = relFromSource;
  2751. } else if ((relBinary && !relSource) || (subBinary && !subSource)) {
  2752. objectName = relFromBinary;
  2753. } else if (relFromBinary.length() < relFromSource.length()) {
  2754. objectName = relFromBinary;
  2755. } else {
  2756. objectName = relFromSource;
  2757. }
  2758. // if it is still a full path check for the try compile case
  2759. // try compile never have in source sources, and should not
  2760. // have conflicting source file names in the same target
  2761. if (cmSystemTools::FileIsFullPath(objectName)) {
  2762. if (this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
  2763. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2764. }
  2765. }
  2766. // Replace the original source file extension with the object file
  2767. // extension.
  2768. bool keptSourceExtension = true;
  2769. if (!source.GetPropertyAsBool("KEEP_EXTENSION")) {
  2770. // Decide whether this language wants to replace the source
  2771. // extension with the object extension. For CMake 2.4
  2772. // compatibility do this by default.
  2773. bool replaceExt = this->NeedBackwardsCompatibility_2_4();
  2774. if (!replaceExt) {
  2775. std::string lang = source.GetLanguage();
  2776. if (!lang.empty()) {
  2777. replaceExt = this->Makefile->IsOn(
  2778. cmStrCat("CMAKE_", lang, "_OUTPUT_EXTENSION_REPLACE"));
  2779. }
  2780. }
  2781. const char* pchExtension = source.GetProperty("PCH_EXTENSION");
  2782. if (pchExtension) {
  2783. customOutputExtension = pchExtension;
  2784. }
  2785. // Remove the source extension if it is to be replaced.
  2786. if (replaceExt || customOutputExtension) {
  2787. keptSourceExtension = false;
  2788. std::string::size_type dot_pos = objectName.rfind('.');
  2789. if (dot_pos != std::string::npos) {
  2790. objectName = objectName.substr(0, dot_pos);
  2791. }
  2792. }
  2793. // Store the new extension.
  2794. if (customOutputExtension) {
  2795. objectName += customOutputExtension;
  2796. } else {
  2797. objectName += this->GlobalGenerator->GetLanguageOutputExtension(source);
  2798. }
  2799. }
  2800. if (hasSourceExtension) {
  2801. *hasSourceExtension = keptSourceExtension;
  2802. }
  2803. // Convert to a safe name.
  2804. return this->CreateSafeUniqueObjectFileName(objectName, dir_max);
  2805. }
  2806. std::string cmLocalGenerator::GetSourceFileLanguage(const cmSourceFile& source)
  2807. {
  2808. return source.GetLanguage();
  2809. }
  2810. cmake* cmLocalGenerator::GetCMakeInstance() const
  2811. {
  2812. return this->GlobalGenerator->GetCMakeInstance();
  2813. }
  2814. std::string const& cmLocalGenerator::GetSourceDirectory() const
  2815. {
  2816. return this->GetCMakeInstance()->GetHomeDirectory();
  2817. }
  2818. std::string const& cmLocalGenerator::GetBinaryDirectory() const
  2819. {
  2820. return this->GetCMakeInstance()->GetHomeOutputDirectory();
  2821. }
  2822. std::string const& cmLocalGenerator::GetCurrentBinaryDirectory() const
  2823. {
  2824. return this->StateSnapshot.GetDirectory().GetCurrentBinary();
  2825. }
  2826. std::string const& cmLocalGenerator::GetCurrentSourceDirectory() const
  2827. {
  2828. return this->StateSnapshot.GetDirectory().GetCurrentSource();
  2829. }
  2830. std::string cmLocalGenerator::MaybeConvertToRelativePath(
  2831. std::string const& local_path, std::string const& remote_path) const
  2832. {
  2833. return this->StateSnapshot.GetDirectory().ConvertToRelPathIfNotContained(
  2834. local_path, remote_path);
  2835. }
  2836. std::string cmLocalGenerator::GetTargetDirectory(
  2837. const cmGeneratorTarget* /*unused*/) const
  2838. {
  2839. cmSystemTools::Error("GetTargetDirectory"
  2840. " called on cmLocalGenerator");
  2841. return "";
  2842. }
  2843. KWIML_INT_uint64_t cmLocalGenerator::GetBackwardsCompatibility()
  2844. {
  2845. // The computed version may change until the project is fully
  2846. // configured.
  2847. if (!this->BackwardsCompatibilityFinal) {
  2848. unsigned int major = 0;
  2849. unsigned int minor = 0;
  2850. unsigned int patch = 0;
  2851. if (const char* value =
  2852. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY")) {
  2853. switch (sscanf(value, "%u.%u.%u", &major, &minor, &patch)) {
  2854. case 2:
  2855. patch = 0;
  2856. break;
  2857. case 1:
  2858. minor = 0;
  2859. patch = 0;
  2860. break;
  2861. default:
  2862. break;
  2863. }
  2864. }
  2865. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2866. this->BackwardsCompatibilityFinal = true;
  2867. }
  2868. return this->BackwardsCompatibility;
  2869. }
  2870. bool cmLocalGenerator::NeedBackwardsCompatibility_2_4()
  2871. {
  2872. // Check the policy to decide whether to pay attention to this
  2873. // variable.
  2874. switch (this->GetPolicyStatus(cmPolicies::CMP0001)) {
  2875. case cmPolicies::WARN:
  2876. // WARN is just OLD without warning because user code does not
  2877. // always affect whether this check is done.
  2878. case cmPolicies::OLD:
  2879. // Old behavior is to check the variable.
  2880. break;
  2881. case cmPolicies::NEW:
  2882. // New behavior is to ignore the variable.
  2883. return false;
  2884. case cmPolicies::REQUIRED_IF_USED:
  2885. case cmPolicies::REQUIRED_ALWAYS:
  2886. // This will never be the case because the only way to require
  2887. // the setting is to require the user to specify version policy
  2888. // 2.6 or higher. Once we add that requirement then this whole
  2889. // method can be removed anyway.
  2890. return false;
  2891. }
  2892. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2893. // equal to or lower than the given version.
  2894. KWIML_INT_uint64_t actual_compat = this->GetBackwardsCompatibility();
  2895. return (actual_compat && actual_compat <= CMake_VERSION_ENCODE(2, 4, 255));
  2896. }
  2897. cmPolicies::PolicyStatus cmLocalGenerator::GetPolicyStatus(
  2898. cmPolicies::PolicyID id) const
  2899. {
  2900. return this->Makefile->GetPolicyStatus(id);
  2901. }
  2902. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  2903. {
  2904. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  2905. std::string::size_type pos = define.find_first_of("(=");
  2906. if (pos != std::string::npos) {
  2907. if (define[pos] == '(') {
  2908. std::ostringstream e;
  2909. /* clang-format off */
  2910. e << "WARNING: Function-style preprocessor definitions may not be "
  2911. << "passed on the compiler command line because many compilers "
  2912. << "do not support it.\n"
  2913. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2914. << "Consider defining the macro in a (configured) header file.\n";
  2915. /* clang-format on */
  2916. cmSystemTools::Message(e.str());
  2917. return false;
  2918. }
  2919. }
  2920. // Many compilers do not support # in the value so we disable it.
  2921. if (define.find_first_of('#') != std::string::npos) {
  2922. std::ostringstream e;
  2923. /* clang-format off */
  2924. e << "WARNING: Preprocessor definitions containing '#' may not be "
  2925. << "passed on the compiler command line because many compilers "
  2926. << "do not support it.\n"
  2927. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2928. << "Consider defining the macro in a (configured) header file.\n";
  2929. /* clang-format on */
  2930. cmSystemTools::Message(e.str());
  2931. return false;
  2932. }
  2933. // Assume it is supported.
  2934. return true;
  2935. }
  2936. static void cmLGInfoProp(cmMakefile* mf, cmGeneratorTarget* target,
  2937. const std::string& prop)
  2938. {
  2939. if (const char* val = target->GetProperty(prop)) {
  2940. mf->AddDefinition(prop, val);
  2941. }
  2942. }
  2943. void cmLocalGenerator::GenerateAppleInfoPList(cmGeneratorTarget* target,
  2944. const std::string& targetName,
  2945. const std::string& fname)
  2946. {
  2947. // Find the Info.plist template.
  2948. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  2949. std::string inFile = (in && *in) ? in : "MacOSXBundleInfo.plist.in";
  2950. if (!cmSystemTools::FileIsFullPath(inFile)) {
  2951. std::string inMod = this->Makefile->GetModulesFile(inFile);
  2952. if (!inMod.empty()) {
  2953. inFile = inMod;
  2954. }
  2955. }
  2956. if (!cmSystemTools::FileExists(inFile, true)) {
  2957. std::ostringstream e;
  2958. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2959. << "\" could not be found.";
  2960. cmSystemTools::Error(e.str());
  2961. return;
  2962. }
  2963. // Convert target properties to variables in an isolated makefile
  2964. // scope to configure the file. If properties are set they will
  2965. // override user make variables. If not the configuration will fall
  2966. // back to the directory-level values set by the user.
  2967. cmMakefile* mf = this->Makefile;
  2968. cmMakefile::ScopePushPop varScope(mf);
  2969. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName);
  2970. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  2971. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  2972. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  2973. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  2974. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  2975. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  2976. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  2977. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  2978. mf->ConfigureFile(inFile, fname, false, false, false);
  2979. }
  2980. void cmLocalGenerator::GenerateFrameworkInfoPList(
  2981. cmGeneratorTarget* target, const std::string& targetName,
  2982. const std::string& fname)
  2983. {
  2984. // Find the Info.plist template.
  2985. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  2986. std::string inFile = (in && *in) ? in : "MacOSXFrameworkInfo.plist.in";
  2987. if (!cmSystemTools::FileIsFullPath(inFile)) {
  2988. std::string inMod = this->Makefile->GetModulesFile(inFile);
  2989. if (!inMod.empty()) {
  2990. inFile = inMod;
  2991. }
  2992. }
  2993. if (!cmSystemTools::FileExists(inFile, true)) {
  2994. std::ostringstream e;
  2995. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2996. << "\" could not be found.";
  2997. cmSystemTools::Error(e.str());
  2998. return;
  2999. }
  3000. // Convert target properties to variables in an isolated makefile
  3001. // scope to configure the file. If properties are set they will
  3002. // override user make variables. If not the configuration will fall
  3003. // back to the directory-level values set by the user.
  3004. cmMakefile* mf = this->Makefile;
  3005. cmMakefile::ScopePushPop varScope(mf);
  3006. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName);
  3007. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  3008. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  3009. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  3010. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  3011. mf->ConfigureFile(inFile, fname, false, false, false);
  3012. }