cmGeneratorTarget.cxx 183 KB

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