cmGeneratorTarget.cxx 191 KB

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