cmGeneratorTarget.cxx 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709
  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. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  2673. {
  2674. if (this->IsImported()) {
  2675. return;
  2676. }
  2677. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  2678. // Get the names.
  2679. std::string name;
  2680. std::string soName;
  2681. std::string realName;
  2682. std::string impName;
  2683. std::string pdbName;
  2684. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2685. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2686. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2687. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2688. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2689. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2690. } else {
  2691. return;
  2692. }
  2693. // Get the directory.
  2694. std::string dir =
  2695. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  2696. // Add each name.
  2697. std::string f;
  2698. if (!name.empty()) {
  2699. f = dir;
  2700. f += "/";
  2701. f += name;
  2702. gg->AddToManifest(f);
  2703. }
  2704. if (!soName.empty()) {
  2705. f = dir;
  2706. f += "/";
  2707. f += soName;
  2708. gg->AddToManifest(f);
  2709. }
  2710. if (!realName.empty()) {
  2711. f = dir;
  2712. f += "/";
  2713. f += realName;
  2714. gg->AddToManifest(f);
  2715. }
  2716. if (!pdbName.empty()) {
  2717. f = dir;
  2718. f += "/";
  2719. f += pdbName;
  2720. gg->AddToManifest(f);
  2721. }
  2722. if (!impName.empty()) {
  2723. f = this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  2724. f += "/";
  2725. f += impName;
  2726. gg->AddToManifest(f);
  2727. }
  2728. }
  2729. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  2730. {
  2731. std::vector<std::string> features;
  2732. this->GetCompileFeatures(features, config);
  2733. for (std::string const& f : features) {
  2734. if (!this->Makefile->AddRequiredTargetFeature(this->Target, f)) {
  2735. return false;
  2736. }
  2737. }
  2738. return true;
  2739. }
  2740. std::string cmGeneratorTarget::GetImportedLibName(
  2741. std::string const& config) const
  2742. {
  2743. if (cmGeneratorTarget::ImportInfo const* info =
  2744. this->GetImportInfo(config)) {
  2745. return info->LibName;
  2746. }
  2747. return std::string();
  2748. }
  2749. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  2750. cmStateEnums::ArtifactType artifact,
  2751. bool realname) const
  2752. {
  2753. if (this->IsImported()) {
  2754. return this->Target->ImportedGetFullPath(config, artifact);
  2755. }
  2756. return this->NormalGetFullPath(config, artifact, realname);
  2757. }
  2758. std::string cmGeneratorTarget::NormalGetFullPath(
  2759. const std::string& config, cmStateEnums::ArtifactType artifact,
  2760. bool realname) const
  2761. {
  2762. std::string fpath = this->GetDirectory(config, artifact);
  2763. fpath += "/";
  2764. if (this->IsAppBundleOnApple()) {
  2765. fpath = this->BuildBundleDirectory(fpath, config, FullLevel);
  2766. fpath += "/";
  2767. }
  2768. // Add the full name of the target.
  2769. switch (artifact) {
  2770. case cmStateEnums::RuntimeBinaryArtifact:
  2771. if (realname) {
  2772. fpath += this->NormalGetRealName(config);
  2773. } else {
  2774. fpath +=
  2775. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  2776. }
  2777. break;
  2778. case cmStateEnums::ImportLibraryArtifact:
  2779. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  2780. break;
  2781. }
  2782. return fpath;
  2783. }
  2784. std::string cmGeneratorTarget::NormalGetRealName(
  2785. const std::string& config) const
  2786. {
  2787. // This should not be called for imported targets.
  2788. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2789. // enforcement of the limited imported target API.
  2790. if (this->IsImported()) {
  2791. std::string msg = "NormalGetRealName called on imported target: ";
  2792. msg += this->GetName();
  2793. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2794. }
  2795. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2796. // Compute the real name that will be built.
  2797. std::string name;
  2798. std::string realName;
  2799. std::string impName;
  2800. std::string pdbName;
  2801. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2802. return realName;
  2803. }
  2804. // Compute the real name that will be built.
  2805. std::string name;
  2806. std::string soName;
  2807. std::string realName;
  2808. std::string impName;
  2809. std::string pdbName;
  2810. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2811. return realName;
  2812. }
  2813. void cmGeneratorTarget::GetLibraryNames(std::string& name, std::string& soName,
  2814. std::string& realName,
  2815. std::string& impName,
  2816. std::string& pdbName,
  2817. const std::string& config) const
  2818. {
  2819. // This should not be called for imported targets.
  2820. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2821. // enforcement of the limited imported target API.
  2822. if (this->IsImported()) {
  2823. std::string msg = "GetLibraryNames called on imported target: ";
  2824. msg += this->GetName();
  2825. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2826. return;
  2827. }
  2828. // Check for library version properties.
  2829. const char* version = this->GetProperty("VERSION");
  2830. const char* soversion = this->GetProperty("SOVERSION");
  2831. if (!this->HasSOName(config) ||
  2832. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  2833. this->IsFrameworkOnApple()) {
  2834. // Versioning is supported only for shared libraries and modules,
  2835. // and then only when the platform supports an soname flag.
  2836. version = nullptr;
  2837. soversion = nullptr;
  2838. }
  2839. if (version && !soversion) {
  2840. // The soversion must be set if the library version is set. Use
  2841. // the library version as the soversion.
  2842. soversion = version;
  2843. }
  2844. if (!version && soversion) {
  2845. // Use the soversion as the library version.
  2846. version = soversion;
  2847. }
  2848. // Get the components of the library name.
  2849. std::string prefix;
  2850. std::string base;
  2851. std::string suffix;
  2852. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2853. prefix, base, suffix);
  2854. // The library name.
  2855. name = prefix + base + suffix;
  2856. if (this->IsFrameworkOnApple()) {
  2857. realName = prefix;
  2858. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  2859. realName += "Versions/";
  2860. realName += this->GetFrameworkVersion();
  2861. realName += "/";
  2862. }
  2863. realName += base;
  2864. soName = realName;
  2865. } else {
  2866. // The library's soname.
  2867. this->ComputeVersionedName(soName, prefix, base, suffix, name, soversion);
  2868. // The library's real name on disk.
  2869. this->ComputeVersionedName(realName, prefix, base, suffix, name, version);
  2870. }
  2871. // The import library name.
  2872. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2873. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2874. impName =
  2875. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2876. } else {
  2877. impName.clear();
  2878. }
  2879. // The program database file name.
  2880. pdbName = this->GetPDBName(config);
  2881. }
  2882. void cmGeneratorTarget::GetExecutableNames(std::string& name,
  2883. std::string& realName,
  2884. std::string& impName,
  2885. std::string& pdbName,
  2886. const std::string& config) const
  2887. {
  2888. // This should not be called for imported targets.
  2889. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2890. // enforcement of the limited imported target API.
  2891. if (this->IsImported()) {
  2892. std::string msg = "GetExecutableNames called on imported target: ";
  2893. msg += this->GetName();
  2894. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2895. }
  2896. // This versioning is supported only for executables and then only
  2897. // when the platform supports symbolic links.
  2898. #if defined(_WIN32) && !defined(__CYGWIN__)
  2899. const char* version = 0;
  2900. #else
  2901. // Check for executable version properties.
  2902. const char* version = this->GetProperty("VERSION");
  2903. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  2904. this->Makefile->IsOn("XCODE")) {
  2905. version = nullptr;
  2906. }
  2907. #endif
  2908. // Get the components of the executable name.
  2909. std::string prefix;
  2910. std::string base;
  2911. std::string suffix;
  2912. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2913. prefix, base, suffix);
  2914. // The executable name.
  2915. name = prefix + base + suffix;
  2916. // The executable's real name on disk.
  2917. #if defined(__CYGWIN__)
  2918. realName = prefix + base;
  2919. #else
  2920. realName = name;
  2921. #endif
  2922. if (version) {
  2923. realName += "-";
  2924. realName += version;
  2925. }
  2926. #if defined(__CYGWIN__)
  2927. realName += suffix;
  2928. #endif
  2929. // The import library name.
  2930. impName =
  2931. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2932. // The program database file name.
  2933. pdbName = this->GetPDBName(config);
  2934. }
  2935. std::string cmGeneratorTarget::GetFullNameInternal(
  2936. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2937. {
  2938. std::string prefix;
  2939. std::string base;
  2940. std::string suffix;
  2941. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  2942. return prefix + base + suffix;
  2943. }
  2944. const char* cmGeneratorTarget::ImportedGetLocation(
  2945. const std::string& config) const
  2946. {
  2947. static std::string location;
  2948. assert(this->IsImported());
  2949. location = this->Target->ImportedGetFullPath(
  2950. config, cmStateEnums::RuntimeBinaryArtifact);
  2951. return location.c_str();
  2952. }
  2953. std::string cmGeneratorTarget::GetFullNameImported(
  2954. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2955. {
  2956. return cmSystemTools::GetFilenameName(
  2957. this->Target->ImportedGetFullPath(config, artifact));
  2958. }
  2959. void cmGeneratorTarget::GetFullNameInternal(
  2960. const std::string& config, cmStateEnums::ArtifactType artifact,
  2961. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  2962. {
  2963. // Use just the target name for non-main target types.
  2964. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  2965. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2966. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2967. this->GetType() != cmStateEnums::EXECUTABLE) {
  2968. outPrefix.clear();
  2969. outBase = this->GetName();
  2970. outSuffix.clear();
  2971. return;
  2972. }
  2973. const bool isImportedLibraryArtifact =
  2974. (artifact == cmStateEnums::ImportLibraryArtifact);
  2975. // Return an empty name for the import library if this platform
  2976. // does not support import libraries.
  2977. if (isImportedLibraryArtifact &&
  2978. !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  2979. outPrefix.clear();
  2980. outBase.clear();
  2981. outSuffix.clear();
  2982. return;
  2983. }
  2984. // The implib option is only allowed for shared libraries, module
  2985. // libraries, and executables.
  2986. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2987. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2988. this->GetType() != cmStateEnums::EXECUTABLE) {
  2989. artifact = cmStateEnums::RuntimeBinaryArtifact;
  2990. }
  2991. // Compute the full name for main target types.
  2992. const char* targetPrefix =
  2993. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
  2994. : this->GetProperty("PREFIX"));
  2995. const char* targetSuffix =
  2996. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
  2997. : this->GetProperty("SUFFIX"));
  2998. const char* configPostfix = nullptr;
  2999. if (!config.empty()) {
  3000. std::string configProp = cmSystemTools::UpperCase(config);
  3001. configProp += "_POSTFIX";
  3002. configPostfix = this->GetProperty(configProp);
  3003. // Mac application bundles and frameworks have no postfix.
  3004. if (configPostfix &&
  3005. (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
  3006. configPostfix = nullptr;
  3007. }
  3008. }
  3009. const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
  3010. const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
  3011. // Check for language-specific default prefix and suffix.
  3012. std::string ll = this->GetLinkerLanguage(config);
  3013. if (!ll.empty()) {
  3014. if (!targetSuffix && suffixVar && *suffixVar) {
  3015. std::string langSuff = suffixVar + std::string("_") + ll;
  3016. targetSuffix = this->Makefile->GetDefinition(langSuff);
  3017. }
  3018. if (!targetPrefix && prefixVar && *prefixVar) {
  3019. std::string langPrefix = prefixVar + std::string("_") + ll;
  3020. targetPrefix = this->Makefile->GetDefinition(langPrefix);
  3021. }
  3022. }
  3023. // if there is no prefix on the target use the cmake definition
  3024. if (!targetPrefix && prefixVar) {
  3025. targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
  3026. }
  3027. // if there is no suffix on the target use the cmake definition
  3028. if (!targetSuffix && suffixVar) {
  3029. targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
  3030. }
  3031. // frameworks have directory prefix but no suffix
  3032. std::string fw_prefix;
  3033. if (this->IsFrameworkOnApple()) {
  3034. fw_prefix = this->GetFrameworkDirectory(config, ContentLevel);
  3035. fw_prefix += "/";
  3036. targetPrefix = fw_prefix.c_str();
  3037. targetSuffix = nullptr;
  3038. }
  3039. if (this->IsCFBundleOnApple()) {
  3040. fw_prefix = this->GetCFBundleDirectory(config, FullLevel);
  3041. fw_prefix += "/";
  3042. targetPrefix = fw_prefix.c_str();
  3043. targetSuffix = nullptr;
  3044. }
  3045. // Begin the final name with the prefix.
  3046. outPrefix = targetPrefix ? targetPrefix : "";
  3047. // Append the target name or property-specified name.
  3048. outBase += this->GetOutputName(config, artifact);
  3049. // Append the per-configuration postfix.
  3050. outBase += configPostfix ? configPostfix : "";
  3051. // Name shared libraries with their version number on some platforms.
  3052. if (const char* soversion = this->GetProperty("SOVERSION")) {
  3053. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  3054. !isImportedLibraryArtifact &&
  3055. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  3056. outBase += "-";
  3057. outBase += soversion;
  3058. }
  3059. }
  3060. // Append the suffix.
  3061. outSuffix = targetSuffix ? targetSuffix : "";
  3062. }
  3063. std::string cmGeneratorTarget::GetLinkerLanguage(
  3064. const std::string& config) const
  3065. {
  3066. return this->GetLinkClosure(config)->LinkerLanguage;
  3067. }
  3068. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  3069. {
  3070. std::string prefix;
  3071. std::string base;
  3072. std::string suffix;
  3073. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  3074. prefix, base, suffix);
  3075. std::vector<std::string> props;
  3076. std::string configUpper = cmSystemTools::UpperCase(config);
  3077. if (!configUpper.empty()) {
  3078. // PDB_NAME_<CONFIG>
  3079. props.push_back("PDB_NAME_" + configUpper);
  3080. }
  3081. // PDB_NAME
  3082. props.push_back("PDB_NAME");
  3083. for (std::string const& p : props) {
  3084. if (const char* outName = this->GetProperty(p)) {
  3085. base = outName;
  3086. break;
  3087. }
  3088. }
  3089. return prefix + base + ".pdb";
  3090. }
  3091. std::string cmGeneratorTarget::GetObjectDirectory(
  3092. std::string const& config) const
  3093. {
  3094. std::string obj_dir =
  3095. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  3096. #if defined(__APPLE__)
  3097. // find and replace $(PROJECT_NAME) xcode placeholder
  3098. const std::string projectName = this->LocalGenerator->GetProjectName();
  3099. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  3100. #endif
  3101. return obj_dir;
  3102. }
  3103. void cmGeneratorTarget::GetTargetObjectNames(
  3104. std::string const& config, std::vector<std::string>& objects) const
  3105. {
  3106. std::vector<cmSourceFile const*> objectSources;
  3107. this->GetObjectSources(objectSources, config);
  3108. std::map<cmSourceFile const*, std::string> mapping;
  3109. for (cmSourceFile const* sf : objectSources) {
  3110. mapping[sf];
  3111. }
  3112. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  3113. for (cmSourceFile const* src : objectSources) {
  3114. // Find the object file name corresponding to this source file.
  3115. std::map<cmSourceFile const*, std::string>::const_iterator map_it =
  3116. mapping.find(src);
  3117. // It must exist because we populated the mapping just above.
  3118. assert(!map_it->second.empty());
  3119. objects.push_back(map_it->second);
  3120. }
  3121. }
  3122. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  3123. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  3124. {
  3125. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  3126. if (nameResult == 0) {
  3127. return strcmp(t1->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  3128. t2->GetLocalGenerator()->GetCurrentBinaryDirectory()) < 0;
  3129. }
  3130. return nameResult < 0;
  3131. }
  3132. struct cmGeneratorTarget::SourceFileFlags
  3133. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  3134. {
  3135. struct SourceFileFlags flags;
  3136. this->ConstructSourceFileFlags();
  3137. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  3138. this->SourceFlagsMap.find(sf);
  3139. if (si != this->SourceFlagsMap.end()) {
  3140. flags = si->second;
  3141. } else {
  3142. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  3143. // were not listed in one of the other lists.
  3144. if (const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  3145. flags.MacFolder = location;
  3146. const bool stripResources =
  3147. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  3148. if (strcmp(location, "Resources") == 0) {
  3149. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3150. if (stripResources) {
  3151. flags.MacFolder = "";
  3152. }
  3153. } else if (cmSystemTools::StringStartsWith(location, "Resources/")) {
  3154. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  3155. if (stripResources) {
  3156. flags.MacFolder += strlen("Resources/");
  3157. }
  3158. } else {
  3159. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  3160. }
  3161. }
  3162. }
  3163. return flags;
  3164. }
  3165. void cmGeneratorTarget::ConstructSourceFileFlags() const
  3166. {
  3167. if (this->SourceFileFlagsConstructed) {
  3168. return;
  3169. }
  3170. this->SourceFileFlagsConstructed = true;
  3171. // Process public headers to mark the source files.
  3172. if (const char* files = this->GetProperty("PUBLIC_HEADER")) {
  3173. std::vector<std::string> relFiles;
  3174. cmSystemTools::ExpandListArgument(files, relFiles);
  3175. for (std::string const& relFile : relFiles) {
  3176. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3177. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3178. flags.MacFolder = "Headers";
  3179. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  3180. }
  3181. }
  3182. }
  3183. // Process private headers after public headers so that they take
  3184. // precedence if a file is listed in both.
  3185. if (const char* files = this->GetProperty("PRIVATE_HEADER")) {
  3186. std::vector<std::string> relFiles;
  3187. cmSystemTools::ExpandListArgument(files, relFiles);
  3188. for (std::string const& relFile : relFiles) {
  3189. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3190. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3191. flags.MacFolder = "PrivateHeaders";
  3192. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  3193. }
  3194. }
  3195. }
  3196. // Mark sources listed as resources.
  3197. if (const char* files = this->GetProperty("RESOURCE")) {
  3198. std::vector<std::string> relFiles;
  3199. cmSystemTools::ExpandListArgument(files, relFiles);
  3200. for (std::string const& relFile : relFiles) {
  3201. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3202. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3203. flags.MacFolder = "";
  3204. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  3205. flags.MacFolder = "Resources";
  3206. }
  3207. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3208. }
  3209. }
  3210. }
  3211. }
  3212. const cmGeneratorTarget::CompatibleInterfacesBase&
  3213. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  3214. {
  3215. cmGeneratorTarget::CompatibleInterfaces& compat =
  3216. this->CompatibleInterfacesMap[config];
  3217. if (!compat.Done) {
  3218. compat.Done = true;
  3219. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  3220. compat.PropsString.insert("AUTOUIC_OPTIONS");
  3221. std::vector<cmGeneratorTarget const*> const& deps =
  3222. this->GetLinkImplementationClosure(config);
  3223. for (cmGeneratorTarget const* li : deps) {
  3224. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  3225. if (const char* prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  3226. std::vector<std::string> props; \
  3227. cmSystemTools::ExpandListArgument(prop, props); \
  3228. compat.Props##x.insert(props.begin(), props.end()); \
  3229. }
  3230. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  3231. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  3232. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  3233. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  3234. #undef CM_READ_COMPATIBLE_INTERFACE
  3235. }
  3236. }
  3237. return compat;
  3238. }
  3239. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  3240. const std::string& p, const std::string& config) const
  3241. {
  3242. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3243. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3244. return false;
  3245. }
  3246. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  3247. }
  3248. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  3249. const std::string& p, const std::string& config) const
  3250. {
  3251. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3252. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3253. return false;
  3254. }
  3255. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  3256. }
  3257. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  3258. const std::string& p, const std::string& config) const
  3259. {
  3260. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3261. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3262. return false;
  3263. }
  3264. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  3265. }
  3266. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  3267. const std::string& p, const std::string& config) const
  3268. {
  3269. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3270. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3271. return false;
  3272. }
  3273. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  3274. }
  3275. enum CompatibleType
  3276. {
  3277. BoolType,
  3278. StringType,
  3279. NumberMinType,
  3280. NumberMaxType
  3281. };
  3282. template <typename PropertyType>
  3283. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3284. const std::string& prop,
  3285. const std::string& config,
  3286. CompatibleType, PropertyType*);
  3287. template <>
  3288. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3289. const std::string& prop,
  3290. const std::string& config,
  3291. CompatibleType /*unused*/,
  3292. bool* /*unused*/)
  3293. {
  3294. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  3295. }
  3296. template <>
  3297. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3298. const std::string& prop,
  3299. const std::string& config,
  3300. CompatibleType t,
  3301. const char** /*unused*/)
  3302. {
  3303. switch (t) {
  3304. case BoolType:
  3305. assert(false &&
  3306. "String compatibility check function called for boolean");
  3307. return nullptr;
  3308. case StringType:
  3309. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  3310. case NumberMinType:
  3311. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  3312. case NumberMaxType:
  3313. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  3314. }
  3315. assert(false && "Unreachable!");
  3316. return nullptr;
  3317. }
  3318. template <typename PropertyType>
  3319. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  3320. cmGeneratorTarget const* dependee,
  3321. const std::string& propName,
  3322. std::set<std::string>& emitted,
  3323. const std::string& config, CompatibleType t,
  3324. PropertyType* /*unused*/)
  3325. {
  3326. const char* prop = dependee->GetProperty(propName);
  3327. if (!prop) {
  3328. return;
  3329. }
  3330. std::vector<std::string> props;
  3331. cmSystemTools::ExpandListArgument(prop, props);
  3332. std::string pdir = cmSystemTools::GetCMakeRoot();
  3333. pdir += "/Help/prop_tgt/";
  3334. for (std::string const& p : props) {
  3335. std::string pname = cmSystemTools::HelpFileName(p);
  3336. std::string pfile = pdir + pname + ".rst";
  3337. if (cmSystemTools::FileExists(pfile, true)) {
  3338. std::ostringstream e;
  3339. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  3340. << "\" listed in its " << propName
  3341. << " property. "
  3342. "This is not allowed. Only user-defined properties may appear "
  3343. "listed in the "
  3344. << propName << " property.";
  3345. depender->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, e.str());
  3346. return;
  3347. }
  3348. if (emitted.insert(p).second) {
  3349. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  3350. nullptr);
  3351. if (cmSystemTools::GetErrorOccuredFlag()) {
  3352. return;
  3353. }
  3354. }
  3355. }
  3356. }
  3357. static std::string intersect(const std::set<std::string>& s1,
  3358. const std::set<std::string>& s2)
  3359. {
  3360. std::set<std::string> intersect;
  3361. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  3362. std::inserter(intersect, intersect.begin()));
  3363. if (!intersect.empty()) {
  3364. return *intersect.begin();
  3365. }
  3366. return "";
  3367. }
  3368. static std::string intersect(const std::set<std::string>& s1,
  3369. const std::set<std::string>& s2,
  3370. const std::set<std::string>& s3)
  3371. {
  3372. std::string result;
  3373. result = intersect(s1, s2);
  3374. if (!result.empty()) {
  3375. return result;
  3376. }
  3377. result = intersect(s1, s3);
  3378. if (!result.empty()) {
  3379. return result;
  3380. }
  3381. return intersect(s2, s3);
  3382. }
  3383. static std::string intersect(const std::set<std::string>& s1,
  3384. const std::set<std::string>& s2,
  3385. const std::set<std::string>& s3,
  3386. const std::set<std::string>& s4)
  3387. {
  3388. std::string result;
  3389. result = intersect(s1, s2);
  3390. if (!result.empty()) {
  3391. return result;
  3392. }
  3393. result = intersect(s1, s3);
  3394. if (!result.empty()) {
  3395. return result;
  3396. }
  3397. result = intersect(s1, s4);
  3398. if (!result.empty()) {
  3399. return result;
  3400. }
  3401. return intersect(s2, s3, s4);
  3402. }
  3403. void cmGeneratorTarget::CheckPropertyCompatibility(
  3404. cmComputeLinkInformation* info, const std::string& config) const
  3405. {
  3406. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  3407. std::set<std::string> emittedBools;
  3408. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  3409. std::set<std::string> emittedStrings;
  3410. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  3411. std::set<std::string> emittedMinNumbers;
  3412. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  3413. std::set<std::string> emittedMaxNumbers;
  3414. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  3415. for (auto const& dep : deps) {
  3416. if (!dep.Target) {
  3417. continue;
  3418. }
  3419. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  3420. config, BoolType, nullptr);
  3421. if (cmSystemTools::GetErrorOccuredFlag()) {
  3422. return;
  3423. }
  3424. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  3425. emittedStrings, config, StringType,
  3426. nullptr);
  3427. if (cmSystemTools::GetErrorOccuredFlag()) {
  3428. return;
  3429. }
  3430. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  3431. emittedMinNumbers, config,
  3432. NumberMinType, nullptr);
  3433. if (cmSystemTools::GetErrorOccuredFlag()) {
  3434. return;
  3435. }
  3436. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  3437. emittedMaxNumbers, config,
  3438. NumberMaxType, nullptr);
  3439. if (cmSystemTools::GetErrorOccuredFlag()) {
  3440. return;
  3441. }
  3442. }
  3443. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  3444. emittedMaxNumbers);
  3445. if (!prop.empty()) {
  3446. // Use a sorted std::vector to keep the error message sorted.
  3447. std::vector<std::string> props;
  3448. std::set<std::string>::const_iterator i = emittedBools.find(prop);
  3449. if (i != emittedBools.end()) {
  3450. props.push_back(strBool);
  3451. }
  3452. i = emittedStrings.find(prop);
  3453. if (i != emittedStrings.end()) {
  3454. props.push_back(strString);
  3455. }
  3456. i = emittedMinNumbers.find(prop);
  3457. if (i != emittedMinNumbers.end()) {
  3458. props.push_back(strNumMin);
  3459. }
  3460. i = emittedMaxNumbers.find(prop);
  3461. if (i != emittedMaxNumbers.end()) {
  3462. props.push_back(strNumMax);
  3463. }
  3464. std::sort(props.begin(), props.end());
  3465. std::string propsString = cmJoin(cmMakeRange(props).retreat(1), ", ");
  3466. propsString += " and the " + props.back();
  3467. std::ostringstream e;
  3468. e << "Property \"" << prop << "\" appears in both the " << propsString
  3469. << " property in the dependencies of target \"" << this->GetName()
  3470. << "\". This is not allowed. A property may only require compatibility "
  3471. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  3472. "or a "
  3473. "string interpretation, but not a mixture.";
  3474. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  3475. }
  3476. }
  3477. std::string compatibilityType(CompatibleType t)
  3478. {
  3479. switch (t) {
  3480. case BoolType:
  3481. return "Boolean compatibility";
  3482. case StringType:
  3483. return "String compatibility";
  3484. case NumberMaxType:
  3485. return "Numeric maximum compatibility";
  3486. case NumberMinType:
  3487. return "Numeric minimum compatibility";
  3488. }
  3489. assert(false && "Unreachable!");
  3490. return "";
  3491. }
  3492. std::string compatibilityAgree(CompatibleType t, bool dominant)
  3493. {
  3494. switch (t) {
  3495. case BoolType:
  3496. case StringType:
  3497. return dominant ? "(Disagree)\n" : "(Agree)\n";
  3498. case NumberMaxType:
  3499. case NumberMinType:
  3500. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  3501. }
  3502. assert(false && "Unreachable!");
  3503. return "";
  3504. }
  3505. template <typename PropertyType>
  3506. PropertyType getTypedProperty(cmGeneratorTarget const* tgt,
  3507. const std::string& prop);
  3508. template <>
  3509. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  3510. const std::string& prop)
  3511. {
  3512. return tgt->GetPropertyAsBool(prop);
  3513. }
  3514. template <>
  3515. const char* getTypedProperty<const char*>(cmGeneratorTarget const* tgt,
  3516. const std::string& prop)
  3517. {
  3518. return tgt->GetProperty(prop);
  3519. }
  3520. template <typename PropertyType>
  3521. std::string valueAsString(PropertyType);
  3522. template <>
  3523. std::string valueAsString<bool>(bool value)
  3524. {
  3525. return value ? "TRUE" : "FALSE";
  3526. }
  3527. template <>
  3528. std::string valueAsString<const char*>(const char* value)
  3529. {
  3530. return value ? value : "(unset)";
  3531. }
  3532. template <typename PropertyType>
  3533. PropertyType impliedValue(PropertyType);
  3534. template <>
  3535. bool impliedValue<bool>(bool /*unused*/)
  3536. {
  3537. return false;
  3538. }
  3539. template <>
  3540. const char* impliedValue<const char*>(const char* /*unused*/)
  3541. {
  3542. return "";
  3543. }
  3544. template <typename PropertyType>
  3545. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  3546. PropertyType rhs,
  3547. CompatibleType t);
  3548. template <>
  3549. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  3550. CompatibleType /*unused*/)
  3551. {
  3552. return std::make_pair(lhs == rhs, lhs);
  3553. }
  3554. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  3555. const char* rhs)
  3556. {
  3557. const bool b = strcmp(lhs, rhs) == 0;
  3558. return std::make_pair(b, b ? lhs : nullptr);
  3559. }
  3560. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  3561. const char* rhs,
  3562. CompatibleType t)
  3563. {
  3564. char* pEnd;
  3565. const char* const null_ptr = nullptr;
  3566. long lnum = strtol(lhs, &pEnd, 0);
  3567. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  3568. return std::pair<bool, const char*>(false, null_ptr);
  3569. }
  3570. long rnum = strtol(rhs, &pEnd, 0);
  3571. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  3572. return std::pair<bool, const char*>(false, null_ptr);
  3573. }
  3574. if (t == NumberMaxType) {
  3575. return std::make_pair(true, std::max(lnum, rnum) == lnum ? lhs : rhs);
  3576. }
  3577. return std::make_pair(true, std::min(lnum, rnum) == lnum ? lhs : rhs);
  3578. }
  3579. template <>
  3580. std::pair<bool, const char*> consistentProperty(const char* lhs,
  3581. const char* rhs,
  3582. CompatibleType t)
  3583. {
  3584. if (!lhs && !rhs) {
  3585. return std::make_pair(true, lhs);
  3586. }
  3587. if (!lhs) {
  3588. return std::make_pair(true, rhs);
  3589. }
  3590. if (!rhs) {
  3591. return std::make_pair(true, lhs);
  3592. }
  3593. const char* const null_ptr = nullptr;
  3594. switch (t) {
  3595. case BoolType:
  3596. assert(false && "consistentProperty for strings called with BoolType");
  3597. return std::pair<bool, const char*>(false, null_ptr);
  3598. case StringType:
  3599. return consistentStringProperty(lhs, rhs);
  3600. case NumberMinType:
  3601. case NumberMaxType:
  3602. return consistentNumberProperty(lhs, rhs, t);
  3603. }
  3604. assert(false && "Unreachable!");
  3605. return std::pair<bool, const char*>(false, null_ptr);
  3606. }
  3607. template <typename PropertyType>
  3608. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  3609. const std::string& p,
  3610. const std::string& config,
  3611. const char* defaultValue,
  3612. CompatibleType t,
  3613. PropertyType* /*unused*/)
  3614. {
  3615. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  3616. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  3617. const bool explicitlySet =
  3618. std::find(headPropKeys.begin(), headPropKeys.end(), p) !=
  3619. headPropKeys.end();
  3620. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  3621. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  3622. std::vector<cmGeneratorTarget const*> const& deps =
  3623. tgt->GetLinkImplementationClosure(config);
  3624. if (deps.empty()) {
  3625. return propContent;
  3626. }
  3627. bool propInitialized = explicitlySet;
  3628. std::string report = " * Target \"";
  3629. report += tgt->GetName();
  3630. if (explicitlySet) {
  3631. report += "\" has property content \"";
  3632. report += valueAsString<PropertyType>(propContent);
  3633. report += "\"\n";
  3634. } else if (impliedByUse) {
  3635. report += "\" property is implied by use.\n";
  3636. } else {
  3637. report += "\" property not set.\n";
  3638. }
  3639. std::string interfaceProperty = "INTERFACE_" + p;
  3640. for (cmGeneratorTarget const* theTarget : deps) {
  3641. // An error should be reported if one dependency
  3642. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  3643. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  3644. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  3645. // with a dependency.
  3646. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  3647. const bool ifaceIsSet = std::find(propKeys.begin(), propKeys.end(),
  3648. interfaceProperty) != propKeys.end();
  3649. PropertyType ifacePropContent =
  3650. getTypedProperty<PropertyType>(theTarget, interfaceProperty);
  3651. std::string reportEntry;
  3652. if (ifaceIsSet) {
  3653. reportEntry += " * Target \"";
  3654. reportEntry += theTarget->GetName();
  3655. reportEntry += "\" property value \"";
  3656. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  3657. reportEntry += "\" ";
  3658. }
  3659. if (explicitlySet) {
  3660. if (ifaceIsSet) {
  3661. std::pair<bool, PropertyType> consistent =
  3662. consistentProperty(propContent, ifacePropContent, t);
  3663. report += reportEntry;
  3664. report += compatibilityAgree(t, propContent != consistent.second);
  3665. if (!consistent.first) {
  3666. std::ostringstream e;
  3667. e << "Property " << p << " on target \"" << tgt->GetName()
  3668. << "\" does\nnot match the "
  3669. "INTERFACE_"
  3670. << p
  3671. << " property requirement\nof "
  3672. "dependency \""
  3673. << theTarget->GetName() << "\".\n";
  3674. cmSystemTools::Error(e.str().c_str());
  3675. break;
  3676. }
  3677. propContent = consistent.second;
  3678. continue;
  3679. }
  3680. // Explicitly set on target and not set in iface. Can't disagree.
  3681. continue;
  3682. }
  3683. if (impliedByUse) {
  3684. propContent = impliedValue<PropertyType>(propContent);
  3685. if (ifaceIsSet) {
  3686. std::pair<bool, PropertyType> consistent =
  3687. consistentProperty(propContent, ifacePropContent, t);
  3688. report += reportEntry;
  3689. report += compatibilityAgree(t, propContent != consistent.second);
  3690. if (!consistent.first) {
  3691. std::ostringstream e;
  3692. e << "Property " << p << " on target \"" << tgt->GetName()
  3693. << "\" is\nimplied to be " << defaultValue
  3694. << " because it was used to determine the link libraries\n"
  3695. "already. The INTERFACE_"
  3696. << p << " property on\ndependency \"" << theTarget->GetName()
  3697. << "\" is in conflict.\n";
  3698. cmSystemTools::Error(e.str().c_str());
  3699. break;
  3700. }
  3701. propContent = consistent.second;
  3702. continue;
  3703. }
  3704. // Implicitly set on target and not set in iface. Can't disagree.
  3705. continue;
  3706. }
  3707. if (ifaceIsSet) {
  3708. if (propInitialized) {
  3709. std::pair<bool, PropertyType> consistent =
  3710. consistentProperty(propContent, ifacePropContent, t);
  3711. report += reportEntry;
  3712. report += compatibilityAgree(t, propContent != consistent.second);
  3713. if (!consistent.first) {
  3714. std::ostringstream e;
  3715. e << "The INTERFACE_" << p << " property of \""
  3716. << theTarget->GetName() << "\" does\nnot agree with the value of "
  3717. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  3718. cmSystemTools::Error(e.str().c_str());
  3719. break;
  3720. }
  3721. propContent = consistent.second;
  3722. continue;
  3723. }
  3724. report += reportEntry + "(Interface set)\n";
  3725. propContent = ifacePropContent;
  3726. propInitialized = true;
  3727. } else {
  3728. // Not set. Nothing to agree on.
  3729. continue;
  3730. }
  3731. }
  3732. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  3733. report, compatibilityType(t));
  3734. return propContent;
  3735. }
  3736. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  3737. const std::string& p, const std::string& config) const
  3738. {
  3739. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  3740. BoolType, nullptr);
  3741. }
  3742. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  3743. const std::string& p, const std::string& config) const
  3744. {
  3745. return checkInterfacePropertyCompatibility<const char*>(
  3746. this, p, config, "empty", StringType, nullptr);
  3747. }
  3748. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  3749. const std::string& p, const std::string& config) const
  3750. {
  3751. return checkInterfacePropertyCompatibility<const char*>(
  3752. this, p, config, "empty", NumberMinType, nullptr);
  3753. }
  3754. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  3755. const std::string& p, const std::string& config) const
  3756. {
  3757. return checkInterfacePropertyCompatibility<const char*>(
  3758. this, p, config, "empty", NumberMaxType, nullptr);
  3759. }
  3760. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  3761. const std::string& config) const
  3762. {
  3763. // Lookup any existing information for this configuration.
  3764. std::string key(cmSystemTools::UpperCase(config));
  3765. cmTargetLinkInformationMap::iterator i = this->LinkInformation.find(key);
  3766. if (i == this->LinkInformation.end()) {
  3767. // Compute information for this configuration.
  3768. cmComputeLinkInformation* info =
  3769. new cmComputeLinkInformation(this, config);
  3770. if (!info || !info->Compute()) {
  3771. delete info;
  3772. info = nullptr;
  3773. }
  3774. // Store the information for this configuration.
  3775. cmTargetLinkInformationMap::value_type entry(key, info);
  3776. i = this->LinkInformation.insert(entry).first;
  3777. if (info) {
  3778. this->CheckPropertyCompatibility(info, config);
  3779. }
  3780. }
  3781. return i->second;
  3782. }
  3783. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  3784. {
  3785. int patch;
  3786. this->GetTargetVersion(false, major, minor, patch);
  3787. }
  3788. void cmGeneratorTarget::GetTargetVersion(bool soversion, int& major,
  3789. int& minor, int& patch) const
  3790. {
  3791. // Set the default values.
  3792. major = 0;
  3793. minor = 0;
  3794. patch = 0;
  3795. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3796. // Look for a VERSION or SOVERSION property.
  3797. const char* prop = soversion ? "SOVERSION" : "VERSION";
  3798. if (const char* version = this->GetProperty(prop)) {
  3799. // Try to parse the version number and store the results that were
  3800. // successfully parsed.
  3801. int parsed_major;
  3802. int parsed_minor;
  3803. int parsed_patch;
  3804. switch (sscanf(version, "%d.%d.%d", &parsed_major, &parsed_minor,
  3805. &parsed_patch)) {
  3806. case 3:
  3807. patch = parsed_patch;
  3808. CM_FALLTHROUGH;
  3809. case 2:
  3810. minor = parsed_minor;
  3811. CM_FALLTHROUGH;
  3812. case 1:
  3813. major = parsed_major;
  3814. CM_FALLTHROUGH;
  3815. default:
  3816. break;
  3817. }
  3818. }
  3819. }
  3820. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  3821. std::string const& working_dir) const
  3822. {
  3823. if (!this->FortranModuleDirectoryCreated) {
  3824. this->FortranModuleDirectory =
  3825. this->CreateFortranModuleDirectory(working_dir);
  3826. this->FortranModuleDirectoryCreated = true;
  3827. }
  3828. return this->FortranModuleDirectory;
  3829. }
  3830. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  3831. std::string const& working_dir) const
  3832. {
  3833. std::string mod_dir;
  3834. std::string target_mod_dir;
  3835. if (const char* prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  3836. target_mod_dir = prop;
  3837. } else {
  3838. std::string const& default_mod_dir =
  3839. this->LocalGenerator->GetCurrentBinaryDirectory();
  3840. if (default_mod_dir != working_dir) {
  3841. target_mod_dir = default_mod_dir;
  3842. }
  3843. }
  3844. const char* moddir_flag =
  3845. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  3846. if (!target_mod_dir.empty() && moddir_flag) {
  3847. // Compute the full path to the module directory.
  3848. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  3849. // Already a full path.
  3850. mod_dir = target_mod_dir;
  3851. } else {
  3852. // Interpret relative to the current output directory.
  3853. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  3854. mod_dir += "/";
  3855. mod_dir += target_mod_dir;
  3856. }
  3857. // Make sure the module output directory exists.
  3858. cmSystemTools::MakeDirectory(mod_dir);
  3859. }
  3860. return mod_dir;
  3861. }
  3862. std::string cmGeneratorTarget::GetFrameworkVersion() const
  3863. {
  3864. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3865. if (const char* fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  3866. return fversion;
  3867. }
  3868. if (const char* tversion = this->GetProperty("VERSION")) {
  3869. return tversion;
  3870. }
  3871. return "A";
  3872. }
  3873. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  3874. std::string const& prefix,
  3875. std::string const& base,
  3876. std::string const& suffix,
  3877. std::string const& name,
  3878. const char* version) const
  3879. {
  3880. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  3881. if (version) {
  3882. vName += ".";
  3883. vName += version;
  3884. }
  3885. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  3886. }
  3887. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  3888. {
  3889. cmPropertyMap const& propsObject = this->Target->GetProperties();
  3890. std::vector<std::string> props;
  3891. props.reserve(propsObject.size());
  3892. for (auto const& it : propsObject) {
  3893. props.push_back(it.first);
  3894. }
  3895. return props;
  3896. }
  3897. void cmGeneratorTarget::ReportPropertyOrigin(
  3898. const std::string& p, const std::string& result, const std::string& report,
  3899. const std::string& compatibilityType) const
  3900. {
  3901. std::vector<std::string> debugProperties;
  3902. const char* debugProp = this->Target->GetMakefile()->GetDefinition(
  3903. "CMAKE_DEBUG_TARGET_PROPERTIES");
  3904. if (debugProp) {
  3905. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  3906. }
  3907. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  3908. std::find(debugProperties.begin(), debugProperties.end(), p) !=
  3909. debugProperties.end();
  3910. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3911. this->DebugCompatiblePropertiesDone[p] = true;
  3912. }
  3913. if (!debugOrigin) {
  3914. return;
  3915. }
  3916. std::string areport = compatibilityType;
  3917. areport += std::string(" of property \"") + p + "\" for target \"";
  3918. areport += std::string(this->GetName());
  3919. areport += "\" (result: \"";
  3920. areport += result;
  3921. areport += "\"):\n" + report;
  3922. this->LocalGenerator->GetCMakeInstance()->IssueMessage(cmake::LOG, areport);
  3923. }
  3924. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  3925. std::vector<cmLinkItem>& items) const
  3926. {
  3927. for (std::string const& n : names) {
  3928. std::string name = this->CheckCMP0004(n);
  3929. if (name == this->GetName() || name.empty()) {
  3930. continue;
  3931. }
  3932. items.emplace_back(name, this->FindTargetToLink(name));
  3933. }
  3934. }
  3935. void cmGeneratorTarget::ExpandLinkItems(
  3936. std::string const& prop, std::string const& value, std::string const& config,
  3937. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  3938. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition) const
  3939. {
  3940. cmGeneratorExpression ge;
  3941. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), prop, nullptr,
  3942. nullptr);
  3943. // The $<LINK_ONLY> expression may be in a link interface to specify private
  3944. // link dependencies that are otherwise excluded from usage requirements.
  3945. if (usage_requirements_only) {
  3946. dagChecker.SetTransitivePropertiesOnly();
  3947. }
  3948. std::vector<std::string> libs;
  3949. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  3950. cmSystemTools::ExpandListArgument(cge->Evaluate(this->LocalGenerator, config,
  3951. false, headTarget, this,
  3952. &dagChecker),
  3953. libs);
  3954. this->LookupLinkItems(libs, items);
  3955. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  3956. }
  3957. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  3958. const std::string& config, cmGeneratorTarget const* head) const
  3959. {
  3960. // Imported targets have their own link interface.
  3961. if (this->IsImported()) {
  3962. return this->GetImportLinkInterface(config, head, false);
  3963. }
  3964. // Link interfaces are not supported for executables that do not
  3965. // export symbols.
  3966. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  3967. !this->IsExecutableWithExports()) {
  3968. return nullptr;
  3969. }
  3970. // Lookup any existing link interface for this configuration.
  3971. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  3972. // If the link interface does not depend on the head target
  3973. // then return the one we computed first.
  3974. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  3975. return &hm.begin()->second;
  3976. }
  3977. cmOptionalLinkInterface& iface = hm[head];
  3978. if (!iface.LibrariesDone) {
  3979. iface.LibrariesDone = true;
  3980. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  3981. }
  3982. if (!iface.AllDone) {
  3983. iface.AllDone = true;
  3984. if (iface.Exists) {
  3985. this->ComputeLinkInterface(config, iface, head);
  3986. }
  3987. }
  3988. return iface.Exists ? &iface : nullptr;
  3989. }
  3990. void cmGeneratorTarget::ComputeLinkInterface(
  3991. const std::string& config, cmOptionalLinkInterface& iface,
  3992. cmGeneratorTarget const* headTarget) const
  3993. {
  3994. if (iface.ExplicitLibraries) {
  3995. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3996. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3997. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3998. // Shared libraries may have runtime implementation dependencies
  3999. // on other shared libraries that are not in the interface.
  4000. std::unordered_set<std::string> emitted;
  4001. for (cmLinkItem const& lib : iface.Libraries) {
  4002. emitted.insert(lib);
  4003. }
  4004. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4005. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  4006. for (cmLinkImplItem const& lib : impl->Libraries) {
  4007. if (emitted.insert(lib).second) {
  4008. if (lib.Target) {
  4009. // This is a runtime dependency on another shared library.
  4010. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4011. iface.SharedDeps.push_back(lib);
  4012. }
  4013. } else {
  4014. // TODO: Recognize shared library file names. Perhaps this
  4015. // should be moved to cmComputeLinkInformation, but that creates
  4016. // a chicken-and-egg problem since this list is needed for its
  4017. // construction.
  4018. }
  4019. }
  4020. }
  4021. }
  4022. }
  4023. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4024. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  4025. // The link implementation is the default link interface.
  4026. cmLinkImplementationLibraries const* impl =
  4027. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4028. iface.ImplementationIsInterface = true;
  4029. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  4030. }
  4031. if (this->LinkLanguagePropagatesToDependents()) {
  4032. // Targets using this archive need its language runtime libraries.
  4033. if (cmLinkImplementation const* impl =
  4034. this->GetLinkImplementation(config)) {
  4035. iface.Languages = impl->Languages;
  4036. }
  4037. }
  4038. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4039. // Construct the property name suffix for this configuration.
  4040. std::string suffix = "_";
  4041. if (!config.empty()) {
  4042. suffix += cmSystemTools::UpperCase(config);
  4043. } else {
  4044. suffix += "NOCONFIG";
  4045. }
  4046. // How many repetitions are needed if this library has cyclic
  4047. // dependencies?
  4048. std::string propName = "LINK_INTERFACE_MULTIPLICITY";
  4049. propName += suffix;
  4050. if (const char* config_reps = this->GetProperty(propName)) {
  4051. sscanf(config_reps, "%u", &iface.Multiplicity);
  4052. } else if (const char* reps =
  4053. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  4054. sscanf(reps, "%u", &iface.Multiplicity);
  4055. }
  4056. }
  4057. }
  4058. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  4059. const std::string& config, cmGeneratorTarget const* head,
  4060. bool usage_requirements_only) const
  4061. {
  4062. // Imported targets have their own link interface.
  4063. if (this->IsImported()) {
  4064. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  4065. }
  4066. // Link interfaces are not supported for executables that do not
  4067. // export symbols.
  4068. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  4069. !this->IsExecutableWithExports()) {
  4070. return nullptr;
  4071. }
  4072. // Lookup any existing link interface for this configuration.
  4073. std::string CONFIG = cmSystemTools::UpperCase(config);
  4074. cmHeadToLinkInterfaceMap& hm =
  4075. (usage_requirements_only
  4076. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4077. : this->GetHeadToLinkInterfaceMap(config));
  4078. // If the link interface does not depend on the head target
  4079. // then return the one we computed first.
  4080. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4081. return &hm.begin()->second;
  4082. }
  4083. cmOptionalLinkInterface& iface = hm[head];
  4084. if (!iface.LibrariesDone) {
  4085. iface.LibrariesDone = true;
  4086. this->ComputeLinkInterfaceLibraries(config, iface, head,
  4087. usage_requirements_only);
  4088. }
  4089. return iface.Exists ? &iface : nullptr;
  4090. }
  4091. std::string cmGeneratorTarget::GetDirectory(
  4092. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4093. {
  4094. if (this->IsImported()) {
  4095. // Return the directory from which the target is imported.
  4096. return cmSystemTools::GetFilenamePath(
  4097. this->Target->ImportedGetFullPath(config, artifact));
  4098. }
  4099. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  4100. // Return the directory in which the target will be built.
  4101. switch (artifact) {
  4102. case cmStateEnums::RuntimeBinaryArtifact:
  4103. return info->OutDir;
  4104. case cmStateEnums::ImportLibraryArtifact:
  4105. return info->ImpDir;
  4106. }
  4107. }
  4108. return "";
  4109. }
  4110. bool cmGeneratorTarget::UsesDefaultOutputDir(
  4111. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4112. {
  4113. std::string dir;
  4114. return this->ComputeOutputDir(config, artifact, dir);
  4115. }
  4116. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  4117. const std::string& config) const
  4118. {
  4119. // There is no output information for imported targets.
  4120. if (this->IsImported()) {
  4121. return nullptr;
  4122. }
  4123. // Only libraries and executables have well-defined output files.
  4124. if (!this->HaveWellDefinedOutputFiles()) {
  4125. std::string msg = "cmGeneratorTarget::GetOutputInfo called for ";
  4126. msg += this->GetName();
  4127. msg += " which has type ";
  4128. msg += cmState::GetTargetTypeName(this->GetType());
  4129. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  4130. return nullptr;
  4131. }
  4132. // Lookup/compute/cache the output information for this configuration.
  4133. std::string config_upper;
  4134. if (!config.empty()) {
  4135. config_upper = cmSystemTools::UpperCase(config);
  4136. }
  4137. OutputInfoMapType::iterator i = this->OutputInfoMap.find(config_upper);
  4138. if (i == this->OutputInfoMap.end()) {
  4139. // Add empty info in map to detect potential recursion.
  4140. OutputInfo info;
  4141. OutputInfoMapType::value_type entry(config_upper, info);
  4142. i = this->OutputInfoMap.insert(entry).first;
  4143. // Compute output directories.
  4144. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  4145. info.OutDir);
  4146. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  4147. info.ImpDir);
  4148. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  4149. info.PdbDir = info.OutDir;
  4150. }
  4151. // Now update the previously-prepared map entry.
  4152. i->second = info;
  4153. } else if (i->second.empty()) {
  4154. // An empty map entry indicates we have been called recursively
  4155. // from the above block.
  4156. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4157. cmake::FATAL_ERROR,
  4158. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  4159. this->GetBacktrace());
  4160. return nullptr;
  4161. }
  4162. return &i->second;
  4163. }
  4164. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  4165. cmStateEnums::ArtifactType artifact,
  4166. std::string& out) const
  4167. {
  4168. bool usesDefaultOutputDir = false;
  4169. std::string conf = config;
  4170. // Look for a target property defining the target output directory
  4171. // based on the target type.
  4172. std::string targetTypeName = this->GetOutputTargetType(artifact);
  4173. const char* propertyName = nullptr;
  4174. std::string propertyNameStr = targetTypeName;
  4175. if (!propertyNameStr.empty()) {
  4176. propertyNameStr += "_OUTPUT_DIRECTORY";
  4177. propertyName = propertyNameStr.c_str();
  4178. }
  4179. // Check for a per-configuration output directory target property.
  4180. std::string configUpper = cmSystemTools::UpperCase(conf);
  4181. const char* configProp = nullptr;
  4182. std::string configPropStr = targetTypeName;
  4183. if (!configPropStr.empty()) {
  4184. configPropStr += "_OUTPUT_DIRECTORY_";
  4185. configPropStr += configUpper;
  4186. configProp = configPropStr.c_str();
  4187. }
  4188. // Select an output directory.
  4189. if (const char* config_outdir = this->GetProperty(configProp)) {
  4190. // Use the user-specified per-configuration output directory.
  4191. cmGeneratorExpression ge;
  4192. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4193. ge.Parse(config_outdir);
  4194. out = cge->Evaluate(this->LocalGenerator, config);
  4195. // Skip per-configuration subdirectory.
  4196. conf.clear();
  4197. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4198. // Use the user-specified output directory.
  4199. cmGeneratorExpression ge;
  4200. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4201. out = cge->Evaluate(this->LocalGenerator, config);
  4202. // Skip per-configuration subdirectory if the value contained a
  4203. // generator expression.
  4204. if (out != outdir) {
  4205. conf.clear();
  4206. }
  4207. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4208. // Lookup the output path for executables.
  4209. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  4210. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4211. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4212. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4213. // Lookup the output path for libraries.
  4214. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  4215. }
  4216. if (out.empty()) {
  4217. // Default to the current output directory.
  4218. usesDefaultOutputDir = true;
  4219. out = ".";
  4220. }
  4221. // Convert the output path to a full path in case it is
  4222. // specified as a relative path. Treat a relative path as
  4223. // relative to the current output directory for this makefile.
  4224. out = (cmSystemTools::CollapseFullPath(
  4225. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4226. // The generator may add the configuration's subdirectory.
  4227. if (!conf.empty()) {
  4228. bool useEPN =
  4229. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  4230. std::string suffix =
  4231. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  4232. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4233. "/", conf, suffix, out);
  4234. }
  4235. return usesDefaultOutputDir;
  4236. }
  4237. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  4238. const std::string& config,
  4239. std::string& out) const
  4240. {
  4241. // Look for a target property defining the target output directory
  4242. // based on the target type.
  4243. const char* propertyName = nullptr;
  4244. std::string propertyNameStr = kind;
  4245. if (!propertyNameStr.empty()) {
  4246. propertyNameStr += "_OUTPUT_DIRECTORY";
  4247. propertyName = propertyNameStr.c_str();
  4248. }
  4249. std::string conf = config;
  4250. // Check for a per-configuration output directory target property.
  4251. std::string configUpper = cmSystemTools::UpperCase(conf);
  4252. const char* configProp = nullptr;
  4253. std::string configPropStr = kind;
  4254. if (!configPropStr.empty()) {
  4255. configPropStr += "_OUTPUT_DIRECTORY_";
  4256. configPropStr += configUpper;
  4257. configProp = configPropStr.c_str();
  4258. }
  4259. // Select an output directory.
  4260. if (const char* config_outdir = this->GetProperty(configProp)) {
  4261. // Use the user-specified per-configuration output directory.
  4262. cmGeneratorExpression ge;
  4263. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4264. ge.Parse(config_outdir);
  4265. out = cge->Evaluate(this->LocalGenerator, config);
  4266. // Skip per-configuration subdirectory.
  4267. conf.clear();
  4268. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4269. // Use the user-specified output directory.
  4270. cmGeneratorExpression ge;
  4271. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4272. out = cge->Evaluate(this->LocalGenerator, config);
  4273. // Skip per-configuration subdirectory if the value contained a
  4274. // generator expression.
  4275. if (out != outdir) {
  4276. conf.clear();
  4277. }
  4278. }
  4279. if (out.empty()) {
  4280. return false;
  4281. }
  4282. // Convert the output path to a full path in case it is
  4283. // specified as a relative path. Treat a relative path as
  4284. // relative to the current output directory for this makefile.
  4285. out = (cmSystemTools::CollapseFullPath(
  4286. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4287. // The generator may add the configuration's subdirectory.
  4288. if (!conf.empty()) {
  4289. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4290. "/", conf, "", out);
  4291. }
  4292. return true;
  4293. }
  4294. bool cmGeneratorTarget::HaveInstallTreeRPATH() const
  4295. {
  4296. const char* install_rpath = this->GetProperty("INSTALL_RPATH");
  4297. return (install_rpath && *install_rpath) &&
  4298. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  4299. }
  4300. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  4301. const std::string& config, cmOptionalLinkInterface& iface,
  4302. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  4303. {
  4304. // Construct the property name suffix for this configuration.
  4305. std::string suffix = "_";
  4306. if (!config.empty()) {
  4307. suffix += cmSystemTools::UpperCase(config);
  4308. } else {
  4309. suffix += "NOCONFIG";
  4310. }
  4311. // An explicit list of interface libraries may be set for shared
  4312. // libraries and executables that export symbols.
  4313. const char* explicitLibraries = nullptr;
  4314. std::string linkIfaceProp;
  4315. if (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  4316. this->GetPolicyStatusCMP0022() != cmPolicies::WARN) {
  4317. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  4318. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  4319. explicitLibraries = this->GetProperty(linkIfaceProp);
  4320. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4321. this->IsExecutableWithExports()) {
  4322. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  4323. // shared lib or executable.
  4324. // Lookup the per-configuration property.
  4325. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4326. linkIfaceProp += suffix;
  4327. explicitLibraries = this->GetProperty(linkIfaceProp);
  4328. // If not set, try the generic property.
  4329. if (!explicitLibraries) {
  4330. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4331. explicitLibraries = this->GetProperty(linkIfaceProp);
  4332. }
  4333. }
  4334. if (explicitLibraries &&
  4335. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4336. !this->PolicyWarnedCMP0022) {
  4337. // Compare the explicitly set old link interface properties to the
  4338. // preferred new link interface property one and warn if different.
  4339. const char* newExplicitLibraries =
  4340. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  4341. if (newExplicitLibraries &&
  4342. strcmp(newExplicitLibraries, explicitLibraries) != 0) {
  4343. std::ostringstream w;
  4344. /* clang-format off */
  4345. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4346. "Target \"" << this->GetName() << "\" has an "
  4347. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  4348. linkIfaceProp << " properties."
  4349. "\n"
  4350. "INTERFACE_LINK_LIBRARIES:\n"
  4351. " " << newExplicitLibraries << "\n" <<
  4352. linkIfaceProp << ":\n"
  4353. " " << explicitLibraries << "\n";
  4354. /* clang-format on */
  4355. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4356. this->PolicyWarnedCMP0022 = true;
  4357. }
  4358. }
  4359. // There is no implicit link interface for executables or modules
  4360. // so if none was explicitly set then there is no link interface.
  4361. if (!explicitLibraries &&
  4362. (this->GetType() == cmStateEnums::EXECUTABLE ||
  4363. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  4364. return;
  4365. }
  4366. iface.Exists = true;
  4367. iface.ExplicitLibraries = explicitLibraries;
  4368. if (explicitLibraries) {
  4369. // The interface libraries have been explicitly set.
  4370. this->ExpandLinkItems(linkIfaceProp, explicitLibraries, config, headTarget,
  4371. usage_requirements_only, iface.Libraries,
  4372. iface.HadHeadSensitiveCondition);
  4373. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4374. this->GetPolicyStatusCMP0022() == cmPolicies::OLD)
  4375. // If CMP0022 is NEW then the plain tll signature sets the
  4376. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  4377. // cleared the property explicitly and we should not fall back
  4378. // to the link implementation.
  4379. {
  4380. // The link implementation is the default link interface.
  4381. cmLinkImplementationLibraries const* impl =
  4382. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4383. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  4384. impl->Libraries.end());
  4385. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4386. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  4387. // Compare the link implementation fallback link interface to the
  4388. // preferred new link interface property and warn if different.
  4389. std::vector<cmLinkItem> ifaceLibs;
  4390. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  4391. if (const char* newExplicitLibraries = this->GetProperty(newProp)) {
  4392. bool hadHeadSensitiveConditionDummy = false;
  4393. this->ExpandLinkItems(newProp, newExplicitLibraries, config,
  4394. headTarget, usage_requirements_only, ifaceLibs,
  4395. hadHeadSensitiveConditionDummy);
  4396. }
  4397. if (ifaceLibs != iface.Libraries) {
  4398. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  4399. std::string newLibraries = cmJoin(ifaceLibs, ";");
  4400. if (oldLibraries.empty()) {
  4401. oldLibraries = "(empty)";
  4402. }
  4403. if (newLibraries.empty()) {
  4404. newLibraries = "(empty)";
  4405. }
  4406. std::ostringstream w;
  4407. /* clang-format off */
  4408. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4409. "Target \"" << this->GetName() << "\" has an "
  4410. "INTERFACE_LINK_LIBRARIES property. "
  4411. "This should be preferred as the source of the link interface "
  4412. "for this library but because CMP0022 is not set CMake is "
  4413. "ignoring the property and using the link implementation "
  4414. "as the link interface instead."
  4415. "\n"
  4416. "INTERFACE_LINK_LIBRARIES:\n"
  4417. " " << newLibraries << "\n"
  4418. "Link implementation:\n"
  4419. " " << oldLibraries << "\n";
  4420. /* clang-format on */
  4421. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4422. this->PolicyWarnedCMP0022 = true;
  4423. }
  4424. }
  4425. }
  4426. }
  4427. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  4428. const std::string& config, cmGeneratorTarget const* headTarget,
  4429. bool usage_requirements_only) const
  4430. {
  4431. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  4432. if (!info) {
  4433. return nullptr;
  4434. }
  4435. std::string CONFIG = cmSystemTools::UpperCase(config);
  4436. cmHeadToLinkInterfaceMap& hm =
  4437. (usage_requirements_only
  4438. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4439. : this->GetHeadToLinkInterfaceMap(config));
  4440. // If the link interface does not depend on the head target
  4441. // then return the one we computed first.
  4442. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4443. return &hm.begin()->second;
  4444. }
  4445. cmOptionalLinkInterface& iface = hm[headTarget];
  4446. if (!iface.AllDone) {
  4447. iface.AllDone = true;
  4448. iface.Multiplicity = info->Multiplicity;
  4449. cmSystemTools::ExpandListArgument(info->Languages, iface.Languages);
  4450. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  4451. headTarget, usage_requirements_only, iface.Libraries,
  4452. iface.HadHeadSensitiveCondition);
  4453. std::vector<std::string> deps;
  4454. cmSystemTools::ExpandListArgument(info->SharedDeps, deps);
  4455. this->LookupLinkItems(deps, iface.SharedDeps);
  4456. }
  4457. return &iface;
  4458. }
  4459. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  4460. const std::string& config) const
  4461. {
  4462. // There is no imported information for non-imported targets.
  4463. if (!this->IsImported()) {
  4464. return nullptr;
  4465. }
  4466. // Lookup/compute/cache the import information for this
  4467. // configuration.
  4468. std::string config_upper;
  4469. if (!config.empty()) {
  4470. config_upper = cmSystemTools::UpperCase(config);
  4471. } else {
  4472. config_upper = "NOCONFIG";
  4473. }
  4474. ImportInfoMapType::const_iterator i = this->ImportInfoMap.find(config_upper);
  4475. if (i == this->ImportInfoMap.end()) {
  4476. ImportInfo info;
  4477. this->ComputeImportInfo(config_upper, info);
  4478. ImportInfoMapType::value_type entry(config_upper, info);
  4479. i = this->ImportInfoMap.insert(entry).first;
  4480. }
  4481. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4482. return &i->second;
  4483. }
  4484. // If the location is empty then the target is not available for
  4485. // this configuration.
  4486. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  4487. return nullptr;
  4488. }
  4489. // Return the import information.
  4490. return &i->second;
  4491. }
  4492. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  4493. ImportInfo& info) const
  4494. {
  4495. // This method finds information about an imported target from its
  4496. // properties. The "IMPORTED_" namespace is reserved for properties
  4497. // defined by the project exporting the target.
  4498. // Initialize members.
  4499. info.NoSOName = false;
  4500. const char* loc = nullptr;
  4501. const char* imp = nullptr;
  4502. std::string suffix;
  4503. if (!this->Target->GetMappedConfig(desired_config, &loc, &imp, suffix)) {
  4504. return;
  4505. }
  4506. // Get the link interface.
  4507. {
  4508. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  4509. const char* propertyLibs = this->GetProperty(linkProp);
  4510. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4511. if (!propertyLibs) {
  4512. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4513. linkProp += suffix;
  4514. propertyLibs = this->GetProperty(linkProp);
  4515. }
  4516. if (!propertyLibs) {
  4517. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4518. propertyLibs = this->GetProperty(linkProp);
  4519. }
  4520. }
  4521. if (propertyLibs) {
  4522. info.LibrariesProp = linkProp;
  4523. info.Libraries = propertyLibs;
  4524. }
  4525. }
  4526. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4527. if (loc) {
  4528. info.LibName = loc;
  4529. }
  4530. return;
  4531. }
  4532. // A provided configuration has been chosen. Load the
  4533. // configuration's properties.
  4534. // Get the location.
  4535. if (loc) {
  4536. info.Location = loc;
  4537. } else {
  4538. std::string impProp = "IMPORTED_LOCATION";
  4539. impProp += suffix;
  4540. if (const char* config_location = this->GetProperty(impProp)) {
  4541. info.Location = config_location;
  4542. } else if (const char* location = this->GetProperty("IMPORTED_LOCATION")) {
  4543. info.Location = location;
  4544. }
  4545. }
  4546. // Get the soname.
  4547. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4548. std::string soProp = "IMPORTED_SONAME";
  4549. soProp += suffix;
  4550. if (const char* config_soname = this->GetProperty(soProp)) {
  4551. info.SOName = config_soname;
  4552. } else if (const char* soname = this->GetProperty("IMPORTED_SONAME")) {
  4553. info.SOName = soname;
  4554. }
  4555. }
  4556. // Get the "no-soname" mark.
  4557. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4558. std::string soProp = "IMPORTED_NO_SONAME";
  4559. soProp += suffix;
  4560. if (const char* config_no_soname = this->GetProperty(soProp)) {
  4561. info.NoSOName = cmSystemTools::IsOn(config_no_soname);
  4562. } else if (const char* no_soname =
  4563. this->GetProperty("IMPORTED_NO_SONAME")) {
  4564. info.NoSOName = cmSystemTools::IsOn(no_soname);
  4565. }
  4566. }
  4567. // Get the import library.
  4568. if (imp) {
  4569. info.ImportLibrary = imp;
  4570. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4571. this->IsExecutableWithExports()) {
  4572. std::string impProp = "IMPORTED_IMPLIB";
  4573. impProp += suffix;
  4574. if (const char* config_implib = this->GetProperty(impProp)) {
  4575. info.ImportLibrary = config_implib;
  4576. } else if (const char* implib = this->GetProperty("IMPORTED_IMPLIB")) {
  4577. info.ImportLibrary = implib;
  4578. }
  4579. }
  4580. // Get the link dependencies.
  4581. {
  4582. std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
  4583. linkProp += suffix;
  4584. if (const char* config_libs = this->GetProperty(linkProp)) {
  4585. info.SharedDeps = config_libs;
  4586. } else if (const char* libs =
  4587. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  4588. info.SharedDeps = libs;
  4589. }
  4590. }
  4591. // Get the link languages.
  4592. if (this->LinkLanguagePropagatesToDependents()) {
  4593. std::string linkProp = "IMPORTED_LINK_INTERFACE_LANGUAGES";
  4594. linkProp += suffix;
  4595. if (const char* config_libs = this->GetProperty(linkProp)) {
  4596. info.Languages = config_libs;
  4597. } else if (const char* libs =
  4598. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  4599. info.Languages = libs;
  4600. }
  4601. }
  4602. // Get information if target is managed assembly.
  4603. {
  4604. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  4605. if (auto pc = this->GetProperty(linkProp + suffix)) {
  4606. info.Managed = this->CheckManagedType(pc);
  4607. } else if (auto p = this->GetProperty(linkProp)) {
  4608. info.Managed = this->CheckManagedType(p);
  4609. }
  4610. }
  4611. // Get the cyclic repetition count.
  4612. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4613. std::string linkProp = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  4614. linkProp += suffix;
  4615. if (const char* config_reps = this->GetProperty(linkProp)) {
  4616. sscanf(config_reps, "%u", &info.Multiplicity);
  4617. } else if (const char* reps =
  4618. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  4619. sscanf(reps, "%u", &info.Multiplicity);
  4620. }
  4621. }
  4622. }
  4623. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  4624. const std::string& config) const
  4625. {
  4626. std::string CONFIG = cmSystemTools::UpperCase(config);
  4627. return this->LinkInterfaceMap[CONFIG];
  4628. }
  4629. cmHeadToLinkInterfaceMap&
  4630. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  4631. const std::string& config) const
  4632. {
  4633. std::string CONFIG = cmSystemTools::UpperCase(config);
  4634. return this->LinkInterfaceUsageRequirementsOnlyMap[CONFIG];
  4635. }
  4636. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  4637. const std::string& config) const
  4638. {
  4639. // There is no link implementation for imported targets.
  4640. if (this->IsImported()) {
  4641. return nullptr;
  4642. }
  4643. std::string CONFIG = cmSystemTools::UpperCase(config);
  4644. cmOptionalLinkImplementation& impl = this->LinkImplMap[CONFIG][this];
  4645. if (!impl.LibrariesDone) {
  4646. impl.LibrariesDone = true;
  4647. this->ComputeLinkImplementationLibraries(config, impl, this);
  4648. }
  4649. if (!impl.LanguagesDone) {
  4650. impl.LanguagesDone = true;
  4651. this->ComputeLinkImplementationLanguages(config, impl);
  4652. }
  4653. return &impl;
  4654. }
  4655. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  4656. std::vector<cmSourceFile*>& files) const
  4657. {
  4658. std::vector<std::string> configs;
  4659. this->Makefile->GetConfigurations(configs);
  4660. if (configs.empty()) {
  4661. configs.emplace_back();
  4662. }
  4663. std::vector<std::string>::const_iterator it = configs.begin();
  4664. const std::string& firstConfig = *it;
  4665. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  4666. for (; it != configs.end(); ++it) {
  4667. std::vector<cmSourceFile*> configFiles;
  4668. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  4669. if (configFiles != files) {
  4670. std::string firstConfigFiles;
  4671. const char* sep = "";
  4672. for (cmSourceFile* f : files) {
  4673. firstConfigFiles += sep;
  4674. firstConfigFiles += f->GetFullPath();
  4675. sep = "\n ";
  4676. }
  4677. std::string thisConfigFiles;
  4678. sep = "";
  4679. for (cmSourceFile* f : configFiles) {
  4680. thisConfigFiles += sep;
  4681. thisConfigFiles += f->GetFullPath();
  4682. sep = "\n ";
  4683. }
  4684. std::ostringstream e;
  4685. /* clang-format off */
  4686. e << "Target \"" << this->GetName()
  4687. << "\" has source files which vary by "
  4688. "configuration. This is not supported by the \""
  4689. << this->GlobalGenerator->GetName()
  4690. << "\" generator.\n"
  4691. "Config \"" << firstConfig << "\":\n"
  4692. " " << firstConfigFiles << "\n"
  4693. "Config \"" << *it << "\":\n"
  4694. " " << thisConfigFiles << "\n";
  4695. /* clang-format on */
  4696. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  4697. return false;
  4698. }
  4699. }
  4700. return true;
  4701. }
  4702. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  4703. std::vector<cmGeneratorTarget*>& objlibs) const
  4704. {
  4705. // At configure-time, this method can be called as part of getting the
  4706. // LOCATION property or to export() a file to be include()d. However
  4707. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  4708. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  4709. // behavior of CMP0024 and CMP0026 only.
  4710. cmStringRange rng = this->Target->GetSourceEntries();
  4711. for (std::string const& entry : rng) {
  4712. std::vector<std::string> files;
  4713. cmSystemTools::ExpandListArgument(entry, files);
  4714. for (std::string const& li : files) {
  4715. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") &&
  4716. li[li.size() - 1] == '>') {
  4717. std::string objLibName = li.substr(17, li.size() - 18);
  4718. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  4719. continue;
  4720. }
  4721. cmGeneratorTarget* objLib =
  4722. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  4723. if (objLib) {
  4724. objlibs.push_back(objLib);
  4725. }
  4726. }
  4727. }
  4728. }
  4729. }
  4730. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  4731. {
  4732. // Strip whitespace off the library names because we used to do this
  4733. // in case variables were expanded at generate time. We no longer
  4734. // do the expansion but users link to libraries like " ${VAR} ".
  4735. std::string lib = item;
  4736. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  4737. if (pos != std::string::npos) {
  4738. lib = lib.substr(pos);
  4739. }
  4740. pos = lib.find_last_not_of(" \t\r\n");
  4741. if (pos != std::string::npos) {
  4742. lib = lib.substr(0, pos + 1);
  4743. }
  4744. if (lib != item) {
  4745. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  4746. switch (this->GetPolicyStatusCMP0004()) {
  4747. case cmPolicies::WARN: {
  4748. std::ostringstream w;
  4749. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  4750. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4751. << "\" which has leading or trailing whitespace.";
  4752. cm->IssueMessage(cmake::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  4753. }
  4754. case cmPolicies::OLD:
  4755. break;
  4756. case cmPolicies::NEW: {
  4757. std::ostringstream e;
  4758. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  4759. << "\" which has leading or trailing whitespace. "
  4760. << "This is now an error according to policy CMP0004.";
  4761. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4762. } break;
  4763. case cmPolicies::REQUIRED_IF_USED:
  4764. case cmPolicies::REQUIRED_ALWAYS: {
  4765. std::ostringstream e;
  4766. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  4767. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4768. << "\" which has leading or trailing whitespace.";
  4769. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4770. } break;
  4771. }
  4772. }
  4773. return lib;
  4774. }
  4775. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  4776. const std::string& config) const
  4777. {
  4778. std::vector<cmSourceFile*> sourceFiles;
  4779. this->GetSourceFiles(sourceFiles, config);
  4780. for (cmSourceFile* src : sourceFiles) {
  4781. const std::string& lang = src->GetLanguage();
  4782. if (!lang.empty()) {
  4783. languages.insert(lang);
  4784. }
  4785. }
  4786. std::vector<cmGeneratorTarget*> objectLibraries;
  4787. std::vector<cmSourceFile const*> externalObjects;
  4788. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4789. std::vector<cmGeneratorTarget*> objectTargets;
  4790. this->GetObjectLibrariesCMP0026(objectTargets);
  4791. objectLibraries.reserve(objectTargets.size());
  4792. for (cmGeneratorTarget* gt : objectTargets) {
  4793. objectLibraries.push_back(gt);
  4794. }
  4795. } else {
  4796. this->GetExternalObjects(externalObjects, config);
  4797. for (cmSourceFile const* extObj : externalObjects) {
  4798. std::string objLib = extObj->GetObjectLibrary();
  4799. if (cmGeneratorTarget* tgt =
  4800. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  4801. auto const objLibIt =
  4802. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  4803. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  4804. if (objectLibraries.cend() == objLibIt) {
  4805. objectLibraries.push_back(tgt);
  4806. }
  4807. }
  4808. }
  4809. }
  4810. for (cmGeneratorTarget* objLib : objectLibraries) {
  4811. objLib->GetLanguages(languages, config);
  4812. }
  4813. }
  4814. bool cmGeneratorTarget::HasLanguage(std::string const& language,
  4815. std::string const& config,
  4816. bool exclusive) const
  4817. {
  4818. std::set<std::string> languages;
  4819. this->GetLanguages(languages, config);
  4820. // add linker language (if it is different from compiler languages)
  4821. languages.insert(this->GetLinkerLanguage(config));
  4822. return (languages.size() == 1 || !exclusive) &&
  4823. languages.count(language) > 0;
  4824. }
  4825. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  4826. const std::string& config, cmOptionalLinkImplementation& impl) const
  4827. {
  4828. // This target needs runtime libraries for its source languages.
  4829. std::set<std::string> languages;
  4830. // Get languages used in our source files.
  4831. this->GetLanguages(languages, config);
  4832. // Copy the set of languages to the link implementation.
  4833. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  4834. languages.end());
  4835. }
  4836. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  4837. {
  4838. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  4839. return false;
  4840. }
  4841. if (this->GetProperty("BUILD_RPATH")) {
  4842. return true;
  4843. }
  4844. if (cmLinkImplementationLibraries const* impl =
  4845. this->GetLinkImplementationLibraries(config)) {
  4846. return !impl->Libraries.empty();
  4847. }
  4848. return false;
  4849. }
  4850. cmLinkImplementationLibraries const*
  4851. cmGeneratorTarget::GetLinkImplementationLibraries(
  4852. const std::string& config) const
  4853. {
  4854. return this->GetLinkImplementationLibrariesInternal(config, this);
  4855. }
  4856. cmLinkImplementationLibraries const*
  4857. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  4858. const std::string& config, cmGeneratorTarget const* head) const
  4859. {
  4860. // There is no link implementation for imported targets.
  4861. if (this->IsImported()) {
  4862. return nullptr;
  4863. }
  4864. // Populate the link implementation libraries for this configuration.
  4865. std::string CONFIG = cmSystemTools::UpperCase(config);
  4866. HeadToLinkImplementationMap& hm = this->LinkImplMap[CONFIG];
  4867. // If the link implementation does not depend on the head target
  4868. // then return the one we computed first.
  4869. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4870. return &hm.begin()->second;
  4871. }
  4872. cmOptionalLinkImplementation& impl = hm[head];
  4873. if (!impl.LibrariesDone) {
  4874. impl.LibrariesDone = true;
  4875. this->ComputeLinkImplementationLibraries(config, impl, head);
  4876. }
  4877. return &impl;
  4878. }
  4879. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  4880. const std::string& p) const
  4881. {
  4882. return this->LinkImplicitNullProperties.find(p) !=
  4883. this->LinkImplicitNullProperties.end();
  4884. }
  4885. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  4886. const std::string& config, cmOptionalLinkImplementation& impl,
  4887. cmGeneratorTarget const* head) const
  4888. {
  4889. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  4890. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  4891. cmBacktraceRange::const_iterator btIt = btRange.begin();
  4892. // Collect libraries directly linked in this configuration.
  4893. for (cmStringRange::const_iterator le = entryRange.begin(),
  4894. end = entryRange.end();
  4895. le != end; ++le, ++btIt) {
  4896. std::vector<std::string> llibs;
  4897. cmGeneratorExpressionDAGChecker dagChecker(
  4898. this->GetName(), "LINK_LIBRARIES", nullptr, nullptr);
  4899. cmGeneratorExpression ge(*btIt);
  4900. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  4901. std::string const evaluated =
  4902. cge->Evaluate(this->LocalGenerator, config, false, head, &dagChecker);
  4903. cmSystemTools::ExpandListArgument(evaluated, llibs);
  4904. if (cge->GetHadHeadSensitiveCondition()) {
  4905. impl.HadHeadSensitiveCondition = true;
  4906. }
  4907. for (std::string const& lib : llibs) {
  4908. // Skip entries that resolve to the target itself or are empty.
  4909. std::string name = this->CheckCMP0004(lib);
  4910. if (name == this->GetName() || name.empty()) {
  4911. if (name == this->GetName()) {
  4912. bool noMessage = false;
  4913. cmake::MessageType messageType = cmake::FATAL_ERROR;
  4914. std::ostringstream e;
  4915. switch (this->GetPolicyStatusCMP0038()) {
  4916. case cmPolicies::WARN: {
  4917. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  4918. messageType = cmake::AUTHOR_WARNING;
  4919. } break;
  4920. case cmPolicies::OLD:
  4921. noMessage = true;
  4922. case cmPolicies::REQUIRED_IF_USED:
  4923. case cmPolicies::REQUIRED_ALWAYS:
  4924. case cmPolicies::NEW:
  4925. // Issue the fatal message.
  4926. break;
  4927. }
  4928. if (!noMessage) {
  4929. e << "Target \"" << this->GetName() << "\" links to itself.";
  4930. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4931. messageType, e.str(), this->GetBacktrace());
  4932. if (messageType == cmake::FATAL_ERROR) {
  4933. return;
  4934. }
  4935. }
  4936. }
  4937. continue;
  4938. }
  4939. // The entry is meant for this configuration.
  4940. impl.Libraries.emplace_back(name, this->FindTargetToLink(name), *btIt,
  4941. evaluated != *le);
  4942. }
  4943. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  4944. for (std::string const& sp : seenProps) {
  4945. if (!this->GetProperty(sp)) {
  4946. this->LinkImplicitNullProperties.insert(sp);
  4947. }
  4948. }
  4949. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  4950. }
  4951. // Get the list of configurations considered to be DEBUG.
  4952. std::vector<std::string> debugConfigs =
  4953. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  4954. cmTargetLinkLibraryType linkType =
  4955. CMP0003_ComputeLinkType(config, debugConfigs);
  4956. cmTarget::LinkLibraryVectorType const& oldllibs =
  4957. this->Target->GetOriginalLinkLibraries();
  4958. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  4959. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  4960. std::string name = this->CheckCMP0004(oldllib.first);
  4961. if (name == this->GetName() || name.empty()) {
  4962. continue;
  4963. }
  4964. // Support OLD behavior for CMP0003.
  4965. impl.WrongConfigLibraries.emplace_back(name,
  4966. this->FindTargetToLink(name));
  4967. }
  4968. }
  4969. }
  4970. cmGeneratorTarget* cmGeneratorTarget::FindTargetToLink(
  4971. std::string const& name) const
  4972. {
  4973. cmGeneratorTarget* tgt =
  4974. this->LocalGenerator->FindGeneratorTargetToUse(name);
  4975. // Skip targets that will not really be linked. This is probably a
  4976. // name conflict between an external library and an executable
  4977. // within the project.
  4978. if (tgt && tgt->GetType() == cmStateEnums::EXECUTABLE &&
  4979. !tgt->IsExecutableWithExports()) {
  4980. tgt = nullptr;
  4981. }
  4982. return tgt;
  4983. }
  4984. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  4985. {
  4986. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  4987. // Return the directory in which the target will be built.
  4988. return info->PdbDir;
  4989. }
  4990. return "";
  4991. }
  4992. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  4993. {
  4994. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  4995. }
  4996. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  4997. std::string const& gnuName,
  4998. std::string& out,
  4999. const char* newExt) const
  5000. {
  5001. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  5002. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  5003. out = gnuName.substr(0, gnuName.size() - 6);
  5004. out += newExt ? newExt : ".lib";
  5005. return true;
  5006. }
  5007. return false;
  5008. }
  5009. bool cmGeneratorTarget::IsExecutableWithExports() const
  5010. {
  5011. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  5012. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  5013. }
  5014. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  5015. {
  5016. return (this->IsDLLPlatform() &&
  5017. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5018. this->IsExecutableWithExports()) &&
  5019. // Assemblies which have only managed code do not have
  5020. // import libraries.
  5021. this->GetManagedType(config) != ManagedType::Managed);
  5022. }
  5023. std::string cmGeneratorTarget::GetSupportDirectory() const
  5024. {
  5025. std::string dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  5026. dir += cmake::GetCMakeFilesDirectory();
  5027. dir += "/";
  5028. dir += this->GetName();
  5029. #if defined(__VMS)
  5030. dir += "_dir";
  5031. #else
  5032. dir += ".dir";
  5033. #endif
  5034. return dir;
  5035. }
  5036. bool cmGeneratorTarget::IsLinkable() const
  5037. {
  5038. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5039. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5040. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  5041. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  5042. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5043. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  5044. this->IsExecutableWithExports());
  5045. }
  5046. bool cmGeneratorTarget::IsFrameworkOnApple() const
  5047. {
  5048. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5049. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  5050. this->Makefile->IsOn("APPLE") &&
  5051. this->GetPropertyAsBool("FRAMEWORK"));
  5052. }
  5053. bool cmGeneratorTarget::IsAppBundleOnApple() const
  5054. {
  5055. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  5056. this->Makefile->IsOn("APPLE") &&
  5057. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  5058. }
  5059. bool cmGeneratorTarget::IsXCTestOnApple() const
  5060. {
  5061. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  5062. }
  5063. bool cmGeneratorTarget::IsCFBundleOnApple() const
  5064. {
  5065. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  5066. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  5067. }
  5068. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  5069. std::string const& propval) const
  5070. {
  5071. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  5072. // or only C++/CLI) does only depend on whether the property is an empty
  5073. // string or contains any value at all. In Visual Studio generators
  5074. // this propval is prepended with /clr[:] which results in:
  5075. //
  5076. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  5077. // lib
  5078. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  5079. // target, has import lib
  5080. // 3. any value (safe,pure): add /clr:[propval] as flag, target with
  5081. // managed code only, no import lib
  5082. return propval.empty() ? ManagedType::Mixed : ManagedType::Managed;
  5083. }
  5084. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  5085. const std::string& config) const
  5086. {
  5087. // Only libraries and executables can be managed targets.
  5088. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  5089. return ManagedType::Undefined;
  5090. }
  5091. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5092. return ManagedType::Native;
  5093. }
  5094. // Check imported target.
  5095. if (this->IsImported()) {
  5096. if (cmGeneratorTarget::ImportInfo const* info =
  5097. this->GetImportInfo(config)) {
  5098. return info->Managed;
  5099. }
  5100. return ManagedType::Undefined;
  5101. }
  5102. // Check for explicitly set clr target property.
  5103. if (auto* clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  5104. return this->CheckManagedType(clr);
  5105. }
  5106. // C# targets are always managed. This language specific check
  5107. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  5108. // has to be set manually for C# targets.
  5109. return this->HasLanguage("CSharp", config) ? ManagedType::Managed
  5110. : ManagedType::Native;
  5111. }