cmGeneratorTarget.cxx 178 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424
  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 "cmGeneratorTarget.h"
  4. #include "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <iterator>
  9. #include <memory> // IWYU pragma: keep
  10. #include <queue>
  11. #include <sstream>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unordered_set>
  16. #include "cmAlgorithms.h"
  17. #include "cmComputeLinkInformation.h"
  18. #include "cmCustomCommand.h"
  19. #include "cmCustomCommandGenerator.h"
  20. #include "cmCustomCommandLines.h"
  21. #include "cmGeneratorExpression.h"
  22. #include "cmGeneratorExpressionDAGChecker.h"
  23. #include "cmGlobalGenerator.h"
  24. #include "cmLocalGenerator.h"
  25. #include "cmMakefile.h"
  26. #include "cmPropertyMap.h"
  27. #include "cmSourceFile.h"
  28. #include "cmSourceFileLocation.h"
  29. #include "cmState.h"
  30. #include "cmSystemTools.h"
  31. #include "cmTarget.h"
  32. #include "cmTargetLinkLibraryType.h"
  33. #include "cmTargetPropertyComputer.h"
  34. #include "cmake.h"
  35. class cmMessenger;
  36. template <>
  37. const char* cmTargetPropertyComputer::GetSources<cmGeneratorTarget>(
  38. cmGeneratorTarget const* tgt, cmMessenger* /* messenger */,
  39. cmListFileBacktrace const& /* context */)
  40. {
  41. return tgt->GetSourcesProperty();
  42. }
  43. template <>
  44. const char* cmTargetPropertyComputer::ComputeLocationForBuild<
  45. cmGeneratorTarget>(cmGeneratorTarget const* tgt)
  46. {
  47. return tgt->GetLocation("");
  48. }
  49. template <>
  50. const char* cmTargetPropertyComputer::ComputeLocation<cmGeneratorTarget>(
  51. cmGeneratorTarget const* tgt, const std::string& config)
  52. {
  53. return tgt->GetLocation(config);
  54. }
  55. class cmGeneratorTarget::TargetPropertyEntry
  56. {
  57. static cmLinkImplItem NoLinkImplItem;
  58. public:
  59. TargetPropertyEntry(std::unique_ptr<cmCompiledGeneratorExpression> cge,
  60. cmLinkImplItem const& item = NoLinkImplItem)
  61. : ge(std::move(cge))
  62. , LinkImplItem(item)
  63. {
  64. }
  65. const std::unique_ptr<cmCompiledGeneratorExpression> ge;
  66. cmLinkImplItem const& LinkImplItem;
  67. };
  68. cmLinkImplItem cmGeneratorTarget::TargetPropertyEntry::NoLinkImplItem;
  69. void CreatePropertyGeneratorExpressions(
  70. cmStringRange entries, cmBacktraceRange backtraces,
  71. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& items,
  72. bool evaluateForBuildsystem = false)
  73. {
  74. std::vector<cmListFileBacktrace>::const_iterator btIt = backtraces.begin();
  75. for (std::vector<std::string>::const_iterator it = entries.begin();
  76. it != entries.end(); ++it, ++btIt) {
  77. cmGeneratorExpression ge(*btIt);
  78. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*it);
  79. cge->SetEvaluateForBuildsystem(evaluateForBuildsystem);
  80. items.push_back(
  81. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge)));
  82. }
  83. }
  84. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t, cmLocalGenerator* lg)
  85. : Target(t)
  86. , FortranModuleDirectoryCreated(false)
  87. , SourceFileFlagsConstructed(false)
  88. , PolicyWarnedCMP0022(false)
  89. , PolicyReportedCMP0069(false)
  90. , DebugIncludesDone(false)
  91. , DebugCompileOptionsDone(false)
  92. , DebugCompileFeaturesDone(false)
  93. , DebugCompileDefinitionsDone(false)
  94. , DebugSourcesDone(false)
  95. , LinkImplementationLanguageIsContextDependent(true)
  96. , UtilityItemsDone(false)
  97. {
  98. this->Makefile = this->Target->GetMakefile();
  99. this->LocalGenerator = lg;
  100. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  101. this->GlobalGenerator->ComputeTargetObjectDirectory(this);
  102. CreatePropertyGeneratorExpressions(t->GetIncludeDirectoriesEntries(),
  103. t->GetIncludeDirectoriesBacktraces(),
  104. this->IncludeDirectoriesEntries);
  105. CreatePropertyGeneratorExpressions(t->GetCompileOptionsEntries(),
  106. t->GetCompileOptionsBacktraces(),
  107. this->CompileOptionsEntries);
  108. CreatePropertyGeneratorExpressions(t->GetCompileFeaturesEntries(),
  109. t->GetCompileFeaturesBacktraces(),
  110. this->CompileFeaturesEntries);
  111. CreatePropertyGeneratorExpressions(t->GetCompileDefinitionsEntries(),
  112. t->GetCompileDefinitionsBacktraces(),
  113. this->CompileDefinitionsEntries);
  114. CreatePropertyGeneratorExpressions(t->GetSourceEntries(),
  115. t->GetSourceBacktraces(),
  116. this->SourceEntries, true);
  117. this->DLLPlatform =
  118. (this->Makefile->IsOn("WIN32") || this->Makefile->IsOn("CYGWIN") ||
  119. this->Makefile->IsOn("MINGW"));
  120. this->PolicyMap = t->PolicyMap;
  121. }
  122. cmGeneratorTarget::~cmGeneratorTarget()
  123. {
  124. cmDeleteAll(this->IncludeDirectoriesEntries);
  125. cmDeleteAll(this->CompileOptionsEntries);
  126. cmDeleteAll(this->CompileFeaturesEntries);
  127. cmDeleteAll(this->CompileDefinitionsEntries);
  128. cmDeleteAll(this->SourceEntries);
  129. cmDeleteAll(this->LinkInformation);
  130. }
  131. const char* cmGeneratorTarget::GetSourcesProperty() const
  132. {
  133. std::vector<std::string> values;
  134. for (TargetPropertyEntry* se : this->SourceEntries) {
  135. values.push_back(se->ge->GetInput());
  136. }
  137. static std::string value;
  138. value.clear();
  139. value = cmJoin(values, ";");
  140. return value.c_str();
  141. }
  142. cmGlobalGenerator* cmGeneratorTarget::GetGlobalGenerator() const
  143. {
  144. return this->GetLocalGenerator()->GetGlobalGenerator();
  145. }
  146. cmLocalGenerator* cmGeneratorTarget::GetLocalGenerator() const
  147. {
  148. return this->LocalGenerator;
  149. }
  150. cmStateEnums::TargetType cmGeneratorTarget::GetType() const
  151. {
  152. return this->Target->GetType();
  153. }
  154. const std::string& cmGeneratorTarget::GetName() const
  155. {
  156. return this->Target->GetName();
  157. }
  158. std::string cmGeneratorTarget::GetExportName() const
  159. {
  160. const char* exportName = this->GetProperty("EXPORT_NAME");
  161. if (exportName && *exportName) {
  162. if (!cmGeneratorExpression::IsValidTargetName(exportName)) {
  163. std::ostringstream e;
  164. e << "EXPORT_NAME property \"" << exportName << "\" for \""
  165. << this->GetName() << "\": is not valid.";
  166. cmSystemTools::Error(e.str().c_str());
  167. return "";
  168. }
  169. return exportName;
  170. }
  171. return this->GetName();
  172. }
  173. const char* cmGeneratorTarget::GetProperty(const std::string& prop) const
  174. {
  175. if (!cmTargetPropertyComputer::PassesWhitelist(
  176. this->GetType(), prop, this->Makefile->GetMessenger(),
  177. this->GetBacktrace())) {
  178. return nullptr;
  179. }
  180. if (const char* result = cmTargetPropertyComputer::GetProperty(
  181. this, prop, this->Makefile->GetMessenger(), this->GetBacktrace())) {
  182. return result;
  183. }
  184. if (cmSystemTools::GetFatalErrorOccured()) {
  185. return nullptr;
  186. }
  187. return this->Target->GetProperty(prop);
  188. }
  189. const char* cmGeneratorTarget::GetOutputTargetType(
  190. cmStateEnums::ArtifactType artifact) const
  191. {
  192. switch (this->GetType()) {
  193. case cmStateEnums::SHARED_LIBRARY:
  194. if (this->IsDLLPlatform()) {
  195. switch (artifact) {
  196. case cmStateEnums::RuntimeBinaryArtifact:
  197. // A DLL shared library is treated as a runtime target.
  198. return "RUNTIME";
  199. case cmStateEnums::ImportLibraryArtifact:
  200. // A DLL import library is treated as an archive target.
  201. return "ARCHIVE";
  202. }
  203. } else {
  204. // For non-DLL platforms shared libraries are treated as
  205. // library targets.
  206. return "LIBRARY";
  207. }
  208. break;
  209. case cmStateEnums::STATIC_LIBRARY:
  210. // Static libraries are always treated as archive targets.
  211. return "ARCHIVE";
  212. case cmStateEnums::MODULE_LIBRARY:
  213. switch (artifact) {
  214. case cmStateEnums::RuntimeBinaryArtifact:
  215. // Module import libraries are treated as archive targets.
  216. return "LIBRARY";
  217. case cmStateEnums::ImportLibraryArtifact:
  218. // Module libraries are always treated as library targets.
  219. return "ARCHIVE";
  220. }
  221. break;
  222. case cmStateEnums::EXECUTABLE:
  223. switch (artifact) {
  224. case cmStateEnums::RuntimeBinaryArtifact:
  225. // Executables are always treated as runtime targets.
  226. return "RUNTIME";
  227. case cmStateEnums::ImportLibraryArtifact:
  228. // Executable import libraries are treated as archive targets.
  229. return "ARCHIVE";
  230. }
  231. break;
  232. default:
  233. break;
  234. }
  235. return "";
  236. }
  237. std::string cmGeneratorTarget::GetOutputName(
  238. const std::string& config, cmStateEnums::ArtifactType artifact) const
  239. {
  240. // Lookup/compute/cache the output name for this configuration.
  241. OutputNameKey key(config, artifact);
  242. cmGeneratorTarget::OutputNameMapType::iterator i =
  243. this->OutputNameMap.find(key);
  244. if (i == this->OutputNameMap.end()) {
  245. // Add empty name in map to detect potential recursion.
  246. OutputNameMapType::value_type entry(key, "");
  247. i = this->OutputNameMap.insert(entry).first;
  248. // Compute output name.
  249. std::vector<std::string> props;
  250. std::string type = this->GetOutputTargetType(artifact);
  251. std::string configUpper = cmSystemTools::UpperCase(config);
  252. if (!type.empty() && !configUpper.empty()) {
  253. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME_<CONFIG>
  254. props.push_back(type + "_OUTPUT_NAME_" + configUpper);
  255. }
  256. if (!type.empty()) {
  257. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME
  258. props.push_back(type + "_OUTPUT_NAME");
  259. }
  260. if (!configUpper.empty()) {
  261. // OUTPUT_NAME_<CONFIG>
  262. props.push_back("OUTPUT_NAME_" + configUpper);
  263. // <CONFIG>_OUTPUT_NAME
  264. props.push_back(configUpper + "_OUTPUT_NAME");
  265. }
  266. // OUTPUT_NAME
  267. props.push_back("OUTPUT_NAME");
  268. std::string outName;
  269. for (std::string const& p : props) {
  270. if (const char* outNameProp = this->GetProperty(p)) {
  271. outName = outNameProp;
  272. break;
  273. }
  274. }
  275. if (outName.empty()) {
  276. outName = this->GetName();
  277. }
  278. // Now evaluate genex and update the previously-prepared map entry.
  279. cmGeneratorExpression ge;
  280. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outName);
  281. i->second = cge->Evaluate(this->LocalGenerator, config);
  282. } else if (i->second.empty()) {
  283. // An empty map entry indicates we have been called recursively
  284. // from the above block.
  285. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  286. cmake::FATAL_ERROR,
  287. "Target '" + this->GetName() + "' OUTPUT_NAME depends on itself.",
  288. this->GetBacktrace());
  289. }
  290. return i->second;
  291. }
  292. void cmGeneratorTarget::ClearSourcesCache()
  293. {
  294. this->KindedSourcesMap.clear();
  295. this->LinkImplementationLanguageIsContextDependent = true;
  296. this->Objects.clear();
  297. }
  298. void cmGeneratorTarget::AddSourceCommon(const std::string& src)
  299. {
  300. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  301. cmGeneratorExpression ge(lfbt);
  302. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(src);
  303. cge->SetEvaluateForBuildsystem(true);
  304. this->SourceEntries.push_back(new TargetPropertyEntry(std::move(cge)));
  305. this->ClearSourcesCache();
  306. }
  307. void cmGeneratorTarget::AddSource(const std::string& src)
  308. {
  309. this->Target->AddSource(src);
  310. this->AddSourceCommon(src);
  311. }
  312. void cmGeneratorTarget::AddTracedSources(std::vector<std::string> const& srcs)
  313. {
  314. this->Target->AddTracedSources(srcs);
  315. if (!srcs.empty()) {
  316. this->AddSourceCommon(cmJoin(srcs, ";"));
  317. }
  318. }
  319. void cmGeneratorTarget::AddIncludeDirectory(const std::string& src,
  320. bool before)
  321. {
  322. this->Target->InsertInclude(src, this->Makefile->GetBacktrace(), before);
  323. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  324. cmGeneratorExpression ge(lfbt);
  325. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(src);
  326. cge->SetEvaluateForBuildsystem(true);
  327. // Insert before begin/end
  328. std::vector<TargetPropertyEntry*>::iterator pos = before
  329. ? this->IncludeDirectoriesEntries.begin()
  330. : this->IncludeDirectoriesEntries.end();
  331. this->IncludeDirectoriesEntries.insert(
  332. pos, new TargetPropertyEntry(std::move(cge)));
  333. }
  334. std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
  335. cmSourceFile const* sf) const
  336. {
  337. SourceEntriesType::const_iterator i = this->SourceDepends.find(sf);
  338. if (i != this->SourceDepends.end()) {
  339. return &i->second.Depends;
  340. }
  341. return nullptr;
  342. }
  343. static void handleSystemIncludesDep(
  344. cmLocalGenerator* lg, cmGeneratorTarget const* depTgt,
  345. const std::string& config, cmGeneratorTarget const* headTarget,
  346. cmGeneratorExpressionDAGChecker* dagChecker,
  347. std::vector<std::string>& result, bool excludeImported)
  348. {
  349. if (const char* dirs =
  350. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")) {
  351. cmGeneratorExpression ge;
  352. cmSystemTools::ExpandListArgument(
  353. ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
  354. dagChecker),
  355. result);
  356. }
  357. if (!depTgt->IsImported() || excludeImported) {
  358. return;
  359. }
  360. if (const char* dirs =
  361. depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
  362. cmGeneratorExpression ge;
  363. cmSystemTools::ExpandListArgument(
  364. ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
  365. dagChecker),
  366. result);
  367. }
  368. }
  369. /* clang-format off */
  370. #define IMPLEMENT_VISIT(KIND) \
  371. { \
  372. KindedSources const& kinded = this->GetKindedSources(config); \
  373. for (SourceAndKind const& s : kinded.Sources) { \
  374. if (s.Kind == KIND) { \
  375. data.push_back(s.Source); \
  376. } \
  377. } \
  378. }
  379. /* clang-format on */
  380. void cmGeneratorTarget::GetObjectSources(
  381. std::vector<cmSourceFile const*>& data, const std::string& config) const
  382. {
  383. IMPLEMENT_VISIT(SourceKindObjectSource);
  384. if (!this->Objects.empty()) {
  385. return;
  386. }
  387. for (cmSourceFile const* it : data) {
  388. this->Objects[it];
  389. }
  390. this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
  391. }
  392. void cmGeneratorTarget::ComputeObjectMapping()
  393. {
  394. if (!this->Objects.empty()) {
  395. return;
  396. }
  397. std::vector<std::string> configs;
  398. this->Makefile->GetConfigurations(configs);
  399. if (configs.empty()) {
  400. configs.push_back("");
  401. }
  402. for (std::string const& c : configs) {
  403. std::vector<cmSourceFile const*> sourceFiles;
  404. this->GetObjectSources(sourceFiles, c);
  405. }
  406. }
  407. const char* cmGeneratorTarget::GetFeature(const std::string& feature,
  408. const std::string& config) const
  409. {
  410. if (!config.empty()) {
  411. std::string featureConfig = feature;
  412. featureConfig += "_";
  413. featureConfig += cmSystemTools::UpperCase(config);
  414. if (const char* value = this->GetProperty(featureConfig)) {
  415. return value;
  416. }
  417. }
  418. if (const char* value = this->GetProperty(feature)) {
  419. return value;
  420. }
  421. return this->LocalGenerator->GetFeature(feature, config);
  422. }
  423. bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
  424. std::string const& config) const
  425. {
  426. const char* feature = "INTERPROCEDURAL_OPTIMIZATION";
  427. const bool result = cmSystemTools::IsOn(this->GetFeature(feature, config));
  428. if (!result) {
  429. // 'INTERPROCEDURAL_OPTIMIZATION' is off, no need to check policies
  430. return false;
  431. }
  432. if (lang != "C" && lang != "CXX" && lang != "Fortran") {
  433. // We do not define IPO behavior for other languages.
  434. return false;
  435. }
  436. cmPolicies::PolicyStatus cmp0069 = this->GetPolicyStatusCMP0069();
  437. if (cmp0069 == cmPolicies::OLD || cmp0069 == cmPolicies::WARN) {
  438. if (this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_LEGACY_BEHAVIOR")) {
  439. return true;
  440. }
  441. if (this->PolicyReportedCMP0069) {
  442. // problem is already reported, no need to issue a message
  443. return false;
  444. }
  445. const bool in_try_compile =
  446. this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile();
  447. if (cmp0069 == cmPolicies::WARN && !in_try_compile) {
  448. std::ostringstream w;
  449. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0069) << "\n";
  450. w << "INTERPROCEDURAL_OPTIMIZATION property will be ignored for target "
  451. << "'" << this->GetName() << "'.";
  452. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  453. cmake::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  454. this->PolicyReportedCMP0069 = true;
  455. }
  456. return false;
  457. }
  458. // Note: check consistency with messages from CheckIPOSupported
  459. const char* message = nullptr;
  460. if (!this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_SUPPORTED_BY_CMAKE")) {
  461. message = "CMake doesn't support IPO for current compiler";
  462. } else if (!this->Makefile->IsOn("_CMAKE_" + lang +
  463. "_IPO_MAY_BE_SUPPORTED_BY_COMPILER")) {
  464. message = "Compiler doesn't support IPO";
  465. } else if (!this->GlobalGenerator->IsIPOSupported()) {
  466. message = "CMake doesn't support IPO for current generator";
  467. }
  468. if (!message) {
  469. // No error/warning messages
  470. return true;
  471. }
  472. if (this->PolicyReportedCMP0069) {
  473. // problem is already reported, no need to issue a message
  474. return false;
  475. }
  476. this->PolicyReportedCMP0069 = true;
  477. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  478. cmake::FATAL_ERROR, message, this->GetBacktrace());
  479. return false;
  480. }
  481. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  482. {
  483. this->ComputeObjectMapping();
  484. return this->Objects[file];
  485. }
  486. const char* cmGeneratorTarget::GetCustomObjectExtension() const
  487. {
  488. static std::string extension;
  489. const bool has_ptx_extension =
  490. this->GetPropertyAsBool("CUDA_PTX_COMPILATION");
  491. if (has_ptx_extension) {
  492. extension = ".ptx";
  493. return extension.c_str();
  494. }
  495. return nullptr;
  496. }
  497. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  498. {
  499. this->ExplicitObjectName.insert(sf);
  500. }
  501. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  502. {
  503. const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
  504. std::set<cmSourceFile const*>::const_iterator it =
  505. this->ExplicitObjectName.find(file);
  506. return it != this->ExplicitObjectName.end();
  507. }
  508. void cmGeneratorTarget::GetModuleDefinitionSources(
  509. std::vector<cmSourceFile const*>& data, const std::string& config) const
  510. {
  511. IMPLEMENT_VISIT(SourceKindModuleDefinition);
  512. }
  513. void cmGeneratorTarget::GetHeaderSources(
  514. std::vector<cmSourceFile const*>& data, const std::string& config) const
  515. {
  516. IMPLEMENT_VISIT(SourceKindHeader);
  517. }
  518. void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile const*>& data,
  519. const std::string& config) const
  520. {
  521. IMPLEMENT_VISIT(SourceKindExtra);
  522. }
  523. void cmGeneratorTarget::GetCustomCommands(
  524. std::vector<cmSourceFile const*>& data, const std::string& config) const
  525. {
  526. IMPLEMENT_VISIT(SourceKindCustomCommand);
  527. }
  528. void cmGeneratorTarget::GetExternalObjects(
  529. std::vector<cmSourceFile const*>& data, const std::string& config) const
  530. {
  531. IMPLEMENT_VISIT(SourceKindExternalObject);
  532. }
  533. void cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& headers,
  534. const std::string& config) const
  535. {
  536. KindedSources const& kinded = this->GetKindedSources(config);
  537. headers = kinded.ExpectedResxHeaders;
  538. }
  539. void cmGeneratorTarget::GetResxSources(std::vector<cmSourceFile const*>& data,
  540. const std::string& config) const
  541. {
  542. IMPLEMENT_VISIT(SourceKindResx);
  543. }
  544. void cmGeneratorTarget::GetAppManifest(std::vector<cmSourceFile const*>& data,
  545. const std::string& config) const
  546. {
  547. IMPLEMENT_VISIT(SourceKindAppManifest);
  548. }
  549. void cmGeneratorTarget::GetManifests(std::vector<cmSourceFile const*>& data,
  550. const std::string& config) const
  551. {
  552. IMPLEMENT_VISIT(SourceKindManifest);
  553. }
  554. void cmGeneratorTarget::GetCertificates(std::vector<cmSourceFile const*>& data,
  555. const std::string& config) const
  556. {
  557. IMPLEMENT_VISIT(SourceKindCertificate);
  558. }
  559. void cmGeneratorTarget::GetExpectedXamlHeaders(std::set<std::string>& headers,
  560. const std::string& config) const
  561. {
  562. KindedSources const& kinded = this->GetKindedSources(config);
  563. headers = kinded.ExpectedXamlHeaders;
  564. }
  565. void cmGeneratorTarget::GetExpectedXamlSources(std::set<std::string>& srcs,
  566. const std::string& config) const
  567. {
  568. KindedSources const& kinded = this->GetKindedSources(config);
  569. srcs = kinded.ExpectedXamlSources;
  570. }
  571. std::set<cmLinkItem> const& cmGeneratorTarget::GetUtilityItems() const
  572. {
  573. if (!this->UtilityItemsDone) {
  574. this->UtilityItemsDone = true;
  575. std::set<std::string> const& utilities = this->GetUtilities();
  576. for (std::string const& i : utilities) {
  577. cmGeneratorTarget* gt =
  578. this->LocalGenerator->FindGeneratorTargetToUse(i);
  579. this->UtilityItems.insert(cmLinkItem(i, gt));
  580. }
  581. }
  582. return this->UtilityItems;
  583. }
  584. void cmGeneratorTarget::GetXamlSources(std::vector<cmSourceFile const*>& data,
  585. const std::string& config) const
  586. {
  587. IMPLEMENT_VISIT(SourceKindXaml);
  588. }
  589. const char* cmGeneratorTarget::GetLocation(const std::string& config) const
  590. {
  591. static std::string location;
  592. if (this->IsImported()) {
  593. location = this->Target->ImportedGetFullPath(
  594. config, cmStateEnums::RuntimeBinaryArtifact);
  595. } else {
  596. location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  597. }
  598. return location.c_str();
  599. }
  600. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreBuildCommands()
  601. const
  602. {
  603. return this->Target->GetPreBuildCommands();
  604. }
  605. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreLinkCommands()
  606. const
  607. {
  608. return this->Target->GetPreLinkCommands();
  609. }
  610. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPostBuildCommands()
  611. const
  612. {
  613. return this->Target->GetPostBuildCommands();
  614. }
  615. bool cmGeneratorTarget::IsImported() const
  616. {
  617. return this->Target->IsImported();
  618. }
  619. bool cmGeneratorTarget::IsImportedGloballyVisible() const
  620. {
  621. return this->Target->IsImportedGloballyVisible();
  622. }
  623. const char* cmGeneratorTarget::GetLocationForBuild() const
  624. {
  625. static std::string location;
  626. if (this->IsImported()) {
  627. location = this->Target->ImportedGetFullPath(
  628. "", cmStateEnums::RuntimeBinaryArtifact);
  629. return location.c_str();
  630. }
  631. // Now handle the deprecated build-time configuration location.
  632. location = this->GetDirectory();
  633. const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  634. if (cfgid && strcmp(cfgid, ".") != 0) {
  635. location += "/";
  636. location += cfgid;
  637. }
  638. if (this->IsAppBundleOnApple()) {
  639. std::string macdir = this->BuildBundleDirectory("", "", FullLevel);
  640. if (!macdir.empty()) {
  641. location += "/";
  642. location += macdir;
  643. }
  644. }
  645. location += "/";
  646. location += this->GetFullName("", cmStateEnums::RuntimeBinaryArtifact);
  647. return location.c_str();
  648. }
  649. bool cmGeneratorTarget::IsSystemIncludeDirectory(
  650. const std::string& dir, const std::string& config) const
  651. {
  652. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  653. std::string config_upper;
  654. if (!config.empty()) {
  655. config_upper = cmSystemTools::UpperCase(config);
  656. }
  657. typedef std::map<std::string, std::vector<std::string>> IncludeCacheType;
  658. IncludeCacheType::const_iterator iter =
  659. this->SystemIncludesCache.find(config_upper);
  660. if (iter == this->SystemIncludesCache.end()) {
  661. cmGeneratorExpressionDAGChecker dagChecker(
  662. this->GetName(), "SYSTEM_INCLUDE_DIRECTORIES", nullptr, nullptr);
  663. bool excludeImported = this->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  664. std::vector<std::string> result;
  665. for (std::string const& it : this->Target->GetSystemIncludeDirectories()) {
  666. cmGeneratorExpression ge;
  667. cmSystemTools::ExpandListArgument(
  668. ge.Parse(it)->Evaluate(this->LocalGenerator, config, false, this,
  669. &dagChecker),
  670. result);
  671. }
  672. std::vector<cmGeneratorTarget const*> const& deps =
  673. this->GetLinkImplementationClosure(config);
  674. for (cmGeneratorTarget const* dep : deps) {
  675. handleSystemIncludesDep(this->LocalGenerator, dep, config, this,
  676. &dagChecker, result, excludeImported);
  677. }
  678. std::for_each(result.begin(), result.end(),
  679. cmSystemTools::ConvertToUnixSlashes);
  680. std::sort(result.begin(), result.end());
  681. result.erase(std::unique(result.begin(), result.end()), result.end());
  682. IncludeCacheType::value_type entry(config_upper, result);
  683. iter = this->SystemIncludesCache.insert(entry).first;
  684. }
  685. return std::binary_search(iter->second.begin(), iter->second.end(), dir);
  686. }
  687. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  688. {
  689. return this->Target->GetPropertyAsBool(prop);
  690. }
  691. static void AddInterfaceEntries(
  692. cmGeneratorTarget const* thisTarget, std::string const& config,
  693. std::string const& prop,
  694. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries)
  695. {
  696. if (cmLinkImplementationLibraries const* impl =
  697. thisTarget->GetLinkImplementationLibraries(config)) {
  698. for (cmLinkImplItem const& lib : impl->Libraries) {
  699. if (lib.Target) {
  700. std::string genex = "$<TARGET_PROPERTY:" + lib + "," + prop + ">";
  701. cmGeneratorExpression ge(lib.Backtrace);
  702. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  703. cge->SetEvaluateForBuildsystem(true);
  704. entries.push_back(
  705. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge), lib));
  706. }
  707. }
  708. }
  709. }
  710. static bool processSources(
  711. cmGeneratorTarget const* tgt,
  712. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  713. std::vector<std::string>& srcs, std::unordered_set<std::string>& uniqueSrcs,
  714. cmGeneratorExpressionDAGChecker* dagChecker, std::string const& config,
  715. bool debugSources)
  716. {
  717. cmMakefile* mf = tgt->Target->GetMakefile();
  718. bool contextDependent = false;
  719. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  720. cmLinkImplItem const& item = entry->LinkImplItem;
  721. std::string const& targetName = item;
  722. std::vector<std::string> entrySources;
  723. cmSystemTools::ExpandListArgument(
  724. entry->ge->Evaluate(tgt->GetLocalGenerator(), config, false, tgt, tgt,
  725. dagChecker),
  726. entrySources);
  727. if (entry->ge->GetHadContextSensitiveCondition()) {
  728. contextDependent = true;
  729. }
  730. for (std::string& src : entrySources) {
  731. cmSourceFile* sf = mf->GetOrCreateSource(src);
  732. std::string e;
  733. std::string fullPath = sf->GetFullPath(&e);
  734. if (fullPath.empty()) {
  735. if (!e.empty()) {
  736. cmake* cm = tgt->GetLocalGenerator()->GetCMakeInstance();
  737. cm->IssueMessage(cmake::FATAL_ERROR, e, tgt->GetBacktrace());
  738. }
  739. return contextDependent;
  740. }
  741. if (!targetName.empty() && !cmSystemTools::FileIsFullPath(src.c_str())) {
  742. std::ostringstream err;
  743. if (!targetName.empty()) {
  744. err << "Target \"" << targetName
  745. << "\" contains relative "
  746. "path in its INTERFACE_SOURCES:\n"
  747. " \""
  748. << src << "\"";
  749. } else {
  750. err << "Found relative path while evaluating sources of "
  751. "\""
  752. << tgt->GetName() << "\":\n \"" << src << "\"\n";
  753. }
  754. tgt->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, err.str());
  755. return contextDependent;
  756. }
  757. src = fullPath;
  758. }
  759. std::string usedSources;
  760. for (std::string const& src : entrySources) {
  761. if (uniqueSrcs.insert(src).second) {
  762. srcs.push_back(src);
  763. if (debugSources) {
  764. usedSources += " * " + src + "\n";
  765. }
  766. }
  767. }
  768. if (!usedSources.empty()) {
  769. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  770. cmake::LOG, std::string("Used sources for target ") + tgt->GetName() +
  771. ":\n" + usedSources,
  772. entry->ge->GetBacktrace());
  773. }
  774. }
  775. return contextDependent;
  776. }
  777. void cmGeneratorTarget::GetSourceFiles(std::vector<std::string>& files,
  778. const std::string& config) const
  779. {
  780. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  781. if (!this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  782. // At configure-time, this method can be called as part of getting the
  783. // LOCATION property or to export() a file to be include()d. However
  784. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  785. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  786. // behavior of CMP0024 and CMP0026 only.
  787. cmStringRange sourceEntries = this->Target->GetSourceEntries();
  788. for (std::string const& entry : sourceEntries) {
  789. std::vector<std::string> items;
  790. cmSystemTools::ExpandListArgument(entry, items);
  791. for (std::string const& item : items) {
  792. if (cmHasLiteralPrefix(item, "$<TARGET_OBJECTS:") &&
  793. item[item.size() - 1] == '>') {
  794. continue;
  795. }
  796. files.push_back(item);
  797. }
  798. }
  799. return;
  800. }
  801. std::vector<std::string> debugProperties;
  802. const char* debugProp =
  803. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  804. if (debugProp) {
  805. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  806. }
  807. bool debugSources = !this->DebugSourcesDone &&
  808. std::find(debugProperties.begin(), debugProperties.end(), "SOURCES") !=
  809. debugProperties.end();
  810. if (this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  811. this->DebugSourcesDone = true;
  812. }
  813. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), "SOURCES",
  814. nullptr, nullptr);
  815. std::unordered_set<std::string> uniqueSrcs;
  816. bool contextDependentDirectSources =
  817. processSources(this, this->SourceEntries, files, uniqueSrcs, &dagChecker,
  818. config, debugSources);
  819. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  820. linkInterfaceSourcesEntries;
  821. AddInterfaceEntries(this, config, "INTERFACE_SOURCES",
  822. linkInterfaceSourcesEntries);
  823. std::vector<std::string>::size_type numFilesBefore = files.size();
  824. bool contextDependentInterfaceSources =
  825. processSources(this, linkInterfaceSourcesEntries, files, uniqueSrcs,
  826. &dagChecker, config, debugSources);
  827. if (!contextDependentDirectSources &&
  828. !(contextDependentInterfaceSources && numFilesBefore < files.size())) {
  829. this->LinkImplementationLanguageIsContextDependent = false;
  830. }
  831. cmDeleteAll(linkInterfaceSourcesEntries);
  832. }
  833. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*>& files,
  834. const std::string& config) const
  835. {
  836. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  837. // Since we are still configuring not all sources may exist yet,
  838. // so we need to avoid full source classification because that
  839. // requires the absolute paths to all sources to be determined.
  840. // Since this is only for compatibility with old policies that
  841. // projects should not depend on anymore, just compute the files
  842. // without memoizing them.
  843. std::vector<std::string> srcs;
  844. this->GetSourceFiles(srcs, config);
  845. std::set<cmSourceFile*> emitted;
  846. for (std::string const& s : srcs) {
  847. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s);
  848. if (emitted.insert(sf).second) {
  849. files.push_back(sf);
  850. }
  851. }
  852. return;
  853. }
  854. KindedSources const& kinded = this->GetKindedSources(config);
  855. files.reserve(kinded.Sources.size());
  856. for (SourceAndKind const& si : kinded.Sources) {
  857. files.push_back(si.Source);
  858. }
  859. }
  860. void cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  861. std::vector<cmSourceFile*>& files, const std::string& config) const
  862. {
  863. KindedSources const& kinded = this->GetKindedSources(config);
  864. files.reserve(kinded.Sources.size());
  865. for (SourceAndKind const& si : kinded.Sources) {
  866. if (si.Source->GetObjectLibrary().empty()) {
  867. files.push_back(si.Source);
  868. }
  869. }
  870. }
  871. cmGeneratorTarget::KindedSources const& cmGeneratorTarget::GetKindedSources(
  872. std::string const& config) const
  873. {
  874. // If we already processed one configuration and found no dependenc
  875. // on configuration then always use the one result.
  876. if (!this->LinkImplementationLanguageIsContextDependent) {
  877. return this->KindedSourcesMap.begin()->second;
  878. }
  879. // Lookup any existing link implementation for this configuration.
  880. std::string const key = cmSystemTools::UpperCase(config);
  881. KindedSourcesMapType::iterator it = this->KindedSourcesMap.find(key);
  882. if (it != this->KindedSourcesMap.end()) {
  883. if (!it->second.Initialized) {
  884. std::ostringstream e;
  885. e << "The SOURCES of \"" << this->GetName()
  886. << "\" use a generator expression that depends on the "
  887. "SOURCES themselves.";
  888. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  889. cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  890. static KindedSources empty;
  891. return empty;
  892. }
  893. return it->second;
  894. }
  895. // Add an entry to the map for this configuration.
  896. KindedSources& files = this->KindedSourcesMap[key];
  897. this->ComputeKindedSources(files, config);
  898. files.Initialized = true;
  899. return files;
  900. }
  901. void cmGeneratorTarget::ComputeKindedSources(KindedSources& files,
  902. std::string const& config) const
  903. {
  904. // Get the source file paths by string.
  905. std::vector<std::string> srcs;
  906. this->GetSourceFiles(srcs, config);
  907. cmsys::RegularExpression header_regex(CM_HEADER_REGEX);
  908. std::vector<cmSourceFile*> badObjLib;
  909. std::set<cmSourceFile*> emitted;
  910. for (std::string const& s : srcs) {
  911. // Create each source at most once.
  912. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s);
  913. if (!emitted.insert(sf).second) {
  914. continue;
  915. }
  916. // Compute the kind (classification) of this source file.
  917. SourceKind kind;
  918. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  919. if (sf->GetCustomCommand()) {
  920. kind = SourceKindCustomCommand;
  921. } else if (this->Target->GetType() == cmStateEnums::UTILITY) {
  922. kind = SourceKindExtra;
  923. } else if (sf->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  924. kind = SourceKindHeader;
  925. } else if (sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  926. kind = SourceKindExternalObject;
  927. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  928. badObjLib.push_back(sf);
  929. }
  930. } else if (!sf->GetLanguage().empty()) {
  931. kind = SourceKindObjectSource;
  932. } else if (ext == "def") {
  933. kind = SourceKindModuleDefinition;
  934. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  935. badObjLib.push_back(sf);
  936. }
  937. } else if (ext == "idl") {
  938. kind = SourceKindIDL;
  939. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  940. badObjLib.push_back(sf);
  941. }
  942. } else if (ext == "resx") {
  943. kind = SourceKindResx;
  944. // Build and save the name of the corresponding .h file
  945. // This relationship will be used later when building the project files.
  946. // Both names would have been auto generated from Visual Studio
  947. // where the user supplied the file name and Visual Studio
  948. // appended the suffix.
  949. std::string resx = sf->GetFullPath();
  950. std::string hFileName = resx.substr(0, resx.find_last_of('.')) + ".h";
  951. files.ExpectedResxHeaders.insert(hFileName);
  952. } else if (ext == "appxmanifest") {
  953. kind = SourceKindAppManifest;
  954. } else if (ext == "manifest") {
  955. kind = SourceKindManifest;
  956. } else if (ext == "pfx") {
  957. kind = SourceKindCertificate;
  958. } else if (ext == "xaml") {
  959. kind = SourceKindXaml;
  960. // Build and save the name of the corresponding .h and .cpp file
  961. // This relationship will be used later when building the project files.
  962. // Both names would have been auto generated from Visual Studio
  963. // where the user supplied the file name and Visual Studio
  964. // appended the suffix.
  965. std::string xaml = sf->GetFullPath();
  966. std::string hFileName = xaml + ".h";
  967. std::string cppFileName = xaml + ".cpp";
  968. files.ExpectedXamlHeaders.insert(hFileName);
  969. files.ExpectedXamlSources.insert(cppFileName);
  970. } else if (header_regex.find(sf->GetFullPath().c_str())) {
  971. kind = SourceKindHeader;
  972. } else {
  973. kind = SourceKindExtra;
  974. }
  975. // Save this classified source file in the result vector.
  976. SourceAndKind entry = { sf, kind };
  977. files.Sources.push_back(entry);
  978. }
  979. if (!badObjLib.empty()) {
  980. std::ostringstream e;
  981. e << "OBJECT library \"" << this->GetName() << "\" contains:\n";
  982. for (cmSourceFile* i : badObjLib) {
  983. e << " " << i->GetLocation().GetName() << "\n";
  984. }
  985. e << "but may contain only sources that compile, header files, and "
  986. "other files that would not affect linking of a normal library.";
  987. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  988. cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  989. }
  990. }
  991. std::vector<cmGeneratorTarget::AllConfigSource> const&
  992. cmGeneratorTarget::GetAllConfigSources() const
  993. {
  994. if (this->AllConfigSources.empty()) {
  995. this->ComputeAllConfigSources();
  996. }
  997. return this->AllConfigSources;
  998. }
  999. void cmGeneratorTarget::ComputeAllConfigSources() const
  1000. {
  1001. std::vector<std::string> configs;
  1002. this->Makefile->GetConfigurations(configs);
  1003. std::map<cmSourceFile const*, size_t> index;
  1004. for (size_t ci = 0; ci < configs.size(); ++ci) {
  1005. KindedSources const& sources = this->GetKindedSources(configs[ci]);
  1006. for (SourceAndKind const& src : sources.Sources) {
  1007. std::map<cmSourceFile const*, size_t>::iterator mi =
  1008. index.find(src.Source);
  1009. if (mi == index.end()) {
  1010. AllConfigSource acs;
  1011. acs.Source = src.Source;
  1012. acs.Kind = src.Kind;
  1013. this->AllConfigSources.push_back(acs);
  1014. std::map<cmSourceFile const*, size_t>::value_type entry(
  1015. src.Source, this->AllConfigSources.size() - 1);
  1016. mi = index.insert(entry).first;
  1017. }
  1018. this->AllConfigSources[mi->second].Configs.push_back(ci);
  1019. }
  1020. }
  1021. }
  1022. std::string cmGeneratorTarget::GetCompilePDBName(
  1023. const std::string& config) const
  1024. {
  1025. std::string prefix;
  1026. std::string base;
  1027. std::string suffix;
  1028. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  1029. prefix, base, suffix);
  1030. // Check for a per-configuration output directory target property.
  1031. std::string configUpper = cmSystemTools::UpperCase(config);
  1032. std::string configProp = "COMPILE_PDB_NAME_";
  1033. configProp += configUpper;
  1034. const char* config_name = this->GetProperty(configProp);
  1035. if (config_name && *config_name) {
  1036. return prefix + config_name + ".pdb";
  1037. }
  1038. const char* name = this->GetProperty("COMPILE_PDB_NAME");
  1039. if (name && *name) {
  1040. return prefix + name + ".pdb";
  1041. }
  1042. return "";
  1043. }
  1044. std::string cmGeneratorTarget::GetCompilePDBPath(
  1045. const std::string& config) const
  1046. {
  1047. std::string dir = this->GetCompilePDBDirectory(config);
  1048. std::string name = this->GetCompilePDBName(config);
  1049. if (dir.empty() && !name.empty() && this->HaveWellDefinedOutputFiles()) {
  1050. dir = this->GetPDBDirectory(config);
  1051. }
  1052. if (!dir.empty()) {
  1053. dir += "/";
  1054. }
  1055. return dir + name;
  1056. }
  1057. bool cmGeneratorTarget::HasSOName(const std::string& config) const
  1058. {
  1059. // soname is supported only for shared libraries and modules,
  1060. // and then only when the platform supports an soname flag.
  1061. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY) &&
  1062. !this->GetPropertyAsBool("NO_SONAME") &&
  1063. this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
  1064. }
  1065. bool cmGeneratorTarget::NeedRelinkBeforeInstall(
  1066. const std::string& config) const
  1067. {
  1068. // Only executables and shared libraries can have an rpath and may
  1069. // need relinking.
  1070. if (this->GetType() != cmStateEnums::EXECUTABLE &&
  1071. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1072. this->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1073. return false;
  1074. }
  1075. // If there is no install location this target will not be installed
  1076. // and therefore does not need relinking.
  1077. if (!this->Target->GetHaveInstallRule()) {
  1078. return false;
  1079. }
  1080. // If skipping all rpaths completely then no relinking is needed.
  1081. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1082. return false;
  1083. }
  1084. // If building with the install-tree rpath no relinking is needed.
  1085. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1086. return false;
  1087. }
  1088. // If chrpath is going to be used no relinking is needed.
  1089. if (this->IsChrpathUsed(config)) {
  1090. return false;
  1091. }
  1092. // Check for rpath support on this platform.
  1093. std::string ll = this->GetLinkerLanguage(config);
  1094. if (!ll.empty()) {
  1095. std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1096. flagVar += ll;
  1097. flagVar += "_FLAG";
  1098. if (!this->Makefile->IsSet(flagVar)) {
  1099. // There is no rpath support on this platform so nothing needs
  1100. // relinking.
  1101. return false;
  1102. }
  1103. } else {
  1104. // No linker language is known. This error will be reported by
  1105. // other code.
  1106. return false;
  1107. }
  1108. // If either a build or install tree rpath is set then the rpath
  1109. // will likely change between the build tree and install tree and
  1110. // this target must be relinked.
  1111. bool have_rpath =
  1112. this->HaveBuildTreeRPATH(config) || this->HaveInstallTreeRPATH();
  1113. bool is_ninja =
  1114. this->LocalGenerator->GetGlobalGenerator()->GetName() == "Ninja";
  1115. if (have_rpath && is_ninja) {
  1116. std::ostringstream w;
  1117. /* clang-format off */
  1118. w <<
  1119. "The install of the " << this->GetName() << " target requires "
  1120. "changing an RPATH from the build tree, but this is not supported "
  1121. "with the Ninja generator unless on an ELF-based platform. The "
  1122. "CMAKE_BUILD_WITH_INSTALL_RPATH variable may be set to avoid this "
  1123. "relinking step."
  1124. ;
  1125. /* clang-format on */
  1126. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1127. cm->IssueMessage(cmake::FATAL_ERROR, w.str(), this->GetBacktrace());
  1128. }
  1129. return have_rpath;
  1130. }
  1131. bool cmGeneratorTarget::IsChrpathUsed(const std::string& config) const
  1132. {
  1133. // Only certain target types have an rpath.
  1134. if (!(this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1135. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1136. this->GetType() == cmStateEnums::EXECUTABLE)) {
  1137. return false;
  1138. }
  1139. // If the target will not be installed we do not need to change its
  1140. // rpath.
  1141. if (!this->Target->GetHaveInstallRule()) {
  1142. return false;
  1143. }
  1144. // Skip chrpath if skipping rpath altogether.
  1145. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1146. return false;
  1147. }
  1148. // Skip chrpath if it does not need to be changed at install time.
  1149. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1150. return false;
  1151. }
  1152. // Allow the user to disable builtin chrpath explicitly.
  1153. if (this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH")) {
  1154. return false;
  1155. }
  1156. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1157. return true;
  1158. }
  1159. #if defined(CMAKE_USE_ELF_PARSER)
  1160. // Enable if the rpath flag uses a separator and the target uses ELF
  1161. // binaries.
  1162. std::string ll = this->GetLinkerLanguage(config);
  1163. if (!ll.empty()) {
  1164. std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1165. sepVar += ll;
  1166. sepVar += "_FLAG_SEP";
  1167. const char* sep = this->Makefile->GetDefinition(sepVar);
  1168. if (sep && *sep) {
  1169. // TODO: Add ELF check to ABI detection and get rid of
  1170. // CMAKE_EXECUTABLE_FORMAT.
  1171. if (const char* fmt =
  1172. this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT")) {
  1173. return strcmp(fmt, "ELF") == 0;
  1174. }
  1175. }
  1176. }
  1177. #endif
  1178. static_cast<void>(config);
  1179. return false;
  1180. }
  1181. bool cmGeneratorTarget::IsImportedSharedLibWithoutSOName(
  1182. const std::string& config) const
  1183. {
  1184. if (this->IsImported() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1185. if (cmGeneratorTarget::ImportInfo const* info =
  1186. this->GetImportInfo(config)) {
  1187. return info->NoSOName;
  1188. }
  1189. }
  1190. return false;
  1191. }
  1192. bool cmGeneratorTarget::HasMacOSXRpathInstallNameDir(
  1193. const std::string& config) const
  1194. {
  1195. bool install_name_is_rpath = false;
  1196. bool macosx_rpath = false;
  1197. if (!this->IsImported()) {
  1198. if (this->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1199. return false;
  1200. }
  1201. const char* install_name = this->GetProperty("INSTALL_NAME_DIR");
  1202. bool use_install_name = this->MacOSXUseInstallNameDir();
  1203. if (install_name && use_install_name &&
  1204. std::string(install_name) == "@rpath") {
  1205. install_name_is_rpath = true;
  1206. } else if (install_name && use_install_name) {
  1207. return false;
  1208. }
  1209. if (!install_name_is_rpath) {
  1210. macosx_rpath = this->MacOSXRpathInstallNameDirDefault();
  1211. }
  1212. } else {
  1213. // Lookup the imported soname.
  1214. if (cmGeneratorTarget::ImportInfo const* info =
  1215. this->GetImportInfo(config)) {
  1216. if (!info->NoSOName && !info->SOName.empty()) {
  1217. if (info->SOName.find("@rpath/") == 0) {
  1218. install_name_is_rpath = true;
  1219. }
  1220. } else {
  1221. std::string install_name;
  1222. cmSystemTools::GuessLibraryInstallName(info->Location, install_name);
  1223. if (install_name.find("@rpath") != std::string::npos) {
  1224. install_name_is_rpath = true;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. if (!install_name_is_rpath && !macosx_rpath) {
  1230. return false;
  1231. }
  1232. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1233. std::ostringstream w;
  1234. w << "Attempting to use ";
  1235. if (macosx_rpath) {
  1236. w << "MACOSX_RPATH";
  1237. } else {
  1238. w << "@rpath";
  1239. }
  1240. w << " without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being set.";
  1241. w << " This could be because you are using a Mac OS X version";
  1242. w << " less than 10.5 or because CMake's platform configuration is";
  1243. w << " corrupt.";
  1244. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1245. cm->IssueMessage(cmake::FATAL_ERROR, w.str(), this->GetBacktrace());
  1246. }
  1247. return true;
  1248. }
  1249. bool cmGeneratorTarget::MacOSXRpathInstallNameDirDefault() const
  1250. {
  1251. // we can't do rpaths when unsupported
  1252. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1253. return false;
  1254. }
  1255. const char* macosx_rpath_str = this->GetProperty("MACOSX_RPATH");
  1256. if (macosx_rpath_str) {
  1257. return this->GetPropertyAsBool("MACOSX_RPATH");
  1258. }
  1259. cmPolicies::PolicyStatus cmp0042 = this->GetPolicyStatusCMP0042();
  1260. if (cmp0042 == cmPolicies::WARN) {
  1261. this->LocalGenerator->GetGlobalGenerator()->AddCMP0042WarnTarget(
  1262. this->GetName());
  1263. }
  1264. return cmp0042 == cmPolicies::NEW;
  1265. }
  1266. bool cmGeneratorTarget::MacOSXUseInstallNameDir() const
  1267. {
  1268. const char* build_with_install_name =
  1269. this->GetProperty("BUILD_WITH_INSTALL_NAME_DIR");
  1270. if (build_with_install_name) {
  1271. return cmSystemTools::IsOn(build_with_install_name);
  1272. }
  1273. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1274. if (cmp0068 == cmPolicies::NEW) {
  1275. return false;
  1276. }
  1277. bool use_install_name = this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1278. if (use_install_name && cmp0068 == cmPolicies::WARN) {
  1279. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1280. this->GetName());
  1281. }
  1282. return use_install_name;
  1283. }
  1284. bool cmGeneratorTarget::CanGenerateInstallNameDir(
  1285. InstallNameType name_type) const
  1286. {
  1287. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1288. if (cmp0068 == cmPolicies::NEW) {
  1289. return true;
  1290. }
  1291. bool skip = this->Makefile->IsOn("CMAKE_SKIP_RPATH");
  1292. if (name_type == INSTALL_NAME_FOR_INSTALL) {
  1293. skip |= this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  1294. } else {
  1295. skip |= this->GetPropertyAsBool("SKIP_BUILD_RPATH");
  1296. }
  1297. if (skip && cmp0068 == cmPolicies::WARN) {
  1298. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1299. this->GetName());
  1300. }
  1301. return !skip;
  1302. }
  1303. std::string cmGeneratorTarget::GetSOName(const std::string& config) const
  1304. {
  1305. if (this->IsImported()) {
  1306. // Lookup the imported soname.
  1307. if (cmGeneratorTarget::ImportInfo const* info =
  1308. this->GetImportInfo(config)) {
  1309. if (info->NoSOName) {
  1310. // The imported library has no builtin soname so the name
  1311. // searched at runtime will be just the filename.
  1312. return cmSystemTools::GetFilenameName(info->Location);
  1313. }
  1314. // Use the soname given if any.
  1315. if (info->SOName.find("@rpath/") == 0) {
  1316. return info->SOName.substr(6);
  1317. }
  1318. return info->SOName;
  1319. }
  1320. return "";
  1321. }
  1322. // Compute the soname that will be built.
  1323. std::string name;
  1324. std::string soName;
  1325. std::string realName;
  1326. std::string impName;
  1327. std::string pdbName;
  1328. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  1329. return soName;
  1330. }
  1331. static bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  1332. {
  1333. return level == cmGeneratorTarget::FullLevel;
  1334. }
  1335. static bool shouldAddContentLevel(
  1336. cmGeneratorTarget::BundleDirectoryLevel level)
  1337. {
  1338. return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level);
  1339. }
  1340. std::string cmGeneratorTarget::GetAppBundleDirectory(
  1341. const std::string& config, BundleDirectoryLevel level) const
  1342. {
  1343. std::string fpath =
  1344. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  1345. fpath += ".";
  1346. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1347. if (!ext) {
  1348. ext = "app";
  1349. }
  1350. fpath += ext;
  1351. if (shouldAddContentLevel(level) && !this->Makefile->PlatformIsAppleIos()) {
  1352. fpath += "/Contents";
  1353. if (shouldAddFullLevel(level)) {
  1354. fpath += "/MacOS";
  1355. }
  1356. }
  1357. return fpath;
  1358. }
  1359. bool cmGeneratorTarget::IsBundleOnApple() const
  1360. {
  1361. return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
  1362. this->IsCFBundleOnApple();
  1363. }
  1364. std::string cmGeneratorTarget::GetCFBundleDirectory(
  1365. const std::string& config, BundleDirectoryLevel level) const
  1366. {
  1367. std::string fpath;
  1368. fpath += this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  1369. fpath += ".";
  1370. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1371. if (!ext) {
  1372. if (this->IsXCTestOnApple()) {
  1373. ext = "xctest";
  1374. } else {
  1375. ext = "bundle";
  1376. }
  1377. }
  1378. fpath += ext;
  1379. if (shouldAddContentLevel(level) && !this->Makefile->PlatformIsAppleIos()) {
  1380. fpath += "/Contents";
  1381. if (shouldAddFullLevel(level)) {
  1382. fpath += "/MacOS";
  1383. }
  1384. }
  1385. return fpath;
  1386. }
  1387. std::string cmGeneratorTarget::GetFrameworkDirectory(
  1388. const std::string& config, BundleDirectoryLevel level) const
  1389. {
  1390. std::string fpath;
  1391. fpath += this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  1392. fpath += ".";
  1393. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1394. if (!ext) {
  1395. ext = "framework";
  1396. }
  1397. fpath += ext;
  1398. if (shouldAddFullLevel(level) && !this->Makefile->PlatformIsAppleIos()) {
  1399. fpath += "/Versions/";
  1400. fpath += this->GetFrameworkVersion();
  1401. }
  1402. return fpath;
  1403. }
  1404. std::string cmGeneratorTarget::GetFullName(
  1405. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1406. {
  1407. if (this->IsImported()) {
  1408. return this->GetFullNameImported(config, artifact);
  1409. }
  1410. return this->GetFullNameInternal(config, artifact);
  1411. }
  1412. std::string cmGeneratorTarget::GetInstallNameDirForBuildTree(
  1413. const std::string& config) const
  1414. {
  1415. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1416. // If building directly for installation then the build tree install_name
  1417. // is the same as the install tree.
  1418. if (this->MacOSXUseInstallNameDir()) {
  1419. return this->GetInstallNameDirForInstallTree();
  1420. }
  1421. // Use the build tree directory for the target.
  1422. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_BUILD)) {
  1423. std::string dir;
  1424. if (this->MacOSXRpathInstallNameDirDefault()) {
  1425. dir = "@rpath";
  1426. } else {
  1427. dir = this->GetDirectory(config);
  1428. }
  1429. dir += "/";
  1430. return dir;
  1431. }
  1432. }
  1433. return "";
  1434. }
  1435. std::string cmGeneratorTarget::GetInstallNameDirForInstallTree() const
  1436. {
  1437. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1438. std::string dir;
  1439. const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  1440. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_INSTALL)) {
  1441. if (install_name_dir && *install_name_dir) {
  1442. dir = install_name_dir;
  1443. dir += "/";
  1444. }
  1445. }
  1446. if (!install_name_dir) {
  1447. if (this->MacOSXRpathInstallNameDirDefault()) {
  1448. dir = "@rpath/";
  1449. }
  1450. }
  1451. return dir;
  1452. }
  1453. return "";
  1454. }
  1455. cmListFileBacktrace cmGeneratorTarget::GetBacktrace() const
  1456. {
  1457. return this->Target->GetBacktrace();
  1458. }
  1459. const std::vector<std::string>& cmGeneratorTarget::GetLinkDirectories() const
  1460. {
  1461. return this->Target->GetLinkDirectories();
  1462. }
  1463. const std::set<std::string>& cmGeneratorTarget::GetUtilities() const
  1464. {
  1465. return this->Target->GetUtilities();
  1466. }
  1467. const cmListFileBacktrace* cmGeneratorTarget::GetUtilityBacktrace(
  1468. const std::string& u) const
  1469. {
  1470. return this->Target->GetUtilityBacktrace(u);
  1471. }
  1472. bool cmGeneratorTarget::HaveWellDefinedOutputFiles() const
  1473. {
  1474. return this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  1475. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1476. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1477. this->GetType() == cmStateEnums::EXECUTABLE;
  1478. }
  1479. const char* cmGeneratorTarget::GetExportMacro() const
  1480. {
  1481. // Define the symbol for targets that export symbols.
  1482. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1483. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1484. this->IsExecutableWithExports()) {
  1485. if (const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL")) {
  1486. this->ExportMacro = custom_export_name;
  1487. } else {
  1488. std::string in = this->GetName();
  1489. in += "_EXPORTS";
  1490. this->ExportMacro = cmSystemTools::MakeCidentifier(in);
  1491. }
  1492. return this->ExportMacro.c_str();
  1493. }
  1494. return nullptr;
  1495. }
  1496. class cmTargetCollectLinkLanguages
  1497. {
  1498. public:
  1499. cmTargetCollectLinkLanguages(cmGeneratorTarget const* target,
  1500. const std::string& config,
  1501. std::unordered_set<std::string>& languages,
  1502. cmGeneratorTarget const* head)
  1503. : Config(config)
  1504. , Languages(languages)
  1505. , HeadTarget(head)
  1506. , Target(target)
  1507. {
  1508. this->Visited.insert(target);
  1509. }
  1510. void Visit(cmLinkItem const& item)
  1511. {
  1512. if (!item.Target) {
  1513. if (item.find("::") != std::string::npos) {
  1514. bool noMessage = false;
  1515. cmake::MessageType messageType = cmake::FATAL_ERROR;
  1516. std::ostringstream e;
  1517. switch (this->Target->GetLocalGenerator()->GetPolicyStatus(
  1518. cmPolicies::CMP0028)) {
  1519. case cmPolicies::WARN: {
  1520. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0028) << "\n";
  1521. messageType = cmake::AUTHOR_WARNING;
  1522. } break;
  1523. case cmPolicies::OLD:
  1524. noMessage = true;
  1525. case cmPolicies::REQUIRED_IF_USED:
  1526. case cmPolicies::REQUIRED_ALWAYS:
  1527. case cmPolicies::NEW:
  1528. // Issue the fatal message.
  1529. break;
  1530. }
  1531. if (!noMessage) {
  1532. e << "Target \"" << this->Target->GetName()
  1533. << "\" links to target \"" << item
  1534. << "\" but the target was not found. Perhaps a find_package() "
  1535. "call is missing for an IMPORTED target, or an ALIAS target is "
  1536. "missing?";
  1537. this->Target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1538. messageType, e.str(), this->Target->GetBacktrace());
  1539. }
  1540. }
  1541. return;
  1542. }
  1543. if (!this->Visited.insert(item.Target).second) {
  1544. return;
  1545. }
  1546. cmLinkInterface const* iface =
  1547. item.Target->GetLinkInterface(this->Config, this->HeadTarget);
  1548. if (!iface) {
  1549. return;
  1550. }
  1551. for (std::string const& language : iface->Languages) {
  1552. this->Languages.insert(language);
  1553. }
  1554. for (cmLinkItem const& lib : iface->Libraries) {
  1555. this->Visit(lib);
  1556. }
  1557. }
  1558. private:
  1559. std::string Config;
  1560. std::unordered_set<std::string>& Languages;
  1561. cmGeneratorTarget const* HeadTarget;
  1562. const cmGeneratorTarget* Target;
  1563. std::set<cmGeneratorTarget const*> Visited;
  1564. };
  1565. cmGeneratorTarget::LinkClosure const* cmGeneratorTarget::GetLinkClosure(
  1566. const std::string& config) const
  1567. {
  1568. std::string key(cmSystemTools::UpperCase(config));
  1569. LinkClosureMapType::iterator i = this->LinkClosureMap.find(key);
  1570. if (i == this->LinkClosureMap.end()) {
  1571. LinkClosure lc;
  1572. this->ComputeLinkClosure(config, lc);
  1573. LinkClosureMapType::value_type entry(key, lc);
  1574. i = this->LinkClosureMap.insert(entry).first;
  1575. }
  1576. return &i->second;
  1577. }
  1578. class cmTargetSelectLinker
  1579. {
  1580. int Preference;
  1581. cmGeneratorTarget const* Target;
  1582. cmGlobalGenerator* GG;
  1583. std::set<std::string> Preferred;
  1584. public:
  1585. cmTargetSelectLinker(cmGeneratorTarget const* target)
  1586. : Preference(0)
  1587. , Target(target)
  1588. {
  1589. this->GG = this->Target->GetLocalGenerator()->GetGlobalGenerator();
  1590. }
  1591. void Consider(const char* lang)
  1592. {
  1593. int preference = this->GG->GetLinkerPreference(lang);
  1594. if (preference > this->Preference) {
  1595. this->Preference = preference;
  1596. this->Preferred.clear();
  1597. }
  1598. if (preference == this->Preference) {
  1599. this->Preferred.insert(lang);
  1600. }
  1601. }
  1602. std::string Choose()
  1603. {
  1604. if (this->Preferred.empty()) {
  1605. return "";
  1606. }
  1607. if (this->Preferred.size() > 1) {
  1608. std::ostringstream e;
  1609. e << "Target " << this->Target->GetName()
  1610. << " contains multiple languages with the highest linker preference"
  1611. << " (" << this->Preference << "):\n";
  1612. for (std::string const& li : this->Preferred) {
  1613. e << " " << li << "\n";
  1614. }
  1615. e << "Set the LINKER_LANGUAGE property for this target.";
  1616. cmake* cm = this->Target->GetLocalGenerator()->GetCMakeInstance();
  1617. cm->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1618. this->Target->GetBacktrace());
  1619. }
  1620. return *this->Preferred.begin();
  1621. }
  1622. };
  1623. void cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  1624. LinkClosure& lc) const
  1625. {
  1626. // Get languages built in this target.
  1627. std::unordered_set<std::string> languages;
  1628. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  1629. assert(impl);
  1630. for (std::string const& li : impl->Languages) {
  1631. languages.insert(li);
  1632. }
  1633. // Add interface languages from linked targets.
  1634. cmTargetCollectLinkLanguages cll(this, config, languages, this);
  1635. for (cmLinkImplItem const& lib : impl->Libraries) {
  1636. cll.Visit(lib);
  1637. }
  1638. // Store the transitive closure of languages.
  1639. for (std::string const& lang : languages) {
  1640. lc.Languages.push_back(lang);
  1641. }
  1642. // Choose the language whose linker should be used.
  1643. if (this->GetProperty("HAS_CXX")) {
  1644. lc.LinkerLanguage = "CXX";
  1645. } else if (const char* linkerLang = this->GetProperty("LINKER_LANGUAGE")) {
  1646. lc.LinkerLanguage = linkerLang;
  1647. } else {
  1648. // Find the language with the highest preference value.
  1649. cmTargetSelectLinker tsl(this);
  1650. // First select from the languages compiled directly in this target.
  1651. for (std::string const& l : impl->Languages) {
  1652. tsl.Consider(l.c_str());
  1653. }
  1654. // Now consider languages that propagate from linked targets.
  1655. for (std::string const& lang : languages) {
  1656. std::string propagates =
  1657. "CMAKE_" + lang + "_LINKER_PREFERENCE_PROPAGATES";
  1658. if (this->Makefile->IsOn(propagates)) {
  1659. tsl.Consider(lang.c_str());
  1660. }
  1661. }
  1662. lc.LinkerLanguage = tsl.Choose();
  1663. }
  1664. }
  1665. void cmGeneratorTarget::GetFullNameComponents(
  1666. std::string& prefix, std::string& base, std::string& suffix,
  1667. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1668. {
  1669. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  1670. }
  1671. std::string cmGeneratorTarget::BuildBundleDirectory(
  1672. const std::string& base, const std::string& config,
  1673. BundleDirectoryLevel level) const
  1674. {
  1675. std::string fpath = base;
  1676. if (this->IsAppBundleOnApple()) {
  1677. fpath += this->GetAppBundleDirectory(config, level);
  1678. }
  1679. if (this->IsFrameworkOnApple()) {
  1680. fpath += this->GetFrameworkDirectory(config, level);
  1681. }
  1682. if (this->IsCFBundleOnApple()) {
  1683. fpath += this->GetCFBundleDirectory(config, level);
  1684. }
  1685. return fpath;
  1686. }
  1687. std::string cmGeneratorTarget::GetMacContentDirectory(
  1688. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1689. {
  1690. // Start with the output directory for the target.
  1691. std::string fpath = this->GetDirectory(config, artifact);
  1692. fpath += "/";
  1693. BundleDirectoryLevel level = ContentLevel;
  1694. if (this->IsFrameworkOnApple()) {
  1695. // additional files with a framework go into the version specific
  1696. // directory
  1697. level = FullLevel;
  1698. }
  1699. fpath = this->BuildBundleDirectory(fpath, config, level);
  1700. return fpath;
  1701. }
  1702. std::string cmGeneratorTarget::GetEffectiveFolderName() const
  1703. {
  1704. std::string effectiveFolder;
  1705. if (!this->GlobalGenerator->UseFolderProperty()) {
  1706. return effectiveFolder;
  1707. }
  1708. const char* targetFolder = this->GetProperty("FOLDER");
  1709. if (targetFolder) {
  1710. effectiveFolder += targetFolder;
  1711. }
  1712. return effectiveFolder;
  1713. }
  1714. cmGeneratorTarget::CompileInfo const* cmGeneratorTarget::GetCompileInfo(
  1715. const std::string& config) const
  1716. {
  1717. // There is no compile information for imported targets.
  1718. if (this->IsImported()) {
  1719. return nullptr;
  1720. }
  1721. if (this->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  1722. std::string msg = "cmTarget::GetCompileInfo called for ";
  1723. msg += this->GetName();
  1724. msg += " which has type ";
  1725. msg += cmState::GetTargetTypeName(this->GetType());
  1726. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  1727. return nullptr;
  1728. }
  1729. // Lookup/compute/cache the compile information for this configuration.
  1730. std::string config_upper;
  1731. if (!config.empty()) {
  1732. config_upper = cmSystemTools::UpperCase(config);
  1733. }
  1734. CompileInfoMapType::const_iterator i =
  1735. this->CompileInfoMap.find(config_upper);
  1736. if (i == this->CompileInfoMap.end()) {
  1737. CompileInfo info;
  1738. this->ComputePDBOutputDir("COMPILE_PDB", config, info.CompilePdbDir);
  1739. CompileInfoMapType::value_type entry(config_upper, info);
  1740. i = this->CompileInfoMap.insert(entry).first;
  1741. }
  1742. return &i->second;
  1743. }
  1744. cmGeneratorTarget::ModuleDefinitionInfo const*
  1745. cmGeneratorTarget::GetModuleDefinitionInfo(std::string const& config) const
  1746. {
  1747. // A module definition file only makes sense on certain target types.
  1748. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1749. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1750. !this->IsExecutableWithExports()) {
  1751. return nullptr;
  1752. }
  1753. // Lookup/compute/cache the compile information for this configuration.
  1754. std::string config_upper;
  1755. if (!config.empty()) {
  1756. config_upper = cmSystemTools::UpperCase(config);
  1757. }
  1758. ModuleDefinitionInfoMapType::const_iterator i =
  1759. this->ModuleDefinitionInfoMap.find(config_upper);
  1760. if (i == this->ModuleDefinitionInfoMap.end()) {
  1761. ModuleDefinitionInfo info;
  1762. this->ComputeModuleDefinitionInfo(config, info);
  1763. ModuleDefinitionInfoMapType::value_type entry(config_upper, info);
  1764. i = this->ModuleDefinitionInfoMap.insert(entry).first;
  1765. }
  1766. return &i->second;
  1767. }
  1768. void cmGeneratorTarget::ComputeModuleDefinitionInfo(
  1769. std::string const& config, ModuleDefinitionInfo& info) const
  1770. {
  1771. this->GetModuleDefinitionSources(info.Sources, config);
  1772. info.WindowsExportAllSymbols =
  1773. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
  1774. this->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS");
  1775. info.DefFileGenerated =
  1776. info.WindowsExportAllSymbols || info.Sources.size() > 1;
  1777. if (info.DefFileGenerated) {
  1778. info.DefFile = this->ObjectDirectory /* has slash */ + "exports.def";
  1779. } else if (!info.Sources.empty()) {
  1780. info.DefFile = info.Sources.front()->GetFullPath();
  1781. }
  1782. }
  1783. bool cmGeneratorTarget::IsDLLPlatform() const
  1784. {
  1785. return this->DLLPlatform;
  1786. }
  1787. void cmGeneratorTarget::GetAutoUicOptions(std::vector<std::string>& result,
  1788. const std::string& config) const
  1789. {
  1790. const char* prop =
  1791. this->GetLinkInterfaceDependentStringProperty("AUTOUIC_OPTIONS", config);
  1792. if (!prop) {
  1793. return;
  1794. }
  1795. cmGeneratorExpression ge;
  1796. cmGeneratorExpressionDAGChecker dagChecker(
  1797. this->GetName(), "AUTOUIC_OPTIONS", nullptr, nullptr);
  1798. cmSystemTools::ExpandListArgument(
  1799. ge.Parse(prop)->Evaluate(this->LocalGenerator, config, false, this,
  1800. &dagChecker),
  1801. result);
  1802. }
  1803. void processILibs(const std::string& config,
  1804. cmGeneratorTarget const* headTarget, cmLinkItem const& item,
  1805. cmGlobalGenerator* gg,
  1806. std::vector<cmGeneratorTarget const*>& tgts,
  1807. std::set<cmGeneratorTarget const*>& emitted)
  1808. {
  1809. if (item.Target && emitted.insert(item.Target).second) {
  1810. tgts.push_back(item.Target);
  1811. if (cmLinkInterfaceLibraries const* iface =
  1812. item.Target->GetLinkInterfaceLibraries(config, headTarget, true)) {
  1813. for (cmLinkItem const& lib : iface->Libraries) {
  1814. processILibs(config, headTarget, lib, gg, tgts, emitted);
  1815. }
  1816. }
  1817. }
  1818. }
  1819. const std::vector<const cmGeneratorTarget*>&
  1820. cmGeneratorTarget::GetLinkImplementationClosure(
  1821. const std::string& config) const
  1822. {
  1823. LinkImplClosure& tgts = this->LinkImplClosureMap[config];
  1824. if (!tgts.Done) {
  1825. tgts.Done = true;
  1826. std::set<cmGeneratorTarget const*> emitted;
  1827. cmLinkImplementationLibraries const* impl =
  1828. this->GetLinkImplementationLibraries(config);
  1829. for (cmLinkImplItem const& lib : impl->Libraries) {
  1830. processILibs(config, this, lib,
  1831. this->LocalGenerator->GetGlobalGenerator(), tgts, emitted);
  1832. }
  1833. }
  1834. return tgts;
  1835. }
  1836. class cmTargetTraceDependencies
  1837. {
  1838. public:
  1839. cmTargetTraceDependencies(cmGeneratorTarget* target);
  1840. void Trace();
  1841. private:
  1842. cmGeneratorTarget* GeneratorTarget;
  1843. cmMakefile* Makefile;
  1844. cmLocalGenerator* LocalGenerator;
  1845. cmGlobalGenerator const* GlobalGenerator;
  1846. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  1847. SourceEntry* CurrentEntry;
  1848. std::queue<cmSourceFile*> SourceQueue;
  1849. std::set<cmSourceFile*> SourcesQueued;
  1850. typedef std::map<std::string, cmSourceFile*> NameMapType;
  1851. NameMapType NameMap;
  1852. std::vector<std::string> NewSources;
  1853. void QueueSource(cmSourceFile* sf);
  1854. void FollowName(std::string const& name);
  1855. void FollowNames(std::vector<std::string> const& names);
  1856. bool IsUtility(std::string const& dep);
  1857. void CheckCustomCommand(cmCustomCommand const& cc);
  1858. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  1859. void FollowCommandDepends(cmCustomCommand const& cc,
  1860. const std::string& config,
  1861. std::set<std::string>& emitted);
  1862. };
  1863. cmTargetTraceDependencies::cmTargetTraceDependencies(cmGeneratorTarget* target)
  1864. : GeneratorTarget(target)
  1865. {
  1866. // Convenience.
  1867. this->Makefile = target->Target->GetMakefile();
  1868. this->LocalGenerator = target->GetLocalGenerator();
  1869. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  1870. this->CurrentEntry = nullptr;
  1871. // Queue all the source files already specified for the target.
  1872. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1873. std::vector<std::string> configs;
  1874. this->Makefile->GetConfigurations(configs);
  1875. if (configs.empty()) {
  1876. configs.push_back("");
  1877. }
  1878. std::set<cmSourceFile*> emitted;
  1879. for (std::string const& c : configs) {
  1880. std::vector<cmSourceFile*> sources;
  1881. this->GeneratorTarget->GetSourceFiles(sources, c);
  1882. for (cmSourceFile* sf : sources) {
  1883. const std::set<cmGeneratorTarget const*> tgts =
  1884. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  1885. if (tgts.find(this->GeneratorTarget) != tgts.end()) {
  1886. std::ostringstream e;
  1887. e << "Evaluation output file\n \"" << sf->GetFullPath()
  1888. << "\"\ndepends on the sources of a target it is used in. This "
  1889. "is a dependency loop and is not allowed.";
  1890. this->GeneratorTarget->LocalGenerator->IssueMessage(
  1891. cmake::FATAL_ERROR, e.str());
  1892. return;
  1893. }
  1894. if (emitted.insert(sf).second &&
  1895. this->SourcesQueued.insert(sf).second) {
  1896. this->SourceQueue.push(sf);
  1897. }
  1898. }
  1899. }
  1900. }
  1901. // Queue pre-build, pre-link, and post-build rule dependencies.
  1902. this->CheckCustomCommands(this->GeneratorTarget->GetPreBuildCommands());
  1903. this->CheckCustomCommands(this->GeneratorTarget->GetPreLinkCommands());
  1904. this->CheckCustomCommands(this->GeneratorTarget->GetPostBuildCommands());
  1905. }
  1906. void cmTargetTraceDependencies::Trace()
  1907. {
  1908. // Process one dependency at a time until the queue is empty.
  1909. while (!this->SourceQueue.empty()) {
  1910. // Get the next source from the queue.
  1911. cmSourceFile* sf = this->SourceQueue.front();
  1912. this->SourceQueue.pop();
  1913. this->CurrentEntry = &this->GeneratorTarget->SourceDepends[sf];
  1914. // Queue dependencies added explicitly by the user.
  1915. if (const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS")) {
  1916. std::vector<std::string> objDeps;
  1917. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  1918. for (std::string& objDep : objDeps) {
  1919. if (cmSystemTools::FileIsFullPath(objDep)) {
  1920. objDep = cmSystemTools::CollapseFullPath(objDep);
  1921. }
  1922. }
  1923. this->FollowNames(objDeps);
  1924. }
  1925. // Queue the source needed to generate this file, if any.
  1926. this->FollowName(sf->GetFullPath());
  1927. // Queue dependencies added programatically by commands.
  1928. this->FollowNames(sf->GetDepends());
  1929. // Queue custom command dependencies.
  1930. if (cmCustomCommand const* cc = sf->GetCustomCommand()) {
  1931. this->CheckCustomCommand(*cc);
  1932. }
  1933. }
  1934. this->CurrentEntry = nullptr;
  1935. this->GeneratorTarget->AddTracedSources(this->NewSources);
  1936. }
  1937. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  1938. {
  1939. if (this->SourcesQueued.insert(sf).second) {
  1940. this->SourceQueue.push(sf);
  1941. // Make sure this file is in the target at the end.
  1942. this->NewSources.push_back(sf->GetFullPath());
  1943. }
  1944. }
  1945. void cmTargetTraceDependencies::FollowName(std::string const& name)
  1946. {
  1947. NameMapType::iterator i = this->NameMap.find(name);
  1948. if (i == this->NameMap.end()) {
  1949. // Check if we know how to generate this file.
  1950. cmSourceFile* sf = this->Makefile->GetSourceFileWithOutput(name);
  1951. NameMapType::value_type entry(name, sf);
  1952. i = this->NameMap.insert(entry).first;
  1953. }
  1954. if (cmSourceFile* sf = i->second) {
  1955. // Record the dependency we just followed.
  1956. if (this->CurrentEntry) {
  1957. this->CurrentEntry->Depends.push_back(sf);
  1958. }
  1959. this->QueueSource(sf);
  1960. }
  1961. }
  1962. void cmTargetTraceDependencies::FollowNames(
  1963. std::vector<std::string> const& names)
  1964. {
  1965. for (std::string const& name : names) {
  1966. this->FollowName(name);
  1967. }
  1968. }
  1969. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  1970. {
  1971. // Dependencies on targets (utilities) are supposed to be named by
  1972. // just the target name. However for compatibility we support
  1973. // naming the output file generated by the target (assuming there is
  1974. // no output-name property which old code would not have set). In
  1975. // that case the target name will be the file basename of the
  1976. // dependency.
  1977. std::string util = cmSystemTools::GetFilenameName(dep);
  1978. if (cmSystemTools::GetFilenameLastExtension(util) == ".exe") {
  1979. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  1980. }
  1981. // Check for a target with this name.
  1982. if (cmGeneratorTarget* t =
  1983. this->GeneratorTarget->GetLocalGenerator()->FindGeneratorTargetToUse(
  1984. util)) {
  1985. // If we find the target and the dep was given as a full path,
  1986. // then make sure it was not a full path to something else, and
  1987. // the fact that the name matched a target was just a coincidence.
  1988. if (cmSystemTools::FileIsFullPath(dep.c_str())) {
  1989. if (t->GetType() >= cmStateEnums::EXECUTABLE &&
  1990. t->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  1991. // This is really only for compatibility so we do not need to
  1992. // worry about configuration names and output names.
  1993. std::string tLocation = t->GetLocationForBuild();
  1994. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1995. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  1996. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  1997. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  1998. if (depLocation == tLocation) {
  1999. this->GeneratorTarget->Target->AddUtility(util);
  2000. return true;
  2001. }
  2002. }
  2003. } else {
  2004. // The original name of the dependency was not a full path. It
  2005. // must name a target, so add the target-level dependency.
  2006. this->GeneratorTarget->Target->AddUtility(util);
  2007. return true;
  2008. }
  2009. }
  2010. // The dependency does not name a target built in this project.
  2011. return false;
  2012. }
  2013. void cmTargetTraceDependencies::CheckCustomCommand(cmCustomCommand const& cc)
  2014. {
  2015. // Transform command names that reference targets built in this
  2016. // project to corresponding target-level dependencies.
  2017. cmGeneratorExpression ge(cc.GetBacktrace());
  2018. // Add target-level dependencies referenced by generator expressions.
  2019. std::set<cmGeneratorTarget*> targets;
  2020. for (cmCustomCommandLine const& cCmdLine : cc.GetCommandLines()) {
  2021. std::string const& command = *cCmdLine.begin();
  2022. // Check for a target with this name.
  2023. if (cmGeneratorTarget* t =
  2024. this->LocalGenerator->FindGeneratorTargetToUse(command)) {
  2025. if (t->GetType() == cmStateEnums::EXECUTABLE) {
  2026. // The command refers to an executable target built in
  2027. // this project. Add the target-level dependency to make
  2028. // sure the executable is up to date before this custom
  2029. // command possibly runs.
  2030. this->GeneratorTarget->Target->AddUtility(command);
  2031. }
  2032. }
  2033. // Check for target references in generator expressions.
  2034. for (std::string const& cl : cCmdLine) {
  2035. const std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(cl);
  2036. cge->Evaluate(this->GeneratorTarget->GetLocalGenerator(), "", true);
  2037. std::set<cmGeneratorTarget*> geTargets = cge->GetTargets();
  2038. targets.insert(geTargets.begin(), geTargets.end());
  2039. }
  2040. }
  2041. for (cmGeneratorTarget* target : targets) {
  2042. this->GeneratorTarget->Target->AddUtility(target->GetName());
  2043. }
  2044. // Queue the custom command dependencies.
  2045. std::vector<std::string> configs;
  2046. std::set<std::string> emitted;
  2047. this->Makefile->GetConfigurations(configs);
  2048. if (configs.empty()) {
  2049. configs.push_back("");
  2050. }
  2051. for (std::string const& conf : configs) {
  2052. this->FollowCommandDepends(cc, conf, emitted);
  2053. }
  2054. }
  2055. void cmTargetTraceDependencies::FollowCommandDepends(
  2056. cmCustomCommand const& cc, const std::string& config,
  2057. std::set<std::string>& emitted)
  2058. {
  2059. cmCustomCommandGenerator ccg(cc, config,
  2060. this->GeneratorTarget->LocalGenerator);
  2061. const std::vector<std::string>& depends = ccg.GetDepends();
  2062. for (std::string const& dep : depends) {
  2063. if (emitted.insert(dep).second) {
  2064. if (!this->IsUtility(dep)) {
  2065. // The dependency does not name a target and may be a file we
  2066. // know how to generate. Queue it.
  2067. this->FollowName(dep);
  2068. }
  2069. }
  2070. }
  2071. }
  2072. void cmTargetTraceDependencies::CheckCustomCommands(
  2073. const std::vector<cmCustomCommand>& commands)
  2074. {
  2075. for (cmCustomCommand const& command : commands) {
  2076. this->CheckCustomCommand(command);
  2077. }
  2078. }
  2079. void cmGeneratorTarget::TraceDependencies()
  2080. {
  2081. // CMake-generated targets have no dependencies to trace. Normally tracing
  2082. // would find nothing anyway, but when building CMake itself the "install"
  2083. // target command ends up referencing the "cmake" target but we do not
  2084. // really want the dependency because "install" depend on "all" anyway.
  2085. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2086. return;
  2087. }
  2088. // Use a helper object to trace the dependencies.
  2089. cmTargetTraceDependencies tracer(this);
  2090. tracer.Trace();
  2091. }
  2092. std::string cmGeneratorTarget::GetCompilePDBDirectory(
  2093. const std::string& config) const
  2094. {
  2095. if (CompileInfo const* info = this->GetCompileInfo(config)) {
  2096. return info->CompilePdbDir;
  2097. }
  2098. return "";
  2099. }
  2100. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  2101. std::vector<std::string>& archVec) const
  2102. {
  2103. const char* archs = nullptr;
  2104. if (!config.empty()) {
  2105. std::string defVarName = "OSX_ARCHITECTURES_";
  2106. defVarName += cmSystemTools::UpperCase(config);
  2107. archs = this->GetProperty(defVarName);
  2108. }
  2109. if (!archs) {
  2110. archs = this->GetProperty("OSX_ARCHITECTURES");
  2111. }
  2112. if (archs) {
  2113. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  2114. }
  2115. }
  2116. //----------------------------------------------------------------------------
  2117. std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
  2118. std::string const& var, std::string const& lang,
  2119. std::string const& config) const
  2120. {
  2121. if (this->IsIPOEnabled(lang, config)) {
  2122. std::string varIPO = var + "_IPO";
  2123. if (this->Makefile->IsDefinitionSet(varIPO)) {
  2124. return varIPO;
  2125. }
  2126. }
  2127. return var;
  2128. }
  2129. //----------------------------------------------------------------------------
  2130. std::string cmGeneratorTarget::GetCreateRuleVariable(
  2131. std::string const& lang, std::string const& config) const
  2132. {
  2133. switch (this->GetType()) {
  2134. case cmStateEnums::STATIC_LIBRARY: {
  2135. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  2136. return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
  2137. }
  2138. case cmStateEnums::SHARED_LIBRARY:
  2139. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  2140. case cmStateEnums::MODULE_LIBRARY:
  2141. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  2142. case cmStateEnums::EXECUTABLE:
  2143. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  2144. default:
  2145. break;
  2146. }
  2147. return "";
  2148. }
  2149. static void processIncludeDirectories(
  2150. cmGeneratorTarget const* tgt,
  2151. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2152. std::vector<std::string>& includes,
  2153. std::unordered_set<std::string>& uniqueIncludes,
  2154. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2155. bool debugIncludes, const std::string& language)
  2156. {
  2157. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  2158. cmLinkImplItem const& item = entry->LinkImplItem;
  2159. std::string const& targetName = item;
  2160. bool const fromImported = item.Target && item.Target->IsImported();
  2161. bool const checkCMP0027 = item.FromGenex;
  2162. std::vector<std::string> entryIncludes;
  2163. cmSystemTools::ExpandListArgument(
  2164. entry->ge->Evaluate(tgt->GetLocalGenerator(), config, false, tgt,
  2165. dagChecker, language),
  2166. entryIncludes);
  2167. std::string usedIncludes;
  2168. for (std::string& entryInclude : entryIncludes) {
  2169. if (fromImported && !cmSystemTools::FileExists(entryInclude.c_str())) {
  2170. std::ostringstream e;
  2171. cmake::MessageType messageType = cmake::FATAL_ERROR;
  2172. if (checkCMP0027) {
  2173. switch (tgt->GetPolicyStatusCMP0027()) {
  2174. case cmPolicies::WARN:
  2175. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0027) << "\n";
  2176. CM_FALLTHROUGH;
  2177. case cmPolicies::OLD:
  2178. messageType = cmake::AUTHOR_WARNING;
  2179. break;
  2180. case cmPolicies::REQUIRED_ALWAYS:
  2181. case cmPolicies::REQUIRED_IF_USED:
  2182. case cmPolicies::NEW:
  2183. break;
  2184. }
  2185. }
  2186. /* clang-format off */
  2187. e << "Imported target \"" << targetName << "\" includes "
  2188. "non-existent path\n \"" << entryInclude << "\"\nin its "
  2189. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  2190. "* The path was deleted, renamed, or moved to another "
  2191. "location.\n"
  2192. "* An install or uninstall procedure did not complete "
  2193. "successfully.\n"
  2194. "* The installation package was faulty and references files it "
  2195. "does not provide.\n";
  2196. /* clang-format on */
  2197. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2198. return;
  2199. }
  2200. if (!cmSystemTools::FileIsFullPath(entryInclude.c_str())) {
  2201. std::ostringstream e;
  2202. bool noMessage = false;
  2203. cmake::MessageType messageType = cmake::FATAL_ERROR;
  2204. if (!targetName.empty()) {
  2205. /* clang-format off */
  2206. e << "Target \"" << targetName << "\" contains relative "
  2207. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  2208. " \"" << entryInclude << "\"";
  2209. /* clang-format on */
  2210. } else {
  2211. switch (tgt->GetPolicyStatusCMP0021()) {
  2212. case cmPolicies::WARN: {
  2213. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  2214. messageType = cmake::AUTHOR_WARNING;
  2215. } break;
  2216. case cmPolicies::OLD:
  2217. noMessage = true;
  2218. case cmPolicies::REQUIRED_IF_USED:
  2219. case cmPolicies::REQUIRED_ALWAYS:
  2220. case cmPolicies::NEW:
  2221. // Issue the fatal message.
  2222. break;
  2223. }
  2224. e << "Found relative path while evaluating include directories of "
  2225. "\""
  2226. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  2227. }
  2228. if (!noMessage) {
  2229. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2230. if (messageType == cmake::FATAL_ERROR) {
  2231. return;
  2232. }
  2233. }
  2234. }
  2235. if (!cmSystemTools::IsOff(entryInclude.c_str())) {
  2236. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  2237. }
  2238. std::string inc = entryInclude;
  2239. if (uniqueIncludes.insert(inc).second) {
  2240. includes.push_back(inc);
  2241. if (debugIncludes) {
  2242. usedIncludes += " * " + inc + "\n";
  2243. }
  2244. }
  2245. }
  2246. if (!usedIncludes.empty()) {
  2247. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2248. cmake::LOG, std::string("Used includes for target ") + tgt->GetName() +
  2249. ":\n" + usedIncludes,
  2250. entry->ge->GetBacktrace());
  2251. }
  2252. }
  2253. }
  2254. std::vector<std::string> cmGeneratorTarget::GetIncludeDirectories(
  2255. const std::string& config, const std::string& lang) const
  2256. {
  2257. std::vector<std::string> includes;
  2258. std::unordered_set<std::string> uniqueIncludes;
  2259. cmGeneratorExpressionDAGChecker dagChecker(
  2260. this->GetName(), "INCLUDE_DIRECTORIES", nullptr, nullptr);
  2261. std::vector<std::string> debugProperties;
  2262. const char* debugProp =
  2263. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2264. if (debugProp) {
  2265. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2266. }
  2267. bool debugIncludes = !this->DebugIncludesDone &&
  2268. std::find(debugProperties.begin(), debugProperties.end(),
  2269. "INCLUDE_DIRECTORIES") != debugProperties.end();
  2270. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2271. this->DebugIncludesDone = true;
  2272. }
  2273. processIncludeDirectories(this, this->IncludeDirectoriesEntries, includes,
  2274. uniqueIncludes, &dagChecker, config, debugIncludes,
  2275. lang);
  2276. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2277. linkInterfaceIncludeDirectoriesEntries;
  2278. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES",
  2279. linkInterfaceIncludeDirectoriesEntries);
  2280. if (this->Makefile->IsOn("APPLE")) {
  2281. cmLinkImplementationLibraries const* impl =
  2282. this->GetLinkImplementationLibraries(config);
  2283. for (cmLinkImplItem const& lib : impl->Libraries) {
  2284. std::string libDir = cmSystemTools::CollapseFullPath(lib);
  2285. static cmsys::RegularExpression frameworkCheck(
  2286. "(.*\\.framework)(/Versions/[^/]+)?/[^/]+$");
  2287. if (!frameworkCheck.find(libDir)) {
  2288. continue;
  2289. }
  2290. libDir = frameworkCheck.match(1);
  2291. cmGeneratorExpression ge;
  2292. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  2293. ge.Parse(libDir.c_str());
  2294. linkInterfaceIncludeDirectoriesEntries.push_back(
  2295. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge)));
  2296. }
  2297. }
  2298. processIncludeDirectories(this, linkInterfaceIncludeDirectoriesEntries,
  2299. includes, uniqueIncludes, &dagChecker, config,
  2300. debugIncludes, lang);
  2301. cmDeleteAll(linkInterfaceIncludeDirectoriesEntries);
  2302. return includes;
  2303. }
  2304. static void processCompileOptionsInternal(
  2305. cmGeneratorTarget const* tgt,
  2306. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2307. std::vector<std::string>& options,
  2308. std::unordered_set<std::string>& uniqueOptions,
  2309. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2310. bool debugOptions, const char* logName, std::string const& language)
  2311. {
  2312. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  2313. std::vector<std::string> entryOptions;
  2314. cmSystemTools::ExpandListArgument(
  2315. entry->ge->Evaluate(tgt->GetLocalGenerator(), config, false, tgt,
  2316. dagChecker, language),
  2317. entryOptions);
  2318. std::string usedOptions;
  2319. for (std::string const& opt : entryOptions) {
  2320. if (uniqueOptions.insert(opt).second) {
  2321. options.push_back(opt);
  2322. if (debugOptions) {
  2323. usedOptions += " * " + opt + "\n";
  2324. }
  2325. }
  2326. }
  2327. if (!usedOptions.empty()) {
  2328. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2329. cmake::LOG, std::string("Used compile ") + logName +
  2330. std::string(" for target ") + tgt->GetName() + ":\n" + usedOptions,
  2331. entry->ge->GetBacktrace());
  2332. }
  2333. }
  2334. }
  2335. static void processCompileOptions(
  2336. cmGeneratorTarget const* tgt,
  2337. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2338. std::vector<std::string>& options,
  2339. std::unordered_set<std::string>& uniqueOptions,
  2340. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2341. bool debugOptions, std::string const& language)
  2342. {
  2343. processCompileOptionsInternal(tgt, entries, options, uniqueOptions,
  2344. dagChecker, config, debugOptions, "options",
  2345. language);
  2346. }
  2347. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  2348. const std::string& config,
  2349. const std::string& language) const
  2350. {
  2351. std::unordered_set<std::string> uniqueOptions;
  2352. cmGeneratorExpressionDAGChecker dagChecker(
  2353. this->GetName(), "COMPILE_OPTIONS", nullptr, nullptr);
  2354. std::vector<std::string> debugProperties;
  2355. const char* debugProp =
  2356. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2357. if (debugProp) {
  2358. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2359. }
  2360. bool debugOptions = !this->DebugCompileOptionsDone &&
  2361. std::find(debugProperties.begin(), debugProperties.end(),
  2362. "COMPILE_OPTIONS") != debugProperties.end();
  2363. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2364. this->DebugCompileOptionsDone = true;
  2365. }
  2366. processCompileOptions(this, this->CompileOptionsEntries, result,
  2367. uniqueOptions, &dagChecker, config, debugOptions,
  2368. language);
  2369. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2370. linkInterfaceCompileOptionsEntries;
  2371. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS",
  2372. linkInterfaceCompileOptionsEntries);
  2373. processCompileOptions(this, linkInterfaceCompileOptionsEntries, result,
  2374. uniqueOptions, &dagChecker, config, debugOptions,
  2375. language);
  2376. cmDeleteAll(linkInterfaceCompileOptionsEntries);
  2377. }
  2378. static void processCompileFeatures(
  2379. cmGeneratorTarget const* tgt,
  2380. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2381. std::vector<std::string>& options,
  2382. std::unordered_set<std::string>& uniqueOptions,
  2383. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2384. bool debugOptions)
  2385. {
  2386. processCompileOptionsInternal(tgt, entries, options, uniqueOptions,
  2387. dagChecker, config, debugOptions, "features",
  2388. std::string());
  2389. }
  2390. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  2391. const std::string& config) const
  2392. {
  2393. std::unordered_set<std::string> uniqueFeatures;
  2394. cmGeneratorExpressionDAGChecker dagChecker(
  2395. this->GetName(), "COMPILE_FEATURES", nullptr, nullptr);
  2396. std::vector<std::string> debugProperties;
  2397. const char* debugProp =
  2398. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2399. if (debugProp) {
  2400. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2401. }
  2402. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  2403. std::find(debugProperties.begin(), debugProperties.end(),
  2404. "COMPILE_FEATURES") != debugProperties.end();
  2405. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2406. this->DebugCompileFeaturesDone = true;
  2407. }
  2408. processCompileFeatures(this, this->CompileFeaturesEntries, result,
  2409. uniqueFeatures, &dagChecker, config, debugFeatures);
  2410. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2411. linkInterfaceCompileFeaturesEntries;
  2412. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  2413. linkInterfaceCompileFeaturesEntries);
  2414. processCompileFeatures(this, linkInterfaceCompileFeaturesEntries, result,
  2415. uniqueFeatures, &dagChecker, config, debugFeatures);
  2416. cmDeleteAll(linkInterfaceCompileFeaturesEntries);
  2417. }
  2418. static void processCompileDefinitions(
  2419. cmGeneratorTarget const* tgt,
  2420. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2421. std::vector<std::string>& options,
  2422. std::unordered_set<std::string>& uniqueOptions,
  2423. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2424. bool debugOptions, std::string const& language)
  2425. {
  2426. processCompileOptionsInternal(tgt, entries, options, uniqueOptions,
  2427. dagChecker, config, debugOptions,
  2428. "definitions", language);
  2429. }
  2430. void cmGeneratorTarget::GetCompileDefinitions(
  2431. std::vector<std::string>& list, const std::string& config,
  2432. const std::string& language) const
  2433. {
  2434. std::unordered_set<std::string> uniqueOptions;
  2435. cmGeneratorExpressionDAGChecker dagChecker(
  2436. this->GetName(), "COMPILE_DEFINITIONS", nullptr, nullptr);
  2437. std::vector<std::string> debugProperties;
  2438. const char* debugProp =
  2439. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2440. if (debugProp) {
  2441. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2442. }
  2443. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  2444. std::find(debugProperties.begin(), debugProperties.end(),
  2445. "COMPILE_DEFINITIONS") != debugProperties.end();
  2446. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2447. this->DebugCompileDefinitionsDone = true;
  2448. }
  2449. processCompileDefinitions(this, this->CompileDefinitionsEntries, list,
  2450. uniqueOptions, &dagChecker, config, debugDefines,
  2451. language);
  2452. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2453. linkInterfaceCompileDefinitionsEntries;
  2454. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS",
  2455. linkInterfaceCompileDefinitionsEntries);
  2456. if (!config.empty()) {
  2457. std::string configPropName =
  2458. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  2459. const char* configProp = this->GetProperty(configPropName);
  2460. if (configProp) {
  2461. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  2462. case cmPolicies::WARN: {
  2463. std::ostringstream e;
  2464. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0043);
  2465. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2466. CM_FALLTHROUGH;
  2467. }
  2468. case cmPolicies::OLD: {
  2469. cmGeneratorExpression ge;
  2470. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  2471. ge.Parse(configProp);
  2472. linkInterfaceCompileDefinitionsEntries.push_back(
  2473. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge)));
  2474. } break;
  2475. case cmPolicies::NEW:
  2476. case cmPolicies::REQUIRED_ALWAYS:
  2477. case cmPolicies::REQUIRED_IF_USED:
  2478. break;
  2479. }
  2480. }
  2481. }
  2482. processCompileDefinitions(this, linkInterfaceCompileDefinitionsEntries, list,
  2483. uniqueOptions, &dagChecker, config, debugDefines,
  2484. language);
  2485. cmDeleteAll(linkInterfaceCompileDefinitionsEntries);
  2486. }
  2487. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  2488. {
  2489. if (this->IsImported()) {
  2490. return;
  2491. }
  2492. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  2493. // Get the names.
  2494. std::string name;
  2495. std::string soName;
  2496. std::string realName;
  2497. std::string impName;
  2498. std::string pdbName;
  2499. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2500. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2501. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2502. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2503. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2504. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2505. } else {
  2506. return;
  2507. }
  2508. // Get the directory.
  2509. std::string dir =
  2510. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  2511. // Add each name.
  2512. std::string f;
  2513. if (!name.empty()) {
  2514. f = dir;
  2515. f += "/";
  2516. f += name;
  2517. gg->AddToManifest(f);
  2518. }
  2519. if (!soName.empty()) {
  2520. f = dir;
  2521. f += "/";
  2522. f += soName;
  2523. gg->AddToManifest(f);
  2524. }
  2525. if (!realName.empty()) {
  2526. f = dir;
  2527. f += "/";
  2528. f += realName;
  2529. gg->AddToManifest(f);
  2530. }
  2531. if (!pdbName.empty()) {
  2532. f = dir;
  2533. f += "/";
  2534. f += pdbName;
  2535. gg->AddToManifest(f);
  2536. }
  2537. if (!impName.empty()) {
  2538. f = this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  2539. f += "/";
  2540. f += impName;
  2541. gg->AddToManifest(f);
  2542. }
  2543. }
  2544. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  2545. {
  2546. std::vector<std::string> features;
  2547. this->GetCompileFeatures(features, config);
  2548. for (std::string const& f : features) {
  2549. if (!this->Makefile->AddRequiredTargetFeature(this->Target, f)) {
  2550. return false;
  2551. }
  2552. }
  2553. return true;
  2554. }
  2555. std::string cmGeneratorTarget::GetImportedLibName(
  2556. std::string const& config) const
  2557. {
  2558. if (cmGeneratorTarget::ImportInfo const* info =
  2559. this->GetImportInfo(config)) {
  2560. return info->LibName;
  2561. }
  2562. return std::string();
  2563. }
  2564. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  2565. cmStateEnums::ArtifactType artifact,
  2566. bool realname) const
  2567. {
  2568. if (this->IsImported()) {
  2569. return this->Target->ImportedGetFullPath(config, artifact);
  2570. }
  2571. return this->NormalGetFullPath(config, artifact, realname);
  2572. }
  2573. std::string cmGeneratorTarget::NormalGetFullPath(
  2574. const std::string& config, cmStateEnums::ArtifactType artifact,
  2575. bool realname) const
  2576. {
  2577. std::string fpath = this->GetDirectory(config, artifact);
  2578. fpath += "/";
  2579. if (this->IsAppBundleOnApple()) {
  2580. fpath = this->BuildBundleDirectory(fpath, config, FullLevel);
  2581. fpath += "/";
  2582. }
  2583. // Add the full name of the target.
  2584. switch (artifact) {
  2585. case cmStateEnums::RuntimeBinaryArtifact:
  2586. if (realname) {
  2587. fpath += this->NormalGetRealName(config);
  2588. } else {
  2589. fpath +=
  2590. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  2591. }
  2592. break;
  2593. case cmStateEnums::ImportLibraryArtifact:
  2594. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  2595. break;
  2596. }
  2597. return fpath;
  2598. }
  2599. std::string cmGeneratorTarget::NormalGetRealName(
  2600. const std::string& config) const
  2601. {
  2602. // This should not be called for imported targets.
  2603. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2604. // enforcement of the limited imported target API.
  2605. if (this->IsImported()) {
  2606. std::string msg = "NormalGetRealName called on imported target: ";
  2607. msg += this->GetName();
  2608. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2609. }
  2610. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2611. // Compute the real name that will be built.
  2612. std::string name;
  2613. std::string realName;
  2614. std::string impName;
  2615. std::string pdbName;
  2616. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2617. return realName;
  2618. }
  2619. // Compute the real name that will be built.
  2620. std::string name;
  2621. std::string soName;
  2622. std::string realName;
  2623. std::string impName;
  2624. std::string pdbName;
  2625. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2626. return realName;
  2627. }
  2628. void cmGeneratorTarget::GetLibraryNames(std::string& name, std::string& soName,
  2629. std::string& realName,
  2630. std::string& impName,
  2631. std::string& pdbName,
  2632. const std::string& config) const
  2633. {
  2634. // This should not be called for imported targets.
  2635. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2636. // enforcement of the limited imported target API.
  2637. if (this->IsImported()) {
  2638. std::string msg = "GetLibraryNames called on imported target: ";
  2639. msg += this->GetName();
  2640. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2641. return;
  2642. }
  2643. // Check for library version properties.
  2644. const char* version = this->GetProperty("VERSION");
  2645. const char* soversion = this->GetProperty("SOVERSION");
  2646. if (!this->HasSOName(config) ||
  2647. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  2648. this->IsFrameworkOnApple()) {
  2649. // Versioning is supported only for shared libraries and modules,
  2650. // and then only when the platform supports an soname flag.
  2651. version = nullptr;
  2652. soversion = nullptr;
  2653. }
  2654. if (version && !soversion) {
  2655. // The soversion must be set if the library version is set. Use
  2656. // the library version as the soversion.
  2657. soversion = version;
  2658. }
  2659. if (!version && soversion) {
  2660. // Use the soversion as the library version.
  2661. version = soversion;
  2662. }
  2663. // Get the components of the library name.
  2664. std::string prefix;
  2665. std::string base;
  2666. std::string suffix;
  2667. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2668. prefix, base, suffix);
  2669. // The library name.
  2670. name = prefix + base + suffix;
  2671. if (this->IsFrameworkOnApple()) {
  2672. realName = prefix;
  2673. if (!this->Makefile->PlatformIsAppleIos()) {
  2674. realName += "Versions/";
  2675. realName += this->GetFrameworkVersion();
  2676. realName += "/";
  2677. }
  2678. realName += base;
  2679. soName = realName;
  2680. } else {
  2681. // The library's soname.
  2682. this->ComputeVersionedName(soName, prefix, base, suffix, name, soversion);
  2683. // The library's real name on disk.
  2684. this->ComputeVersionedName(realName, prefix, base, suffix, name, version);
  2685. }
  2686. // The import library name.
  2687. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2688. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2689. impName =
  2690. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2691. } else {
  2692. impName.clear();
  2693. }
  2694. // The program database file name.
  2695. pdbName = this->GetPDBName(config);
  2696. }
  2697. void cmGeneratorTarget::GetExecutableNames(std::string& name,
  2698. std::string& realName,
  2699. std::string& impName,
  2700. std::string& pdbName,
  2701. const std::string& config) const
  2702. {
  2703. // This should not be called for imported targets.
  2704. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2705. // enforcement of the limited imported target API.
  2706. if (this->IsImported()) {
  2707. std::string msg = "GetExecutableNames called on imported target: ";
  2708. msg += this->GetName();
  2709. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2710. }
  2711. // This versioning is supported only for executables and then only
  2712. // when the platform supports symbolic links.
  2713. #if defined(_WIN32) && !defined(__CYGWIN__)
  2714. const char* version = 0;
  2715. #else
  2716. // Check for executable version properties.
  2717. const char* version = this->GetProperty("VERSION");
  2718. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  2719. this->Makefile->IsOn("XCODE")) {
  2720. version = nullptr;
  2721. }
  2722. #endif
  2723. // Get the components of the executable name.
  2724. std::string prefix;
  2725. std::string base;
  2726. std::string suffix;
  2727. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2728. prefix, base, suffix);
  2729. // The executable name.
  2730. name = prefix + base + suffix;
  2731. // The executable's real name on disk.
  2732. #if defined(__CYGWIN__)
  2733. realName = prefix + base;
  2734. #else
  2735. realName = name;
  2736. #endif
  2737. if (version) {
  2738. realName += "-";
  2739. realName += version;
  2740. }
  2741. #if defined(__CYGWIN__)
  2742. realName += suffix;
  2743. #endif
  2744. // The import library name.
  2745. impName =
  2746. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2747. // The program database file name.
  2748. pdbName = this->GetPDBName(config);
  2749. }
  2750. std::string cmGeneratorTarget::GetFullNameInternal(
  2751. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2752. {
  2753. std::string prefix;
  2754. std::string base;
  2755. std::string suffix;
  2756. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  2757. return prefix + base + suffix;
  2758. }
  2759. const char* cmGeneratorTarget::ImportedGetLocation(
  2760. const std::string& config) const
  2761. {
  2762. static std::string location;
  2763. assert(this->IsImported());
  2764. location = this->Target->ImportedGetFullPath(
  2765. config, cmStateEnums::RuntimeBinaryArtifact);
  2766. return location.c_str();
  2767. }
  2768. std::string cmGeneratorTarget::GetFullNameImported(
  2769. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2770. {
  2771. return cmSystemTools::GetFilenameName(
  2772. this->Target->ImportedGetFullPath(config, artifact));
  2773. }
  2774. void cmGeneratorTarget::GetFullNameInternal(
  2775. const std::string& config, cmStateEnums::ArtifactType artifact,
  2776. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  2777. {
  2778. // Use just the target name for non-main target types.
  2779. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  2780. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2781. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2782. this->GetType() != cmStateEnums::EXECUTABLE) {
  2783. outPrefix.clear();
  2784. outBase = this->GetName();
  2785. outSuffix.clear();
  2786. return;
  2787. }
  2788. const bool isImportedLibraryArtifact =
  2789. (artifact == cmStateEnums::ImportLibraryArtifact);
  2790. // Return an empty name for the import library if this platform
  2791. // does not support import libraries.
  2792. if (isImportedLibraryArtifact &&
  2793. !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  2794. outPrefix.clear();
  2795. outBase.clear();
  2796. outSuffix.clear();
  2797. return;
  2798. }
  2799. // The implib option is only allowed for shared libraries, module
  2800. // libraries, and executables.
  2801. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2802. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2803. this->GetType() != cmStateEnums::EXECUTABLE) {
  2804. artifact = cmStateEnums::RuntimeBinaryArtifact;
  2805. }
  2806. // Compute the full name for main target types.
  2807. const char* targetPrefix =
  2808. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
  2809. : this->GetProperty("PREFIX"));
  2810. const char* targetSuffix =
  2811. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
  2812. : this->GetProperty("SUFFIX"));
  2813. const char* configPostfix = nullptr;
  2814. if (!config.empty()) {
  2815. std::string configProp = cmSystemTools::UpperCase(config);
  2816. configProp += "_POSTFIX";
  2817. configPostfix = this->GetProperty(configProp);
  2818. // Mac application bundles and frameworks have no postfix.
  2819. if (configPostfix &&
  2820. (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
  2821. configPostfix = nullptr;
  2822. }
  2823. }
  2824. const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
  2825. const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
  2826. // Check for language-specific default prefix and suffix.
  2827. std::string ll = this->GetLinkerLanguage(config);
  2828. if (!ll.empty()) {
  2829. if (!targetSuffix && suffixVar && *suffixVar) {
  2830. std::string langSuff = suffixVar + std::string("_") + ll;
  2831. targetSuffix = this->Makefile->GetDefinition(langSuff);
  2832. }
  2833. if (!targetPrefix && prefixVar && *prefixVar) {
  2834. std::string langPrefix = prefixVar + std::string("_") + ll;
  2835. targetPrefix = this->Makefile->GetDefinition(langPrefix);
  2836. }
  2837. }
  2838. // if there is no prefix on the target use the cmake definition
  2839. if (!targetPrefix && prefixVar) {
  2840. targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
  2841. }
  2842. // if there is no suffix on the target use the cmake definition
  2843. if (!targetSuffix && suffixVar) {
  2844. targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
  2845. }
  2846. // frameworks have directory prefix but no suffix
  2847. std::string fw_prefix;
  2848. if (this->IsFrameworkOnApple()) {
  2849. fw_prefix = this->GetFrameworkDirectory(config, ContentLevel);
  2850. fw_prefix += "/";
  2851. targetPrefix = fw_prefix.c_str();
  2852. targetSuffix = nullptr;
  2853. }
  2854. if (this->IsCFBundleOnApple()) {
  2855. fw_prefix = this->GetCFBundleDirectory(config, FullLevel);
  2856. fw_prefix += "/";
  2857. targetPrefix = fw_prefix.c_str();
  2858. targetSuffix = nullptr;
  2859. }
  2860. // Begin the final name with the prefix.
  2861. outPrefix = targetPrefix ? targetPrefix : "";
  2862. // Append the target name or property-specified name.
  2863. outBase += this->GetOutputName(config, artifact);
  2864. // Append the per-configuration postfix.
  2865. outBase += configPostfix ? configPostfix : "";
  2866. // Name shared libraries with their version number on some platforms.
  2867. if (const char* soversion = this->GetProperty("SOVERSION")) {
  2868. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  2869. !isImportedLibraryArtifact &&
  2870. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  2871. outBase += "-";
  2872. outBase += soversion;
  2873. }
  2874. }
  2875. // Append the suffix.
  2876. outSuffix = targetSuffix ? targetSuffix : "";
  2877. }
  2878. std::string cmGeneratorTarget::GetLinkerLanguage(
  2879. const std::string& config) const
  2880. {
  2881. return this->GetLinkClosure(config)->LinkerLanguage;
  2882. }
  2883. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  2884. {
  2885. std::string prefix;
  2886. std::string base;
  2887. std::string suffix;
  2888. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2889. prefix, base, suffix);
  2890. std::vector<std::string> props;
  2891. std::string configUpper = cmSystemTools::UpperCase(config);
  2892. if (!configUpper.empty()) {
  2893. // PDB_NAME_<CONFIG>
  2894. props.push_back("PDB_NAME_" + configUpper);
  2895. }
  2896. // PDB_NAME
  2897. props.push_back("PDB_NAME");
  2898. for (std::string const& p : props) {
  2899. if (const char* outName = this->GetProperty(p)) {
  2900. base = outName;
  2901. break;
  2902. }
  2903. }
  2904. return prefix + base + ".pdb";
  2905. }
  2906. std::string cmGeneratorTarget::GetObjectDirectory(
  2907. std::string const& config) const
  2908. {
  2909. std::string obj_dir =
  2910. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  2911. #if defined(__APPLE__)
  2912. // find and replace $(PROJECT_NAME) xcode placeholder
  2913. const std::string projectName = this->LocalGenerator->GetProjectName();
  2914. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  2915. #endif
  2916. return obj_dir;
  2917. }
  2918. void cmGeneratorTarget::GetTargetObjectNames(
  2919. std::string const& config, std::vector<std::string>& objects) const
  2920. {
  2921. std::vector<cmSourceFile const*> objectSources;
  2922. this->GetObjectSources(objectSources, config);
  2923. std::map<cmSourceFile const*, std::string> mapping;
  2924. for (cmSourceFile const* sf : objectSources) {
  2925. mapping[sf];
  2926. }
  2927. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  2928. for (cmSourceFile const* src : objectSources) {
  2929. // Find the object file name corresponding to this source file.
  2930. std::map<cmSourceFile const*, std::string>::const_iterator map_it =
  2931. mapping.find(src);
  2932. // It must exist because we populated the mapping just above.
  2933. assert(!map_it->second.empty());
  2934. objects.push_back(map_it->second);
  2935. }
  2936. }
  2937. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  2938. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  2939. {
  2940. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  2941. if (nameResult == 0) {
  2942. return strcmp(t1->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  2943. t2->GetLocalGenerator()->GetCurrentBinaryDirectory()) < 0;
  2944. }
  2945. return nameResult < 0;
  2946. }
  2947. struct cmGeneratorTarget::SourceFileFlags
  2948. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  2949. {
  2950. struct SourceFileFlags flags;
  2951. this->ConstructSourceFileFlags();
  2952. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  2953. this->SourceFlagsMap.find(sf);
  2954. if (si != this->SourceFlagsMap.end()) {
  2955. flags = si->second;
  2956. } else {
  2957. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  2958. // were not listed in one of the other lists.
  2959. if (const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  2960. flags.MacFolder = location;
  2961. const bool stripResources =
  2962. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  2963. if (strcmp(location, "Resources") == 0) {
  2964. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  2965. if (stripResources) {
  2966. flags.MacFolder = "";
  2967. }
  2968. } else if (cmSystemTools::StringStartsWith(location, "Resources/")) {
  2969. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  2970. if (stripResources) {
  2971. flags.MacFolder += strlen("Resources/");
  2972. }
  2973. } else {
  2974. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  2975. }
  2976. }
  2977. }
  2978. return flags;
  2979. }
  2980. void cmGeneratorTarget::ConstructSourceFileFlags() const
  2981. {
  2982. if (this->SourceFileFlagsConstructed) {
  2983. return;
  2984. }
  2985. this->SourceFileFlagsConstructed = true;
  2986. // Process public headers to mark the source files.
  2987. if (const char* files = this->GetProperty("PUBLIC_HEADER")) {
  2988. std::vector<std::string> relFiles;
  2989. cmSystemTools::ExpandListArgument(files, relFiles);
  2990. for (std::string const& relFile : relFiles) {
  2991. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  2992. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  2993. flags.MacFolder = "Headers";
  2994. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  2995. }
  2996. }
  2997. }
  2998. // Process private headers after public headers so that they take
  2999. // precedence if a file is listed in both.
  3000. if (const char* files = this->GetProperty("PRIVATE_HEADER")) {
  3001. std::vector<std::string> relFiles;
  3002. cmSystemTools::ExpandListArgument(files, relFiles);
  3003. for (std::string const& relFile : relFiles) {
  3004. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3005. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3006. flags.MacFolder = "PrivateHeaders";
  3007. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  3008. }
  3009. }
  3010. }
  3011. // Mark sources listed as resources.
  3012. if (const char* files = this->GetProperty("RESOURCE")) {
  3013. std::vector<std::string> relFiles;
  3014. cmSystemTools::ExpandListArgument(files, relFiles);
  3015. for (std::string const& relFile : relFiles) {
  3016. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3017. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3018. flags.MacFolder = "";
  3019. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  3020. flags.MacFolder = "Resources";
  3021. }
  3022. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3023. }
  3024. }
  3025. }
  3026. }
  3027. const cmGeneratorTarget::CompatibleInterfacesBase&
  3028. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  3029. {
  3030. cmGeneratorTarget::CompatibleInterfaces& compat =
  3031. this->CompatibleInterfacesMap[config];
  3032. if (!compat.Done) {
  3033. compat.Done = true;
  3034. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  3035. compat.PropsString.insert("AUTOUIC_OPTIONS");
  3036. std::vector<cmGeneratorTarget const*> const& deps =
  3037. this->GetLinkImplementationClosure(config);
  3038. for (cmGeneratorTarget const* li : deps) {
  3039. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  3040. if (const char* prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  3041. std::vector<std::string> props; \
  3042. cmSystemTools::ExpandListArgument(prop, props); \
  3043. compat.Props##x.insert(props.begin(), props.end()); \
  3044. }
  3045. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  3046. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  3047. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  3048. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  3049. #undef CM_READ_COMPATIBLE_INTERFACE
  3050. }
  3051. }
  3052. return compat;
  3053. }
  3054. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  3055. const std::string& p, const std::string& config) const
  3056. {
  3057. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3058. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3059. return false;
  3060. }
  3061. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  3062. }
  3063. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  3064. const std::string& p, const std::string& config) const
  3065. {
  3066. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3067. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3068. return false;
  3069. }
  3070. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  3071. }
  3072. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  3073. const std::string& p, const std::string& config) const
  3074. {
  3075. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3076. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3077. return false;
  3078. }
  3079. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  3080. }
  3081. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  3082. const std::string& p, const std::string& config) const
  3083. {
  3084. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3085. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3086. return false;
  3087. }
  3088. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  3089. }
  3090. enum CompatibleType
  3091. {
  3092. BoolType,
  3093. StringType,
  3094. NumberMinType,
  3095. NumberMaxType
  3096. };
  3097. template <typename PropertyType>
  3098. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3099. const std::string& prop,
  3100. const std::string& config,
  3101. CompatibleType, PropertyType*);
  3102. template <>
  3103. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3104. const std::string& prop,
  3105. const std::string& config,
  3106. CompatibleType /*unused*/,
  3107. bool* /*unused*/)
  3108. {
  3109. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  3110. }
  3111. template <>
  3112. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3113. const std::string& prop,
  3114. const std::string& config,
  3115. CompatibleType t,
  3116. const char** /*unused*/)
  3117. {
  3118. switch (t) {
  3119. case BoolType:
  3120. assert(false &&
  3121. "String compatibility check function called for boolean");
  3122. return nullptr;
  3123. case StringType:
  3124. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  3125. case NumberMinType:
  3126. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  3127. case NumberMaxType:
  3128. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  3129. }
  3130. assert(false && "Unreachable!");
  3131. return nullptr;
  3132. }
  3133. template <typename PropertyType>
  3134. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  3135. cmGeneratorTarget const* dependee,
  3136. const std::string& propName,
  3137. std::set<std::string>& emitted,
  3138. const std::string& config, CompatibleType t,
  3139. PropertyType* /*unused*/)
  3140. {
  3141. const char* prop = dependee->GetProperty(propName);
  3142. if (!prop) {
  3143. return;
  3144. }
  3145. std::vector<std::string> props;
  3146. cmSystemTools::ExpandListArgument(prop, props);
  3147. std::string pdir = cmSystemTools::GetCMakeRoot();
  3148. pdir += "/Help/prop_tgt/";
  3149. for (std::string const& p : props) {
  3150. std::string pname = cmSystemTools::HelpFileName(p);
  3151. std::string pfile = pdir + pname + ".rst";
  3152. if (cmSystemTools::FileExists(pfile.c_str(), true)) {
  3153. std::ostringstream e;
  3154. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  3155. << "\" listed in its " << propName
  3156. << " property. "
  3157. "This is not allowed. Only user-defined properties may appear "
  3158. "listed in the "
  3159. << propName << " property.";
  3160. depender->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, e.str());
  3161. return;
  3162. }
  3163. if (emitted.insert(p).second) {
  3164. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  3165. nullptr);
  3166. if (cmSystemTools::GetErrorOccuredFlag()) {
  3167. return;
  3168. }
  3169. }
  3170. }
  3171. }
  3172. static std::string intersect(const std::set<std::string>& s1,
  3173. const std::set<std::string>& s2)
  3174. {
  3175. std::set<std::string> intersect;
  3176. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  3177. std::inserter(intersect, intersect.begin()));
  3178. if (!intersect.empty()) {
  3179. return *intersect.begin();
  3180. }
  3181. return "";
  3182. }
  3183. static std::string intersect(const std::set<std::string>& s1,
  3184. const std::set<std::string>& s2,
  3185. const std::set<std::string>& s3)
  3186. {
  3187. std::string result;
  3188. result = intersect(s1, s2);
  3189. if (!result.empty()) {
  3190. return result;
  3191. }
  3192. result = intersect(s1, s3);
  3193. if (!result.empty()) {
  3194. return result;
  3195. }
  3196. return intersect(s2, s3);
  3197. }
  3198. static std::string intersect(const std::set<std::string>& s1,
  3199. const std::set<std::string>& s2,
  3200. const std::set<std::string>& s3,
  3201. const std::set<std::string>& s4)
  3202. {
  3203. std::string result;
  3204. result = intersect(s1, s2);
  3205. if (!result.empty()) {
  3206. return result;
  3207. }
  3208. result = intersect(s1, s3);
  3209. if (!result.empty()) {
  3210. return result;
  3211. }
  3212. result = intersect(s1, s4);
  3213. if (!result.empty()) {
  3214. return result;
  3215. }
  3216. return intersect(s2, s3, s4);
  3217. }
  3218. void cmGeneratorTarget::CheckPropertyCompatibility(
  3219. cmComputeLinkInformation* info, const std::string& config) const
  3220. {
  3221. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  3222. std::set<std::string> emittedBools;
  3223. static std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  3224. std::set<std::string> emittedStrings;
  3225. static std::string strString = "COMPATIBLE_INTERFACE_STRING";
  3226. std::set<std::string> emittedMinNumbers;
  3227. static std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  3228. std::set<std::string> emittedMaxNumbers;
  3229. static std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  3230. for (auto const& dep : deps) {
  3231. if (!dep.Target) {
  3232. continue;
  3233. }
  3234. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  3235. config, BoolType, nullptr);
  3236. if (cmSystemTools::GetErrorOccuredFlag()) {
  3237. return;
  3238. }
  3239. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  3240. emittedStrings, config, StringType,
  3241. nullptr);
  3242. if (cmSystemTools::GetErrorOccuredFlag()) {
  3243. return;
  3244. }
  3245. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  3246. emittedMinNumbers, config,
  3247. NumberMinType, nullptr);
  3248. if (cmSystemTools::GetErrorOccuredFlag()) {
  3249. return;
  3250. }
  3251. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  3252. emittedMaxNumbers, config,
  3253. NumberMaxType, nullptr);
  3254. if (cmSystemTools::GetErrorOccuredFlag()) {
  3255. return;
  3256. }
  3257. }
  3258. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  3259. emittedMaxNumbers);
  3260. if (!prop.empty()) {
  3261. // Use a sorted std::vector to keep the error message sorted.
  3262. std::vector<std::string> props;
  3263. std::set<std::string>::const_iterator i = emittedBools.find(prop);
  3264. if (i != emittedBools.end()) {
  3265. props.push_back(strBool);
  3266. }
  3267. i = emittedStrings.find(prop);
  3268. if (i != emittedStrings.end()) {
  3269. props.push_back(strString);
  3270. }
  3271. i = emittedMinNumbers.find(prop);
  3272. if (i != emittedMinNumbers.end()) {
  3273. props.push_back(strNumMin);
  3274. }
  3275. i = emittedMaxNumbers.find(prop);
  3276. if (i != emittedMaxNumbers.end()) {
  3277. props.push_back(strNumMax);
  3278. }
  3279. std::sort(props.begin(), props.end());
  3280. std::string propsString = cmJoin(cmMakeRange(props).retreat(1), ", ");
  3281. propsString += " and the " + props.back();
  3282. std::ostringstream e;
  3283. e << "Property \"" << prop << "\" appears in both the " << propsString
  3284. << " property in the dependencies of target \"" << this->GetName()
  3285. << "\". This is not allowed. A property may only require compatibility "
  3286. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  3287. "or a "
  3288. "string interpretation, but not a mixture.";
  3289. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  3290. }
  3291. }
  3292. std::string compatibilityType(CompatibleType t)
  3293. {
  3294. switch (t) {
  3295. case BoolType:
  3296. return "Boolean compatibility";
  3297. case StringType:
  3298. return "String compatibility";
  3299. case NumberMaxType:
  3300. return "Numeric maximum compatibility";
  3301. case NumberMinType:
  3302. return "Numeric minimum compatibility";
  3303. }
  3304. assert(false && "Unreachable!");
  3305. return "";
  3306. }
  3307. std::string compatibilityAgree(CompatibleType t, bool dominant)
  3308. {
  3309. switch (t) {
  3310. case BoolType:
  3311. case StringType:
  3312. return dominant ? "(Disagree)\n" : "(Agree)\n";
  3313. case NumberMaxType:
  3314. case NumberMinType:
  3315. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  3316. }
  3317. assert(false && "Unreachable!");
  3318. return "";
  3319. }
  3320. template <typename PropertyType>
  3321. PropertyType getTypedProperty(cmGeneratorTarget const* tgt,
  3322. const std::string& prop);
  3323. template <>
  3324. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  3325. const std::string& prop)
  3326. {
  3327. return tgt->GetPropertyAsBool(prop);
  3328. }
  3329. template <>
  3330. const char* getTypedProperty<const char*>(cmGeneratorTarget const* tgt,
  3331. const std::string& prop)
  3332. {
  3333. return tgt->GetProperty(prop);
  3334. }
  3335. template <typename PropertyType>
  3336. std::string valueAsString(PropertyType);
  3337. template <>
  3338. std::string valueAsString<bool>(bool value)
  3339. {
  3340. return value ? "TRUE" : "FALSE";
  3341. }
  3342. template <>
  3343. std::string valueAsString<const char*>(const char* value)
  3344. {
  3345. return value ? value : "(unset)";
  3346. }
  3347. template <typename PropertyType>
  3348. PropertyType impliedValue(PropertyType);
  3349. template <>
  3350. bool impliedValue<bool>(bool /*unused*/)
  3351. {
  3352. return false;
  3353. }
  3354. template <>
  3355. const char* impliedValue<const char*>(const char* /*unused*/)
  3356. {
  3357. return "";
  3358. }
  3359. template <typename PropertyType>
  3360. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  3361. PropertyType rhs,
  3362. CompatibleType t);
  3363. template <>
  3364. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  3365. CompatibleType /*unused*/)
  3366. {
  3367. return std::make_pair(lhs == rhs, lhs);
  3368. }
  3369. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  3370. const char* rhs)
  3371. {
  3372. const bool b = strcmp(lhs, rhs) == 0;
  3373. return std::make_pair(b, b ? lhs : nullptr);
  3374. }
  3375. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  3376. const char* rhs,
  3377. CompatibleType t)
  3378. {
  3379. char* pEnd;
  3380. const char* const null_ptr = nullptr;
  3381. long lnum = strtol(lhs, &pEnd, 0);
  3382. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  3383. return std::pair<bool, const char*>(false, null_ptr);
  3384. }
  3385. long rnum = strtol(rhs, &pEnd, 0);
  3386. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  3387. return std::pair<bool, const char*>(false, null_ptr);
  3388. }
  3389. if (t == NumberMaxType) {
  3390. return std::make_pair(true, std::max(lnum, rnum) == lnum ? lhs : rhs);
  3391. }
  3392. return std::make_pair(true, std::min(lnum, rnum) == lnum ? lhs : rhs);
  3393. }
  3394. template <>
  3395. std::pair<bool, const char*> consistentProperty(const char* lhs,
  3396. const char* rhs,
  3397. CompatibleType t)
  3398. {
  3399. if (!lhs && !rhs) {
  3400. return std::make_pair(true, lhs);
  3401. }
  3402. if (!lhs) {
  3403. return std::make_pair(true, rhs);
  3404. }
  3405. if (!rhs) {
  3406. return std::make_pair(true, lhs);
  3407. }
  3408. const char* const null_ptr = nullptr;
  3409. switch (t) {
  3410. case BoolType:
  3411. assert(false && "consistentProperty for strings called with BoolType");
  3412. return std::pair<bool, const char*>(false, null_ptr);
  3413. case StringType:
  3414. return consistentStringProperty(lhs, rhs);
  3415. case NumberMinType:
  3416. case NumberMaxType:
  3417. return consistentNumberProperty(lhs, rhs, t);
  3418. }
  3419. assert(false && "Unreachable!");
  3420. return std::pair<bool, const char*>(false, null_ptr);
  3421. }
  3422. template <typename PropertyType>
  3423. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  3424. const std::string& p,
  3425. const std::string& config,
  3426. const char* defaultValue,
  3427. CompatibleType t,
  3428. PropertyType* /*unused*/)
  3429. {
  3430. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  3431. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  3432. const bool explicitlySet =
  3433. std::find(headPropKeys.begin(), headPropKeys.end(), p) !=
  3434. headPropKeys.end();
  3435. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  3436. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  3437. std::vector<cmGeneratorTarget const*> const& deps =
  3438. tgt->GetLinkImplementationClosure(config);
  3439. if (deps.empty()) {
  3440. return propContent;
  3441. }
  3442. bool propInitialized = explicitlySet;
  3443. std::string report = " * Target \"";
  3444. report += tgt->GetName();
  3445. if (explicitlySet) {
  3446. report += "\" has property content \"";
  3447. report += valueAsString<PropertyType>(propContent);
  3448. report += "\"\n";
  3449. } else if (impliedByUse) {
  3450. report += "\" property is implied by use.\n";
  3451. } else {
  3452. report += "\" property not set.\n";
  3453. }
  3454. std::string interfaceProperty = "INTERFACE_" + p;
  3455. for (cmGeneratorTarget const* theTarget : deps) {
  3456. // An error should be reported if one dependency
  3457. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  3458. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  3459. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  3460. // with a dependency.
  3461. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  3462. const bool ifaceIsSet = std::find(propKeys.begin(), propKeys.end(),
  3463. interfaceProperty) != propKeys.end();
  3464. PropertyType ifacePropContent =
  3465. getTypedProperty<PropertyType>(theTarget, interfaceProperty);
  3466. std::string reportEntry;
  3467. if (ifaceIsSet) {
  3468. reportEntry += " * Target \"";
  3469. reportEntry += theTarget->GetName();
  3470. reportEntry += "\" property value \"";
  3471. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  3472. reportEntry += "\" ";
  3473. }
  3474. if (explicitlySet) {
  3475. if (ifaceIsSet) {
  3476. std::pair<bool, PropertyType> consistent =
  3477. consistentProperty(propContent, ifacePropContent, t);
  3478. report += reportEntry;
  3479. report += compatibilityAgree(t, propContent != consistent.second);
  3480. if (!consistent.first) {
  3481. std::ostringstream e;
  3482. e << "Property " << p << " on target \"" << tgt->GetName()
  3483. << "\" does\nnot match the "
  3484. "INTERFACE_"
  3485. << p << " property requirement\nof "
  3486. "dependency \""
  3487. << theTarget->GetName() << "\".\n";
  3488. cmSystemTools::Error(e.str().c_str());
  3489. break;
  3490. }
  3491. propContent = consistent.second;
  3492. continue;
  3493. }
  3494. // Explicitly set on target and not set in iface. Can't disagree.
  3495. continue;
  3496. }
  3497. if (impliedByUse) {
  3498. propContent = impliedValue<PropertyType>(propContent);
  3499. if (ifaceIsSet) {
  3500. std::pair<bool, PropertyType> consistent =
  3501. consistentProperty(propContent, ifacePropContent, t);
  3502. report += reportEntry;
  3503. report += compatibilityAgree(t, propContent != consistent.second);
  3504. if (!consistent.first) {
  3505. std::ostringstream e;
  3506. e << "Property " << p << " on target \"" << tgt->GetName()
  3507. << "\" is\nimplied to be " << defaultValue
  3508. << " because it was used to determine the link libraries\n"
  3509. "already. The INTERFACE_"
  3510. << p << " property on\ndependency \"" << theTarget->GetName()
  3511. << "\" is in conflict.\n";
  3512. cmSystemTools::Error(e.str().c_str());
  3513. break;
  3514. }
  3515. propContent = consistent.second;
  3516. continue;
  3517. }
  3518. // Implicitly set on target and not set in iface. Can't disagree.
  3519. continue;
  3520. }
  3521. if (ifaceIsSet) {
  3522. if (propInitialized) {
  3523. std::pair<bool, PropertyType> consistent =
  3524. consistentProperty(propContent, ifacePropContent, t);
  3525. report += reportEntry;
  3526. report += compatibilityAgree(t, propContent != consistent.second);
  3527. if (!consistent.first) {
  3528. std::ostringstream e;
  3529. e << "The INTERFACE_" << p << " property of \""
  3530. << theTarget->GetName() << "\" does\nnot agree with the value of "
  3531. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  3532. cmSystemTools::Error(e.str().c_str());
  3533. break;
  3534. }
  3535. propContent = consistent.second;
  3536. continue;
  3537. }
  3538. report += reportEntry + "(Interface set)\n";
  3539. propContent = ifacePropContent;
  3540. propInitialized = true;
  3541. } else {
  3542. // Not set. Nothing to agree on.
  3543. continue;
  3544. }
  3545. }
  3546. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  3547. report, compatibilityType(t));
  3548. return propContent;
  3549. }
  3550. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  3551. const std::string& p, const std::string& config) const
  3552. {
  3553. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  3554. BoolType, nullptr);
  3555. }
  3556. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  3557. const std::string& p, const std::string& config) const
  3558. {
  3559. return checkInterfacePropertyCompatibility<const char*>(
  3560. this, p, config, "empty", StringType, nullptr);
  3561. }
  3562. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  3563. const std::string& p, const std::string& config) const
  3564. {
  3565. return checkInterfacePropertyCompatibility<const char*>(
  3566. this, p, config, "empty", NumberMinType, nullptr);
  3567. }
  3568. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  3569. const std::string& p, const std::string& config) const
  3570. {
  3571. return checkInterfacePropertyCompatibility<const char*>(
  3572. this, p, config, "empty", NumberMaxType, nullptr);
  3573. }
  3574. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  3575. const std::string& config) const
  3576. {
  3577. // Lookup any existing information for this configuration.
  3578. std::string key(cmSystemTools::UpperCase(config));
  3579. cmTargetLinkInformationMap::iterator i = this->LinkInformation.find(key);
  3580. if (i == this->LinkInformation.end()) {
  3581. // Compute information for this configuration.
  3582. cmComputeLinkInformation* info =
  3583. new cmComputeLinkInformation(this, config);
  3584. if (!info || !info->Compute()) {
  3585. delete info;
  3586. info = nullptr;
  3587. }
  3588. // Store the information for this configuration.
  3589. cmTargetLinkInformationMap::value_type entry(key, info);
  3590. i = this->LinkInformation.insert(entry).first;
  3591. if (info) {
  3592. this->CheckPropertyCompatibility(info, config);
  3593. }
  3594. }
  3595. return i->second;
  3596. }
  3597. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  3598. {
  3599. int patch;
  3600. this->GetTargetVersion(false, major, minor, patch);
  3601. }
  3602. void cmGeneratorTarget::GetTargetVersion(bool soversion, int& major,
  3603. int& minor, int& patch) const
  3604. {
  3605. // Set the default values.
  3606. major = 0;
  3607. minor = 0;
  3608. patch = 0;
  3609. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3610. // Look for a VERSION or SOVERSION property.
  3611. const char* prop = soversion ? "SOVERSION" : "VERSION";
  3612. if (const char* version = this->GetProperty(prop)) {
  3613. // Try to parse the version number and store the results that were
  3614. // successfully parsed.
  3615. int parsed_major;
  3616. int parsed_minor;
  3617. int parsed_patch;
  3618. switch (sscanf(version, "%d.%d.%d", &parsed_major, &parsed_minor,
  3619. &parsed_patch)) {
  3620. case 3:
  3621. patch = parsed_patch;
  3622. CM_FALLTHROUGH;
  3623. case 2:
  3624. minor = parsed_minor;
  3625. CM_FALLTHROUGH;
  3626. case 1:
  3627. major = parsed_major;
  3628. CM_FALLTHROUGH;
  3629. default:
  3630. break;
  3631. }
  3632. }
  3633. }
  3634. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  3635. std::string const& working_dir) const
  3636. {
  3637. if (!this->FortranModuleDirectoryCreated) {
  3638. this->FortranModuleDirectory =
  3639. this->CreateFortranModuleDirectory(working_dir);
  3640. this->FortranModuleDirectoryCreated = true;
  3641. }
  3642. return this->FortranModuleDirectory;
  3643. }
  3644. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  3645. std::string const& working_dir) const
  3646. {
  3647. std::string mod_dir;
  3648. std::string target_mod_dir;
  3649. if (const char* prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  3650. target_mod_dir = prop;
  3651. } else {
  3652. std::string const& default_mod_dir =
  3653. this->LocalGenerator->GetCurrentBinaryDirectory();
  3654. if (default_mod_dir != working_dir) {
  3655. target_mod_dir = default_mod_dir;
  3656. }
  3657. }
  3658. const char* moddir_flag =
  3659. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  3660. if (!target_mod_dir.empty() && moddir_flag) {
  3661. // Compute the full path to the module directory.
  3662. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  3663. // Already a full path.
  3664. mod_dir = target_mod_dir;
  3665. } else {
  3666. // Interpret relative to the current output directory.
  3667. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  3668. mod_dir += "/";
  3669. mod_dir += target_mod_dir;
  3670. }
  3671. // Make sure the module output directory exists.
  3672. cmSystemTools::MakeDirectory(mod_dir);
  3673. }
  3674. return mod_dir;
  3675. }
  3676. std::string cmGeneratorTarget::GetFrameworkVersion() const
  3677. {
  3678. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3679. if (const char* fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  3680. return fversion;
  3681. }
  3682. if (const char* tversion = this->GetProperty("VERSION")) {
  3683. return tversion;
  3684. }
  3685. return "A";
  3686. }
  3687. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  3688. std::string const& prefix,
  3689. std::string const& base,
  3690. std::string const& suffix,
  3691. std::string const& name,
  3692. const char* version) const
  3693. {
  3694. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  3695. if (version) {
  3696. vName += ".";
  3697. vName += version;
  3698. }
  3699. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  3700. }
  3701. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  3702. {
  3703. cmPropertyMap const& propsObject = this->Target->GetProperties();
  3704. std::vector<std::string> props;
  3705. props.reserve(propsObject.size());
  3706. for (auto const& it : propsObject) {
  3707. props.push_back(it.first);
  3708. }
  3709. return props;
  3710. }
  3711. void cmGeneratorTarget::ReportPropertyOrigin(
  3712. const std::string& p, const std::string& result, const std::string& report,
  3713. const std::string& compatibilityType) const
  3714. {
  3715. std::vector<std::string> debugProperties;
  3716. const char* debugProp = this->Target->GetMakefile()->GetDefinition(
  3717. "CMAKE_DEBUG_TARGET_PROPERTIES");
  3718. if (debugProp) {
  3719. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  3720. }
  3721. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  3722. std::find(debugProperties.begin(), debugProperties.end(), p) !=
  3723. debugProperties.end();
  3724. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3725. this->DebugCompatiblePropertiesDone[p] = true;
  3726. }
  3727. if (!debugOrigin) {
  3728. return;
  3729. }
  3730. std::string areport = compatibilityType;
  3731. areport += std::string(" of property \"") + p + "\" for target \"";
  3732. areport += std::string(this->GetName());
  3733. areport += "\" (result: \"";
  3734. areport += result;
  3735. areport += "\"):\n" + report;
  3736. this->LocalGenerator->GetCMakeInstance()->IssueMessage(cmake::LOG, areport);
  3737. }
  3738. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  3739. std::vector<cmLinkItem>& items) const
  3740. {
  3741. for (std::string const& n : names) {
  3742. std::string name = this->CheckCMP0004(n);
  3743. if (name == this->GetName() || name.empty()) {
  3744. continue;
  3745. }
  3746. items.push_back(cmLinkItem(name, this->FindTargetToLink(name)));
  3747. }
  3748. }
  3749. void cmGeneratorTarget::ExpandLinkItems(
  3750. std::string const& prop, std::string const& value, std::string const& config,
  3751. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  3752. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition) const
  3753. {
  3754. cmGeneratorExpression ge;
  3755. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), prop, nullptr,
  3756. nullptr);
  3757. // The $<LINK_ONLY> expression may be in a link interface to specify private
  3758. // link dependencies that are otherwise excluded from usage requirements.
  3759. if (usage_requirements_only) {
  3760. dagChecker.SetTransitivePropertiesOnly();
  3761. }
  3762. std::vector<std::string> libs;
  3763. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  3764. cmSystemTools::ExpandListArgument(cge->Evaluate(this->LocalGenerator, config,
  3765. false, headTarget, this,
  3766. &dagChecker),
  3767. libs);
  3768. this->LookupLinkItems(libs, items);
  3769. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  3770. }
  3771. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  3772. const std::string& config, cmGeneratorTarget const* head) const
  3773. {
  3774. // Imported targets have their own link interface.
  3775. if (this->IsImported()) {
  3776. return this->GetImportLinkInterface(config, head, false);
  3777. }
  3778. // Link interfaces are not supported for executables that do not
  3779. // export symbols.
  3780. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  3781. !this->IsExecutableWithExports()) {
  3782. return nullptr;
  3783. }
  3784. // Lookup any existing link interface for this configuration.
  3785. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  3786. // If the link interface does not depend on the head target
  3787. // then return the one we computed first.
  3788. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  3789. return &hm.begin()->second;
  3790. }
  3791. cmOptionalLinkInterface& iface = hm[head];
  3792. if (!iface.LibrariesDone) {
  3793. iface.LibrariesDone = true;
  3794. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  3795. }
  3796. if (!iface.AllDone) {
  3797. iface.AllDone = true;
  3798. if (iface.Exists) {
  3799. this->ComputeLinkInterface(config, iface, head);
  3800. }
  3801. }
  3802. return iface.Exists ? &iface : nullptr;
  3803. }
  3804. void cmGeneratorTarget::ComputeLinkInterface(
  3805. const std::string& config, cmOptionalLinkInterface& iface,
  3806. cmGeneratorTarget const* headTarget) const
  3807. {
  3808. if (iface.ExplicitLibraries) {
  3809. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3810. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3811. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3812. // Shared libraries may have runtime implementation dependencies
  3813. // on other shared libraries that are not in the interface.
  3814. std::unordered_set<std::string> emitted;
  3815. for (cmLinkItem const& lib : iface.Libraries) {
  3816. emitted.insert(lib);
  3817. }
  3818. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  3819. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  3820. for (cmLinkImplItem const& lib : impl->Libraries) {
  3821. if (emitted.insert(lib).second) {
  3822. if (lib.Target) {
  3823. // This is a runtime dependency on another shared library.
  3824. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  3825. iface.SharedDeps.push_back(lib);
  3826. }
  3827. } else {
  3828. // TODO: Recognize shared library file names. Perhaps this
  3829. // should be moved to cmComputeLinkInformation, but that creates
  3830. // a chicken-and-egg problem since this list is needed for its
  3831. // construction.
  3832. }
  3833. }
  3834. }
  3835. }
  3836. }
  3837. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  3838. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  3839. // The link implementation is the default link interface.
  3840. cmLinkImplementationLibraries const* impl =
  3841. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  3842. iface.ImplementationIsInterface = true;
  3843. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  3844. }
  3845. if (this->LinkLanguagePropagatesToDependents()) {
  3846. // Targets using this archive need its language runtime libraries.
  3847. if (cmLinkImplementation const* impl =
  3848. this->GetLinkImplementation(config)) {
  3849. iface.Languages = impl->Languages;
  3850. }
  3851. }
  3852. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3853. // Construct the property name suffix for this configuration.
  3854. std::string suffix = "_";
  3855. if (!config.empty()) {
  3856. suffix += cmSystemTools::UpperCase(config);
  3857. } else {
  3858. suffix += "NOCONFIG";
  3859. }
  3860. // How many repetitions are needed if this library has cyclic
  3861. // dependencies?
  3862. std::string propName = "LINK_INTERFACE_MULTIPLICITY";
  3863. propName += suffix;
  3864. if (const char* config_reps = this->GetProperty(propName)) {
  3865. sscanf(config_reps, "%u", &iface.Multiplicity);
  3866. } else if (const char* reps =
  3867. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  3868. sscanf(reps, "%u", &iface.Multiplicity);
  3869. }
  3870. }
  3871. }
  3872. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  3873. const std::string& config, cmGeneratorTarget const* head,
  3874. bool usage_requirements_only) const
  3875. {
  3876. // Imported targets have their own link interface.
  3877. if (this->IsImported()) {
  3878. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  3879. }
  3880. // Link interfaces are not supported for executables that do not
  3881. // export symbols.
  3882. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  3883. !this->IsExecutableWithExports()) {
  3884. return nullptr;
  3885. }
  3886. // Lookup any existing link interface for this configuration.
  3887. std::string CONFIG = cmSystemTools::UpperCase(config);
  3888. cmHeadToLinkInterfaceMap& hm =
  3889. (usage_requirements_only
  3890. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  3891. : this->GetHeadToLinkInterfaceMap(config));
  3892. // If the link interface does not depend on the head target
  3893. // then return the one we computed first.
  3894. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  3895. return &hm.begin()->second;
  3896. }
  3897. cmOptionalLinkInterface& iface = hm[head];
  3898. if (!iface.LibrariesDone) {
  3899. iface.LibrariesDone = true;
  3900. this->ComputeLinkInterfaceLibraries(config, iface, head,
  3901. usage_requirements_only);
  3902. }
  3903. return iface.Exists ? &iface : nullptr;
  3904. }
  3905. std::string cmGeneratorTarget::GetDirectory(
  3906. const std::string& config, cmStateEnums::ArtifactType artifact) const
  3907. {
  3908. if (this->IsImported()) {
  3909. // Return the directory from which the target is imported.
  3910. return cmSystemTools::GetFilenamePath(
  3911. this->Target->ImportedGetFullPath(config, artifact));
  3912. }
  3913. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  3914. // Return the directory in which the target will be built.
  3915. switch (artifact) {
  3916. case cmStateEnums::RuntimeBinaryArtifact:
  3917. return info->OutDir;
  3918. case cmStateEnums::ImportLibraryArtifact:
  3919. return info->ImpDir;
  3920. }
  3921. }
  3922. return "";
  3923. }
  3924. bool cmGeneratorTarget::UsesDefaultOutputDir(
  3925. const std::string& config, cmStateEnums::ArtifactType artifact) const
  3926. {
  3927. std::string dir;
  3928. return this->ComputeOutputDir(config, artifact, dir);
  3929. }
  3930. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  3931. const std::string& config) const
  3932. {
  3933. // There is no output information for imported targets.
  3934. if (this->IsImported()) {
  3935. return nullptr;
  3936. }
  3937. // Only libraries and executables have well-defined output files.
  3938. if (!this->HaveWellDefinedOutputFiles()) {
  3939. std::string msg = "cmGeneratorTarget::GetOutputInfo called for ";
  3940. msg += this->GetName();
  3941. msg += " which has type ";
  3942. msg += cmState::GetTargetTypeName(this->GetType());
  3943. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  3944. return nullptr;
  3945. }
  3946. // Lookup/compute/cache the output information for this configuration.
  3947. std::string config_upper;
  3948. if (!config.empty()) {
  3949. config_upper = cmSystemTools::UpperCase(config);
  3950. }
  3951. OutputInfoMapType::iterator i = this->OutputInfoMap.find(config_upper);
  3952. if (i == this->OutputInfoMap.end()) {
  3953. // Add empty info in map to detect potential recursion.
  3954. OutputInfo info;
  3955. OutputInfoMapType::value_type entry(config_upper, info);
  3956. i = this->OutputInfoMap.insert(entry).first;
  3957. // Compute output directories.
  3958. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  3959. info.OutDir);
  3960. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  3961. info.ImpDir);
  3962. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  3963. info.PdbDir = info.OutDir;
  3964. }
  3965. // Now update the previously-prepared map entry.
  3966. i->second = info;
  3967. } else if (i->second.empty()) {
  3968. // An empty map entry indicates we have been called recursively
  3969. // from the above block.
  3970. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  3971. cmake::FATAL_ERROR,
  3972. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  3973. this->GetBacktrace());
  3974. return nullptr;
  3975. }
  3976. return &i->second;
  3977. }
  3978. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  3979. cmStateEnums::ArtifactType artifact,
  3980. std::string& out) const
  3981. {
  3982. bool usesDefaultOutputDir = false;
  3983. std::string conf = config;
  3984. // Look for a target property defining the target output directory
  3985. // based on the target type.
  3986. std::string targetTypeName = this->GetOutputTargetType(artifact);
  3987. const char* propertyName = nullptr;
  3988. std::string propertyNameStr = targetTypeName;
  3989. if (!propertyNameStr.empty()) {
  3990. propertyNameStr += "_OUTPUT_DIRECTORY";
  3991. propertyName = propertyNameStr.c_str();
  3992. }
  3993. // Check for a per-configuration output directory target property.
  3994. std::string configUpper = cmSystemTools::UpperCase(conf);
  3995. const char* configProp = nullptr;
  3996. std::string configPropStr = targetTypeName;
  3997. if (!configPropStr.empty()) {
  3998. configPropStr += "_OUTPUT_DIRECTORY_";
  3999. configPropStr += configUpper;
  4000. configProp = configPropStr.c_str();
  4001. }
  4002. // Select an output directory.
  4003. if (const char* config_outdir = this->GetProperty(configProp)) {
  4004. // Use the user-specified per-configuration output directory.
  4005. cmGeneratorExpression ge;
  4006. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4007. ge.Parse(config_outdir);
  4008. out = cge->Evaluate(this->LocalGenerator, config);
  4009. // Skip per-configuration subdirectory.
  4010. conf.clear();
  4011. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4012. // Use the user-specified output directory.
  4013. cmGeneratorExpression ge;
  4014. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4015. out = cge->Evaluate(this->LocalGenerator, config);
  4016. // Skip per-configuration subdirectory if the value contained a
  4017. // generator expression.
  4018. if (out != outdir) {
  4019. conf.clear();
  4020. }
  4021. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4022. // Lookup the output path for executables.
  4023. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  4024. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4025. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4026. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4027. // Lookup the output path for libraries.
  4028. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  4029. }
  4030. if (out.empty()) {
  4031. // Default to the current output directory.
  4032. usesDefaultOutputDir = true;
  4033. out = ".";
  4034. }
  4035. // Convert the output path to a full path in case it is
  4036. // specified as a relative path. Treat a relative path as
  4037. // relative to the current output directory for this makefile.
  4038. out = (cmSystemTools::CollapseFullPath(
  4039. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4040. // The generator may add the configuration's subdirectory.
  4041. if (!conf.empty()) {
  4042. bool useEPN =
  4043. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  4044. std::string suffix =
  4045. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  4046. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4047. "/", conf, suffix, out);
  4048. }
  4049. return usesDefaultOutputDir;
  4050. }
  4051. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  4052. const std::string& config,
  4053. std::string& out) const
  4054. {
  4055. // Look for a target property defining the target output directory
  4056. // based on the target type.
  4057. const char* propertyName = nullptr;
  4058. std::string propertyNameStr = kind;
  4059. if (!propertyNameStr.empty()) {
  4060. propertyNameStr += "_OUTPUT_DIRECTORY";
  4061. propertyName = propertyNameStr.c_str();
  4062. }
  4063. std::string conf = config;
  4064. // Check for a per-configuration output directory target property.
  4065. std::string configUpper = cmSystemTools::UpperCase(conf);
  4066. const char* configProp = nullptr;
  4067. std::string configPropStr = kind;
  4068. if (!configPropStr.empty()) {
  4069. configPropStr += "_OUTPUT_DIRECTORY_";
  4070. configPropStr += configUpper;
  4071. configProp = configPropStr.c_str();
  4072. }
  4073. // Select an output directory.
  4074. if (const char* config_outdir = this->GetProperty(configProp)) {
  4075. // Use the user-specified per-configuration output directory.
  4076. out = config_outdir;
  4077. // Skip per-configuration subdirectory.
  4078. conf.clear();
  4079. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4080. // Use the user-specified output directory.
  4081. out = outdir;
  4082. }
  4083. if (out.empty()) {
  4084. return false;
  4085. }
  4086. // Convert the output path to a full path in case it is
  4087. // specified as a relative path. Treat a relative path as
  4088. // relative to the current output directory for this makefile.
  4089. out = (cmSystemTools::CollapseFullPath(
  4090. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4091. // The generator may add the configuration's subdirectory.
  4092. if (!conf.empty()) {
  4093. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4094. "/", conf, "", out);
  4095. }
  4096. return true;
  4097. }
  4098. bool cmGeneratorTarget::HaveInstallTreeRPATH() const
  4099. {
  4100. const char* install_rpath = this->GetProperty("INSTALL_RPATH");
  4101. return (install_rpath && *install_rpath) &&
  4102. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  4103. }
  4104. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  4105. const std::string& config, cmOptionalLinkInterface& iface,
  4106. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  4107. {
  4108. // Construct the property name suffix for this configuration.
  4109. std::string suffix = "_";
  4110. if (!config.empty()) {
  4111. suffix += cmSystemTools::UpperCase(config);
  4112. } else {
  4113. suffix += "NOCONFIG";
  4114. }
  4115. // An explicit list of interface libraries may be set for shared
  4116. // libraries and executables that export symbols.
  4117. const char* explicitLibraries = nullptr;
  4118. std::string linkIfaceProp;
  4119. if (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  4120. this->GetPolicyStatusCMP0022() != cmPolicies::WARN) {
  4121. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  4122. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  4123. explicitLibraries = this->GetProperty(linkIfaceProp);
  4124. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4125. this->IsExecutableWithExports()) {
  4126. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  4127. // shared lib or executable.
  4128. // Lookup the per-configuration property.
  4129. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4130. linkIfaceProp += suffix;
  4131. explicitLibraries = this->GetProperty(linkIfaceProp);
  4132. // If not set, try the generic property.
  4133. if (!explicitLibraries) {
  4134. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4135. explicitLibraries = this->GetProperty(linkIfaceProp);
  4136. }
  4137. }
  4138. if (explicitLibraries &&
  4139. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4140. !this->PolicyWarnedCMP0022) {
  4141. // Compare the explicitly set old link interface properties to the
  4142. // preferred new link interface property one and warn if different.
  4143. const char* newExplicitLibraries =
  4144. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  4145. if (newExplicitLibraries &&
  4146. strcmp(newExplicitLibraries, explicitLibraries) != 0) {
  4147. std::ostringstream w;
  4148. /* clang-format off */
  4149. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4150. "Target \"" << this->GetName() << "\" has an "
  4151. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  4152. linkIfaceProp << " properties."
  4153. "\n"
  4154. "INTERFACE_LINK_LIBRARIES:\n"
  4155. " " << newExplicitLibraries << "\n" <<
  4156. linkIfaceProp << ":\n"
  4157. " " << explicitLibraries << "\n";
  4158. /* clang-format on */
  4159. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4160. this->PolicyWarnedCMP0022 = true;
  4161. }
  4162. }
  4163. // There is no implicit link interface for executables or modules
  4164. // so if none was explicitly set then there is no link interface.
  4165. if (!explicitLibraries &&
  4166. (this->GetType() == cmStateEnums::EXECUTABLE ||
  4167. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  4168. return;
  4169. }
  4170. iface.Exists = true;
  4171. iface.ExplicitLibraries = explicitLibraries;
  4172. if (explicitLibraries) {
  4173. // The interface libraries have been explicitly set.
  4174. this->ExpandLinkItems(linkIfaceProp, explicitLibraries, config, headTarget,
  4175. usage_requirements_only, iface.Libraries,
  4176. iface.HadHeadSensitiveCondition);
  4177. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4178. this->GetPolicyStatusCMP0022() == cmPolicies::OLD)
  4179. // If CMP0022 is NEW then the plain tll signature sets the
  4180. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  4181. // cleared the property explicitly and we should not fall back
  4182. // to the link implementation.
  4183. {
  4184. // The link implementation is the default link interface.
  4185. cmLinkImplementationLibraries const* impl =
  4186. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4187. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  4188. impl->Libraries.end());
  4189. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4190. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  4191. // Compare the link implementation fallback link interface to the
  4192. // preferred new link interface property and warn if different.
  4193. std::vector<cmLinkItem> ifaceLibs;
  4194. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  4195. if (const char* newExplicitLibraries = this->GetProperty(newProp)) {
  4196. bool hadHeadSensitiveConditionDummy = false;
  4197. this->ExpandLinkItems(newProp, newExplicitLibraries, config,
  4198. headTarget, usage_requirements_only, ifaceLibs,
  4199. hadHeadSensitiveConditionDummy);
  4200. }
  4201. if (ifaceLibs != iface.Libraries) {
  4202. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  4203. std::string newLibraries = cmJoin(ifaceLibs, ";");
  4204. if (oldLibraries.empty()) {
  4205. oldLibraries = "(empty)";
  4206. }
  4207. if (newLibraries.empty()) {
  4208. newLibraries = "(empty)";
  4209. }
  4210. std::ostringstream w;
  4211. /* clang-format off */
  4212. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4213. "Target \"" << this->GetName() << "\" has an "
  4214. "INTERFACE_LINK_LIBRARIES property. "
  4215. "This should be preferred as the source of the link interface "
  4216. "for this library but because CMP0022 is not set CMake is "
  4217. "ignoring the property and using the link implementation "
  4218. "as the link interface instead."
  4219. "\n"
  4220. "INTERFACE_LINK_LIBRARIES:\n"
  4221. " " << newLibraries << "\n"
  4222. "Link implementation:\n"
  4223. " " << oldLibraries << "\n";
  4224. /* clang-format on */
  4225. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4226. this->PolicyWarnedCMP0022 = true;
  4227. }
  4228. }
  4229. }
  4230. }
  4231. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  4232. const std::string& config, cmGeneratorTarget const* headTarget,
  4233. bool usage_requirements_only) const
  4234. {
  4235. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  4236. if (!info) {
  4237. return nullptr;
  4238. }
  4239. std::string CONFIG = cmSystemTools::UpperCase(config);
  4240. cmHeadToLinkInterfaceMap& hm =
  4241. (usage_requirements_only
  4242. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4243. : this->GetHeadToLinkInterfaceMap(config));
  4244. // If the link interface does not depend on the head target
  4245. // then return the one we computed first.
  4246. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4247. return &hm.begin()->second;
  4248. }
  4249. cmOptionalLinkInterface& iface = hm[headTarget];
  4250. if (!iface.AllDone) {
  4251. iface.AllDone = true;
  4252. iface.Multiplicity = info->Multiplicity;
  4253. cmSystemTools::ExpandListArgument(info->Languages, iface.Languages);
  4254. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  4255. headTarget, usage_requirements_only, iface.Libraries,
  4256. iface.HadHeadSensitiveCondition);
  4257. std::vector<std::string> deps;
  4258. cmSystemTools::ExpandListArgument(info->SharedDeps, deps);
  4259. this->LookupLinkItems(deps, iface.SharedDeps);
  4260. }
  4261. return &iface;
  4262. }
  4263. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  4264. const std::string& config) const
  4265. {
  4266. // There is no imported information for non-imported targets.
  4267. if (!this->IsImported()) {
  4268. return nullptr;
  4269. }
  4270. // Lookup/compute/cache the import information for this
  4271. // configuration.
  4272. std::string config_upper;
  4273. if (!config.empty()) {
  4274. config_upper = cmSystemTools::UpperCase(config);
  4275. } else {
  4276. config_upper = "NOCONFIG";
  4277. }
  4278. ImportInfoMapType::const_iterator i = this->ImportInfoMap.find(config_upper);
  4279. if (i == this->ImportInfoMap.end()) {
  4280. ImportInfo info;
  4281. this->ComputeImportInfo(config_upper, info);
  4282. ImportInfoMapType::value_type entry(config_upper, info);
  4283. i = this->ImportInfoMap.insert(entry).first;
  4284. }
  4285. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4286. return &i->second;
  4287. }
  4288. // If the location is empty then the target is not available for
  4289. // this configuration.
  4290. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  4291. return nullptr;
  4292. }
  4293. // Return the import information.
  4294. return &i->second;
  4295. }
  4296. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  4297. ImportInfo& info) const
  4298. {
  4299. // This method finds information about an imported target from its
  4300. // properties. The "IMPORTED_" namespace is reserved for properties
  4301. // defined by the project exporting the target.
  4302. // Initialize members.
  4303. info.NoSOName = false;
  4304. const char* loc = nullptr;
  4305. const char* imp = nullptr;
  4306. std::string suffix;
  4307. if (!this->Target->GetMappedConfig(desired_config, &loc, &imp, suffix)) {
  4308. return;
  4309. }
  4310. // Get the link interface.
  4311. {
  4312. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  4313. const char* propertyLibs = this->GetProperty(linkProp);
  4314. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4315. if (!propertyLibs) {
  4316. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4317. linkProp += suffix;
  4318. propertyLibs = this->GetProperty(linkProp);
  4319. }
  4320. if (!propertyLibs) {
  4321. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4322. propertyLibs = this->GetProperty(linkProp);
  4323. }
  4324. }
  4325. if (propertyLibs) {
  4326. info.LibrariesProp = linkProp;
  4327. info.Libraries = propertyLibs;
  4328. }
  4329. }
  4330. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4331. if (loc) {
  4332. info.LibName = loc;
  4333. }
  4334. return;
  4335. }
  4336. // A provided configuration has been chosen. Load the
  4337. // configuration's properties.
  4338. // Get the location.
  4339. if (loc) {
  4340. info.Location = loc;
  4341. } else {
  4342. std::string impProp = "IMPORTED_LOCATION";
  4343. impProp += suffix;
  4344. if (const char* config_location = this->GetProperty(impProp)) {
  4345. info.Location = config_location;
  4346. } else if (const char* location = this->GetProperty("IMPORTED_LOCATION")) {
  4347. info.Location = location;
  4348. }
  4349. }
  4350. // Get the soname.
  4351. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4352. std::string soProp = "IMPORTED_SONAME";
  4353. soProp += suffix;
  4354. if (const char* config_soname = this->GetProperty(soProp)) {
  4355. info.SOName = config_soname;
  4356. } else if (const char* soname = this->GetProperty("IMPORTED_SONAME")) {
  4357. info.SOName = soname;
  4358. }
  4359. }
  4360. // Get the "no-soname" mark.
  4361. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4362. std::string soProp = "IMPORTED_NO_SONAME";
  4363. soProp += suffix;
  4364. if (const char* config_no_soname = this->GetProperty(soProp)) {
  4365. info.NoSOName = cmSystemTools::IsOn(config_no_soname);
  4366. } else if (const char* no_soname =
  4367. this->GetProperty("IMPORTED_NO_SONAME")) {
  4368. info.NoSOName = cmSystemTools::IsOn(no_soname);
  4369. }
  4370. }
  4371. // Get the import library.
  4372. if (imp) {
  4373. info.ImportLibrary = imp;
  4374. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4375. this->IsExecutableWithExports()) {
  4376. std::string impProp = "IMPORTED_IMPLIB";
  4377. impProp += suffix;
  4378. if (const char* config_implib = this->GetProperty(impProp)) {
  4379. info.ImportLibrary = config_implib;
  4380. } else if (const char* implib = this->GetProperty("IMPORTED_IMPLIB")) {
  4381. info.ImportLibrary = implib;
  4382. }
  4383. }
  4384. // Get the link dependencies.
  4385. {
  4386. std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
  4387. linkProp += suffix;
  4388. if (const char* config_libs = this->GetProperty(linkProp)) {
  4389. info.SharedDeps = config_libs;
  4390. } else if (const char* libs =
  4391. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  4392. info.SharedDeps = libs;
  4393. }
  4394. }
  4395. // Get the link languages.
  4396. if (this->LinkLanguagePropagatesToDependents()) {
  4397. std::string linkProp = "IMPORTED_LINK_INTERFACE_LANGUAGES";
  4398. linkProp += suffix;
  4399. if (const char* config_libs = this->GetProperty(linkProp)) {
  4400. info.Languages = config_libs;
  4401. } else if (const char* libs =
  4402. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  4403. info.Languages = libs;
  4404. }
  4405. }
  4406. // Get the cyclic repetition count.
  4407. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4408. std::string linkProp = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  4409. linkProp += suffix;
  4410. if (const char* config_reps = this->GetProperty(linkProp)) {
  4411. sscanf(config_reps, "%u", &info.Multiplicity);
  4412. } else if (const char* reps =
  4413. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  4414. sscanf(reps, "%u", &info.Multiplicity);
  4415. }
  4416. }
  4417. }
  4418. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  4419. const std::string& config) const
  4420. {
  4421. std::string CONFIG = cmSystemTools::UpperCase(config);
  4422. return this->LinkInterfaceMap[CONFIG];
  4423. }
  4424. cmHeadToLinkInterfaceMap&
  4425. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  4426. const std::string& config) const
  4427. {
  4428. std::string CONFIG = cmSystemTools::UpperCase(config);
  4429. return this->LinkInterfaceUsageRequirementsOnlyMap[CONFIG];
  4430. }
  4431. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  4432. const std::string& config) const
  4433. {
  4434. // There is no link implementation for imported targets.
  4435. if (this->IsImported()) {
  4436. return nullptr;
  4437. }
  4438. std::string CONFIG = cmSystemTools::UpperCase(config);
  4439. cmOptionalLinkImplementation& impl = this->LinkImplMap[CONFIG][this];
  4440. if (!impl.LibrariesDone) {
  4441. impl.LibrariesDone = true;
  4442. this->ComputeLinkImplementationLibraries(config, impl, this);
  4443. }
  4444. if (!impl.LanguagesDone) {
  4445. impl.LanguagesDone = true;
  4446. this->ComputeLinkImplementationLanguages(config, impl);
  4447. }
  4448. return &impl;
  4449. }
  4450. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  4451. std::vector<cmSourceFile*>& files) const
  4452. {
  4453. std::vector<std::string> configs;
  4454. this->Makefile->GetConfigurations(configs);
  4455. if (configs.empty()) {
  4456. configs.push_back("");
  4457. }
  4458. std::vector<std::string>::const_iterator it = configs.begin();
  4459. const std::string& firstConfig = *it;
  4460. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  4461. for (; it != configs.end(); ++it) {
  4462. std::vector<cmSourceFile*> configFiles;
  4463. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  4464. if (configFiles != files) {
  4465. std::string firstConfigFiles;
  4466. const char* sep = "";
  4467. for (cmSourceFile* f : files) {
  4468. firstConfigFiles += sep;
  4469. firstConfigFiles += f->GetFullPath();
  4470. sep = "\n ";
  4471. }
  4472. std::string thisConfigFiles;
  4473. sep = "";
  4474. for (cmSourceFile* f : configFiles) {
  4475. thisConfigFiles += sep;
  4476. thisConfigFiles += f->GetFullPath();
  4477. sep = "\n ";
  4478. }
  4479. std::ostringstream e;
  4480. /* clang-format off */
  4481. e << "Target \"" << this->GetName()
  4482. << "\" has source files which vary by "
  4483. "configuration. This is not supported by the \""
  4484. << this->GlobalGenerator->GetName()
  4485. << "\" generator.\n"
  4486. "Config \"" << firstConfig << "\":\n"
  4487. " " << firstConfigFiles << "\n"
  4488. "Config \"" << *it << "\":\n"
  4489. " " << thisConfigFiles << "\n";
  4490. /* clang-format on */
  4491. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  4492. return false;
  4493. }
  4494. }
  4495. return true;
  4496. }
  4497. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  4498. std::vector<cmGeneratorTarget*>& objlibs) const
  4499. {
  4500. // At configure-time, this method can be called as part of getting the
  4501. // LOCATION property or to export() a file to be include()d. However
  4502. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  4503. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  4504. // behavior of CMP0024 and CMP0026 only.
  4505. cmStringRange rng = this->Target->GetSourceEntries();
  4506. for (std::string const& entry : rng) {
  4507. std::vector<std::string> files;
  4508. cmSystemTools::ExpandListArgument(entry, files);
  4509. for (std::string const& li : files) {
  4510. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") &&
  4511. li[li.size() - 1] == '>') {
  4512. std::string objLibName = li.substr(17, li.size() - 18);
  4513. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  4514. continue;
  4515. }
  4516. cmGeneratorTarget* objLib =
  4517. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  4518. if (objLib) {
  4519. objlibs.push_back(objLib);
  4520. }
  4521. }
  4522. }
  4523. }
  4524. }
  4525. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  4526. {
  4527. // Strip whitespace off the library names because we used to do this
  4528. // in case variables were expanded at generate time. We no longer
  4529. // do the expansion but users link to libraries like " ${VAR} ".
  4530. std::string lib = item;
  4531. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  4532. if (pos != std::string::npos) {
  4533. lib = lib.substr(pos);
  4534. }
  4535. pos = lib.find_last_not_of(" \t\r\n");
  4536. if (pos != std::string::npos) {
  4537. lib = lib.substr(0, pos + 1);
  4538. }
  4539. if (lib != item) {
  4540. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  4541. switch (this->GetPolicyStatusCMP0004()) {
  4542. case cmPolicies::WARN: {
  4543. std::ostringstream w;
  4544. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  4545. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4546. << "\" which has leading or trailing whitespace.";
  4547. cm->IssueMessage(cmake::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  4548. }
  4549. case cmPolicies::OLD:
  4550. break;
  4551. case cmPolicies::NEW: {
  4552. std::ostringstream e;
  4553. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  4554. << "\" which has leading or trailing whitespace. "
  4555. << "This is now an error according to policy CMP0004.";
  4556. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4557. } break;
  4558. case cmPolicies::REQUIRED_IF_USED:
  4559. case cmPolicies::REQUIRED_ALWAYS: {
  4560. std::ostringstream e;
  4561. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  4562. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4563. << "\" which has leading or trailing whitespace.";
  4564. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4565. } break;
  4566. }
  4567. }
  4568. return lib;
  4569. }
  4570. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  4571. const std::string& config) const
  4572. {
  4573. std::vector<cmSourceFile*> sourceFiles;
  4574. this->GetSourceFiles(sourceFiles, config);
  4575. for (cmSourceFile* src : sourceFiles) {
  4576. const std::string& lang = src->GetLanguage();
  4577. if (!lang.empty()) {
  4578. languages.insert(lang);
  4579. }
  4580. }
  4581. std::vector<cmGeneratorTarget*> objectLibraries;
  4582. std::vector<cmSourceFile const*> externalObjects;
  4583. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4584. std::vector<cmGeneratorTarget*> objectTargets;
  4585. this->GetObjectLibrariesCMP0026(objectTargets);
  4586. objectLibraries.reserve(objectTargets.size());
  4587. for (cmGeneratorTarget* gt : objectTargets) {
  4588. objectLibraries.push_back(gt);
  4589. }
  4590. } else {
  4591. this->GetExternalObjects(externalObjects, config);
  4592. for (cmSourceFile const* extObj : externalObjects) {
  4593. std::string objLib = extObj->GetObjectLibrary();
  4594. if (cmGeneratorTarget* tgt =
  4595. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  4596. objectLibraries.push_back(tgt);
  4597. }
  4598. }
  4599. }
  4600. for (cmGeneratorTarget* objLib : objectLibraries) {
  4601. objLib->GetLanguages(languages, config);
  4602. }
  4603. }
  4604. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  4605. const std::string& config, cmOptionalLinkImplementation& impl) const
  4606. {
  4607. // This target needs runtime libraries for its source languages.
  4608. std::set<std::string> languages;
  4609. // Get languages used in our source files.
  4610. this->GetLanguages(languages, config);
  4611. // Copy the set of langauges to the link implementation.
  4612. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  4613. languages.end());
  4614. }
  4615. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  4616. {
  4617. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  4618. return false;
  4619. }
  4620. if (this->GetProperty("BUILD_RPATH")) {
  4621. return true;
  4622. }
  4623. if (cmLinkImplementationLibraries const* impl =
  4624. this->GetLinkImplementationLibraries(config)) {
  4625. return !impl->Libraries.empty();
  4626. }
  4627. return false;
  4628. }
  4629. cmLinkImplementationLibraries const*
  4630. cmGeneratorTarget::GetLinkImplementationLibraries(
  4631. const std::string& config) const
  4632. {
  4633. return this->GetLinkImplementationLibrariesInternal(config, this);
  4634. }
  4635. cmLinkImplementationLibraries const*
  4636. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  4637. const std::string& config, cmGeneratorTarget const* head) const
  4638. {
  4639. // There is no link implementation for imported targets.
  4640. if (this->IsImported()) {
  4641. return nullptr;
  4642. }
  4643. // Populate the link implementation libraries for this configuration.
  4644. std::string CONFIG = cmSystemTools::UpperCase(config);
  4645. HeadToLinkImplementationMap& hm = this->LinkImplMap[CONFIG];
  4646. // If the link implementation does not depend on the head target
  4647. // then return the one we computed first.
  4648. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4649. return &hm.begin()->second;
  4650. }
  4651. cmOptionalLinkImplementation& impl = hm[head];
  4652. if (!impl.LibrariesDone) {
  4653. impl.LibrariesDone = true;
  4654. this->ComputeLinkImplementationLibraries(config, impl, head);
  4655. }
  4656. return &impl;
  4657. }
  4658. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  4659. const std::string& p) const
  4660. {
  4661. return this->LinkImplicitNullProperties.find(p) !=
  4662. this->LinkImplicitNullProperties.end();
  4663. }
  4664. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  4665. const std::string& config, cmOptionalLinkImplementation& impl,
  4666. cmGeneratorTarget const* head) const
  4667. {
  4668. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  4669. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  4670. cmBacktraceRange::const_iterator btIt = btRange.begin();
  4671. // Collect libraries directly linked in this configuration.
  4672. for (cmStringRange::const_iterator le = entryRange.begin(),
  4673. end = entryRange.end();
  4674. le != end; ++le, ++btIt) {
  4675. std::vector<std::string> llibs;
  4676. cmGeneratorExpressionDAGChecker dagChecker(
  4677. this->GetName(), "LINK_LIBRARIES", nullptr, nullptr);
  4678. cmGeneratorExpression ge(*btIt);
  4679. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  4680. std::string const evaluated =
  4681. cge->Evaluate(this->LocalGenerator, config, false, head, &dagChecker);
  4682. cmSystemTools::ExpandListArgument(evaluated, llibs);
  4683. if (cge->GetHadHeadSensitiveCondition()) {
  4684. impl.HadHeadSensitiveCondition = true;
  4685. }
  4686. for (std::string const& lib : llibs) {
  4687. // Skip entries that resolve to the target itself or are empty.
  4688. std::string name = this->CheckCMP0004(lib);
  4689. if (name == this->GetName() || name.empty()) {
  4690. if (name == this->GetName()) {
  4691. bool noMessage = false;
  4692. cmake::MessageType messageType = cmake::FATAL_ERROR;
  4693. std::ostringstream e;
  4694. switch (this->GetPolicyStatusCMP0038()) {
  4695. case cmPolicies::WARN: {
  4696. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  4697. messageType = cmake::AUTHOR_WARNING;
  4698. } break;
  4699. case cmPolicies::OLD:
  4700. noMessage = true;
  4701. case cmPolicies::REQUIRED_IF_USED:
  4702. case cmPolicies::REQUIRED_ALWAYS:
  4703. case cmPolicies::NEW:
  4704. // Issue the fatal message.
  4705. break;
  4706. }
  4707. if (!noMessage) {
  4708. e << "Target \"" << this->GetName() << "\" links to itself.";
  4709. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4710. messageType, e.str(), this->GetBacktrace());
  4711. if (messageType == cmake::FATAL_ERROR) {
  4712. return;
  4713. }
  4714. }
  4715. }
  4716. continue;
  4717. }
  4718. // The entry is meant for this configuration.
  4719. impl.Libraries.push_back(cmLinkImplItem(
  4720. name, this->FindTargetToLink(name), *btIt, evaluated != *le));
  4721. }
  4722. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  4723. for (std::string const& sp : seenProps) {
  4724. if (!this->GetProperty(sp)) {
  4725. this->LinkImplicitNullProperties.insert(sp);
  4726. }
  4727. }
  4728. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  4729. }
  4730. // Get the list of configurations considered to be DEBUG.
  4731. std::vector<std::string> debugConfigs =
  4732. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  4733. cmTargetLinkLibraryType linkType =
  4734. CMP0003_ComputeLinkType(config, debugConfigs);
  4735. cmTarget::LinkLibraryVectorType const& oldllibs =
  4736. this->Target->GetOriginalLinkLibraries();
  4737. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  4738. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  4739. std::string name = this->CheckCMP0004(oldllib.first);
  4740. if (name == this->GetName() || name.empty()) {
  4741. continue;
  4742. }
  4743. // Support OLD behavior for CMP0003.
  4744. impl.WrongConfigLibraries.push_back(
  4745. cmLinkItem(name, this->FindTargetToLink(name)));
  4746. }
  4747. }
  4748. }
  4749. cmGeneratorTarget* cmGeneratorTarget::FindTargetToLink(
  4750. std::string const& name) const
  4751. {
  4752. cmGeneratorTarget* tgt =
  4753. this->LocalGenerator->FindGeneratorTargetToUse(name);
  4754. // Skip targets that will not really be linked. This is probably a
  4755. // name conflict between an external library and an executable
  4756. // within the project.
  4757. if (tgt && tgt->GetType() == cmStateEnums::EXECUTABLE &&
  4758. !tgt->IsExecutableWithExports()) {
  4759. tgt = nullptr;
  4760. }
  4761. if (tgt && tgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  4762. std::ostringstream e;
  4763. e << "Target \"" << this->GetName() << "\" links to "
  4764. "OBJECT library \""
  4765. << tgt->GetName()
  4766. << "\" but this is not "
  4767. "allowed. "
  4768. "One may link only to STATIC or SHARED libraries, or to executables "
  4769. "with the ENABLE_EXPORTS property set.";
  4770. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  4771. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4772. tgt = nullptr;
  4773. }
  4774. return tgt;
  4775. }
  4776. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  4777. {
  4778. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  4779. // Return the directory in which the target will be built.
  4780. return info->PdbDir;
  4781. }
  4782. return "";
  4783. }
  4784. bool cmGeneratorTarget::HasImplibGNUtoMS() const
  4785. {
  4786. return this->HasImportLibrary() && this->GetPropertyAsBool("GNUtoMS");
  4787. }
  4788. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& gnuName,
  4789. std::string& out,
  4790. const char* newExt) const
  4791. {
  4792. if (this->HasImplibGNUtoMS() && gnuName.size() > 6 &&
  4793. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  4794. out = gnuName.substr(0, gnuName.size() - 6);
  4795. out += newExt ? newExt : ".lib";
  4796. return true;
  4797. }
  4798. return false;
  4799. }
  4800. bool cmGeneratorTarget::IsExecutableWithExports() const
  4801. {
  4802. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  4803. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  4804. }
  4805. bool cmGeneratorTarget::HasImportLibrary() const
  4806. {
  4807. return (this->IsDLLPlatform() &&
  4808. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4809. this->IsExecutableWithExports()));
  4810. }
  4811. std::string cmGeneratorTarget::GetSupportDirectory() const
  4812. {
  4813. std::string dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  4814. dir += cmake::GetCMakeFilesDirectory();
  4815. dir += "/";
  4816. dir += this->GetName();
  4817. #if defined(__VMS)
  4818. dir += "_dir";
  4819. #else
  4820. dir += ".dir";
  4821. #endif
  4822. return dir;
  4823. }
  4824. bool cmGeneratorTarget::IsLinkable() const
  4825. {
  4826. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4827. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4828. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  4829. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  4830. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  4831. this->IsExecutableWithExports());
  4832. }
  4833. bool cmGeneratorTarget::IsFrameworkOnApple() const
  4834. {
  4835. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4836. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  4837. this->Makefile->IsOn("APPLE") &&
  4838. this->GetPropertyAsBool("FRAMEWORK"));
  4839. }
  4840. bool cmGeneratorTarget::IsAppBundleOnApple() const
  4841. {
  4842. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  4843. this->Makefile->IsOn("APPLE") &&
  4844. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  4845. }
  4846. bool cmGeneratorTarget::IsXCTestOnApple() const
  4847. {
  4848. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  4849. }
  4850. bool cmGeneratorTarget::IsCFBundleOnApple() const
  4851. {
  4852. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  4853. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  4854. }