cmGeneratorTarget.cxx 193 KB

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