cmGeneratorTarget.cxx 229 KB

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