cmGeneratorTarget.cxx 184 KB

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