cmGeneratorTarget.cxx 193 KB

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