cmGeneratorTarget.cxx 304 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087
  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 <array>
  6. #include <cassert>
  7. #include <cerrno>
  8. #include <cstddef>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <iterator>
  13. #include <queue>
  14. #include <sstream>
  15. #include <unordered_set>
  16. #include <utility>
  17. #include <cm/memory>
  18. #include <cm/optional>
  19. #include <cm/string_view>
  20. #include <cmext/algorithm>
  21. #include <cmext/string_view>
  22. #include "cmsys/RegularExpression.hxx"
  23. #include "cmAlgorithms.h"
  24. #include "cmComputeLinkInformation.h"
  25. #include "cmCustomCommandGenerator.h"
  26. #include "cmFileSet.h"
  27. #include "cmFileTimes.h"
  28. #include "cmGeneratedFileStream.h"
  29. #include "cmGeneratorExpression.h"
  30. #include "cmGeneratorExpressionContext.h"
  31. #include "cmGeneratorExpressionDAGChecker.h"
  32. #include "cmGeneratorExpressionNode.h"
  33. #include "cmGlobalGenerator.h"
  34. #include "cmLocalGenerator.h"
  35. #include "cmMakefile.h"
  36. #include "cmMessageType.h"
  37. #include "cmOutputConverter.h"
  38. #include "cmPropertyMap.h"
  39. #include "cmRange.h"
  40. #include "cmSourceFile.h"
  41. #include "cmSourceFileLocation.h"
  42. #include "cmSourceFileLocationKind.h"
  43. #include "cmSourceGroup.h"
  44. #include "cmStandardLevelResolver.h"
  45. #include "cmState.h"
  46. #include "cmStringAlgorithms.h"
  47. #include "cmSystemTools.h"
  48. #include "cmTarget.h"
  49. #include "cmTargetLinkLibraryType.h"
  50. #include "cmTargetPropertyComputer.h"
  51. #include "cmake.h"
  52. namespace {
  53. using LinkInterfaceFor = cmGeneratorTarget::LinkInterfaceFor;
  54. const cmsys::RegularExpression FrameworkRegularExpression(
  55. "^(.*/)?([^/]*)\\.framework/(.*)$");
  56. const std::string kINTERFACE_LINK_LIBRARIES = "INTERFACE_LINK_LIBRARIES";
  57. const std::string kINTERFACE_LINK_LIBRARIES_DIRECT =
  58. "INTERFACE_LINK_LIBRARIES_DIRECT";
  59. const std::string kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE =
  60. "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE";
  61. }
  62. template <>
  63. cmValue cmTargetPropertyComputer::GetSources<cmGeneratorTarget>(
  64. cmGeneratorTarget const* tgt, cmMakefile const& /* mf */)
  65. {
  66. return tgt->GetSourcesProperty();
  67. }
  68. template <>
  69. const std::string&
  70. cmTargetPropertyComputer::ComputeLocationForBuild<cmGeneratorTarget>(
  71. cmGeneratorTarget const* tgt)
  72. {
  73. return tgt->GetLocation("");
  74. }
  75. template <>
  76. const std::string&
  77. cmTargetPropertyComputer::ComputeLocation<cmGeneratorTarget>(
  78. cmGeneratorTarget const* tgt, const std::string& config)
  79. {
  80. return tgt->GetLocation(config);
  81. }
  82. class cmGeneratorTarget::TargetPropertyEntry
  83. {
  84. protected:
  85. static cmLinkImplItem NoLinkImplItem;
  86. public:
  87. TargetPropertyEntry(cmLinkImplItem const& item)
  88. : LinkImplItem(item)
  89. {
  90. }
  91. virtual ~TargetPropertyEntry() = default;
  92. virtual const std::string& Evaluate(
  93. cmLocalGenerator* lg, const std::string& config,
  94. cmGeneratorTarget const* headTarget,
  95. cmGeneratorExpressionDAGChecker* dagChecker,
  96. std::string const& language) const = 0;
  97. virtual cmListFileBacktrace GetBacktrace() const = 0;
  98. virtual std::string const& GetInput() const = 0;
  99. virtual bool GetHadContextSensitiveCondition() const { return false; }
  100. cmLinkImplItem const& LinkImplItem;
  101. };
  102. cmLinkImplItem cmGeneratorTarget::TargetPropertyEntry::NoLinkImplItem;
  103. class TargetPropertyEntryGenex : public cmGeneratorTarget::TargetPropertyEntry
  104. {
  105. public:
  106. TargetPropertyEntryGenex(std::unique_ptr<cmCompiledGeneratorExpression> cge,
  107. cmLinkImplItem const& item = NoLinkImplItem)
  108. : cmGeneratorTarget::TargetPropertyEntry(item)
  109. , ge(std::move(cge))
  110. {
  111. }
  112. const std::string& Evaluate(cmLocalGenerator* lg, const std::string& config,
  113. cmGeneratorTarget const* headTarget,
  114. cmGeneratorExpressionDAGChecker* dagChecker,
  115. std::string const& language) const override
  116. {
  117. return this->ge->Evaluate(lg, config, headTarget, dagChecker, nullptr,
  118. language);
  119. }
  120. cmListFileBacktrace GetBacktrace() const override
  121. {
  122. return this->ge->GetBacktrace();
  123. }
  124. std::string const& GetInput() const override { return this->ge->GetInput(); }
  125. bool GetHadContextSensitiveCondition() const override
  126. {
  127. return this->ge->GetHadContextSensitiveCondition();
  128. }
  129. private:
  130. const std::unique_ptr<cmCompiledGeneratorExpression> ge;
  131. };
  132. class TargetPropertyEntryString : public cmGeneratorTarget::TargetPropertyEntry
  133. {
  134. public:
  135. TargetPropertyEntryString(BT<std::string> propertyValue,
  136. cmLinkImplItem const& item = NoLinkImplItem)
  137. : cmGeneratorTarget::TargetPropertyEntry(item)
  138. , PropertyValue(std::move(propertyValue))
  139. {
  140. }
  141. const std::string& Evaluate(cmLocalGenerator*, const std::string&,
  142. cmGeneratorTarget const*,
  143. cmGeneratorExpressionDAGChecker*,
  144. std::string const&) const override
  145. {
  146. return this->PropertyValue.Value;
  147. }
  148. cmListFileBacktrace GetBacktrace() const override
  149. {
  150. return this->PropertyValue.Backtrace;
  151. }
  152. std::string const& GetInput() const override
  153. {
  154. return this->PropertyValue.Value;
  155. }
  156. private:
  157. BT<std::string> PropertyValue;
  158. };
  159. class TargetPropertyEntryFileSet
  160. : public cmGeneratorTarget::TargetPropertyEntry
  161. {
  162. public:
  163. TargetPropertyEntryFileSet(
  164. std::vector<std::string> dirs, bool contextSensitiveDirs,
  165. std::unique_ptr<cmCompiledGeneratorExpression> entryCge,
  166. const cmFileSet* fileSet, cmLinkImplItem const& item = NoLinkImplItem)
  167. : cmGeneratorTarget::TargetPropertyEntry(item)
  168. , BaseDirs(std::move(dirs))
  169. , ContextSensitiveDirs(contextSensitiveDirs)
  170. , EntryCge(std::move(entryCge))
  171. , FileSet(fileSet)
  172. {
  173. }
  174. const std::string& Evaluate(cmLocalGenerator* lg, const std::string& config,
  175. cmGeneratorTarget const* headTarget,
  176. cmGeneratorExpressionDAGChecker* dagChecker,
  177. std::string const& /*lang*/) const override
  178. {
  179. std::map<std::string, std::vector<std::string>> filesPerDir;
  180. this->FileSet->EvaluateFileEntry(this->BaseDirs, filesPerDir,
  181. this->EntryCge, lg, config, headTarget,
  182. dagChecker);
  183. std::vector<std::string> files;
  184. for (auto const& it : filesPerDir) {
  185. files.insert(files.end(), it.second.begin(), it.second.end());
  186. }
  187. static std::string filesStr;
  188. filesStr = cmJoin(files, ";");
  189. return filesStr;
  190. }
  191. cmListFileBacktrace GetBacktrace() const override
  192. {
  193. return this->EntryCge->GetBacktrace();
  194. }
  195. std::string const& GetInput() const override
  196. {
  197. return this->EntryCge->GetInput();
  198. }
  199. bool GetHadContextSensitiveCondition() const override
  200. {
  201. return this->ContextSensitiveDirs ||
  202. this->EntryCge->GetHadContextSensitiveCondition();
  203. }
  204. private:
  205. const std::vector<std::string> BaseDirs;
  206. const bool ContextSensitiveDirs;
  207. const std::unique_ptr<cmCompiledGeneratorExpression> EntryCge;
  208. const cmFileSet* FileSet;
  209. };
  210. std::unique_ptr<
  211. cmGeneratorTarget::
  212. TargetPropertyEntry> static CreateTargetPropertyEntry(cmake& cmakeInstance,
  213. const BT<
  214. std::string>&
  215. propertyValue,
  216. bool
  217. evaluateForBuildsystem =
  218. false)
  219. {
  220. if (cmGeneratorExpression::Find(propertyValue.Value) != std::string::npos) {
  221. cmGeneratorExpression ge(cmakeInstance, propertyValue.Backtrace);
  222. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  223. ge.Parse(propertyValue.Value);
  224. cge->SetEvaluateForBuildsystem(evaluateForBuildsystem);
  225. return std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>(
  226. cm::make_unique<TargetPropertyEntryGenex>(std::move(cge)));
  227. }
  228. return std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>(
  229. cm::make_unique<TargetPropertyEntryString>(propertyValue));
  230. }
  231. static void CreatePropertyGeneratorExpressions(
  232. cmake& cmakeInstance, cmBTStringRange entries,
  233. std::vector<std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>>& items,
  234. bool evaluateForBuildsystem = false)
  235. {
  236. for (auto const& entry : entries) {
  237. items.push_back(
  238. CreateTargetPropertyEntry(cmakeInstance, entry, evaluateForBuildsystem));
  239. }
  240. }
  241. namespace {
  242. // Represent a target property entry after evaluating generator expressions
  243. // and splitting up lists.
  244. struct EvaluatedTargetPropertyEntry
  245. {
  246. EvaluatedTargetPropertyEntry(cmLinkImplItem const& item,
  247. cmListFileBacktrace bt)
  248. : LinkImplItem(item)
  249. , Backtrace(std::move(bt))
  250. {
  251. }
  252. // Move-only.
  253. EvaluatedTargetPropertyEntry(EvaluatedTargetPropertyEntry&&) = default;
  254. EvaluatedTargetPropertyEntry(EvaluatedTargetPropertyEntry const&) = delete;
  255. EvaluatedTargetPropertyEntry& operator=(EvaluatedTargetPropertyEntry&&) =
  256. delete;
  257. EvaluatedTargetPropertyEntry& operator=(
  258. EvaluatedTargetPropertyEntry const&) = delete;
  259. cmLinkImplItem const& LinkImplItem;
  260. cmListFileBacktrace Backtrace;
  261. std::vector<std::string> Values;
  262. bool ContextDependent = false;
  263. };
  264. EvaluatedTargetPropertyEntry EvaluateTargetPropertyEntry(
  265. cmGeneratorTarget const* thisTarget, std::string const& config,
  266. std::string const& lang, cmGeneratorExpressionDAGChecker* dagChecker,
  267. cmGeneratorTarget::TargetPropertyEntry& entry)
  268. {
  269. EvaluatedTargetPropertyEntry ee(entry.LinkImplItem, entry.GetBacktrace());
  270. cmExpandList(entry.Evaluate(thisTarget->GetLocalGenerator(), config,
  271. thisTarget, dagChecker, lang),
  272. ee.Values);
  273. if (entry.GetHadContextSensitiveCondition()) {
  274. ee.ContextDependent = true;
  275. }
  276. return ee;
  277. }
  278. struct EvaluatedTargetPropertyEntries
  279. {
  280. std::vector<EvaluatedTargetPropertyEntry> Entries;
  281. bool HadContextSensitiveCondition = false;
  282. };
  283. EvaluatedTargetPropertyEntries EvaluateTargetPropertyEntries(
  284. cmGeneratorTarget const* thisTarget, std::string const& config,
  285. std::string const& lang, cmGeneratorExpressionDAGChecker* dagChecker,
  286. std::vector<std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>> const&
  287. in)
  288. {
  289. EvaluatedTargetPropertyEntries out;
  290. out.Entries.reserve(in.size());
  291. for (auto const& entry : in) {
  292. out.Entries.emplace_back(EvaluateTargetPropertyEntry(
  293. thisTarget, config, lang, dagChecker, *entry));
  294. }
  295. return out;
  296. }
  297. }
  298. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t, cmLocalGenerator* lg)
  299. : Target(t)
  300. {
  301. this->Makefile = this->Target->GetMakefile();
  302. this->LocalGenerator = lg;
  303. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  304. this->GlobalGenerator->ComputeTargetObjectDirectory(this);
  305. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  306. t->GetIncludeDirectoriesEntries(),
  307. this->IncludeDirectoriesEntries);
  308. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  309. t->GetCompileOptionsEntries(),
  310. this->CompileOptionsEntries);
  311. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  312. t->GetCompileFeaturesEntries(),
  313. this->CompileFeaturesEntries);
  314. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  315. t->GetCompileDefinitionsEntries(),
  316. this->CompileDefinitionsEntries);
  317. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  318. t->GetLinkOptionsEntries(),
  319. this->LinkOptionsEntries);
  320. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  321. t->GetLinkDirectoriesEntries(),
  322. this->LinkDirectoriesEntries);
  323. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  324. t->GetPrecompileHeadersEntries(),
  325. this->PrecompileHeadersEntries);
  326. CreatePropertyGeneratorExpressions(
  327. *lg->GetCMakeInstance(), t->GetSourceEntries(), this->SourceEntries, true);
  328. this->PolicyMap = t->GetPolicyMap();
  329. // Get hard-coded linker language
  330. if (this->Target->GetProperty("HAS_CXX")) {
  331. this->LinkerLanguage = "CXX";
  332. } else {
  333. this->LinkerLanguage = this->Target->GetSafeProperty("LINKER_LANGUAGE");
  334. }
  335. }
  336. cmGeneratorTarget::~cmGeneratorTarget() = default;
  337. cmValue cmGeneratorTarget::GetSourcesProperty() const
  338. {
  339. std::vector<std::string> values;
  340. for (auto const& se : this->SourceEntries) {
  341. values.push_back(se->GetInput());
  342. }
  343. static std::string value;
  344. value.clear();
  345. value = cmJoin(values, ";");
  346. return cmValue(value);
  347. }
  348. cmGlobalGenerator* cmGeneratorTarget::GetGlobalGenerator() const
  349. {
  350. return this->GetLocalGenerator()->GetGlobalGenerator();
  351. }
  352. cmLocalGenerator* cmGeneratorTarget::GetLocalGenerator() const
  353. {
  354. return this->LocalGenerator;
  355. }
  356. cmStateEnums::TargetType cmGeneratorTarget::GetType() const
  357. {
  358. return this->Target->GetType();
  359. }
  360. const std::string& cmGeneratorTarget::GetName() const
  361. {
  362. return this->Target->GetName();
  363. }
  364. std::string cmGeneratorTarget::GetExportName() const
  365. {
  366. cmValue exportName = this->GetProperty("EXPORT_NAME");
  367. if (cmNonempty(exportName)) {
  368. if (!cmGeneratorExpression::IsValidTargetName(*exportName)) {
  369. std::ostringstream e;
  370. e << "EXPORT_NAME property \"" << *exportName << "\" for \""
  371. << this->GetName() << "\": is not valid.";
  372. cmSystemTools::Error(e.str());
  373. return "";
  374. }
  375. return *exportName;
  376. }
  377. return this->GetName();
  378. }
  379. cmValue cmGeneratorTarget::GetProperty(const std::string& prop) const
  380. {
  381. if (cmValue result =
  382. cmTargetPropertyComputer::GetProperty(this, prop, *this->Makefile)) {
  383. return result;
  384. }
  385. if (cmSystemTools::GetFatalErrorOccurred()) {
  386. return nullptr;
  387. }
  388. return this->Target->GetProperty(prop);
  389. }
  390. std::string const& cmGeneratorTarget::GetSafeProperty(
  391. std::string const& prop) const
  392. {
  393. return this->GetProperty(prop);
  394. }
  395. const char* cmGeneratorTarget::GetOutputTargetType(
  396. cmStateEnums::ArtifactType artifact) const
  397. {
  398. switch (this->GetType()) {
  399. case cmStateEnums::SHARED_LIBRARY:
  400. if (this->IsDLLPlatform()) {
  401. switch (artifact) {
  402. case cmStateEnums::RuntimeBinaryArtifact:
  403. // A DLL shared library is treated as a runtime target.
  404. return "RUNTIME";
  405. case cmStateEnums::ImportLibraryArtifact:
  406. // A DLL import library is treated as an archive target.
  407. return "ARCHIVE";
  408. }
  409. } else {
  410. // For non-DLL platforms shared libraries are treated as
  411. // library targets.
  412. return "LIBRARY";
  413. }
  414. break;
  415. case cmStateEnums::STATIC_LIBRARY:
  416. // Static libraries are always treated as archive targets.
  417. return "ARCHIVE";
  418. case cmStateEnums::MODULE_LIBRARY:
  419. switch (artifact) {
  420. case cmStateEnums::RuntimeBinaryArtifact:
  421. // Module libraries are always treated as library targets.
  422. return "LIBRARY";
  423. case cmStateEnums::ImportLibraryArtifact:
  424. // Module import libraries are treated as archive targets.
  425. return "ARCHIVE";
  426. }
  427. break;
  428. case cmStateEnums::OBJECT_LIBRARY:
  429. // Object libraries are always treated as object targets.
  430. return "OBJECT";
  431. case cmStateEnums::EXECUTABLE:
  432. switch (artifact) {
  433. case cmStateEnums::RuntimeBinaryArtifact:
  434. // Executables are always treated as runtime targets.
  435. return "RUNTIME";
  436. case cmStateEnums::ImportLibraryArtifact:
  437. // Executable import libraries are treated as archive targets.
  438. return "ARCHIVE";
  439. }
  440. break;
  441. default:
  442. break;
  443. }
  444. return "";
  445. }
  446. std::string cmGeneratorTarget::GetOutputName(
  447. const std::string& config, cmStateEnums::ArtifactType artifact) const
  448. {
  449. // Lookup/compute/cache the output name for this configuration.
  450. OutputNameKey key(config, artifact);
  451. auto i = this->OutputNameMap.find(key);
  452. if (i == this->OutputNameMap.end()) {
  453. // Add empty name in map to detect potential recursion.
  454. OutputNameMapType::value_type entry(key, "");
  455. i = this->OutputNameMap.insert(entry).first;
  456. // Compute output name.
  457. std::vector<std::string> props;
  458. std::string type = this->GetOutputTargetType(artifact);
  459. std::string configUpper = cmSystemTools::UpperCase(config);
  460. if (!type.empty() && !configUpper.empty()) {
  461. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME_<CONFIG>
  462. props.push_back(type + "_OUTPUT_NAME_" + configUpper);
  463. }
  464. if (!type.empty()) {
  465. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME
  466. props.push_back(type + "_OUTPUT_NAME");
  467. }
  468. if (!configUpper.empty()) {
  469. // OUTPUT_NAME_<CONFIG>
  470. props.push_back("OUTPUT_NAME_" + configUpper);
  471. // <CONFIG>_OUTPUT_NAME
  472. props.push_back(configUpper + "_OUTPUT_NAME");
  473. }
  474. // OUTPUT_NAME
  475. props.emplace_back("OUTPUT_NAME");
  476. std::string outName;
  477. for (std::string const& p : props) {
  478. if (cmValue outNameProp = this->GetProperty(p)) {
  479. outName = *outNameProp;
  480. break;
  481. }
  482. }
  483. if (outName.empty()) {
  484. outName = this->GetName();
  485. }
  486. // Now evaluate genex and update the previously-prepared map entry.
  487. i->second =
  488. cmGeneratorExpression::Evaluate(outName, this->LocalGenerator, config);
  489. } else if (i->second.empty()) {
  490. // An empty map entry indicates we have been called recursively
  491. // from the above block.
  492. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  493. MessageType::FATAL_ERROR,
  494. "Target '" + this->GetName() + "' OUTPUT_NAME depends on itself.",
  495. this->GetBacktrace());
  496. }
  497. return i->second;
  498. }
  499. std::string cmGeneratorTarget::GetFilePrefix(
  500. const std::string& config, cmStateEnums::ArtifactType artifact) const
  501. {
  502. if (this->IsImported()) {
  503. cmValue prefix = this->GetFilePrefixInternal(config, artifact);
  504. return prefix ? *prefix : std::string();
  505. }
  506. return this->GetFullNameInternalComponents(config, artifact).prefix;
  507. }
  508. std::string cmGeneratorTarget::GetFileSuffix(
  509. const std::string& config, cmStateEnums::ArtifactType artifact) const
  510. {
  511. if (this->IsImported()) {
  512. cmValue suffix = this->GetFileSuffixInternal(config, artifact);
  513. return suffix ? *suffix : std::string();
  514. }
  515. return this->GetFullNameInternalComponents(config, artifact).suffix;
  516. }
  517. std::string cmGeneratorTarget::GetFilePostfix(const std::string& config) const
  518. {
  519. cmValue postfix = nullptr;
  520. std::string frameworkPostfix;
  521. if (!config.empty()) {
  522. std::string configProp =
  523. cmStrCat(cmSystemTools::UpperCase(config), "_POSTFIX");
  524. postfix = this->GetProperty(configProp);
  525. // Mac application bundles and frameworks have no regular postfix like
  526. // libraries do.
  527. if (!this->IsImported() && postfix &&
  528. (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
  529. postfix = nullptr;
  530. }
  531. // Frameworks created by multi config generators can have a special
  532. // framework postfix.
  533. frameworkPostfix = this->GetFrameworkMultiConfigPostfix(config);
  534. if (!frameworkPostfix.empty()) {
  535. postfix = cmValue(frameworkPostfix);
  536. }
  537. }
  538. return postfix ? *postfix : std::string();
  539. }
  540. std::string cmGeneratorTarget::GetFrameworkMultiConfigPostfix(
  541. const std::string& config) const
  542. {
  543. cmValue postfix = nullptr;
  544. if (!config.empty()) {
  545. std::string configProp = cmStrCat("FRAMEWORK_MULTI_CONFIG_POSTFIX_",
  546. cmSystemTools::UpperCase(config));
  547. postfix = this->GetProperty(configProp);
  548. if (!this->IsImported() && postfix &&
  549. (this->IsFrameworkOnApple() &&
  550. !this->GetGlobalGenerator()->IsMultiConfig())) {
  551. postfix = nullptr;
  552. }
  553. }
  554. return postfix ? *postfix : std::string();
  555. }
  556. cmValue cmGeneratorTarget::GetFilePrefixInternal(
  557. std::string const& config, cmStateEnums::ArtifactType artifact,
  558. const std::string& language) const
  559. {
  560. // no prefix for non-main target types.
  561. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  562. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  563. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  564. this->GetType() != cmStateEnums::EXECUTABLE) {
  565. return nullptr;
  566. }
  567. const bool isImportedLibraryArtifact =
  568. (artifact == cmStateEnums::ImportLibraryArtifact);
  569. // Return an empty prefix for the import library if this platform
  570. // does not support import libraries.
  571. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  572. return nullptr;
  573. }
  574. // The implib option is only allowed for shared libraries, module
  575. // libraries, and executables.
  576. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  577. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  578. this->GetType() != cmStateEnums::EXECUTABLE) {
  579. artifact = cmStateEnums::RuntimeBinaryArtifact;
  580. }
  581. // Compute prefix value.
  582. cmValue targetPrefix =
  583. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
  584. : this->GetProperty("PREFIX"));
  585. if (!targetPrefix) {
  586. const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
  587. if (!language.empty() && cmNonempty(prefixVar)) {
  588. std::string langPrefix = cmStrCat(prefixVar, "_", language);
  589. targetPrefix = this->Makefile->GetDefinition(langPrefix);
  590. }
  591. // if there is no prefix on the target nor specific language
  592. // use the cmake definition.
  593. if (!targetPrefix && prefixVar) {
  594. targetPrefix = this->Makefile->GetDefinition(prefixVar);
  595. }
  596. }
  597. return targetPrefix;
  598. }
  599. cmValue cmGeneratorTarget::GetFileSuffixInternal(
  600. std::string const& config, cmStateEnums::ArtifactType artifact,
  601. const std::string& language) const
  602. {
  603. // no suffix for non-main target types.
  604. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  605. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  606. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  607. this->GetType() != cmStateEnums::EXECUTABLE) {
  608. return nullptr;
  609. }
  610. const bool isImportedLibraryArtifact =
  611. (artifact == cmStateEnums::ImportLibraryArtifact);
  612. // Return an empty suffix for the import library if this platform
  613. // does not support import libraries.
  614. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  615. return nullptr;
  616. }
  617. // The implib option is only allowed for shared libraries, module
  618. // libraries, and executables.
  619. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  620. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  621. this->GetType() != cmStateEnums::EXECUTABLE) {
  622. artifact = cmStateEnums::RuntimeBinaryArtifact;
  623. }
  624. // Compute suffix value.
  625. cmValue targetSuffix =
  626. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
  627. : this->GetProperty("SUFFIX"));
  628. if (!targetSuffix) {
  629. const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
  630. if (!language.empty() && cmNonempty(suffixVar)) {
  631. std::string langSuffix = cmStrCat(suffixVar, "_", language);
  632. targetSuffix = this->Makefile->GetDefinition(langSuffix);
  633. }
  634. // if there is no suffix on the target nor specific language
  635. // use the cmake definition.
  636. if (!targetSuffix && suffixVar) {
  637. targetSuffix = this->Makefile->GetDefinition(suffixVar);
  638. }
  639. }
  640. return targetSuffix;
  641. }
  642. void cmGeneratorTarget::ClearSourcesCache()
  643. {
  644. this->AllConfigSources.clear();
  645. this->KindedSourcesMap.clear();
  646. this->SourcesAreContextDependent = Tribool::Indeterminate;
  647. this->Objects.clear();
  648. this->VisitedConfigsForObjects.clear();
  649. this->LinkImplMap.clear();
  650. this->LinkImplUsageRequirementsOnlyMap.clear();
  651. this->IncludeDirectoriesCache.clear();
  652. this->CompileOptionsCache.clear();
  653. this->CompileDefinitionsCache.clear();
  654. this->PrecompileHeadersCache.clear();
  655. this->LinkOptionsCache.clear();
  656. this->LinkDirectoriesCache.clear();
  657. this->RuntimeBinaryFullNameCache.clear();
  658. this->ImportLibraryFullNameCache.clear();
  659. }
  660. void cmGeneratorTarget::ClearLinkInterfaceCache()
  661. {
  662. this->LinkInterfaceMap.clear();
  663. this->LinkInterfaceUsageRequirementsOnlyMap.clear();
  664. }
  665. void cmGeneratorTarget::AddSourceCommon(const std::string& src, bool before)
  666. {
  667. this->SourceEntries.insert(
  668. before ? this->SourceEntries.begin() : this->SourceEntries.end(),
  669. CreateTargetPropertyEntry(
  670. *this->LocalGenerator->GetCMakeInstance(),
  671. BT<std::string>(src, this->Makefile->GetBacktrace()), true));
  672. this->ClearSourcesCache();
  673. }
  674. void cmGeneratorTarget::AddSource(const std::string& src, bool before)
  675. {
  676. this->Target->AddSource(src, before);
  677. this->AddSourceCommon(src, before);
  678. }
  679. void cmGeneratorTarget::AddTracedSources(std::vector<std::string> const& srcs)
  680. {
  681. this->Target->AddTracedSources(srcs);
  682. if (!srcs.empty()) {
  683. this->AddSourceCommon(cmJoin(srcs, ";"));
  684. }
  685. }
  686. void cmGeneratorTarget::AddIncludeDirectory(const std::string& src,
  687. bool before)
  688. {
  689. this->Target->InsertInclude(
  690. BT<std::string>(src, this->Makefile->GetBacktrace()), before);
  691. this->IncludeDirectoriesEntries.insert(
  692. before ? this->IncludeDirectoriesEntries.begin()
  693. : this->IncludeDirectoriesEntries.end(),
  694. CreateTargetPropertyEntry(
  695. *this->Makefile->GetCMakeInstance(),
  696. BT<std::string>(src, this->Makefile->GetBacktrace()), true));
  697. }
  698. std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
  699. cmSourceFile const* sf) const
  700. {
  701. auto i = this->SourceDepends.find(sf);
  702. if (i != this->SourceDepends.end()) {
  703. return &i->second.Depends;
  704. }
  705. return nullptr;
  706. }
  707. namespace {
  708. void handleSystemIncludesDep(cmLocalGenerator* lg,
  709. cmGeneratorTarget const* depTgt,
  710. const std::string& config,
  711. cmGeneratorTarget const* headTarget,
  712. cmGeneratorExpressionDAGChecker* dagChecker,
  713. std::vector<std::string>& result,
  714. bool excludeImported, std::string const& language)
  715. {
  716. if (cmValue dirs =
  717. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")) {
  718. cmExpandList(cmGeneratorExpression::Evaluate(*dirs, lg, config, headTarget,
  719. dagChecker, depTgt, language),
  720. result);
  721. }
  722. if (!depTgt->GetPropertyAsBool("SYSTEM")) {
  723. return;
  724. }
  725. if (depTgt->IsImported()) {
  726. if (excludeImported) {
  727. return;
  728. }
  729. if (depTgt->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) {
  730. return;
  731. }
  732. }
  733. if (cmValue dirs = depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
  734. cmExpandList(cmGeneratorExpression::Evaluate(*dirs, lg, config, headTarget,
  735. dagChecker, depTgt, language),
  736. result);
  737. }
  738. }
  739. }
  740. /* clang-format off */
  741. #define IMPLEMENT_VISIT(KIND) \
  742. do { \
  743. KindedSources const& kinded = this->GetKindedSources(config); \
  744. for (SourceAndKind const& s : kinded.Sources) { \
  745. if (s.Kind == KIND) { \
  746. data.push_back(s.Source.Value); \
  747. } \
  748. } \
  749. } while (false)
  750. /* clang-format on */
  751. void cmGeneratorTarget::GetObjectSources(
  752. std::vector<cmSourceFile const*>& data, const std::string& config) const
  753. {
  754. IMPLEMENT_VISIT(SourceKindObjectSource);
  755. if (this->VisitedConfigsForObjects.count(config)) {
  756. return;
  757. }
  758. for (cmSourceFile const* it : data) {
  759. this->Objects[it];
  760. }
  761. this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
  762. this->VisitedConfigsForObjects.insert(config);
  763. }
  764. void cmGeneratorTarget::ComputeObjectMapping()
  765. {
  766. auto const& configs =
  767. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  768. std::set<std::string> configSet(configs.begin(), configs.end());
  769. if (configSet == this->VisitedConfigsForObjects) {
  770. return;
  771. }
  772. for (std::string const& c : configs) {
  773. std::vector<cmSourceFile const*> sourceFiles;
  774. this->GetObjectSources(sourceFiles, c);
  775. }
  776. }
  777. cmValue cmGeneratorTarget::GetFeature(const std::string& feature,
  778. const std::string& config) const
  779. {
  780. if (!config.empty()) {
  781. std::string featureConfig =
  782. cmStrCat(feature, '_', cmSystemTools::UpperCase(config));
  783. if (cmValue value = this->GetProperty(featureConfig)) {
  784. return value;
  785. }
  786. }
  787. if (cmValue value = this->GetProperty(feature)) {
  788. return value;
  789. }
  790. return this->LocalGenerator->GetFeature(feature, config);
  791. }
  792. const char* cmGeneratorTarget::GetLinkPIEProperty(
  793. const std::string& config) const
  794. {
  795. static std::string PICValue;
  796. PICValue = this->GetLinkInterfaceDependentStringAsBoolProperty(
  797. "POSITION_INDEPENDENT_CODE", config);
  798. if (PICValue == "(unset)") {
  799. // POSITION_INDEPENDENT_CODE is not set
  800. return nullptr;
  801. }
  802. auto status = this->GetPolicyStatusCMP0083();
  803. return (status != cmPolicies::WARN && status != cmPolicies::OLD)
  804. ? PICValue.c_str()
  805. : nullptr;
  806. }
  807. bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
  808. std::string const& config) const
  809. {
  810. cmValue feature = this->GetFeature("INTERPROCEDURAL_OPTIMIZATION", config);
  811. if (!cmIsOn(feature)) {
  812. // 'INTERPROCEDURAL_OPTIMIZATION' is off, no need to check policies
  813. return false;
  814. }
  815. if (lang != "C" && lang != "CXX" && lang != "CUDA" && lang != "Fortran") {
  816. // We do not define IPO behavior for other languages.
  817. return false;
  818. }
  819. if (lang == "CUDA") {
  820. // CUDA IPO requires both CUDA_ARCHITECTURES and CUDA_SEPARABLE_COMPILATION
  821. if (cmIsOff(this->GetSafeProperty("CUDA_ARCHITECTURES")) ||
  822. cmIsOff(this->GetSafeProperty("CUDA_SEPARABLE_COMPILATION"))) {
  823. return false;
  824. }
  825. }
  826. cmPolicies::PolicyStatus cmp0069 = this->GetPolicyStatusCMP0069();
  827. if (cmp0069 == cmPolicies::OLD || cmp0069 == cmPolicies::WARN) {
  828. if (this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_LEGACY_BEHAVIOR")) {
  829. return true;
  830. }
  831. if (this->PolicyReportedCMP0069) {
  832. // problem is already reported, no need to issue a message
  833. return false;
  834. }
  835. const bool in_try_compile =
  836. this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile();
  837. if (cmp0069 == cmPolicies::WARN && !in_try_compile) {
  838. std::ostringstream w;
  839. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0069) << "\n";
  840. w << "INTERPROCEDURAL_OPTIMIZATION property will be ignored for target "
  841. << "'" << this->GetName() << "'.";
  842. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  843. MessageType::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  844. this->PolicyReportedCMP0069 = true;
  845. }
  846. return false;
  847. }
  848. // Note: check consistency with messages from CheckIPOSupported
  849. const char* message = nullptr;
  850. if (!this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_SUPPORTED_BY_CMAKE")) {
  851. message = "CMake doesn't support IPO for current compiler";
  852. } else if (!this->Makefile->IsOn("_CMAKE_" + lang +
  853. "_IPO_MAY_BE_SUPPORTED_BY_COMPILER")) {
  854. message = "Compiler doesn't support IPO";
  855. } else if (!this->GlobalGenerator->IsIPOSupported()) {
  856. message = "CMake doesn't support IPO for current generator";
  857. }
  858. if (!message) {
  859. // No error/warning messages
  860. return true;
  861. }
  862. if (this->PolicyReportedCMP0069) {
  863. // problem is already reported, no need to issue a message
  864. return false;
  865. }
  866. this->PolicyReportedCMP0069 = true;
  867. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  868. MessageType::FATAL_ERROR, message, this->GetBacktrace());
  869. return false;
  870. }
  871. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  872. {
  873. this->ComputeObjectMapping();
  874. return this->Objects[file];
  875. }
  876. const char* cmGeneratorTarget::GetCustomObjectExtension() const
  877. {
  878. struct compiler_mode
  879. {
  880. std::string variable;
  881. std::string extension;
  882. };
  883. static std::array<compiler_mode, 4> const modes{
  884. { { "CUDA_PTX_COMPILATION", ".ptx" },
  885. { "CUDA_CUBIN_COMPILATION", ".cubin" },
  886. { "CUDA_FATBIN_COMPILATION", ".fatbin" },
  887. { "CUDA_OPTIX_COMPILATION", ".optixir" } }
  888. };
  889. std::string const& compiler =
  890. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  891. if (!compiler.empty()) {
  892. for (const auto& m : modes) {
  893. const bool has_extension = this->GetPropertyAsBool(m.variable);
  894. if (has_extension) {
  895. return m.extension.c_str();
  896. }
  897. }
  898. }
  899. return nullptr;
  900. }
  901. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  902. {
  903. this->ExplicitObjectName.insert(sf);
  904. }
  905. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  906. {
  907. const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
  908. auto it = this->ExplicitObjectName.find(file);
  909. return it != this->ExplicitObjectName.end();
  910. }
  911. BTs<std::string> const* cmGeneratorTarget::GetLanguageStandardProperty(
  912. std::string const& lang, std::string const& config) const
  913. {
  914. std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
  915. auto langStandardIter = this->LanguageStandardMap.find(key);
  916. if (langStandardIter != this->LanguageStandardMap.end()) {
  917. return &langStandardIter->second;
  918. }
  919. return this->Target->GetLanguageStandardProperty(
  920. cmStrCat(lang, "_STANDARD"));
  921. }
  922. cmValue cmGeneratorTarget::GetLanguageStandard(std::string const& lang,
  923. std::string const& config) const
  924. {
  925. BTs<std::string> const* languageStandard =
  926. this->GetLanguageStandardProperty(lang, config);
  927. if (languageStandard) {
  928. return cmValue(languageStandard->Value);
  929. }
  930. return nullptr;
  931. }
  932. cmValue cmGeneratorTarget::GetPropertyWithPairedLanguageSupport(
  933. std::string const& lang, const char* suffix) const
  934. {
  935. cmValue propertyValue = this->Target->GetProperty(cmStrCat(lang, suffix));
  936. if (!propertyValue) {
  937. // Check if we should use the value set by another language.
  938. if (lang == "OBJC") {
  939. propertyValue = this->GetPropertyWithPairedLanguageSupport("C", suffix);
  940. } else if (lang == "OBJCXX" || lang == "CUDA" || lang == "HIP") {
  941. propertyValue =
  942. this->GetPropertyWithPairedLanguageSupport("CXX", suffix);
  943. }
  944. }
  945. return propertyValue;
  946. }
  947. cmValue cmGeneratorTarget::GetLanguageExtensions(std::string const& lang) const
  948. {
  949. return this->GetPropertyWithPairedLanguageSupport(lang, "_EXTENSIONS");
  950. }
  951. bool cmGeneratorTarget::GetLanguageStandardRequired(
  952. std::string const& lang) const
  953. {
  954. return cmIsOn(
  955. this->GetPropertyWithPairedLanguageSupport(lang, "_STANDARD_REQUIRED"));
  956. }
  957. void cmGeneratorTarget::GetModuleDefinitionSources(
  958. std::vector<cmSourceFile const*>& data, const std::string& config) const
  959. {
  960. IMPLEMENT_VISIT(SourceKindModuleDefinition);
  961. }
  962. void cmGeneratorTarget::GetHeaderSources(
  963. std::vector<cmSourceFile const*>& data, const std::string& config) const
  964. {
  965. IMPLEMENT_VISIT(SourceKindHeader);
  966. }
  967. void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile const*>& data,
  968. const std::string& config) const
  969. {
  970. IMPLEMENT_VISIT(SourceKindExtra);
  971. }
  972. void cmGeneratorTarget::GetCustomCommands(
  973. std::vector<cmSourceFile const*>& data, const std::string& config) const
  974. {
  975. IMPLEMENT_VISIT(SourceKindCustomCommand);
  976. }
  977. void cmGeneratorTarget::GetExternalObjects(
  978. std::vector<cmSourceFile const*>& data, const std::string& config) const
  979. {
  980. IMPLEMENT_VISIT(SourceKindExternalObject);
  981. }
  982. void cmGeneratorTarget::GetManifests(std::vector<cmSourceFile const*>& data,
  983. const std::string& config) const
  984. {
  985. IMPLEMENT_VISIT(SourceKindManifest);
  986. }
  987. std::set<cmLinkItem> const& cmGeneratorTarget::GetUtilityItems() const
  988. {
  989. if (!this->UtilityItemsDone) {
  990. this->UtilityItemsDone = true;
  991. std::set<BT<std::pair<std::string, bool>>> const& utilities =
  992. this->GetUtilities();
  993. for (BT<std::pair<std::string, bool>> const& i : utilities) {
  994. if (cmGeneratorTarget* gt =
  995. this->LocalGenerator->FindGeneratorTargetToUse(i.Value.first)) {
  996. this->UtilityItems.insert(cmLinkItem(gt, i.Value.second, i.Backtrace));
  997. } else {
  998. this->UtilityItems.insert(
  999. cmLinkItem(i.Value.first, i.Value.second, i.Backtrace));
  1000. }
  1001. }
  1002. }
  1003. return this->UtilityItems;
  1004. }
  1005. const std::string& cmGeneratorTarget::GetLocation(
  1006. const std::string& config) const
  1007. {
  1008. static std::string location;
  1009. if (this->IsImported()) {
  1010. location = this->Target->ImportedGetFullPath(
  1011. config, cmStateEnums::RuntimeBinaryArtifact);
  1012. } else {
  1013. location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  1014. }
  1015. return location;
  1016. }
  1017. cm::optional<std::string> cmGeneratorTarget::MaybeGetLocation(
  1018. std::string const& config) const
  1019. {
  1020. cm::optional<std::string> location;
  1021. if (cmGeneratorTarget::ImportInfo const* imp = this->GetImportInfo(config)) {
  1022. if (!imp->Location.empty()) {
  1023. location = imp->Location;
  1024. }
  1025. } else {
  1026. location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  1027. }
  1028. return location;
  1029. }
  1030. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreBuildCommands()
  1031. const
  1032. {
  1033. return this->Target->GetPreBuildCommands();
  1034. }
  1035. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreLinkCommands()
  1036. const
  1037. {
  1038. return this->Target->GetPreLinkCommands();
  1039. }
  1040. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPostBuildCommands()
  1041. const
  1042. {
  1043. return this->Target->GetPostBuildCommands();
  1044. }
  1045. void cmGeneratorTarget::AppendCustomCommandSideEffects(
  1046. std::set<cmGeneratorTarget const*>& sideEffects) const
  1047. {
  1048. if (!this->GetPreBuildCommands().empty() ||
  1049. !this->GetPreLinkCommands().empty() ||
  1050. !this->GetPostBuildCommands().empty()) {
  1051. sideEffects.insert(this);
  1052. } else {
  1053. for (auto const& source : this->GetAllConfigSources()) {
  1054. if (source.Source->GetCustomCommand() != nullptr) {
  1055. sideEffects.insert(this);
  1056. break;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. void cmGeneratorTarget::AppendLanguageSideEffects(
  1062. std::map<std::string, std::set<cmGeneratorTarget const*>>& sideEffects) const
  1063. {
  1064. static const std::set<cm::string_view> LANGS_WITH_NO_SIDE_EFFECTS = {
  1065. "C"_s, "CXX"_s, "OBJC"_s, "OBJCXX"_s, "ASM"_s, "CUDA"_s, "HIP"_s
  1066. };
  1067. for (auto const& lang : this->GetAllConfigCompileLanguages()) {
  1068. if (!LANGS_WITH_NO_SIDE_EFFECTS.count(lang)) {
  1069. sideEffects[lang].insert(this);
  1070. }
  1071. }
  1072. }
  1073. bool cmGeneratorTarget::IsInBuildSystem() const
  1074. {
  1075. if (this->IsImported()) {
  1076. return false;
  1077. }
  1078. switch (this->Target->GetType()) {
  1079. case cmStateEnums::EXECUTABLE:
  1080. case cmStateEnums::STATIC_LIBRARY:
  1081. case cmStateEnums::SHARED_LIBRARY:
  1082. case cmStateEnums::MODULE_LIBRARY:
  1083. case cmStateEnums::OBJECT_LIBRARY:
  1084. case cmStateEnums::UTILITY:
  1085. case cmStateEnums::GLOBAL_TARGET:
  1086. return true;
  1087. case cmStateEnums::INTERFACE_LIBRARY:
  1088. // An INTERFACE library is in the build system if it has SOURCES,
  1089. // HEADER_SETS, or C++ module filesets.
  1090. if (!this->SourceEntries.empty() ||
  1091. !this->Target->GetHeaderSetsEntries().empty() ||
  1092. !this->Target->GetCxxModuleSetsEntries().empty() ||
  1093. !this->Target->GetCxxModuleHeaderSetsEntries().empty()) {
  1094. return true;
  1095. }
  1096. break;
  1097. case cmStateEnums::UNKNOWN_LIBRARY:
  1098. break;
  1099. }
  1100. return false;
  1101. }
  1102. bool cmGeneratorTarget::IsNormal() const
  1103. {
  1104. return this->Target->IsNormal();
  1105. }
  1106. bool cmGeneratorTarget::IsSynthetic() const
  1107. {
  1108. return this->Target->IsSynthetic();
  1109. }
  1110. bool cmGeneratorTarget::IsImported() const
  1111. {
  1112. return this->Target->IsImported();
  1113. }
  1114. bool cmGeneratorTarget::IsImportedGloballyVisible() const
  1115. {
  1116. return this->Target->IsImportedGloballyVisible();
  1117. }
  1118. bool cmGeneratorTarget::CanCompileSources() const
  1119. {
  1120. return this->Target->CanCompileSources();
  1121. }
  1122. const std::string& cmGeneratorTarget::GetLocationForBuild() const
  1123. {
  1124. static std::string location;
  1125. if (this->IsImported()) {
  1126. location = this->Target->ImportedGetFullPath(
  1127. "", cmStateEnums::RuntimeBinaryArtifact);
  1128. return location;
  1129. }
  1130. // Now handle the deprecated build-time configuration location.
  1131. std::string const noConfig;
  1132. location = this->GetDirectory(noConfig);
  1133. cmValue cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  1134. if (cfgid && (*cfgid != ".")) {
  1135. location += "/";
  1136. location += *cfgid;
  1137. }
  1138. if (this->IsAppBundleOnApple()) {
  1139. std::string macdir = this->BuildBundleDirectory("", "", FullLevel);
  1140. if (!macdir.empty()) {
  1141. location += "/";
  1142. location += macdir;
  1143. }
  1144. }
  1145. location += "/";
  1146. location += this->GetFullName("", cmStateEnums::RuntimeBinaryArtifact);
  1147. return location;
  1148. }
  1149. bool cmGeneratorTarget::IsSystemIncludeDirectory(
  1150. const std::string& dir, const std::string& config,
  1151. const std::string& language) const
  1152. {
  1153. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  1154. std::string config_upper;
  1155. if (!config.empty()) {
  1156. config_upper = cmSystemTools::UpperCase(config);
  1157. }
  1158. std::string key = cmStrCat(config_upper, "/", language);
  1159. auto iter = this->SystemIncludesCache.find(key);
  1160. if (iter == this->SystemIncludesCache.end()) {
  1161. cmGeneratorExpressionDAGChecker dagChecker(
  1162. this, "SYSTEM_INCLUDE_DIRECTORIES", nullptr, nullptr);
  1163. bool excludeImported = this->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  1164. std::vector<std::string> result;
  1165. for (std::string const& it : this->Target->GetSystemIncludeDirectories()) {
  1166. cmExpandList(cmGeneratorExpression::Evaluate(it, this->LocalGenerator,
  1167. config, this, &dagChecker,
  1168. nullptr, language),
  1169. result);
  1170. }
  1171. std::vector<cmGeneratorTarget const*> const& deps =
  1172. this->GetLinkImplementationClosure(config);
  1173. for (cmGeneratorTarget const* dep : deps) {
  1174. handleSystemIncludesDep(this->LocalGenerator, dep, config, this,
  1175. &dagChecker, result, excludeImported, language);
  1176. }
  1177. cmLinkImplementation const* impl =
  1178. this->GetLinkImplementation(config, LinkInterfaceFor::Usage);
  1179. if (impl != nullptr) {
  1180. auto runtimeEntries = impl->LanguageRuntimeLibraries.find(language);
  1181. if (runtimeEntries != impl->LanguageRuntimeLibraries.end()) {
  1182. for (auto const& lib : runtimeEntries->second) {
  1183. if (lib.Target) {
  1184. handleSystemIncludesDep(this->LocalGenerator, lib.Target, config,
  1185. this, &dagChecker, result, excludeImported,
  1186. language);
  1187. }
  1188. }
  1189. }
  1190. }
  1191. std::for_each(result.begin(), result.end(),
  1192. cmSystemTools::ConvertToUnixSlashes);
  1193. std::sort(result.begin(), result.end());
  1194. result.erase(std::unique(result.begin(), result.end()), result.end());
  1195. iter = this->SystemIncludesCache.emplace(key, result).first;
  1196. }
  1197. return std::binary_search(iter->second.begin(), iter->second.end(), dir);
  1198. }
  1199. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  1200. {
  1201. return this->Target->GetPropertyAsBool(prop);
  1202. }
  1203. bool cmGeneratorTarget::MaybeHaveInterfaceProperty(
  1204. std::string const& prop, cmGeneratorExpressionContext* context,
  1205. LinkInterfaceFor interfaceFor) const
  1206. {
  1207. std::string const key = prop + '@' + context->Config;
  1208. auto i = this->MaybeInterfacePropertyExists.find(key);
  1209. if (i == this->MaybeInterfacePropertyExists.end()) {
  1210. // Insert an entry now in case there is a cycle.
  1211. i = this->MaybeInterfacePropertyExists.emplace(key, false).first;
  1212. bool& maybeInterfaceProp = i->second;
  1213. // If this target itself has a non-empty property value, we are done.
  1214. maybeInterfaceProp = cmNonempty(this->GetProperty(prop));
  1215. // Otherwise, recurse to interface dependencies.
  1216. if (!maybeInterfaceProp) {
  1217. cmGeneratorTarget const* headTarget =
  1218. context->HeadTarget ? context->HeadTarget : this;
  1219. if (cmLinkInterfaceLibraries const* iface =
  1220. this->GetLinkInterfaceLibraries(context->Config, headTarget,
  1221. interfaceFor)) {
  1222. if (iface->HadHeadSensitiveCondition) {
  1223. // With a different head target we may get to a library with
  1224. // this interface property.
  1225. maybeInterfaceProp = true;
  1226. } else {
  1227. // The transitive interface libraries do not depend on the
  1228. // head target, so we can follow them.
  1229. for (cmLinkItem const& lib : iface->Libraries) {
  1230. if (lib.Target &&
  1231. lib.Target->MaybeHaveInterfaceProperty(prop, context,
  1232. interfaceFor)) {
  1233. maybeInterfaceProp = true;
  1234. break;
  1235. }
  1236. }
  1237. }
  1238. }
  1239. }
  1240. }
  1241. return i->second;
  1242. }
  1243. std::string cmGeneratorTarget::EvaluateInterfaceProperty(
  1244. std::string const& prop, cmGeneratorExpressionContext* context,
  1245. cmGeneratorExpressionDAGChecker* dagCheckerParent,
  1246. LinkInterfaceFor interfaceFor) const
  1247. {
  1248. std::string result;
  1249. // If the property does not appear transitively at all, we are done.
  1250. if (!this->MaybeHaveInterfaceProperty(prop, context, interfaceFor)) {
  1251. return result;
  1252. }
  1253. // Evaluate $<TARGET_PROPERTY:this,prop> as if it were compiled. This is
  1254. // a subset of TargetPropertyNode::Evaluate without stringify/parse steps
  1255. // but sufficient for transitive interface properties.
  1256. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace, this, prop,
  1257. nullptr, dagCheckerParent);
  1258. switch (dagChecker.Check()) {
  1259. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1260. dagChecker.ReportError(
  1261. context, "$<TARGET_PROPERTY:" + this->GetName() + "," + prop + ">");
  1262. return result;
  1263. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1264. // No error. We just skip cyclic references.
  1265. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1266. // No error. We have already seen this transitive property.
  1267. return result;
  1268. case cmGeneratorExpressionDAGChecker::DAG:
  1269. break;
  1270. }
  1271. cmGeneratorTarget const* headTarget =
  1272. context->HeadTarget ? context->HeadTarget : this;
  1273. if (cmValue p = this->GetProperty(prop)) {
  1274. result = cmGeneratorExpressionNode::EvaluateDependentExpression(
  1275. *p, context->LG, context, headTarget, &dagChecker, this);
  1276. }
  1277. if (cmLinkInterfaceLibraries const* iface = this->GetLinkInterfaceLibraries(
  1278. context->Config, headTarget, interfaceFor)) {
  1279. context->HadContextSensitiveCondition =
  1280. context->HadContextSensitiveCondition ||
  1281. iface->HadContextSensitiveCondition;
  1282. for (cmLinkItem const& lib : iface->Libraries) {
  1283. // Broken code can have a target in its own link interface.
  1284. // Don't follow such link interface entries so as not to create a
  1285. // self-referencing loop.
  1286. if (lib.Target && lib.Target != this) {
  1287. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in the
  1288. // above property and hand-evaluate it as if it were compiled.
  1289. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1290. cmGeneratorExpressionContext libContext(
  1291. context->LG, context->Config, context->Quiet, headTarget, this,
  1292. context->EvaluateForBuildsystem, context->Backtrace,
  1293. context->Language);
  1294. std::string libResult = cmGeneratorExpression::StripEmptyListElements(
  1295. lib.Target->EvaluateInterfaceProperty(prop, &libContext, &dagChecker,
  1296. interfaceFor));
  1297. if (!libResult.empty()) {
  1298. if (result.empty()) {
  1299. result = std::move(libResult);
  1300. } else {
  1301. result.reserve(result.size() + 1 + libResult.size());
  1302. result += ";";
  1303. result += libResult;
  1304. }
  1305. }
  1306. context->HadContextSensitiveCondition =
  1307. context->HadContextSensitiveCondition ||
  1308. libContext.HadContextSensitiveCondition;
  1309. context->HadHeadSensitiveCondition =
  1310. context->HadHeadSensitiveCondition ||
  1311. libContext.HadHeadSensitiveCondition;
  1312. }
  1313. }
  1314. }
  1315. return result;
  1316. }
  1317. namespace {
  1318. enum class IncludeDirectoryFallBack
  1319. {
  1320. BINARY,
  1321. OBJECT
  1322. };
  1323. std::string AddLangSpecificInterfaceIncludeDirectories(
  1324. const cmGeneratorTarget* root, const cmGeneratorTarget* target,
  1325. const std::string& lang, const std::string& config,
  1326. const std::string& propertyName, IncludeDirectoryFallBack mode,
  1327. cmGeneratorExpressionDAGChecker* context)
  1328. {
  1329. cmGeneratorExpressionDAGChecker dag{ target->GetBacktrace(), target,
  1330. propertyName, nullptr, context };
  1331. switch (dag.Check()) {
  1332. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1333. dag.ReportError(
  1334. nullptr, "$<TARGET_PROPERTY:" + target->GetName() + ",propertyName");
  1335. CM_FALLTHROUGH;
  1336. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1337. // No error. We just skip cyclic references.
  1338. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1339. // No error. We have already seen this transitive property.
  1340. return "";
  1341. case cmGeneratorExpressionDAGChecker::DAG:
  1342. break;
  1343. }
  1344. std::string directories;
  1345. if (const auto* interface = target->GetLinkInterfaceLibraries(
  1346. config, root, LinkInterfaceFor::Usage)) {
  1347. for (const cmLinkItem& library : interface->Libraries) {
  1348. if (const cmGeneratorTarget* dependency = library.Target) {
  1349. if (cm::contains(dependency->GetAllConfigCompileLanguages(), lang)) {
  1350. auto* lg = dependency->GetLocalGenerator();
  1351. std::string value = dependency->GetSafeProperty(propertyName);
  1352. if (value.empty()) {
  1353. if (mode == IncludeDirectoryFallBack::BINARY) {
  1354. value = lg->GetCurrentBinaryDirectory();
  1355. } else if (mode == IncludeDirectoryFallBack::OBJECT) {
  1356. value = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
  1357. lg->GetTargetDirectory(dependency));
  1358. }
  1359. }
  1360. if (!directories.empty()) {
  1361. directories += ";";
  1362. }
  1363. directories += value;
  1364. }
  1365. }
  1366. }
  1367. }
  1368. return directories;
  1369. }
  1370. void AddLangSpecificImplicitIncludeDirectories(
  1371. const cmGeneratorTarget* target, const std::string& lang,
  1372. const std::string& config, const std::string& propertyName,
  1373. IncludeDirectoryFallBack mode, EvaluatedTargetPropertyEntries& entries)
  1374. {
  1375. if (const auto* libraries = target->GetLinkImplementationLibraries(
  1376. config, LinkInterfaceFor::Usage)) {
  1377. cmGeneratorExpressionDAGChecker dag{ target->GetBacktrace(), target,
  1378. propertyName, nullptr, nullptr };
  1379. for (const cmLinkImplItem& library : libraries->Libraries) {
  1380. if (const cmGeneratorTarget* dependency = library.Target) {
  1381. if (!dependency->IsInBuildSystem()) {
  1382. continue;
  1383. }
  1384. if (cm::contains(dependency->GetAllConfigCompileLanguages(), lang)) {
  1385. auto* lg = dependency->GetLocalGenerator();
  1386. EvaluatedTargetPropertyEntry entry{ library, library.Backtrace };
  1387. if (cmValue val = dependency->GetProperty(propertyName)) {
  1388. entry.Values.emplace_back(*val);
  1389. } else {
  1390. if (mode == IncludeDirectoryFallBack::BINARY) {
  1391. entry.Values.emplace_back(lg->GetCurrentBinaryDirectory());
  1392. } else if (mode == IncludeDirectoryFallBack::OBJECT) {
  1393. entry.Values.emplace_back(
  1394. dependency->GetObjectDirectory(config));
  1395. }
  1396. }
  1397. cmExpandList(
  1398. AddLangSpecificInterfaceIncludeDirectories(
  1399. target, dependency, lang, config, propertyName, mode, &dag),
  1400. entry.Values);
  1401. entries.Entries.emplace_back(std::move(entry));
  1402. }
  1403. }
  1404. }
  1405. }
  1406. }
  1407. void addInterfaceEntry(cmGeneratorTarget const* headTarget,
  1408. std::string const& config, std::string const& prop,
  1409. std::string const& lang,
  1410. cmGeneratorExpressionDAGChecker* dagChecker,
  1411. EvaluatedTargetPropertyEntries& entries,
  1412. LinkInterfaceFor interfaceFor,
  1413. std::vector<cmLinkImplItem> const& libraries)
  1414. {
  1415. for (cmLinkImplItem const& lib : libraries) {
  1416. if (lib.Target) {
  1417. EvaluatedTargetPropertyEntry ee(lib, lib.Backtrace);
  1418. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1419. // caller's property and hand-evaluate it as if it were compiled.
  1420. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1421. cmGeneratorExpressionContext context(
  1422. headTarget->GetLocalGenerator(), config, false, headTarget, headTarget,
  1423. true, lib.Backtrace, lang);
  1424. cmExpandList(lib.Target->EvaluateInterfaceProperty(
  1425. prop, &context, dagChecker, interfaceFor),
  1426. ee.Values);
  1427. ee.ContextDependent = context.HadContextSensitiveCondition;
  1428. entries.Entries.emplace_back(std::move(ee));
  1429. }
  1430. }
  1431. }
  1432. // IncludeRuntimeInterface is used to break the cycle in computing
  1433. // the necessary transitive dependencies of targets that can occur
  1434. // now that we have implicit language runtime targets.
  1435. //
  1436. // To determine the set of languages that a target has we need to iterate
  1437. // all the sources which includes transitive INTERFACE sources.
  1438. // Therefore we can't determine what language runtimes are needed
  1439. // for a target until after all sources are computed.
  1440. //
  1441. // Therefore while computing the applicable INTERFACE_SOURCES we
  1442. // must ignore anything in LanguageRuntimeLibraries or we would
  1443. // create a cycle ( INTERFACE_SOURCES requires LanguageRuntimeLibraries,
  1444. // LanguageRuntimeLibraries requires INTERFACE_SOURCES).
  1445. //
  1446. enum class IncludeRuntimeInterface
  1447. {
  1448. Yes,
  1449. No
  1450. };
  1451. void AddInterfaceEntries(
  1452. cmGeneratorTarget const* headTarget, std::string const& config,
  1453. std::string const& prop, std::string const& lang,
  1454. cmGeneratorExpressionDAGChecker* dagChecker,
  1455. EvaluatedTargetPropertyEntries& entries,
  1456. IncludeRuntimeInterface searchRuntime,
  1457. LinkInterfaceFor interfaceFor = LinkInterfaceFor::Usage)
  1458. {
  1459. if (searchRuntime == IncludeRuntimeInterface::Yes) {
  1460. if (cmLinkImplementation const* impl =
  1461. headTarget->GetLinkImplementation(config, interfaceFor)) {
  1462. entries.HadContextSensitiveCondition =
  1463. impl->HadContextSensitiveCondition;
  1464. auto runtimeLibIt = impl->LanguageRuntimeLibraries.find(lang);
  1465. if (runtimeLibIt != impl->LanguageRuntimeLibraries.end()) {
  1466. addInterfaceEntry(headTarget, config, prop, lang, dagChecker, entries,
  1467. interfaceFor, runtimeLibIt->second);
  1468. }
  1469. addInterfaceEntry(headTarget, config, prop, lang, dagChecker, entries,
  1470. interfaceFor, impl->Libraries);
  1471. }
  1472. } else {
  1473. if (cmLinkImplementationLibraries const* impl =
  1474. headTarget->GetLinkImplementationLibraries(config, interfaceFor)) {
  1475. entries.HadContextSensitiveCondition =
  1476. impl->HadContextSensitiveCondition;
  1477. addInterfaceEntry(headTarget, config, prop, lang, dagChecker, entries,
  1478. interfaceFor, impl->Libraries);
  1479. }
  1480. }
  1481. }
  1482. void AddObjectEntries(cmGeneratorTarget const* headTarget,
  1483. std::string const& config,
  1484. cmGeneratorExpressionDAGChecker* dagChecker,
  1485. EvaluatedTargetPropertyEntries& entries)
  1486. {
  1487. if (cmLinkImplementationLibraries const* impl =
  1488. headTarget->GetLinkImplementationLibraries(config,
  1489. LinkInterfaceFor::Usage)) {
  1490. entries.HadContextSensitiveCondition = impl->HadContextSensitiveCondition;
  1491. for (cmLinkImplItem const& lib : impl->Libraries) {
  1492. if (lib.Target &&
  1493. lib.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1494. std::string uniqueName =
  1495. headTarget->GetGlobalGenerator()->IndexGeneratorTargetUniquely(
  1496. lib.Target);
  1497. std::string genex = "$<TARGET_OBJECTS:" + std::move(uniqueName) + ">";
  1498. cmGeneratorExpression ge(*headTarget->Makefile->GetCMakeInstance(),
  1499. lib.Backtrace);
  1500. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  1501. cge->SetEvaluateForBuildsystem(true);
  1502. EvaluatedTargetPropertyEntry ee(lib, lib.Backtrace);
  1503. cmExpandList(cge->Evaluate(headTarget->GetLocalGenerator(), config,
  1504. headTarget, dagChecker),
  1505. ee.Values);
  1506. if (cge->GetHadContextSensitiveCondition()) {
  1507. ee.ContextDependent = true;
  1508. }
  1509. entries.Entries.emplace_back(std::move(ee));
  1510. }
  1511. }
  1512. }
  1513. }
  1514. void addFileSetEntry(cmGeneratorTarget const* headTarget,
  1515. std::string const& config,
  1516. cmGeneratorExpressionDAGChecker* dagChecker,
  1517. cmFileSet const* fileSet,
  1518. EvaluatedTargetPropertyEntries& entries)
  1519. {
  1520. auto dirCges = fileSet->CompileDirectoryEntries();
  1521. auto dirs = fileSet->EvaluateDirectoryEntries(
  1522. dirCges, headTarget->GetLocalGenerator(), config, headTarget, dagChecker);
  1523. bool contextSensitiveDirs = false;
  1524. for (auto const& dirCge : dirCges) {
  1525. if (dirCge->GetHadContextSensitiveCondition()) {
  1526. contextSensitiveDirs = true;
  1527. break;
  1528. }
  1529. }
  1530. cmake* cm = headTarget->GetLocalGenerator()->GetCMakeInstance();
  1531. for (auto& entryCge : fileSet->CompileFileEntries()) {
  1532. TargetPropertyEntryFileSet tpe(dirs, contextSensitiveDirs,
  1533. std::move(entryCge), fileSet);
  1534. entries.Entries.emplace_back(
  1535. EvaluateTargetPropertyEntry(headTarget, config, "", dagChecker, tpe));
  1536. for (auto const& file : entries.Entries.back().Values) {
  1537. auto* sf = headTarget->Makefile->GetOrCreateSource(file);
  1538. if (fileSet->GetType() == "HEADERS"_s) {
  1539. sf->SetProperty("HEADER_FILE_ONLY", "TRUE");
  1540. }
  1541. #ifndef CMAKE_BOOTSTRAP
  1542. std::string e;
  1543. std::string w;
  1544. auto path = sf->ResolveFullPath(&e, &w);
  1545. if (!w.empty()) {
  1546. cm->IssueMessage(MessageType::AUTHOR_WARNING, w,
  1547. headTarget->GetBacktrace());
  1548. }
  1549. if (path.empty()) {
  1550. if (!e.empty()) {
  1551. cm->IssueMessage(MessageType::FATAL_ERROR, e,
  1552. headTarget->GetBacktrace());
  1553. }
  1554. return;
  1555. }
  1556. bool found = false;
  1557. for (auto const& sg : headTarget->Makefile->GetSourceGroups()) {
  1558. if (sg.MatchChildrenFiles(path)) {
  1559. found = true;
  1560. break;
  1561. }
  1562. }
  1563. if (!found) {
  1564. if (fileSet->GetType() == "HEADERS"_s ||
  1565. fileSet->GetType() == "CXX_MODULE_HEADER_UNITS"_s) {
  1566. headTarget->Makefile->GetOrCreateSourceGroup("Header Files")
  1567. ->AddGroupFile(path);
  1568. }
  1569. }
  1570. #endif
  1571. }
  1572. }
  1573. }
  1574. void AddFileSetEntries(cmGeneratorTarget const* headTarget,
  1575. std::string const& config,
  1576. cmGeneratorExpressionDAGChecker* dagChecker,
  1577. EvaluatedTargetPropertyEntries& entries)
  1578. {
  1579. for (auto const& entry : headTarget->Target->GetHeaderSetsEntries()) {
  1580. for (auto const& name : cmExpandedList(entry.Value)) {
  1581. auto const* headerSet = headTarget->Target->GetFileSet(name);
  1582. addFileSetEntry(headTarget, config, dagChecker, headerSet, entries);
  1583. }
  1584. }
  1585. for (auto const& entry : headTarget->Target->GetCxxModuleSetsEntries()) {
  1586. for (auto const& name : cmExpandedList(entry.Value)) {
  1587. auto const* cxxModuleSet = headTarget->Target->GetFileSet(name);
  1588. addFileSetEntry(headTarget, config, dagChecker, cxxModuleSet, entries);
  1589. }
  1590. }
  1591. for (auto const& entry :
  1592. headTarget->Target->GetCxxModuleHeaderSetsEntries()) {
  1593. for (auto const& name : cmExpandedList(entry.Value)) {
  1594. auto const* cxxModuleHeaderSet = headTarget->Target->GetFileSet(name);
  1595. addFileSetEntry(headTarget, config, dagChecker, cxxModuleHeaderSet,
  1596. entries);
  1597. }
  1598. }
  1599. }
  1600. bool processSources(cmGeneratorTarget const* tgt,
  1601. EvaluatedTargetPropertyEntries& entries,
  1602. std::vector<BT<std::string>>& srcs,
  1603. std::unordered_set<std::string>& uniqueSrcs,
  1604. bool debugSources)
  1605. {
  1606. cmMakefile* mf = tgt->Target->GetMakefile();
  1607. bool contextDependent = entries.HadContextSensitiveCondition;
  1608. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  1609. if (entry.ContextDependent) {
  1610. contextDependent = true;
  1611. }
  1612. cmLinkImplItem const& item = entry.LinkImplItem;
  1613. std::string const& targetName = item.AsStr();
  1614. for (std::string& src : entry.Values) {
  1615. cmSourceFile* sf = mf->GetOrCreateSource(src);
  1616. std::string e;
  1617. std::string w;
  1618. std::string fullPath = sf->ResolveFullPath(&e, &w);
  1619. cmake* cm = tgt->GetLocalGenerator()->GetCMakeInstance();
  1620. if (!w.empty()) {
  1621. cm->IssueMessage(MessageType::AUTHOR_WARNING, w, tgt->GetBacktrace());
  1622. }
  1623. if (fullPath.empty()) {
  1624. if (!e.empty()) {
  1625. cm->IssueMessage(MessageType::FATAL_ERROR, e, tgt->GetBacktrace());
  1626. }
  1627. return contextDependent;
  1628. }
  1629. if (!targetName.empty() && !cmSystemTools::FileIsFullPath(src)) {
  1630. std::ostringstream err;
  1631. if (!targetName.empty()) {
  1632. err << "Target \"" << targetName
  1633. << "\" contains relative path in its INTERFACE_SOURCES:\n \""
  1634. << src << "\"";
  1635. } else {
  1636. err << "Found relative path while evaluating sources of \""
  1637. << tgt->GetName() << "\":\n \"" << src << "\"\n";
  1638. }
  1639. tgt->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  1640. err.str());
  1641. return contextDependent;
  1642. }
  1643. src = fullPath;
  1644. }
  1645. std::string usedSources;
  1646. for (std::string const& src : entry.Values) {
  1647. if (uniqueSrcs.insert(src).second) {
  1648. srcs.emplace_back(src, entry.Backtrace);
  1649. if (debugSources) {
  1650. usedSources += " * " + src + "\n";
  1651. }
  1652. }
  1653. }
  1654. if (!usedSources.empty()) {
  1655. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1656. MessageType::LOG,
  1657. std::string("Used sources for target ") + tgt->GetName() + ":\n" +
  1658. usedSources,
  1659. entry.Backtrace);
  1660. }
  1661. }
  1662. return contextDependent;
  1663. }
  1664. }
  1665. std::vector<BT<std::string>> cmGeneratorTarget::GetSourceFilePaths(
  1666. std::string const& config) const
  1667. {
  1668. std::vector<BT<std::string>> files;
  1669. if (!this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  1670. // At configure-time, this method can be called as part of getting the
  1671. // LOCATION property or to export() a file to be include()d. However
  1672. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  1673. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  1674. // behavior of CMP0024 and CMP0026 only.
  1675. cmBTStringRange sourceEntries = this->Target->GetSourceEntries();
  1676. for (auto const& entry : sourceEntries) {
  1677. std::vector<std::string> items = cmExpandedList(entry.Value);
  1678. for (std::string const& item : items) {
  1679. if (cmHasLiteralPrefix(item, "$<TARGET_OBJECTS:") &&
  1680. item.back() == '>') {
  1681. continue;
  1682. }
  1683. files.emplace_back(item);
  1684. }
  1685. }
  1686. return files;
  1687. }
  1688. std::vector<std::string> debugProperties;
  1689. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  1690. debugProperties);
  1691. bool debugSources =
  1692. !this->DebugSourcesDone && cm::contains(debugProperties, "SOURCES");
  1693. if (this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  1694. this->DebugSourcesDone = true;
  1695. }
  1696. cmGeneratorExpressionDAGChecker dagChecker(this, "SOURCES", nullptr,
  1697. nullptr);
  1698. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  1699. this, config, std::string(), &dagChecker, this->SourceEntries);
  1700. std::unordered_set<std::string> uniqueSrcs;
  1701. bool contextDependentDirectSources =
  1702. processSources(this, entries, files, uniqueSrcs, debugSources);
  1703. // Collect INTERFACE_SOURCES of all direct link-dependencies.
  1704. EvaluatedTargetPropertyEntries linkInterfaceSourcesEntries;
  1705. AddInterfaceEntries(this, config, "INTERFACE_SOURCES", std::string(),
  1706. &dagChecker, linkInterfaceSourcesEntries,
  1707. IncludeRuntimeInterface::No, LinkInterfaceFor::Usage);
  1708. std::vector<std::string>::size_type numFilesBefore = files.size();
  1709. bool contextDependentInterfaceSources = processSources(
  1710. this, linkInterfaceSourcesEntries, files, uniqueSrcs, debugSources);
  1711. // Collect TARGET_OBJECTS of direct object link-dependencies.
  1712. bool contextDependentObjects = false;
  1713. std::vector<std::string>::size_type numFilesBefore2 = files.size();
  1714. if (this->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1715. EvaluatedTargetPropertyEntries linkObjectsEntries;
  1716. AddObjectEntries(this, config, &dagChecker, linkObjectsEntries);
  1717. contextDependentObjects = processSources(this, linkObjectsEntries, files,
  1718. uniqueSrcs, debugSources);
  1719. }
  1720. // Collect this target's file sets.
  1721. std::vector<std::string>::size_type numFilesBefore3 = files.size();
  1722. EvaluatedTargetPropertyEntries fileSetEntries;
  1723. AddFileSetEntries(this, config, &dagChecker, fileSetEntries);
  1724. bool contextDependentFileSets =
  1725. processSources(this, fileSetEntries, files, uniqueSrcs, debugSources);
  1726. // Determine if sources are context-dependent or not.
  1727. if (!contextDependentDirectSources &&
  1728. !(contextDependentInterfaceSources && numFilesBefore < files.size()) &&
  1729. !(contextDependentObjects && numFilesBefore2 < files.size()) &&
  1730. !(contextDependentFileSets && numFilesBefore3 < files.size())) {
  1731. this->SourcesAreContextDependent = Tribool::False;
  1732. } else {
  1733. this->SourcesAreContextDependent = Tribool::True;
  1734. }
  1735. return files;
  1736. }
  1737. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*>& files,
  1738. const std::string& config) const
  1739. {
  1740. std::vector<BT<cmSourceFile*>> tmp = this->GetSourceFiles(config);
  1741. files.reserve(tmp.size());
  1742. for (BT<cmSourceFile*>& v : tmp) {
  1743. files.push_back(v.Value);
  1744. }
  1745. }
  1746. std::vector<BT<cmSourceFile*>> cmGeneratorTarget::GetSourceFiles(
  1747. std::string const& config) const
  1748. {
  1749. std::vector<BT<cmSourceFile*>> files;
  1750. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  1751. // Since we are still configuring not all sources may exist yet,
  1752. // so we need to avoid full source classification because that
  1753. // requires the absolute paths to all sources to be determined.
  1754. // Since this is only for compatibility with old policies that
  1755. // projects should not depend on anymore, just compute the files
  1756. // without memoizing them.
  1757. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1758. std::set<cmSourceFile*> emitted;
  1759. for (BT<std::string> const& s : srcs) {
  1760. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1761. if (emitted.insert(sf).second) {
  1762. files.emplace_back(sf, s.Backtrace);
  1763. }
  1764. }
  1765. return files;
  1766. }
  1767. KindedSources const& kinded = this->GetKindedSources(config);
  1768. files.reserve(kinded.Sources.size());
  1769. for (SourceAndKind const& si : kinded.Sources) {
  1770. files.push_back(si.Source);
  1771. }
  1772. return files;
  1773. }
  1774. void cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1775. std::vector<cmSourceFile*>& files, const std::string& config) const
  1776. {
  1777. std::vector<BT<cmSourceFile*>> tmp =
  1778. this->GetSourceFilesWithoutObjectLibraries(config);
  1779. files.reserve(tmp.size());
  1780. for (BT<cmSourceFile*>& v : tmp) {
  1781. files.push_back(v.Value);
  1782. }
  1783. }
  1784. std::vector<BT<cmSourceFile*>>
  1785. cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1786. std::string const& config) const
  1787. {
  1788. std::vector<BT<cmSourceFile*>> files;
  1789. KindedSources const& kinded = this->GetKindedSources(config);
  1790. files.reserve(kinded.Sources.size());
  1791. for (SourceAndKind const& si : kinded.Sources) {
  1792. if (si.Source.Value->GetObjectLibrary().empty()) {
  1793. files.push_back(si.Source);
  1794. }
  1795. }
  1796. return files;
  1797. }
  1798. cmGeneratorTarget::KindedSources const& cmGeneratorTarget::GetKindedSources(
  1799. std::string const& config) const
  1800. {
  1801. // If we already processed one configuration and found no dependency
  1802. // on configuration then always use the one result.
  1803. if (this->SourcesAreContextDependent == Tribool::False) {
  1804. return this->KindedSourcesMap.begin()->second;
  1805. }
  1806. // Lookup any existing link implementation for this configuration.
  1807. std::string const key = cmSystemTools::UpperCase(config);
  1808. auto it = this->KindedSourcesMap.find(key);
  1809. if (it != this->KindedSourcesMap.end()) {
  1810. if (!it->second.Initialized) {
  1811. std::ostringstream e;
  1812. e << "The SOURCES of \"" << this->GetName()
  1813. << "\" use a generator expression that depends on the "
  1814. "SOURCES themselves.";
  1815. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1816. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1817. static KindedSources empty;
  1818. return empty;
  1819. }
  1820. return it->second;
  1821. }
  1822. // Add an entry to the map for this configuration.
  1823. KindedSources& files = this->KindedSourcesMap[key];
  1824. this->ComputeKindedSources(files, config);
  1825. files.Initialized = true;
  1826. return files;
  1827. }
  1828. void cmGeneratorTarget::ComputeKindedSources(KindedSources& files,
  1829. std::string const& config) const
  1830. {
  1831. // Get the source file paths by string.
  1832. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1833. cmsys::RegularExpression header_regex(CM_HEADER_REGEX);
  1834. std::vector<cmSourceFile*> badObjLib;
  1835. std::set<cmSourceFile*> emitted;
  1836. for (BT<std::string> const& s : srcs) {
  1837. // Create each source at most once.
  1838. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1839. if (!emitted.insert(sf).second) {
  1840. continue;
  1841. }
  1842. // Compute the kind (classification) of this source file.
  1843. SourceKind kind;
  1844. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  1845. if (sf->GetCustomCommand()) {
  1846. kind = SourceKindCustomCommand;
  1847. } else if (this->Target->GetType() == cmStateEnums::UTILITY ||
  1848. this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY
  1849. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  1850. // NOLINTNEXTLINE(bugprone-branch-clone)
  1851. ) {
  1852. kind = SourceKindExtra;
  1853. } else if (this->IsSourceFilePartOfUnityBatch(sf->ResolveFullPath())) {
  1854. kind = SourceKindUnityBatched;
  1855. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  1856. // NOLINTNEXTLINE(bugprone-branch-clone)
  1857. } else if (sf->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  1858. kind = SourceKindHeader;
  1859. } else if (sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1860. kind = SourceKindExternalObject;
  1861. } else if (!sf->GetOrDetermineLanguage().empty()) {
  1862. kind = SourceKindObjectSource;
  1863. } else if (ext == "def") {
  1864. kind = SourceKindModuleDefinition;
  1865. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1866. badObjLib.push_back(sf);
  1867. }
  1868. } else if (ext == "idl") {
  1869. kind = SourceKindIDL;
  1870. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1871. badObjLib.push_back(sf);
  1872. }
  1873. } else if (ext == "resx") {
  1874. kind = SourceKindResx;
  1875. } else if (ext == "appxmanifest") {
  1876. kind = SourceKindAppManifest;
  1877. } else if (ext == "manifest") {
  1878. if (sf->GetPropertyAsBool("VS_DEPLOYMENT_CONTENT")) {
  1879. kind = SourceKindExtra;
  1880. } else {
  1881. kind = SourceKindManifest;
  1882. }
  1883. } else if (ext == "pfx") {
  1884. kind = SourceKindCertificate;
  1885. } else if (ext == "xaml") {
  1886. kind = SourceKindXaml;
  1887. } else if (header_regex.find(sf->ResolveFullPath())) {
  1888. kind = SourceKindHeader;
  1889. } else {
  1890. kind = SourceKindExtra;
  1891. }
  1892. // Save this classified source file in the result vector.
  1893. files.Sources.push_back({ BT<cmSourceFile*>(sf, s.Backtrace), kind });
  1894. }
  1895. if (!badObjLib.empty()) {
  1896. std::ostringstream e;
  1897. e << "OBJECT library \"" << this->GetName() << "\" contains:\n";
  1898. for (cmSourceFile* i : badObjLib) {
  1899. e << " " << i->GetLocation().GetName() << "\n";
  1900. }
  1901. e << "but may contain only sources that compile, header files, and "
  1902. "other files that would not affect linking of a normal library.";
  1903. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1904. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1905. }
  1906. }
  1907. std::vector<cmGeneratorTarget::AllConfigSource> const&
  1908. cmGeneratorTarget::GetAllConfigSources() const
  1909. {
  1910. if (this->AllConfigSources.empty()) {
  1911. this->ComputeAllConfigSources();
  1912. }
  1913. return this->AllConfigSources;
  1914. }
  1915. void cmGeneratorTarget::ComputeAllConfigSources() const
  1916. {
  1917. std::vector<std::string> configs =
  1918. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  1919. std::map<cmSourceFile const*, size_t> index;
  1920. for (size_t ci = 0; ci < configs.size(); ++ci) {
  1921. KindedSources const& sources = this->GetKindedSources(configs[ci]);
  1922. for (SourceAndKind const& src : sources.Sources) {
  1923. auto mi = index.find(src.Source.Value);
  1924. if (mi == index.end()) {
  1925. AllConfigSource acs;
  1926. acs.Source = src.Source.Value;
  1927. acs.Kind = src.Kind;
  1928. this->AllConfigSources.push_back(std::move(acs));
  1929. std::map<cmSourceFile const*, size_t>::value_type entry(
  1930. src.Source.Value, this->AllConfigSources.size() - 1);
  1931. mi = index.insert(entry).first;
  1932. }
  1933. this->AllConfigSources[mi->second].Configs.push_back(ci);
  1934. }
  1935. }
  1936. }
  1937. std::vector<cmGeneratorTarget::AllConfigSource>
  1938. cmGeneratorTarget::GetAllConfigSources(SourceKind kind) const
  1939. {
  1940. std::vector<AllConfigSource> result;
  1941. for (AllConfigSource const& source : this->GetAllConfigSources()) {
  1942. if (source.Kind == kind) {
  1943. result.push_back(source);
  1944. }
  1945. }
  1946. return result;
  1947. }
  1948. std::set<std::string> cmGeneratorTarget::GetAllConfigCompileLanguages() const
  1949. {
  1950. std::set<std::string> languages;
  1951. std::vector<AllConfigSource> const& sources = this->GetAllConfigSources();
  1952. for (AllConfigSource const& si : sources) {
  1953. std::string const& lang = si.Source->GetOrDetermineLanguage();
  1954. if (!lang.empty()) {
  1955. languages.emplace(lang);
  1956. }
  1957. }
  1958. return languages;
  1959. }
  1960. std::string cmGeneratorTarget::GetCompilePDBName(
  1961. const std::string& config) const
  1962. {
  1963. // Check for a per-configuration output directory target property.
  1964. std::string configUpper = cmSystemTools::UpperCase(config);
  1965. std::string configProp = cmStrCat("COMPILE_PDB_NAME_", configUpper);
  1966. cmValue config_name = this->GetProperty(configProp);
  1967. if (cmNonempty(config_name)) {
  1968. NameComponents const& components = GetFullNameInternalComponents(
  1969. config, cmStateEnums::RuntimeBinaryArtifact);
  1970. return components.prefix + *config_name + ".pdb";
  1971. }
  1972. cmValue name = this->GetProperty("COMPILE_PDB_NAME");
  1973. if (cmNonempty(name)) {
  1974. NameComponents const& components = GetFullNameInternalComponents(
  1975. config, cmStateEnums::RuntimeBinaryArtifact);
  1976. return components.prefix + *name + ".pdb";
  1977. }
  1978. return "";
  1979. }
  1980. std::string cmGeneratorTarget::GetCompilePDBPath(
  1981. const std::string& config) const
  1982. {
  1983. std::string dir = this->GetCompilePDBDirectory(config);
  1984. std::string name = this->GetCompilePDBName(config);
  1985. if (dir.empty() && !name.empty() && this->HaveWellDefinedOutputFiles()) {
  1986. dir = this->GetPDBDirectory(config);
  1987. }
  1988. if (!dir.empty()) {
  1989. dir += "/";
  1990. }
  1991. return dir + name;
  1992. }
  1993. bool cmGeneratorTarget::HasSOName(const std::string& config) const
  1994. {
  1995. // soname is supported only for shared libraries and modules,
  1996. // and then only when the platform supports an soname flag.
  1997. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY) &&
  1998. !this->GetPropertyAsBool("NO_SONAME") &&
  1999. this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
  2000. }
  2001. bool cmGeneratorTarget::NeedRelinkBeforeInstall(
  2002. const std::string& config) const
  2003. {
  2004. // Only executables and shared libraries can have an rpath and may
  2005. // need relinking.
  2006. if (this->GetType() != cmStateEnums::EXECUTABLE &&
  2007. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2008. this->GetType() != cmStateEnums::MODULE_LIBRARY) {
  2009. return false;
  2010. }
  2011. // If there is no install location this target will not be installed
  2012. // and therefore does not need relinking.
  2013. if (!this->Target->GetHaveInstallRule()) {
  2014. return false;
  2015. }
  2016. // If skipping all rpaths completely then no relinking is needed.
  2017. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  2018. return false;
  2019. }
  2020. // If building with the install-tree rpath no relinking is needed.
  2021. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  2022. return false;
  2023. }
  2024. // If chrpath is going to be used no relinking is needed.
  2025. if (this->IsChrpathUsed(config)) {
  2026. return false;
  2027. }
  2028. // Check for rpath support on this platform.
  2029. std::string ll = this->GetLinkerLanguage(config);
  2030. if (!ll.empty()) {
  2031. std::string flagVar =
  2032. cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG");
  2033. if (!this->Makefile->IsSet(flagVar)) {
  2034. // There is no rpath support on this platform so nothing needs
  2035. // relinking.
  2036. return false;
  2037. }
  2038. } else {
  2039. // No linker language is known. This error will be reported by
  2040. // other code.
  2041. return false;
  2042. }
  2043. // If either a build or install tree rpath is set then the rpath
  2044. // will likely change between the build tree and install tree and
  2045. // this target must be relinked.
  2046. bool have_rpath =
  2047. this->HaveBuildTreeRPATH(config) || this->HaveInstallTreeRPATH(config);
  2048. bool is_ninja = this->LocalGenerator->GetGlobalGenerator()->IsNinja();
  2049. if (have_rpath && is_ninja) {
  2050. std::ostringstream w;
  2051. /* clang-format off */
  2052. w <<
  2053. "The install of the " << this->GetName() << " target requires changing "
  2054. "an RPATH from the build tree, but this is not supported with the Ninja "
  2055. "generator unless on an ELF-based or XCOFF-based platform. "
  2056. "The CMAKE_BUILD_WITH_INSTALL_RPATH variable may be set to avoid this "
  2057. "relinking step."
  2058. ;
  2059. /* clang-format on */
  2060. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  2061. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  2062. }
  2063. return have_rpath;
  2064. }
  2065. bool cmGeneratorTarget::IsChrpathUsed(const std::string& config) const
  2066. {
  2067. // Only certain target types have an rpath.
  2068. if (!(this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2069. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2070. this->GetType() == cmStateEnums::EXECUTABLE)) {
  2071. return false;
  2072. }
  2073. // If the target will not be installed we do not need to change its
  2074. // rpath.
  2075. if (!this->Target->GetHaveInstallRule()) {
  2076. return false;
  2077. }
  2078. // Skip chrpath if skipping rpath altogether.
  2079. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  2080. return false;
  2081. }
  2082. // Skip chrpath if it does not need to be changed at install time.
  2083. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  2084. return false;
  2085. }
  2086. // Allow the user to disable builtin chrpath explicitly.
  2087. if (this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH")) {
  2088. return false;
  2089. }
  2090. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  2091. return true;
  2092. }
  2093. // Enable if the rpath flag uses a separator and the target uses
  2094. // binaries we know how to edit.
  2095. std::string ll = this->GetLinkerLanguage(config);
  2096. if (!ll.empty()) {
  2097. std::string sepVar =
  2098. cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG_SEP");
  2099. cmValue sep = this->Makefile->GetDefinition(sepVar);
  2100. if (cmNonempty(sep)) {
  2101. // TODO: Add binary format check to ABI detection and get rid of
  2102. // CMAKE_EXECUTABLE_FORMAT.
  2103. if (cmValue fmt =
  2104. this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT")) {
  2105. if (*fmt == "ELF") {
  2106. return true;
  2107. }
  2108. #if defined(CMake_USE_XCOFF_PARSER)
  2109. if (*fmt == "XCOFF") {
  2110. return true;
  2111. }
  2112. #endif
  2113. }
  2114. }
  2115. }
  2116. return false;
  2117. }
  2118. bool cmGeneratorTarget::IsImportedSharedLibWithoutSOName(
  2119. const std::string& config) const
  2120. {
  2121. if (this->IsImported() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2122. if (cmGeneratorTarget::ImportInfo const* info =
  2123. this->GetImportInfo(config)) {
  2124. return info->NoSOName;
  2125. }
  2126. }
  2127. return false;
  2128. }
  2129. bool cmGeneratorTarget::HasMacOSXRpathInstallNameDir(
  2130. const std::string& config) const
  2131. {
  2132. TargetPtrToBoolMap& cache = this->MacOSXRpathInstallNameDirCache[config];
  2133. const auto lookup = cache.find(this->Target);
  2134. if (lookup != cache.cend()) {
  2135. return lookup->second;
  2136. }
  2137. const bool result = this->DetermineHasMacOSXRpathInstallNameDir(config);
  2138. cache[this->Target] = result;
  2139. return result;
  2140. }
  2141. bool cmGeneratorTarget::DetermineHasMacOSXRpathInstallNameDir(
  2142. const std::string& config) const
  2143. {
  2144. bool install_name_is_rpath = false;
  2145. bool macosx_rpath = false;
  2146. if (!this->IsImported()) {
  2147. if (this->GetType() != cmStateEnums::SHARED_LIBRARY) {
  2148. return false;
  2149. }
  2150. cmValue install_name = this->GetProperty("INSTALL_NAME_DIR");
  2151. bool use_install_name = this->MacOSXUseInstallNameDir();
  2152. if (install_name && use_install_name && *install_name == "@rpath") {
  2153. install_name_is_rpath = true;
  2154. } else if (install_name && use_install_name) {
  2155. return false;
  2156. }
  2157. if (!install_name_is_rpath) {
  2158. macosx_rpath = this->MacOSXRpathInstallNameDirDefault();
  2159. }
  2160. } else {
  2161. // Lookup the imported soname.
  2162. if (cmGeneratorTarget::ImportInfo const* info =
  2163. this->GetImportInfo(config)) {
  2164. if (!info->NoSOName && !info->SOName.empty()) {
  2165. if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
  2166. install_name_is_rpath = true;
  2167. }
  2168. } else {
  2169. std::string install_name;
  2170. cmSystemTools::GuessLibraryInstallName(info->Location, install_name);
  2171. if (install_name.find("@rpath") != std::string::npos) {
  2172. install_name_is_rpath = true;
  2173. }
  2174. }
  2175. }
  2176. }
  2177. if (!install_name_is_rpath && !macosx_rpath) {
  2178. return false;
  2179. }
  2180. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  2181. std::ostringstream w;
  2182. w << "Attempting to use ";
  2183. if (macosx_rpath) {
  2184. w << "MACOSX_RPATH";
  2185. } else {
  2186. w << "@rpath";
  2187. }
  2188. w << " without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being set.";
  2189. w << " This could be because you are using a Mac OS X version";
  2190. w << " less than 10.5 or because CMake's platform configuration is";
  2191. w << " corrupt.";
  2192. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  2193. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  2194. }
  2195. return true;
  2196. }
  2197. bool cmGeneratorTarget::MacOSXRpathInstallNameDirDefault() const
  2198. {
  2199. // we can't do rpaths when unsupported
  2200. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  2201. return false;
  2202. }
  2203. cmValue macosx_rpath_str = this->GetProperty("MACOSX_RPATH");
  2204. if (macosx_rpath_str) {
  2205. return this->GetPropertyAsBool("MACOSX_RPATH");
  2206. }
  2207. cmPolicies::PolicyStatus cmp0042 = this->GetPolicyStatusCMP0042();
  2208. if (cmp0042 == cmPolicies::WARN) {
  2209. this->LocalGenerator->GetGlobalGenerator()->AddCMP0042WarnTarget(
  2210. this->GetName());
  2211. }
  2212. return cmp0042 == cmPolicies::NEW;
  2213. }
  2214. bool cmGeneratorTarget::MacOSXUseInstallNameDir() const
  2215. {
  2216. cmValue build_with_install_name =
  2217. this->GetProperty("BUILD_WITH_INSTALL_NAME_DIR");
  2218. if (build_with_install_name) {
  2219. return cmIsOn(*build_with_install_name);
  2220. }
  2221. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  2222. if (cmp0068 == cmPolicies::NEW) {
  2223. return false;
  2224. }
  2225. bool use_install_name = this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  2226. if (use_install_name && cmp0068 == cmPolicies::WARN) {
  2227. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  2228. this->GetName());
  2229. }
  2230. return use_install_name;
  2231. }
  2232. bool cmGeneratorTarget::CanGenerateInstallNameDir(
  2233. InstallNameType name_type) const
  2234. {
  2235. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  2236. if (cmp0068 == cmPolicies::NEW) {
  2237. return true;
  2238. }
  2239. bool skip = this->Makefile->IsOn("CMAKE_SKIP_RPATH");
  2240. if (name_type == INSTALL_NAME_FOR_INSTALL) {
  2241. skip |= this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  2242. } else {
  2243. skip |= this->GetPropertyAsBool("SKIP_BUILD_RPATH");
  2244. }
  2245. if (skip && cmp0068 == cmPolicies::WARN) {
  2246. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  2247. this->GetName());
  2248. }
  2249. return !skip;
  2250. }
  2251. std::string cmGeneratorTarget::GetSOName(const std::string& config) const
  2252. {
  2253. if (this->IsImported()) {
  2254. // Lookup the imported soname.
  2255. if (cmGeneratorTarget::ImportInfo const* info =
  2256. this->GetImportInfo(config)) {
  2257. if (info->NoSOName) {
  2258. // The imported library has no builtin soname so the name
  2259. // searched at runtime will be just the filename.
  2260. return cmSystemTools::GetFilenameName(info->Location);
  2261. }
  2262. // Use the soname given if any.
  2263. if (this->IsFrameworkOnApple()) {
  2264. cmsys::RegularExpressionMatch match;
  2265. if (FrameworkRegularExpression.find(info->SOName.c_str(), match)) {
  2266. auto frameworkName = match.match(2);
  2267. auto fileName = match.match(3);
  2268. return cmStrCat(frameworkName, ".framework/", fileName);
  2269. }
  2270. }
  2271. if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
  2272. return info->SOName.substr(cmStrLen("@rpath/"));
  2273. }
  2274. return info->SOName;
  2275. }
  2276. return "";
  2277. }
  2278. // Compute the soname that will be built.
  2279. return this->GetLibraryNames(config).SharedObject;
  2280. }
  2281. namespace {
  2282. bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  2283. {
  2284. return level == cmGeneratorTarget::FullLevel;
  2285. }
  2286. bool shouldAddContentLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  2287. {
  2288. return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level);
  2289. }
  2290. }
  2291. std::string cmGeneratorTarget::GetAppBundleDirectory(
  2292. const std::string& config, BundleDirectoryLevel level) const
  2293. {
  2294. std::string fpath = cmStrCat(
  2295. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  2296. cmValue ext = this->GetProperty("BUNDLE_EXTENSION");
  2297. fpath += (ext ? *ext : "app");
  2298. if (shouldAddContentLevel(level) &&
  2299. !this->Makefile->PlatformIsAppleEmbedded()) {
  2300. fpath += "/Contents";
  2301. if (shouldAddFullLevel(level)) {
  2302. fpath += "/MacOS";
  2303. }
  2304. }
  2305. return fpath;
  2306. }
  2307. bool cmGeneratorTarget::IsBundleOnApple() const
  2308. {
  2309. return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
  2310. this->IsCFBundleOnApple();
  2311. }
  2312. bool cmGeneratorTarget::IsWin32Executable(const std::string& config) const
  2313. {
  2314. return cmIsOn(cmGeneratorExpression::Evaluate(
  2315. this->GetSafeProperty("WIN32_EXECUTABLE"), this->LocalGenerator, config));
  2316. }
  2317. std::string cmGeneratorTarget::GetCFBundleDirectory(
  2318. const std::string& config, BundleDirectoryLevel level) const
  2319. {
  2320. std::string fpath = cmStrCat(
  2321. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  2322. std::string ext;
  2323. if (cmValue p = this->GetProperty("BUNDLE_EXTENSION")) {
  2324. ext = *p;
  2325. } else {
  2326. if (this->IsXCTestOnApple()) {
  2327. ext = "xctest";
  2328. } else {
  2329. ext = "bundle";
  2330. }
  2331. }
  2332. fpath += ext;
  2333. if (shouldAddContentLevel(level) &&
  2334. !this->Makefile->PlatformIsAppleEmbedded()) {
  2335. fpath += "/Contents";
  2336. if (shouldAddFullLevel(level)) {
  2337. fpath += "/MacOS";
  2338. }
  2339. }
  2340. return fpath;
  2341. }
  2342. std::string cmGeneratorTarget::GetFrameworkDirectory(
  2343. const std::string& config, BundleDirectoryLevel level) const
  2344. {
  2345. std::string fpath = cmStrCat(
  2346. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  2347. cmValue ext = this->GetProperty("BUNDLE_EXTENSION");
  2348. fpath += (ext ? *ext : "framework");
  2349. if (shouldAddFullLevel(level) &&
  2350. !this->Makefile->PlatformIsAppleEmbedded()) {
  2351. fpath += "/Versions/";
  2352. fpath += this->GetFrameworkVersion();
  2353. }
  2354. return fpath;
  2355. }
  2356. std::string cmGeneratorTarget::GetFullName(
  2357. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2358. {
  2359. if (this->IsImported()) {
  2360. return this->GetFullNameImported(config, artifact);
  2361. }
  2362. return this->GetFullNameInternal(config, artifact);
  2363. }
  2364. std::string cmGeneratorTarget::GetInstallNameDirForBuildTree(
  2365. const std::string& config) const
  2366. {
  2367. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  2368. // If building directly for installation then the build tree install_name
  2369. // is the same as the install tree.
  2370. if (this->MacOSXUseInstallNameDir()) {
  2371. std::string installPrefix =
  2372. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  2373. return this->GetInstallNameDirForInstallTree(config, installPrefix);
  2374. }
  2375. // Use the build tree directory for the target.
  2376. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_BUILD)) {
  2377. std::string dir;
  2378. if (this->MacOSXRpathInstallNameDirDefault()) {
  2379. dir = "@rpath";
  2380. } else {
  2381. dir = this->GetDirectory(config);
  2382. }
  2383. dir += "/";
  2384. return dir;
  2385. }
  2386. }
  2387. return "";
  2388. }
  2389. std::string cmGeneratorTarget::GetInstallNameDirForInstallTree(
  2390. const std::string& config, const std::string& installPrefix) const
  2391. {
  2392. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  2393. std::string dir;
  2394. cmValue install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  2395. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_INSTALL)) {
  2396. if (cmNonempty(install_name_dir)) {
  2397. dir = *install_name_dir;
  2398. cmGeneratorExpression::ReplaceInstallPrefix(dir, installPrefix);
  2399. dir =
  2400. cmGeneratorExpression::Evaluate(dir, this->LocalGenerator, config);
  2401. if (!dir.empty()) {
  2402. dir = cmStrCat(dir, '/');
  2403. }
  2404. }
  2405. }
  2406. if (!install_name_dir) {
  2407. if (this->MacOSXRpathInstallNameDirDefault()) {
  2408. dir = "@rpath/";
  2409. }
  2410. }
  2411. return dir;
  2412. }
  2413. return "";
  2414. }
  2415. cmListFileBacktrace cmGeneratorTarget::GetBacktrace() const
  2416. {
  2417. return this->Target->GetBacktrace();
  2418. }
  2419. const std::set<BT<std::pair<std::string, bool>>>&
  2420. cmGeneratorTarget::GetUtilities() const
  2421. {
  2422. return this->Target->GetUtilities();
  2423. }
  2424. bool cmGeneratorTarget::HaveWellDefinedOutputFiles() const
  2425. {
  2426. return this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2427. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2428. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2429. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2430. this->GetType() == cmStateEnums::EXECUTABLE;
  2431. }
  2432. const std::string* cmGeneratorTarget::GetExportMacro() const
  2433. {
  2434. // Define the symbol for targets that export symbols.
  2435. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2436. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2437. this->IsExecutableWithExports()) {
  2438. if (cmValue custom_export_name = this->GetProperty("DEFINE_SYMBOL")) {
  2439. this->ExportMacro = *custom_export_name;
  2440. } else {
  2441. std::string in = cmStrCat(this->GetName(), "_EXPORTS");
  2442. this->ExportMacro = cmSystemTools::MakeCidentifier(in);
  2443. }
  2444. return &this->ExportMacro;
  2445. }
  2446. return nullptr;
  2447. }
  2448. class cmTargetCollectLinkLanguages
  2449. {
  2450. public:
  2451. cmTargetCollectLinkLanguages(cmGeneratorTarget const* target,
  2452. std::string config,
  2453. std::unordered_set<std::string>& languages,
  2454. cmGeneratorTarget const* head, bool secondPass)
  2455. : Config(std::move(config))
  2456. , Languages(languages)
  2457. , HeadTarget(head)
  2458. , SecondPass(secondPass)
  2459. {
  2460. this->Visited.insert(target);
  2461. }
  2462. void Visit(cmLinkItem const& item)
  2463. {
  2464. if (!item.Target) {
  2465. return;
  2466. }
  2467. if (!this->Visited.insert(item.Target).second) {
  2468. return;
  2469. }
  2470. cmLinkInterface const* iface = item.Target->GetLinkInterface(
  2471. this->Config, this->HeadTarget, this->SecondPass);
  2472. if (!iface) {
  2473. return;
  2474. }
  2475. if (iface->HadLinkLanguageSensitiveCondition) {
  2476. this->HadLinkLanguageSensitiveCondition = true;
  2477. }
  2478. for (std::string const& language : iface->Languages) {
  2479. this->Languages.insert(language);
  2480. }
  2481. for (cmLinkItem const& lib : iface->Libraries) {
  2482. this->Visit(lib);
  2483. }
  2484. }
  2485. bool GetHadLinkLanguageSensitiveCondition() const
  2486. {
  2487. return this->HadLinkLanguageSensitiveCondition;
  2488. }
  2489. private:
  2490. std::string Config;
  2491. std::unordered_set<std::string>& Languages;
  2492. cmGeneratorTarget const* HeadTarget;
  2493. std::set<cmGeneratorTarget const*> Visited;
  2494. bool SecondPass;
  2495. bool HadLinkLanguageSensitiveCondition = false;
  2496. };
  2497. cmGeneratorTarget::LinkClosure const* cmGeneratorTarget::GetLinkClosure(
  2498. const std::string& config) const
  2499. {
  2500. // There is no link implementation for targets that cannot compile sources.
  2501. if (!this->CanCompileSources()) {
  2502. static LinkClosure const empty = { {}, {} };
  2503. return &empty;
  2504. }
  2505. std::string key(cmSystemTools::UpperCase(config));
  2506. auto i = this->LinkClosureMap.find(key);
  2507. if (i == this->LinkClosureMap.end()) {
  2508. LinkClosure lc;
  2509. this->ComputeLinkClosure(config, lc);
  2510. LinkClosureMapType::value_type entry(key, lc);
  2511. i = this->LinkClosureMap.insert(entry).first;
  2512. }
  2513. return &i->second;
  2514. }
  2515. class cmTargetSelectLinker
  2516. {
  2517. int Preference = 0;
  2518. cmGeneratorTarget const* Target;
  2519. cmGlobalGenerator* GG;
  2520. std::set<std::string> Preferred;
  2521. public:
  2522. cmTargetSelectLinker(cmGeneratorTarget const* target)
  2523. : Target(target)
  2524. {
  2525. this->GG = this->Target->GetLocalGenerator()->GetGlobalGenerator();
  2526. }
  2527. void Consider(const std::string& lang)
  2528. {
  2529. int preference = this->GG->GetLinkerPreference(lang);
  2530. if (preference > this->Preference) {
  2531. this->Preference = preference;
  2532. this->Preferred.clear();
  2533. }
  2534. if (preference == this->Preference) {
  2535. this->Preferred.insert(lang);
  2536. }
  2537. }
  2538. std::string Choose()
  2539. {
  2540. if (this->Preferred.empty()) {
  2541. return "";
  2542. }
  2543. if (this->Preferred.size() > 1) {
  2544. std::ostringstream e;
  2545. e << "Target " << this->Target->GetName()
  2546. << " contains multiple languages with the highest linker preference"
  2547. << " (" << this->Preference << "):\n";
  2548. for (std::string const& li : this->Preferred) {
  2549. e << " " << li << "\n";
  2550. }
  2551. e << "Set the LINKER_LANGUAGE property for this target.";
  2552. cmake* cm = this->Target->GetLocalGenerator()->GetCMakeInstance();
  2553. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  2554. this->Target->GetBacktrace());
  2555. }
  2556. return *this->Preferred.begin();
  2557. }
  2558. };
  2559. bool cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  2560. LinkClosure& lc,
  2561. bool secondPass) const
  2562. {
  2563. // Get languages built in this target.
  2564. std::unordered_set<std::string> languages;
  2565. cmLinkImplementation const* impl =
  2566. this->GetLinkImplementation(config, LinkInterfaceFor::Link, secondPass);
  2567. assert(impl);
  2568. languages.insert(impl->Languages.cbegin(), impl->Languages.cend());
  2569. // Add interface languages from linked targets.
  2570. // cmTargetCollectLinkLanguages cll(this, config, languages, this,
  2571. // secondPass);
  2572. cmTargetCollectLinkLanguages cll(this, config, languages, this, secondPass);
  2573. for (cmLinkImplItem const& lib : impl->Libraries) {
  2574. cll.Visit(lib);
  2575. }
  2576. // Store the transitive closure of languages.
  2577. cm::append(lc.Languages, languages);
  2578. // Choose the language whose linker should be used.
  2579. if (secondPass || lc.LinkerLanguage.empty()) {
  2580. // Find the language with the highest preference value.
  2581. cmTargetSelectLinker tsl(this);
  2582. // First select from the languages compiled directly in this target.
  2583. for (std::string const& l : impl->Languages) {
  2584. tsl.Consider(l);
  2585. }
  2586. // Now consider languages that propagate from linked targets.
  2587. for (std::string const& lang : languages) {
  2588. std::string propagates =
  2589. "CMAKE_" + lang + "_LINKER_PREFERENCE_PROPAGATES";
  2590. if (this->Makefile->IsOn(propagates)) {
  2591. tsl.Consider(lang);
  2592. }
  2593. }
  2594. lc.LinkerLanguage = tsl.Choose();
  2595. }
  2596. return impl->HadLinkLanguageSensitiveCondition ||
  2597. cll.GetHadLinkLanguageSensitiveCondition();
  2598. }
  2599. void cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  2600. LinkClosure& lc) const
  2601. {
  2602. bool secondPass = false;
  2603. {
  2604. LinkClosure linkClosure;
  2605. linkClosure.LinkerLanguage = this->LinkerLanguage;
  2606. bool hasHardCodedLinkerLanguage = this->Target->GetProperty("HAS_CXX") ||
  2607. !this->Target->GetSafeProperty("LINKER_LANGUAGE").empty();
  2608. // Get languages built in this target.
  2609. secondPass = this->ComputeLinkClosure(config, linkClosure, false) &&
  2610. !hasHardCodedLinkerLanguage;
  2611. this->LinkerLanguage = linkClosure.LinkerLanguage;
  2612. if (!secondPass) {
  2613. lc = std::move(linkClosure);
  2614. }
  2615. }
  2616. if (secondPass) {
  2617. LinkClosure linkClosure;
  2618. this->ComputeLinkClosure(config, linkClosure, secondPass);
  2619. lc = std::move(linkClosure);
  2620. // linker language must not be changed between the two passes
  2621. if (this->LinkerLanguage != lc.LinkerLanguage) {
  2622. std::ostringstream e;
  2623. e << "Evaluation of $<LINK_LANGUAGE:...> or $<LINK_LAND_AND_ID:...> "
  2624. "changes\nthe linker language for target \""
  2625. << this->GetName() << "\" (from '" << this->LinkerLanguage << "' to '"
  2626. << lc.LinkerLanguage << "') which is invalid.";
  2627. cmSystemTools::Error(e.str());
  2628. }
  2629. }
  2630. }
  2631. cmGeneratorTarget::NameComponents const&
  2632. cmGeneratorTarget::GetFullNameComponents(
  2633. std::string const& config, cmStateEnums::ArtifactType artifact) const
  2634. {
  2635. return this->GetFullNameInternalComponents(config, artifact);
  2636. }
  2637. std::string cmGeneratorTarget::BuildBundleDirectory(
  2638. const std::string& base, const std::string& config,
  2639. BundleDirectoryLevel level) const
  2640. {
  2641. std::string fpath = base;
  2642. if (this->IsAppBundleOnApple()) {
  2643. fpath += this->GetAppBundleDirectory(config, level);
  2644. }
  2645. if (this->IsFrameworkOnApple()) {
  2646. fpath += this->GetFrameworkDirectory(config, level);
  2647. }
  2648. if (this->IsCFBundleOnApple()) {
  2649. fpath += this->GetCFBundleDirectory(config, level);
  2650. }
  2651. return fpath;
  2652. }
  2653. std::string cmGeneratorTarget::GetMacContentDirectory(
  2654. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2655. {
  2656. // Start with the output directory for the target.
  2657. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  2658. BundleDirectoryLevel level = ContentLevel;
  2659. if (this->IsFrameworkOnApple()) {
  2660. // additional files with a framework go into the version specific
  2661. // directory
  2662. level = FullLevel;
  2663. }
  2664. fpath = this->BuildBundleDirectory(fpath, config, level);
  2665. return fpath;
  2666. }
  2667. std::string cmGeneratorTarget::GetEffectiveFolderName() const
  2668. {
  2669. std::string effectiveFolder;
  2670. if (!this->GlobalGenerator->UseFolderProperty()) {
  2671. return effectiveFolder;
  2672. }
  2673. cmValue targetFolder = this->GetProperty("FOLDER");
  2674. if (targetFolder) {
  2675. effectiveFolder += *targetFolder;
  2676. }
  2677. return effectiveFolder;
  2678. }
  2679. cmGeneratorTarget::CompileInfo const* cmGeneratorTarget::GetCompileInfo(
  2680. const std::string& config) const
  2681. {
  2682. // There is no compile information for imported targets.
  2683. if (this->IsImported()) {
  2684. return nullptr;
  2685. }
  2686. if (this->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  2687. std::string msg = cmStrCat("cmTarget::GetCompileInfo called for ",
  2688. this->GetName(), " which has type ",
  2689. cmState::GetTargetTypeName(this->GetType()));
  2690. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  2691. return nullptr;
  2692. }
  2693. // Lookup/compute/cache the compile information for this configuration.
  2694. std::string config_upper;
  2695. if (!config.empty()) {
  2696. config_upper = cmSystemTools::UpperCase(config);
  2697. }
  2698. auto i = this->CompileInfoMap.find(config_upper);
  2699. if (i == this->CompileInfoMap.end()) {
  2700. CompileInfo info;
  2701. this->ComputePDBOutputDir("COMPILE_PDB", config, info.CompilePdbDir);
  2702. CompileInfoMapType::value_type entry(config_upper, info);
  2703. i = this->CompileInfoMap.insert(entry).first;
  2704. }
  2705. return &i->second;
  2706. }
  2707. cmGeneratorTarget::ModuleDefinitionInfo const*
  2708. cmGeneratorTarget::GetModuleDefinitionInfo(std::string const& config) const
  2709. {
  2710. // A module definition file only makes sense on certain target types.
  2711. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2712. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2713. !this->IsExecutableWithExports()) {
  2714. return nullptr;
  2715. }
  2716. // Lookup/compute/cache the compile information for this configuration.
  2717. std::string config_upper;
  2718. if (!config.empty()) {
  2719. config_upper = cmSystemTools::UpperCase(config);
  2720. }
  2721. auto i = this->ModuleDefinitionInfoMap.find(config_upper);
  2722. if (i == this->ModuleDefinitionInfoMap.end()) {
  2723. ModuleDefinitionInfo info;
  2724. this->ComputeModuleDefinitionInfo(config, info);
  2725. ModuleDefinitionInfoMapType::value_type entry(config_upper, info);
  2726. i = this->ModuleDefinitionInfoMap.insert(entry).first;
  2727. }
  2728. return &i->second;
  2729. }
  2730. void cmGeneratorTarget::ComputeModuleDefinitionInfo(
  2731. std::string const& config, ModuleDefinitionInfo& info) const
  2732. {
  2733. this->GetModuleDefinitionSources(info.Sources, config);
  2734. info.WindowsExportAllSymbols =
  2735. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
  2736. this->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS");
  2737. #if !defined(CMAKE_BOOTSTRAP)
  2738. info.DefFileGenerated =
  2739. info.WindowsExportAllSymbols || info.Sources.size() > 1;
  2740. #else
  2741. // Our __create_def helper is not available during CMake bootstrap.
  2742. info.DefFileGenerated = false;
  2743. #endif
  2744. if (info.DefFileGenerated) {
  2745. info.DefFile =
  2746. this->GetObjectDirectory(config) /* has slash */ + "exports.def";
  2747. } else if (!info.Sources.empty()) {
  2748. info.DefFile = info.Sources.front()->GetFullPath();
  2749. }
  2750. }
  2751. bool cmGeneratorTarget::IsDLLPlatform() const
  2752. {
  2753. return this->Target->IsDLLPlatform();
  2754. }
  2755. void cmGeneratorTarget::GetAutoUicOptions(std::vector<std::string>& result,
  2756. const std::string& config) const
  2757. {
  2758. const char* prop =
  2759. this->GetLinkInterfaceDependentStringProperty("AUTOUIC_OPTIONS", config);
  2760. if (!prop) {
  2761. return;
  2762. }
  2763. cmGeneratorExpressionDAGChecker dagChecker(this, "AUTOUIC_OPTIONS", nullptr,
  2764. nullptr);
  2765. cmExpandList(cmGeneratorExpression::Evaluate(prop, this->LocalGenerator,
  2766. config, this, &dagChecker),
  2767. result);
  2768. }
  2769. static void processILibs(const std::string& config,
  2770. cmGeneratorTarget const* headTarget,
  2771. cmLinkItem const& item, cmGlobalGenerator* gg,
  2772. std::vector<cmGeneratorTarget const*>& tgts,
  2773. std::set<cmGeneratorTarget const*>& emitted)
  2774. {
  2775. if (item.Target && emitted.insert(item.Target).second) {
  2776. tgts.push_back(item.Target);
  2777. if (cmLinkInterfaceLibraries const* iface =
  2778. item.Target->GetLinkInterfaceLibraries(config, headTarget,
  2779. LinkInterfaceFor::Usage)) {
  2780. for (cmLinkItem const& lib : iface->Libraries) {
  2781. processILibs(config, headTarget, lib, gg, tgts, emitted);
  2782. }
  2783. }
  2784. }
  2785. }
  2786. const std::vector<const cmGeneratorTarget*>&
  2787. cmGeneratorTarget::GetLinkImplementationClosure(
  2788. const std::string& config) const
  2789. {
  2790. // There is no link implementation for targets that cannot compile sources.
  2791. if (!this->CanCompileSources()) {
  2792. static std::vector<const cmGeneratorTarget*> const empty;
  2793. return empty;
  2794. }
  2795. LinkImplClosure& tgts = this->LinkImplClosureMap[config];
  2796. if (!tgts.Done) {
  2797. tgts.Done = true;
  2798. std::set<cmGeneratorTarget const*> emitted;
  2799. cmLinkImplementationLibraries const* impl =
  2800. this->GetLinkImplementationLibraries(config, LinkInterfaceFor::Usage);
  2801. assert(impl);
  2802. for (cmLinkImplItem const& lib : impl->Libraries) {
  2803. processILibs(config, this, lib,
  2804. this->LocalGenerator->GetGlobalGenerator(), tgts, emitted);
  2805. }
  2806. }
  2807. return tgts;
  2808. }
  2809. class cmTargetTraceDependencies
  2810. {
  2811. public:
  2812. cmTargetTraceDependencies(cmGeneratorTarget* target);
  2813. void Trace();
  2814. private:
  2815. cmGeneratorTarget* GeneratorTarget;
  2816. cmMakefile* Makefile;
  2817. cmLocalGenerator* LocalGenerator;
  2818. cmGlobalGenerator const* GlobalGenerator;
  2819. using SourceEntry = cmGeneratorTarget::SourceEntry;
  2820. SourceEntry* CurrentEntry;
  2821. std::queue<cmSourceFile*> SourceQueue;
  2822. std::set<cmSourceFile*> SourcesQueued;
  2823. using NameMapType = std::map<std::string, cmSourcesWithOutput>;
  2824. NameMapType NameMap;
  2825. std::vector<std::string> NewSources;
  2826. void QueueSource(cmSourceFile* sf);
  2827. void FollowName(std::string const& name);
  2828. void FollowNames(std::vector<std::string> const& names);
  2829. bool IsUtility(std::string const& dep);
  2830. void CheckCustomCommand(cmCustomCommand const& cc);
  2831. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  2832. };
  2833. cmTargetTraceDependencies::cmTargetTraceDependencies(cmGeneratorTarget* target)
  2834. : GeneratorTarget(target)
  2835. {
  2836. // Convenience.
  2837. this->Makefile = target->Target->GetMakefile();
  2838. this->LocalGenerator = target->GetLocalGenerator();
  2839. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  2840. this->CurrentEntry = nullptr;
  2841. // Queue all the source files already specified for the target.
  2842. std::set<cmSourceFile*> emitted;
  2843. std::vector<std::string> const& configs =
  2844. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  2845. for (std::string const& c : configs) {
  2846. std::vector<cmSourceFile*> sources;
  2847. this->GeneratorTarget->GetSourceFiles(sources, c);
  2848. for (cmSourceFile* sf : sources) {
  2849. const std::set<cmGeneratorTarget const*> tgts =
  2850. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  2851. if (cm::contains(tgts, this->GeneratorTarget)) {
  2852. std::ostringstream e;
  2853. e << "Evaluation output file\n \"" << sf->ResolveFullPath()
  2854. << "\"\ndepends on the sources of a target it is used in. This "
  2855. "is a dependency loop and is not allowed.";
  2856. this->GeneratorTarget->LocalGenerator->IssueMessage(
  2857. MessageType::FATAL_ERROR, e.str());
  2858. return;
  2859. }
  2860. if (emitted.insert(sf).second && this->SourcesQueued.insert(sf).second) {
  2861. this->SourceQueue.push(sf);
  2862. }
  2863. }
  2864. }
  2865. // Queue pre-build, pre-link, and post-build rule dependencies.
  2866. this->CheckCustomCommands(this->GeneratorTarget->GetPreBuildCommands());
  2867. this->CheckCustomCommands(this->GeneratorTarget->GetPreLinkCommands());
  2868. this->CheckCustomCommands(this->GeneratorTarget->GetPostBuildCommands());
  2869. }
  2870. void cmTargetTraceDependencies::Trace()
  2871. {
  2872. // Process one dependency at a time until the queue is empty.
  2873. while (!this->SourceQueue.empty()) {
  2874. // Get the next source from the queue.
  2875. cmSourceFile* sf = this->SourceQueue.front();
  2876. this->SourceQueue.pop();
  2877. this->CurrentEntry = &this->GeneratorTarget->SourceDepends[sf];
  2878. // Queue dependencies added explicitly by the user.
  2879. if (cmValue additionalDeps = sf->GetProperty("OBJECT_DEPENDS")) {
  2880. std::vector<std::string> objDeps = cmExpandedList(*additionalDeps);
  2881. for (std::string& objDep : objDeps) {
  2882. if (cmSystemTools::FileIsFullPath(objDep)) {
  2883. objDep = cmSystemTools::CollapseFullPath(objDep);
  2884. }
  2885. }
  2886. this->FollowNames(objDeps);
  2887. }
  2888. // Queue the source needed to generate this file, if any.
  2889. this->FollowName(sf->ResolveFullPath());
  2890. // Queue dependencies added programmatically by commands.
  2891. this->FollowNames(sf->GetDepends());
  2892. // Queue custom command dependencies.
  2893. if (cmCustomCommand const* cc = sf->GetCustomCommand()) {
  2894. this->CheckCustomCommand(*cc);
  2895. }
  2896. }
  2897. this->CurrentEntry = nullptr;
  2898. this->GeneratorTarget->AddTracedSources(this->NewSources);
  2899. }
  2900. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  2901. {
  2902. if (this->SourcesQueued.insert(sf).second) {
  2903. this->SourceQueue.push(sf);
  2904. // Make sure this file is in the target at the end.
  2905. this->NewSources.push_back(sf->ResolveFullPath());
  2906. }
  2907. }
  2908. void cmTargetTraceDependencies::FollowName(std::string const& name)
  2909. {
  2910. // Use lower bound with key comparison to not repeat the search for the
  2911. // insert position if the name could not be found (which is the common case).
  2912. auto i = this->NameMap.lower_bound(name);
  2913. if (i == this->NameMap.end() || i->first != name) {
  2914. // Check if we know how to generate this file.
  2915. cmSourcesWithOutput sources =
  2916. this->LocalGenerator->GetSourcesWithOutput(name);
  2917. // If we failed to find a target or source and we have a relative path, it
  2918. // might be a valid source if made relative to the current binary
  2919. // directory.
  2920. if (!sources.Target && !sources.Source &&
  2921. !cmSystemTools::FileIsFullPath(name)) {
  2922. auto fullname =
  2923. cmStrCat(this->Makefile->GetCurrentBinaryDirectory(), '/', name);
  2924. fullname = cmSystemTools::CollapseFullPath(
  2925. fullname, this->Makefile->GetHomeOutputDirectory());
  2926. sources = this->LocalGenerator->GetSourcesWithOutput(fullname);
  2927. }
  2928. i = this->NameMap.emplace_hint(i, name, sources);
  2929. }
  2930. if (cmTarget* t = i->second.Target) {
  2931. // The name is a byproduct of a utility target or a PRE_BUILD, PRE_LINK, or
  2932. // POST_BUILD command.
  2933. this->GeneratorTarget->Target->AddUtility(t->GetName(), false);
  2934. }
  2935. if (cmSourceFile* sf = i->second.Source) {
  2936. // For now only follow the dependency if the source file is not a
  2937. // byproduct. Semantics of byproducts in a non-Ninja context will have to
  2938. // be defined first.
  2939. if (!i->second.SourceIsByproduct) {
  2940. // Record the dependency we just followed.
  2941. if (this->CurrentEntry) {
  2942. this->CurrentEntry->Depends.push_back(sf);
  2943. }
  2944. this->QueueSource(sf);
  2945. }
  2946. }
  2947. }
  2948. void cmTargetTraceDependencies::FollowNames(
  2949. std::vector<std::string> const& names)
  2950. {
  2951. for (std::string const& name : names) {
  2952. this->FollowName(name);
  2953. }
  2954. }
  2955. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  2956. {
  2957. // Dependencies on targets (utilities) are supposed to be named by
  2958. // just the target name. However for compatibility we support
  2959. // naming the output file generated by the target (assuming there is
  2960. // no output-name property which old code would not have set). In
  2961. // that case the target name will be the file basename of the
  2962. // dependency.
  2963. std::string util = cmSystemTools::GetFilenameName(dep);
  2964. if (cmSystemTools::GetFilenameLastExtension(util) == ".exe") {
  2965. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  2966. }
  2967. // Check for a target with this name.
  2968. if (cmGeneratorTarget* t =
  2969. this->GeneratorTarget->GetLocalGenerator()->FindGeneratorTargetToUse(
  2970. util)) {
  2971. // If we find the target and the dep was given as a full path,
  2972. // then make sure it was not a full path to something else, and
  2973. // the fact that the name matched a target was just a coincidence.
  2974. if (cmSystemTools::FileIsFullPath(dep)) {
  2975. if (t->GetType() >= cmStateEnums::EXECUTABLE &&
  2976. t->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  2977. // This is really only for compatibility so we do not need to
  2978. // worry about configuration names and output names.
  2979. std::string tLocation = t->GetLocationForBuild();
  2980. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  2981. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  2982. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  2983. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  2984. if (depLocation == tLocation) {
  2985. this->GeneratorTarget->Target->AddUtility(util, false);
  2986. return true;
  2987. }
  2988. }
  2989. } else {
  2990. // The original name of the dependency was not a full path. It
  2991. // must name a target, so add the target-level dependency.
  2992. this->GeneratorTarget->Target->AddUtility(util, true);
  2993. return true;
  2994. }
  2995. }
  2996. // The dependency does not name a target built in this project.
  2997. return false;
  2998. }
  2999. void cmTargetTraceDependencies::CheckCustomCommand(cmCustomCommand const& cc)
  3000. {
  3001. // Collect dependencies referenced by all configurations.
  3002. std::set<std::string> depends;
  3003. for (std::string const& config :
  3004. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig)) {
  3005. for (cmCustomCommandGenerator const& ccg :
  3006. this->LocalGenerator->MakeCustomCommandGenerators(cc, config)) {
  3007. // Collect target-level dependencies referenced in command lines.
  3008. for (auto const& util : ccg.GetUtilities()) {
  3009. this->GeneratorTarget->Target->AddUtility(util);
  3010. }
  3011. // Collect file-level dependencies referenced in DEPENDS.
  3012. depends.insert(ccg.GetDepends().begin(), ccg.GetDepends().end());
  3013. }
  3014. }
  3015. // Queue file-level dependencies.
  3016. for (std::string const& dep : depends) {
  3017. if (!this->IsUtility(dep)) {
  3018. // The dependency does not name a target and may be a file we
  3019. // know how to generate. Queue it.
  3020. this->FollowName(dep);
  3021. }
  3022. }
  3023. }
  3024. void cmTargetTraceDependencies::CheckCustomCommands(
  3025. const std::vector<cmCustomCommand>& commands)
  3026. {
  3027. for (cmCustomCommand const& command : commands) {
  3028. this->CheckCustomCommand(command);
  3029. }
  3030. }
  3031. void cmGeneratorTarget::TraceDependencies()
  3032. {
  3033. // CMake-generated targets have no dependencies to trace. Normally tracing
  3034. // would find nothing anyway, but when building CMake itself the "install"
  3035. // target command ends up referencing the "cmake" target but we do not
  3036. // really want the dependency because "install" depend on "all" anyway.
  3037. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  3038. return;
  3039. }
  3040. // Use a helper object to trace the dependencies.
  3041. cmTargetTraceDependencies tracer(this);
  3042. tracer.Trace();
  3043. }
  3044. std::string cmGeneratorTarget::GetCompilePDBDirectory(
  3045. const std::string& config) const
  3046. {
  3047. if (CompileInfo const* info = this->GetCompileInfo(config)) {
  3048. return info->CompilePdbDir;
  3049. }
  3050. return "";
  3051. }
  3052. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  3053. std::vector<std::string>& archVec) const
  3054. {
  3055. if (!this->Makefile->IsOn("APPLE")) {
  3056. return;
  3057. }
  3058. cmValue archs = nullptr;
  3059. if (!config.empty()) {
  3060. std::string defVarName =
  3061. cmStrCat("OSX_ARCHITECTURES_", cmSystemTools::UpperCase(config));
  3062. archs = this->GetProperty(defVarName);
  3063. }
  3064. if (!archs) {
  3065. archs = this->GetProperty("OSX_ARCHITECTURES");
  3066. }
  3067. if (archs) {
  3068. cmExpandList(*archs, archVec);
  3069. }
  3070. if (archVec.empty()) {
  3071. this->Makefile->GetDefExpandList("_CMAKE_APPLE_ARCHS_DEFAULT", archVec);
  3072. }
  3073. }
  3074. void cmGeneratorTarget::AddExplicitLanguageFlags(std::string& flags,
  3075. cmSourceFile const& sf) const
  3076. {
  3077. cmValue lang = sf.GetProperty("LANGUAGE");
  3078. if (!lang) {
  3079. return;
  3080. }
  3081. switch (this->GetPolicyStatusCMP0119()) {
  3082. case cmPolicies::WARN:
  3083. CM_FALLTHROUGH;
  3084. case cmPolicies::OLD:
  3085. // The OLD behavior is to not add explicit language flags.
  3086. return;
  3087. case cmPolicies::REQUIRED_ALWAYS:
  3088. case cmPolicies::REQUIRED_IF_USED:
  3089. case cmPolicies::NEW:
  3090. // The NEW behavior is to add explicit language flags.
  3091. break;
  3092. }
  3093. this->LocalGenerator->AppendFeatureOptions(flags, *lang,
  3094. "EXPLICIT_LANGUAGE");
  3095. }
  3096. void cmGeneratorTarget::AddCUDAArchitectureFlags(cmBuildStep compileOrLink,
  3097. const std::string& config,
  3098. std::string& flags) const
  3099. {
  3100. std::string property = this->GetSafeProperty("CUDA_ARCHITECTURES");
  3101. if (property.empty()) {
  3102. switch (this->GetPolicyStatusCMP0104()) {
  3103. case cmPolicies::WARN:
  3104. if (!this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile()) {
  3105. this->Makefile->IssueMessage(
  3106. MessageType::AUTHOR_WARNING,
  3107. cmPolicies::GetPolicyWarning(cmPolicies::CMP0104) +
  3108. "\nCUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  3109. "\".");
  3110. }
  3111. CM_FALLTHROUGH;
  3112. case cmPolicies::OLD:
  3113. break;
  3114. default:
  3115. this->Makefile->IssueMessage(
  3116. MessageType::FATAL_ERROR,
  3117. "CUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  3118. "\".");
  3119. }
  3120. }
  3121. // If CUDA_ARCHITECTURES is false we don't add any architectures.
  3122. if (cmIsOff(property)) {
  3123. return;
  3124. }
  3125. std::string const& compiler =
  3126. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  3127. const bool ipoEnabled = this->IsIPOEnabled("CUDA", config);
  3128. // Check for special modes: `all`, `all-major`.
  3129. if (property == "all" || property == "all-major") {
  3130. if (compiler == "NVIDIA" &&
  3131. cmSystemTools::VersionCompare(
  3132. cmSystemTools::OP_GREATER_EQUAL,
  3133. this->Makefile->GetDefinition("CMAKE_CUDA_COMPILER_VERSION"),
  3134. "11.5")) {
  3135. flags = cmStrCat(flags, " -arch=", property);
  3136. return;
  3137. }
  3138. if (property == "all") {
  3139. property =
  3140. *this->Makefile->GetDefinition("CMAKE_CUDA_ARCHITECTURES_ALL");
  3141. } else if (property == "all-major") {
  3142. property =
  3143. *this->Makefile->GetDefinition("CMAKE_CUDA_ARCHITECTURES_ALL_MAJOR");
  3144. }
  3145. } else if (property == "native") {
  3146. cmValue native =
  3147. this->Makefile->GetDefinition("CMAKE_CUDA_ARCHITECTURES_NATIVE");
  3148. if (native.IsEmpty()) {
  3149. this->Makefile->IssueMessage(
  3150. MessageType::FATAL_ERROR,
  3151. "CUDA_ARCHITECTURES is set to \"native\", but no GPU was detected.");
  3152. }
  3153. if (compiler == "NVIDIA" &&
  3154. cmSystemTools::VersionCompare(
  3155. cmSystemTools::OP_GREATER_EQUAL,
  3156. this->Makefile->GetDefinition("CMAKE_CUDA_COMPILER_VERSION"),
  3157. "11.6")) {
  3158. flags = cmStrCat(flags, " -arch=", property);
  3159. return;
  3160. }
  3161. property = *native;
  3162. }
  3163. struct CudaArchitecture
  3164. {
  3165. std::string name;
  3166. bool real{ true };
  3167. bool virtual_{ true };
  3168. };
  3169. std::vector<CudaArchitecture> architectures;
  3170. {
  3171. std::vector<std::string> options;
  3172. cmExpandList(property, options);
  3173. for (std::string& option : options) {
  3174. CudaArchitecture architecture;
  3175. // Architecture name is up to the first specifier.
  3176. std::size_t pos = option.find_first_of('-');
  3177. architecture.name = option.substr(0, pos);
  3178. if (pos != std::string::npos) {
  3179. cm::string_view specifier{ option.c_str() + pos + 1,
  3180. option.length() - pos - 1 };
  3181. if (specifier == "real") {
  3182. architecture.real = true;
  3183. architecture.virtual_ = false;
  3184. } else if (specifier == "virtual") {
  3185. architecture.real = false;
  3186. architecture.virtual_ = true;
  3187. } else {
  3188. this->Makefile->IssueMessage(
  3189. MessageType::FATAL_ERROR,
  3190. "Unknown CUDA architecture specifier \"" + std::string(specifier) +
  3191. "\".");
  3192. }
  3193. }
  3194. architectures.emplace_back(architecture);
  3195. }
  3196. }
  3197. if (compiler == "NVIDIA") {
  3198. if (ipoEnabled && compileOrLink == cmBuildStep::Link) {
  3199. if (cmValue cudaIPOFlags =
  3200. this->Makefile->GetDefinition("CMAKE_CUDA_LINK_OPTIONS_IPO")) {
  3201. flags += *cudaIPOFlags;
  3202. }
  3203. }
  3204. for (CudaArchitecture& architecture : architectures) {
  3205. flags +=
  3206. " --generate-code=arch=compute_" + architecture.name + ",code=[";
  3207. if (architecture.virtual_) {
  3208. flags += "compute_" + architecture.name;
  3209. if (architecture.real) {
  3210. flags += ",";
  3211. }
  3212. }
  3213. if (ipoEnabled) {
  3214. if (compileOrLink == cmBuildStep::Compile) {
  3215. flags += "lto_" + architecture.name;
  3216. } else if (compileOrLink == cmBuildStep::Link) {
  3217. flags += "sm_" + architecture.name;
  3218. }
  3219. } else if (architecture.real) {
  3220. flags += "sm_" + architecture.name;
  3221. }
  3222. flags += "]";
  3223. }
  3224. } else if (compiler == "Clang") {
  3225. for (CudaArchitecture& architecture : architectures) {
  3226. flags += " --cuda-gpu-arch=sm_" + architecture.name;
  3227. if (!architecture.real) {
  3228. this->Makefile->IssueMessage(
  3229. MessageType::WARNING,
  3230. "Clang doesn't support disabling CUDA real code generation.");
  3231. }
  3232. if (!architecture.virtual_) {
  3233. flags += " --no-cuda-include-ptx=sm_" + architecture.name;
  3234. }
  3235. }
  3236. }
  3237. }
  3238. void cmGeneratorTarget::AddISPCTargetFlags(std::string& flags) const
  3239. {
  3240. const std::string& property = this->GetSafeProperty("ISPC_INSTRUCTION_SETS");
  3241. // If ISPC_TARGET is false we don't add any architectures.
  3242. if (cmIsOff(property)) {
  3243. return;
  3244. }
  3245. std::string const& compiler =
  3246. this->Makefile->GetSafeDefinition("CMAKE_ISPC_COMPILER_ID");
  3247. if (compiler == "Intel") {
  3248. std::vector<std::string> targets;
  3249. cmExpandList(property, targets);
  3250. if (!targets.empty()) {
  3251. flags += cmStrCat(" --target=", cmWrap("", targets, "", ","));
  3252. }
  3253. }
  3254. }
  3255. void cmGeneratorTarget::AddHIPArchitectureFlags(std::string& flags) const
  3256. {
  3257. const std::string& property = this->GetSafeProperty("HIP_ARCHITECTURES");
  3258. if (property.empty()) {
  3259. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  3260. "HIP_ARCHITECTURES is empty for target \"" +
  3261. this->GetName() + "\".");
  3262. }
  3263. // If HIP_ARCHITECTURES is false we don't add any architectures.
  3264. if (cmIsOff(property)) {
  3265. return;
  3266. }
  3267. std::vector<std::string> options;
  3268. cmExpandList(property, options);
  3269. for (std::string& option : options) {
  3270. flags += " --offload-arch=" + option;
  3271. }
  3272. }
  3273. void cmGeneratorTarget::AddCUDAToolkitFlags(std::string& flags) const
  3274. {
  3275. std::string const& compiler =
  3276. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  3277. if (compiler == "Clang") {
  3278. // Pass CUDA toolkit explicitly to Clang.
  3279. // Clang's searching for the system CUDA toolkit isn't very good and it's
  3280. // expected the user will explicitly pass the toolkit path.
  3281. // This also avoids Clang having to search for the toolkit on every
  3282. // invocation.
  3283. std::string toolkitRoot =
  3284. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_LIBRARY_ROOT");
  3285. if (!toolkitRoot.empty()) {
  3286. flags += " --cuda-path=" +
  3287. this->LocalGenerator->ConvertToOutputFormat(toolkitRoot,
  3288. cmOutputConverter::SHELL);
  3289. }
  3290. }
  3291. }
  3292. //----------------------------------------------------------------------------
  3293. std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
  3294. std::string const& var, std::string const& lang,
  3295. std::string const& config) const
  3296. {
  3297. if (this->IsIPOEnabled(lang, config)) {
  3298. std::string varIPO = var + "_IPO";
  3299. if (this->Makefile->IsDefinitionSet(varIPO)) {
  3300. return varIPO;
  3301. }
  3302. }
  3303. return var;
  3304. }
  3305. //----------------------------------------------------------------------------
  3306. std::string cmGeneratorTarget::GetCreateRuleVariable(
  3307. std::string const& lang, std::string const& config) const
  3308. {
  3309. switch (this->GetType()) {
  3310. case cmStateEnums::STATIC_LIBRARY: {
  3311. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  3312. return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
  3313. }
  3314. case cmStateEnums::SHARED_LIBRARY:
  3315. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  3316. case cmStateEnums::MODULE_LIBRARY:
  3317. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  3318. case cmStateEnums::EXECUTABLE:
  3319. if (this->IsExecutableWithExports()) {
  3320. std::string linkExeWithExports =
  3321. "CMAKE_" + lang + "_LINK_EXECUTABLE_WITH_EXPORTS";
  3322. if (this->Makefile->IsDefinitionSet(linkExeWithExports)) {
  3323. return linkExeWithExports;
  3324. }
  3325. }
  3326. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  3327. default:
  3328. break;
  3329. }
  3330. return "";
  3331. }
  3332. //----------------------------------------------------------------------------
  3333. std::string cmGeneratorTarget::GetClangTidyExportFixesDirectory(
  3334. const std::string& lang) const
  3335. {
  3336. cmValue val =
  3337. this->GetProperty(cmStrCat(lang, "_CLANG_TIDY_EXPORT_FIXES_DIR"));
  3338. if (!cmNonempty(val)) {
  3339. return {};
  3340. }
  3341. std::string path = *val;
  3342. if (!cmSystemTools::FileIsFullPath(path)) {
  3343. path =
  3344. cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/', path);
  3345. }
  3346. return cmSystemTools::CollapseFullPath(path);
  3347. }
  3348. namespace {
  3349. void processIncludeDirectories(cmGeneratorTarget const* tgt,
  3350. EvaluatedTargetPropertyEntries& entries,
  3351. std::vector<BT<std::string>>& includes,
  3352. std::unordered_set<std::string>& uniqueIncludes,
  3353. bool debugIncludes)
  3354. {
  3355. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  3356. cmLinkImplItem const& item = entry.LinkImplItem;
  3357. std::string const& targetName = item.AsStr();
  3358. bool const fromImported = item.Target && item.Target->IsImported();
  3359. bool const checkCMP0027 = item.CheckCMP0027;
  3360. std::string usedIncludes;
  3361. for (std::string& entryInclude : entry.Values) {
  3362. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  3363. std::ostringstream e;
  3364. MessageType messageType = MessageType::FATAL_ERROR;
  3365. if (checkCMP0027) {
  3366. switch (tgt->GetPolicyStatusCMP0027()) {
  3367. case cmPolicies::WARN:
  3368. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0027) << "\n";
  3369. CM_FALLTHROUGH;
  3370. case cmPolicies::OLD:
  3371. messageType = MessageType::AUTHOR_WARNING;
  3372. break;
  3373. case cmPolicies::REQUIRED_ALWAYS:
  3374. case cmPolicies::REQUIRED_IF_USED:
  3375. case cmPolicies::NEW:
  3376. break;
  3377. }
  3378. }
  3379. /* clang-format off */
  3380. e << "Imported target \"" << targetName << "\" includes "
  3381. "non-existent path\n \"" << entryInclude << "\"\nin its "
  3382. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  3383. "* The path was deleted, renamed, or moved to another "
  3384. "location.\n"
  3385. "* An install or uninstall procedure did not complete "
  3386. "successfully.\n"
  3387. "* The installation package was faulty and references files it "
  3388. "does not provide.\n";
  3389. /* clang-format on */
  3390. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3391. return;
  3392. }
  3393. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  3394. std::ostringstream e;
  3395. bool noMessage = false;
  3396. MessageType messageType = MessageType::FATAL_ERROR;
  3397. if (!targetName.empty()) {
  3398. /* clang-format off */
  3399. e << "Target \"" << targetName << "\" contains relative "
  3400. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  3401. " \"" << entryInclude << "\"";
  3402. /* clang-format on */
  3403. } else {
  3404. switch (tgt->GetPolicyStatusCMP0021()) {
  3405. case cmPolicies::WARN: {
  3406. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  3407. messageType = MessageType::AUTHOR_WARNING;
  3408. } break;
  3409. case cmPolicies::OLD:
  3410. noMessage = true;
  3411. break;
  3412. case cmPolicies::REQUIRED_IF_USED:
  3413. case cmPolicies::REQUIRED_ALWAYS:
  3414. case cmPolicies::NEW:
  3415. // Issue the fatal message.
  3416. break;
  3417. }
  3418. e << "Found relative path while evaluating include directories of "
  3419. "\""
  3420. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  3421. }
  3422. if (!noMessage) {
  3423. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3424. if (messageType == MessageType::FATAL_ERROR) {
  3425. return;
  3426. }
  3427. }
  3428. }
  3429. if (!cmIsOff(entryInclude)) {
  3430. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  3431. }
  3432. if (uniqueIncludes.insert(entryInclude).second) {
  3433. includes.emplace_back(entryInclude, entry.Backtrace);
  3434. if (debugIncludes) {
  3435. usedIncludes += " * " + entryInclude + "\n";
  3436. }
  3437. }
  3438. }
  3439. if (!usedIncludes.empty()) {
  3440. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3441. MessageType::LOG,
  3442. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  3443. usedIncludes,
  3444. entry.Backtrace);
  3445. }
  3446. }
  3447. }
  3448. }
  3449. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  3450. const std::string& config, const std::string& lang) const
  3451. {
  3452. ConfigAndLanguage cacheKey(config, lang);
  3453. {
  3454. auto it = this->IncludeDirectoriesCache.find(cacheKey);
  3455. if (it != this->IncludeDirectoriesCache.end()) {
  3456. return it->second;
  3457. }
  3458. }
  3459. std::vector<BT<std::string>> includes;
  3460. std::unordered_set<std::string> uniqueIncludes;
  3461. cmGeneratorExpressionDAGChecker dagChecker(this, "INCLUDE_DIRECTORIES",
  3462. nullptr, nullptr);
  3463. std::vector<std::string> debugProperties;
  3464. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3465. debugProperties);
  3466. bool debugIncludes = !this->DebugIncludesDone &&
  3467. cm::contains(debugProperties, "INCLUDE_DIRECTORIES");
  3468. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3469. this->DebugIncludesDone = true;
  3470. }
  3471. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3472. this, config, lang, &dagChecker, this->IncludeDirectoriesEntries);
  3473. if (lang == "Swift") {
  3474. AddLangSpecificImplicitIncludeDirectories(
  3475. this, lang, config, "Swift_MODULE_DIRECTORY",
  3476. IncludeDirectoryFallBack::BINARY, entries);
  3477. }
  3478. if (this->CanCompileSources() && (lang != "Swift" && lang != "Fortran")) {
  3479. const std::string propertyName = "ISPC_HEADER_DIRECTORY";
  3480. // If this target has ISPC sources make sure to add the header
  3481. // directory to other compilation units
  3482. if (cm::contains(this->GetAllConfigCompileLanguages(), "ISPC")) {
  3483. if (cmValue val = this->GetProperty(propertyName)) {
  3484. includes.emplace_back(*val);
  3485. } else {
  3486. includes.emplace_back(this->GetObjectDirectory(config));
  3487. }
  3488. }
  3489. AddLangSpecificImplicitIncludeDirectories(
  3490. this, "ISPC", config, propertyName, IncludeDirectoryFallBack::OBJECT,
  3491. entries);
  3492. }
  3493. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES", lang,
  3494. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3495. if (this->Makefile->IsOn("APPLE")) {
  3496. if (cmLinkImplementationLibraries const* impl =
  3497. this->GetLinkImplementationLibraries(config,
  3498. LinkInterfaceFor::Usage)) {
  3499. for (cmLinkImplItem const& lib : impl->Libraries) {
  3500. std::string libDir = cmSystemTools::CollapseFullPath(
  3501. lib.AsStr(), this->Makefile->GetHomeOutputDirectory());
  3502. static cmsys::RegularExpression frameworkCheck(
  3503. "(.*\\.framework)(/Versions/[^/]+)?/[^/]+$");
  3504. if (!frameworkCheck.find(libDir)) {
  3505. continue;
  3506. }
  3507. libDir = frameworkCheck.match(1);
  3508. EvaluatedTargetPropertyEntry ee(lib, cmListFileBacktrace());
  3509. ee.Values.emplace_back(std::move(libDir));
  3510. entries.Entries.emplace_back(std::move(ee));
  3511. }
  3512. }
  3513. }
  3514. processIncludeDirectories(this, entries, includes, uniqueIncludes,
  3515. debugIncludes);
  3516. this->IncludeDirectoriesCache.emplace(cacheKey, includes);
  3517. return includes;
  3518. }
  3519. enum class OptionsParse
  3520. {
  3521. None,
  3522. Shell
  3523. };
  3524. namespace {
  3525. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  3526. const auto DL_END = "</DEVICE_LINK>"_s;
  3527. void processOptions(cmGeneratorTarget const* tgt,
  3528. EvaluatedTargetPropertyEntries const& entries,
  3529. std::vector<BT<std::string>>& options,
  3530. std::unordered_set<std::string>& uniqueOptions,
  3531. bool debugOptions, const char* logName, OptionsParse parse,
  3532. bool processDeviceOptions = false)
  3533. {
  3534. bool splitOption = !processDeviceOptions;
  3535. for (EvaluatedTargetPropertyEntry const& entry : entries.Entries) {
  3536. std::string usedOptions;
  3537. for (std::string const& opt : entry.Values) {
  3538. if (processDeviceOptions && (opt == DL_BEGIN || opt == DL_END)) {
  3539. options.emplace_back(opt, entry.Backtrace);
  3540. splitOption = opt == DL_BEGIN;
  3541. continue;
  3542. }
  3543. if (uniqueOptions.insert(opt).second) {
  3544. if (parse == OptionsParse::Shell &&
  3545. cmHasLiteralPrefix(opt, "SHELL:")) {
  3546. if (splitOption) {
  3547. std::vector<std::string> tmp;
  3548. cmSystemTools::ParseUnixCommandLine(opt.c_str() + 6, tmp);
  3549. for (std::string& o : tmp) {
  3550. options.emplace_back(std::move(o), entry.Backtrace);
  3551. }
  3552. } else {
  3553. options.emplace_back(std::string(opt.c_str() + 6),
  3554. entry.Backtrace);
  3555. }
  3556. } else {
  3557. options.emplace_back(opt, entry.Backtrace);
  3558. }
  3559. if (debugOptions) {
  3560. usedOptions += " * " + opt + "\n";
  3561. }
  3562. }
  3563. }
  3564. if (!usedOptions.empty()) {
  3565. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3566. MessageType::LOG,
  3567. std::string("Used ") + logName + std::string(" for target ") +
  3568. tgt->GetName() + ":\n" + usedOptions,
  3569. entry.Backtrace);
  3570. }
  3571. }
  3572. }
  3573. std::vector<BT<std::string>> wrapOptions(
  3574. std::vector<std::string>& options, const cmListFileBacktrace& bt,
  3575. const std::vector<std::string>& wrapperFlag, const std::string& wrapperSep,
  3576. bool concatFlagAndArgs)
  3577. {
  3578. std::vector<BT<std::string>> result;
  3579. if (options.empty()) {
  3580. return result;
  3581. }
  3582. if (wrapperFlag.empty()) {
  3583. // nothing specified, insert elements as is
  3584. result.reserve(options.size());
  3585. for (std::string& o : options) {
  3586. result.emplace_back(std::move(o), bt);
  3587. }
  3588. return result;
  3589. }
  3590. for (std::vector<std::string>::size_type index = 0; index < options.size();
  3591. index++) {
  3592. if (cmHasLiteralPrefix(options[index], "LINKER:")) {
  3593. // LINKER wrapper specified, insert elements as is
  3594. result.emplace_back(std::move(options[index]), bt);
  3595. continue;
  3596. }
  3597. if (cmHasLiteralPrefix(options[index], "-Wl,")) {
  3598. // replace option by LINKER wrapper
  3599. result.emplace_back(options[index].replace(0, 4, "LINKER:"), bt);
  3600. continue;
  3601. }
  3602. if (cmHasLiteralPrefix(options[index], "-Xlinker=")) {
  3603. // replace option by LINKER wrapper
  3604. result.emplace_back(options[index].replace(0, 9, "LINKER:"), bt);
  3605. continue;
  3606. }
  3607. if (options[index] == "-Xlinker") {
  3608. // replace option by LINKER wrapper
  3609. if (index + 1 < options.size()) {
  3610. result.emplace_back("LINKER:" + options[++index], bt);
  3611. } else {
  3612. result.emplace_back(std::move(options[index]), bt);
  3613. }
  3614. continue;
  3615. }
  3616. // collect all options which must be transformed
  3617. std::vector<std::string> opts;
  3618. while (index < options.size()) {
  3619. if (!cmHasLiteralPrefix(options[index], "LINKER:") &&
  3620. !cmHasLiteralPrefix(options[index], "-Wl,") &&
  3621. !cmHasLiteralPrefix(options[index], "-Xlinker")) {
  3622. opts.emplace_back(std::move(options[index++]));
  3623. } else {
  3624. --index;
  3625. break;
  3626. }
  3627. }
  3628. if (opts.empty()) {
  3629. continue;
  3630. }
  3631. if (!wrapperSep.empty()) {
  3632. if (concatFlagAndArgs) {
  3633. // insert flag elements except last one
  3634. for (auto i = wrapperFlag.begin(); i != wrapperFlag.end() - 1; ++i) {
  3635. result.emplace_back(*i, bt);
  3636. }
  3637. // concatenate last flag element and all list values
  3638. // in one option
  3639. result.emplace_back(wrapperFlag.back() + cmJoin(opts, wrapperSep), bt);
  3640. } else {
  3641. for (std::string const& i : wrapperFlag) {
  3642. result.emplace_back(i, bt);
  3643. }
  3644. // concatenate all list values in one option
  3645. result.emplace_back(cmJoin(opts, wrapperSep), bt);
  3646. }
  3647. } else {
  3648. // prefix each element of list with wrapper
  3649. if (concatFlagAndArgs) {
  3650. std::transform(opts.begin(), opts.end(), opts.begin(),
  3651. [&wrapperFlag](std::string const& o) -> std::string {
  3652. return wrapperFlag.back() + o;
  3653. });
  3654. }
  3655. for (std::string& o : opts) {
  3656. for (auto i = wrapperFlag.begin(),
  3657. e = concatFlagAndArgs ? wrapperFlag.end() - 1
  3658. : wrapperFlag.end();
  3659. i != e; ++i) {
  3660. result.emplace_back(*i, bt);
  3661. }
  3662. result.emplace_back(std::move(o), bt);
  3663. }
  3664. }
  3665. }
  3666. return result;
  3667. }
  3668. }
  3669. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  3670. const std::string& config,
  3671. const std::string& language) const
  3672. {
  3673. std::vector<BT<std::string>> tmp = this->GetCompileOptions(config, language);
  3674. result.reserve(tmp.size());
  3675. for (BT<std::string>& v : tmp) {
  3676. result.emplace_back(std::move(v.Value));
  3677. }
  3678. }
  3679. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileOptions(
  3680. std::string const& config, std::string const& language) const
  3681. {
  3682. ConfigAndLanguage cacheKey(config, language);
  3683. {
  3684. auto it = this->CompileOptionsCache.find(cacheKey);
  3685. if (it != this->CompileOptionsCache.end()) {
  3686. return it->second;
  3687. }
  3688. }
  3689. std::vector<BT<std::string>> result;
  3690. std::unordered_set<std::string> uniqueOptions;
  3691. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_OPTIONS", nullptr,
  3692. nullptr);
  3693. std::vector<std::string> debugProperties;
  3694. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3695. debugProperties);
  3696. bool debugOptions = !this->DebugCompileOptionsDone &&
  3697. cm::contains(debugProperties, "COMPILE_OPTIONS");
  3698. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3699. this->DebugCompileOptionsDone = true;
  3700. }
  3701. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3702. this, config, language, &dagChecker, this->CompileOptionsEntries);
  3703. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS", language,
  3704. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3705. processOptions(this, entries, result, uniqueOptions, debugOptions,
  3706. "compile options", OptionsParse::Shell);
  3707. CompileOptionsCache.emplace(cacheKey, result);
  3708. return result;
  3709. }
  3710. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  3711. const std::string& config) const
  3712. {
  3713. std::vector<BT<std::string>> tmp = this->GetCompileFeatures(config);
  3714. result.reserve(tmp.size());
  3715. for (BT<std::string>& v : tmp) {
  3716. result.emplace_back(std::move(v.Value));
  3717. }
  3718. }
  3719. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileFeatures(
  3720. std::string const& config) const
  3721. {
  3722. std::vector<BT<std::string>> result;
  3723. std::unordered_set<std::string> uniqueFeatures;
  3724. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_FEATURES", nullptr,
  3725. nullptr);
  3726. std::vector<std::string> debugProperties;
  3727. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3728. debugProperties);
  3729. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  3730. cm::contains(debugProperties, "COMPILE_FEATURES");
  3731. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3732. this->DebugCompileFeaturesDone = true;
  3733. }
  3734. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3735. this, config, std::string(), &dagChecker, this->CompileFeaturesEntries);
  3736. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  3737. std::string(), &dagChecker, entries,
  3738. IncludeRuntimeInterface::Yes);
  3739. processOptions(this, entries, result, uniqueFeatures, debugFeatures,
  3740. "compile features", OptionsParse::None);
  3741. return result;
  3742. }
  3743. void cmGeneratorTarget::GetCompileDefinitions(
  3744. std::vector<std::string>& result, const std::string& config,
  3745. const std::string& language) const
  3746. {
  3747. std::vector<BT<std::string>> tmp =
  3748. this->GetCompileDefinitions(config, language);
  3749. result.reserve(tmp.size());
  3750. for (BT<std::string>& v : tmp) {
  3751. result.emplace_back(std::move(v.Value));
  3752. }
  3753. }
  3754. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileDefinitions(
  3755. std::string const& config, std::string const& language) const
  3756. {
  3757. ConfigAndLanguage cacheKey(config, language);
  3758. {
  3759. auto it = this->CompileDefinitionsCache.find(cacheKey);
  3760. if (it != this->CompileDefinitionsCache.end()) {
  3761. return it->second;
  3762. }
  3763. }
  3764. std::vector<BT<std::string>> list;
  3765. std::unordered_set<std::string> uniqueOptions;
  3766. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_DEFINITIONS",
  3767. nullptr, nullptr);
  3768. std::vector<std::string> debugProperties;
  3769. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3770. debugProperties);
  3771. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  3772. cm::contains(debugProperties, "COMPILE_DEFINITIONS");
  3773. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3774. this->DebugCompileDefinitionsDone = true;
  3775. }
  3776. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3777. this, config, language, &dagChecker, this->CompileDefinitionsEntries);
  3778. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS", language,
  3779. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3780. if (!config.empty()) {
  3781. std::string configPropName =
  3782. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  3783. cmValue configProp = this->GetProperty(configPropName);
  3784. if (configProp) {
  3785. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  3786. case cmPolicies::WARN: {
  3787. this->LocalGenerator->IssueMessage(
  3788. MessageType::AUTHOR_WARNING,
  3789. cmPolicies::GetPolicyWarning(cmPolicies::CMP0043));
  3790. CM_FALLTHROUGH;
  3791. }
  3792. case cmPolicies::OLD: {
  3793. std::unique_ptr<TargetPropertyEntry> entry =
  3794. CreateTargetPropertyEntry(
  3795. *this->LocalGenerator->GetCMakeInstance(), *configProp);
  3796. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  3797. this, config, language, &dagChecker, *entry));
  3798. } break;
  3799. case cmPolicies::NEW:
  3800. case cmPolicies::REQUIRED_ALWAYS:
  3801. case cmPolicies::REQUIRED_IF_USED:
  3802. break;
  3803. }
  3804. }
  3805. }
  3806. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3807. "compile definitions", OptionsParse::None);
  3808. this->CompileDefinitionsCache.emplace(cacheKey, list);
  3809. return list;
  3810. }
  3811. std::vector<BT<std::string>> cmGeneratorTarget::GetPrecompileHeaders(
  3812. const std::string& config, const std::string& language) const
  3813. {
  3814. ConfigAndLanguage cacheKey(config, language);
  3815. {
  3816. auto it = this->PrecompileHeadersCache.find(cacheKey);
  3817. if (it != this->PrecompileHeadersCache.end()) {
  3818. return it->second;
  3819. }
  3820. }
  3821. std::unordered_set<std::string> uniqueOptions;
  3822. cmGeneratorExpressionDAGChecker dagChecker(this, "PRECOMPILE_HEADERS",
  3823. nullptr, nullptr);
  3824. std::vector<std::string> debugProperties;
  3825. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3826. debugProperties);
  3827. bool debugDefines = !this->DebugPrecompileHeadersDone &&
  3828. std::find(debugProperties.begin(), debugProperties.end(),
  3829. "PRECOMPILE_HEADERS") != debugProperties.end();
  3830. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3831. this->DebugPrecompileHeadersDone = true;
  3832. }
  3833. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3834. this, config, language, &dagChecker, this->PrecompileHeadersEntries);
  3835. AddInterfaceEntries(this, config, "INTERFACE_PRECOMPILE_HEADERS", language,
  3836. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3837. std::vector<BT<std::string>> list;
  3838. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3839. "precompile headers", OptionsParse::None);
  3840. this->PrecompileHeadersCache.emplace(cacheKey, list);
  3841. return list;
  3842. }
  3843. std::string cmGeneratorTarget::GetPchHeader(const std::string& config,
  3844. const std::string& language,
  3845. const std::string& arch) const
  3846. {
  3847. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3848. language != "OBJCXX") {
  3849. return std::string();
  3850. }
  3851. if (this->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
  3852. return std::string();
  3853. }
  3854. const cmGeneratorTarget* generatorTarget = this;
  3855. cmValue pchReuseFrom =
  3856. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3857. const auto inserted =
  3858. this->PchHeaders.insert(std::make_pair(language + config + arch, ""));
  3859. if (inserted.second) {
  3860. const std::vector<BT<std::string>> headers =
  3861. this->GetPrecompileHeaders(config, language);
  3862. if (headers.empty() && !pchReuseFrom) {
  3863. return std::string();
  3864. }
  3865. std::string& filename = inserted.first->second;
  3866. if (pchReuseFrom) {
  3867. generatorTarget =
  3868. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3869. }
  3870. filename = cmStrCat(
  3871. generatorTarget->LocalGenerator->GetCurrentBinaryDirectory(), "/");
  3872. const std::map<std::string, std::string> languageToExtension = {
  3873. { "C", ".h" },
  3874. { "CXX", ".hxx" },
  3875. { "OBJC", ".objc.h" },
  3876. { "OBJCXX", ".objcxx.hxx" }
  3877. };
  3878. filename =
  3879. cmStrCat(filename, "CMakeFiles/", generatorTarget->GetName(), ".dir");
  3880. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  3881. filename = cmStrCat(filename, "/", config);
  3882. }
  3883. filename =
  3884. cmStrCat(filename, "/cmake_pch", arch.empty() ? "" : cmStrCat("_", arch),
  3885. languageToExtension.at(language));
  3886. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3887. if (!pchReuseFrom) {
  3888. cmValue pchPrologue =
  3889. this->Makefile->GetDefinition("CMAKE_PCH_PROLOGUE");
  3890. cmValue pchEpilogue =
  3891. this->Makefile->GetDefinition("CMAKE_PCH_EPILOGUE");
  3892. std::string firstHeaderOnDisk;
  3893. {
  3894. cmGeneratedFileStream file(
  3895. filename_tmp, false,
  3896. this->GetGlobalGenerator()->GetMakefileEncoding());
  3897. file << "/* generated by CMake */\n\n";
  3898. if (pchPrologue) {
  3899. file << *pchPrologue << "\n";
  3900. }
  3901. if (this->GetGlobalGenerator()->IsXcode()) {
  3902. file << "#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3903. }
  3904. if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) {
  3905. file << "#ifdef __cplusplus\n";
  3906. }
  3907. for (auto const& header_bt : headers) {
  3908. if (header_bt.Value.empty()) {
  3909. continue;
  3910. }
  3911. if (header_bt.Value[0] == '<' || header_bt.Value[0] == '\"') {
  3912. file << "#include " << header_bt.Value << "\n";
  3913. } else {
  3914. file << "#include \"" << header_bt.Value << "\"\n";
  3915. }
  3916. if (cmSystemTools::FileExists(header_bt.Value) &&
  3917. firstHeaderOnDisk.empty()) {
  3918. firstHeaderOnDisk = header_bt.Value;
  3919. }
  3920. }
  3921. if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) {
  3922. file << "#endif // __cplusplus\n";
  3923. }
  3924. if (this->GetGlobalGenerator()->IsXcode()) {
  3925. file << "#endif // CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3926. }
  3927. if (pchEpilogue) {
  3928. file << *pchEpilogue << "\n";
  3929. }
  3930. }
  3931. if (!firstHeaderOnDisk.empty()) {
  3932. cmFileTimes::Copy(firstHeaderOnDisk, filename_tmp);
  3933. }
  3934. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3935. }
  3936. }
  3937. return inserted.first->second;
  3938. }
  3939. std::string cmGeneratorTarget::GetPchSource(const std::string& config,
  3940. const std::string& language,
  3941. const std::string& arch) const
  3942. {
  3943. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3944. language != "OBJCXX") {
  3945. return std::string();
  3946. }
  3947. const auto inserted =
  3948. this->PchSources.insert(std::make_pair(language + config + arch, ""));
  3949. if (inserted.second) {
  3950. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3951. if (pchHeader.empty()) {
  3952. return std::string();
  3953. }
  3954. std::string& filename = inserted.first->second;
  3955. const cmGeneratorTarget* generatorTarget = this;
  3956. cmValue pchReuseFrom =
  3957. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3958. if (pchReuseFrom) {
  3959. generatorTarget =
  3960. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3961. }
  3962. filename =
  3963. cmStrCat(generatorTarget->LocalGenerator->GetCurrentBinaryDirectory(),
  3964. "/CMakeFiles/", generatorTarget->GetName(), ".dir/cmake_pch");
  3965. // For GCC the source extension will be transformed into .h[xx].gch
  3966. if (!this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  3967. const std::map<std::string, std::string> languageToExtension = {
  3968. { "C", ".h.c" },
  3969. { "CXX", ".hxx.cxx" },
  3970. { "OBJC", ".objc.h.m" },
  3971. { "OBJCXX", ".objcxx.hxx.mm" }
  3972. };
  3973. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3974. languageToExtension.at(language));
  3975. } else {
  3976. const std::map<std::string, std::string> languageToExtension = {
  3977. { "C", ".c" }, { "CXX", ".cxx" }, { "OBJC", ".m" }, { "OBJCXX", ".mm" }
  3978. };
  3979. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3980. languageToExtension.at(language));
  3981. }
  3982. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3983. if (!pchReuseFrom) {
  3984. {
  3985. cmGeneratedFileStream file(filename_tmp);
  3986. file << "/* generated by CMake */\n";
  3987. }
  3988. cmFileTimes::Copy(pchHeader, filename_tmp);
  3989. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3990. }
  3991. }
  3992. return inserted.first->second;
  3993. }
  3994. std::string cmGeneratorTarget::GetPchFileObject(const std::string& config,
  3995. const std::string& language,
  3996. const std::string& arch)
  3997. {
  3998. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3999. language != "OBJCXX") {
  4000. return std::string();
  4001. }
  4002. const auto inserted =
  4003. this->PchObjectFiles.insert(std::make_pair(language + config + arch, ""));
  4004. if (inserted.second) {
  4005. const std::string pchSource = this->GetPchSource(config, language, arch);
  4006. if (pchSource.empty()) {
  4007. return std::string();
  4008. }
  4009. std::string& filename = inserted.first->second;
  4010. auto* pchSf = this->Makefile->GetOrCreateSource(
  4011. pchSource, false, cmSourceFileLocationKind::Known);
  4012. filename = cmStrCat(this->ObjectDirectory, this->GetObjectName(pchSf));
  4013. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  4014. cmSystemTools::ReplaceString(
  4015. filename, this->GetGlobalGenerator()->GetCMakeCFGIntDir(), config);
  4016. }
  4017. }
  4018. return inserted.first->second;
  4019. }
  4020. std::string cmGeneratorTarget::GetPchFile(const std::string& config,
  4021. const std::string& language,
  4022. const std::string& arch)
  4023. {
  4024. const auto inserted =
  4025. this->PchFiles.insert(std::make_pair(language + config + arch, ""));
  4026. if (inserted.second) {
  4027. std::string& pchFile = inserted.first->second;
  4028. const std::string pchExtension =
  4029. this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
  4030. if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  4031. auto replaceExtension = [](const std::string& str,
  4032. const std::string& ext) -> std::string {
  4033. auto dot_pos = str.rfind('.');
  4034. std::string result;
  4035. if (dot_pos != std::string::npos) {
  4036. result = str.substr(0, dot_pos);
  4037. }
  4038. result += ext;
  4039. return result;
  4040. };
  4041. cmGeneratorTarget* generatorTarget = this;
  4042. cmValue pchReuseFrom =
  4043. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  4044. if (pchReuseFrom) {
  4045. generatorTarget =
  4046. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  4047. }
  4048. const std::string pchFileObject =
  4049. generatorTarget->GetPchFileObject(config, language, arch);
  4050. if (!pchExtension.empty()) {
  4051. pchFile = replaceExtension(pchFileObject, pchExtension);
  4052. }
  4053. } else {
  4054. pchFile = this->GetPchHeader(config, language, arch);
  4055. pchFile += pchExtension;
  4056. }
  4057. }
  4058. return inserted.first->second;
  4059. }
  4060. std::string cmGeneratorTarget::GetPchCreateCompileOptions(
  4061. const std::string& config, const std::string& language,
  4062. const std::string& arch)
  4063. {
  4064. const auto inserted = this->PchCreateCompileOptions.insert(
  4065. std::make_pair(language + config + arch, ""));
  4066. if (inserted.second) {
  4067. std::string& createOptionList = inserted.first->second;
  4068. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  4069. createOptionList = this->Makefile->GetSafeDefinition(
  4070. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  4071. }
  4072. if (this->GetPropertyAsBool("PCH_INSTANTIATE_TEMPLATES")) {
  4073. std::string varName = cmStrCat(
  4074. "CMAKE_", language, "_COMPILE_OPTIONS_INSTANTIATE_TEMPLATES_PCH");
  4075. std::string instantiateOption =
  4076. this->Makefile->GetSafeDefinition(varName);
  4077. if (!instantiateOption.empty()) {
  4078. createOptionList = cmStrCat(createOptionList, ";", instantiateOption);
  4079. }
  4080. }
  4081. const std::string createOptVar =
  4082. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_CREATE_PCH");
  4083. createOptionList = cmStrCat(
  4084. createOptionList, ";", this->Makefile->GetSafeDefinition(createOptVar));
  4085. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  4086. const std::string pchFile = this->GetPchFile(config, language, arch);
  4087. cmSystemTools::ReplaceString(createOptionList, "<PCH_HEADER>", pchHeader);
  4088. cmSystemTools::ReplaceString(createOptionList, "<PCH_FILE>", pchFile);
  4089. }
  4090. return inserted.first->second;
  4091. }
  4092. std::string cmGeneratorTarget::GetPchUseCompileOptions(
  4093. const std::string& config, const std::string& language,
  4094. const std::string& arch)
  4095. {
  4096. const auto inserted = this->PchUseCompileOptions.insert(
  4097. std::make_pair(language + config + arch, ""));
  4098. if (inserted.second) {
  4099. std::string& useOptionList = inserted.first->second;
  4100. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  4101. useOptionList = this->Makefile->GetSafeDefinition(
  4102. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  4103. }
  4104. const std::string useOptVar =
  4105. cmStrCat(language, "_COMPILE_OPTIONS_USE_PCH");
  4106. std::string const& useOptionListProperty =
  4107. this->GetSafeProperty(useOptVar);
  4108. useOptionList = cmStrCat(
  4109. useOptionList, ";",
  4110. useOptionListProperty.empty()
  4111. ? this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_", useOptVar))
  4112. : useOptionListProperty);
  4113. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  4114. const std::string pchFile = this->GetPchFile(config, language, arch);
  4115. cmSystemTools::ReplaceString(useOptionList, "<PCH_HEADER>", pchHeader);
  4116. cmSystemTools::ReplaceString(useOptionList, "<PCH_FILE>", pchFile);
  4117. }
  4118. return inserted.first->second;
  4119. }
  4120. void cmGeneratorTarget::AddSourceFileToUnityBatch(
  4121. const std::string& sourceFilename)
  4122. {
  4123. this->UnityBatchedSourceFiles.insert(sourceFilename);
  4124. }
  4125. bool cmGeneratorTarget::IsSourceFilePartOfUnityBatch(
  4126. const std::string& sourceFilename) const
  4127. {
  4128. if (!this->GetPropertyAsBool("UNITY_BUILD")) {
  4129. return false;
  4130. }
  4131. return this->UnityBatchedSourceFiles.find(sourceFilename) !=
  4132. this->UnityBatchedSourceFiles.end();
  4133. }
  4134. void cmGeneratorTarget::GetLinkOptions(std::vector<std::string>& result,
  4135. const std::string& config,
  4136. const std::string& language) const
  4137. {
  4138. if (this->IsDeviceLink() &&
  4139. this->GetPolicyStatusCMP0105() != cmPolicies::NEW) {
  4140. // link options are not propagated to the device link step
  4141. return;
  4142. }
  4143. std::vector<BT<std::string>> tmp = this->GetLinkOptions(config, language);
  4144. result.reserve(tmp.size());
  4145. for (BT<std::string>& v : tmp) {
  4146. result.emplace_back(std::move(v.Value));
  4147. }
  4148. }
  4149. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkOptions(
  4150. std::string const& config, std::string const& language) const
  4151. {
  4152. ConfigAndLanguage cacheKey(
  4153. config, cmStrCat(language, this->IsDeviceLink() ? "-device" : ""));
  4154. {
  4155. auto it = this->LinkOptionsCache.find(cacheKey);
  4156. if (it != this->LinkOptionsCache.end()) {
  4157. return it->second;
  4158. }
  4159. }
  4160. std::vector<BT<std::string>> result;
  4161. std::unordered_set<std::string> uniqueOptions;
  4162. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_OPTIONS", nullptr,
  4163. nullptr);
  4164. std::vector<std::string> debugProperties;
  4165. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  4166. debugProperties);
  4167. bool debugOptions = !this->DebugLinkOptionsDone &&
  4168. cm::contains(debugProperties, "LINK_OPTIONS");
  4169. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4170. this->DebugLinkOptionsDone = true;
  4171. }
  4172. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  4173. this, config, language, &dagChecker, this->LinkOptionsEntries);
  4174. AddInterfaceEntries(this, config, "INTERFACE_LINK_OPTIONS", language,
  4175. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  4176. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  4177. ? LinkInterfaceFor::Link
  4178. : LinkInterfaceFor::Usage);
  4179. processOptions(this, entries, result, uniqueOptions, debugOptions,
  4180. "link options", OptionsParse::Shell, this->IsDeviceLink());
  4181. if (this->IsDeviceLink()) {
  4182. // wrap host link options
  4183. const std::string wrapper(this->Makefile->GetSafeDefinition(
  4184. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG"));
  4185. std::vector<std::string> wrapperFlag = cmExpandedList(wrapper);
  4186. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  4187. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG_SEP"));
  4188. bool concatFlagAndArgs = true;
  4189. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  4190. concatFlagAndArgs = false;
  4191. wrapperFlag.pop_back();
  4192. }
  4193. auto it = result.begin();
  4194. while (it != result.end()) {
  4195. if (it->Value == DL_BEGIN) {
  4196. // device link options, no treatment
  4197. it = result.erase(it);
  4198. it = std::find_if(it, result.end(), [](const BT<std::string>& item) {
  4199. return item.Value == DL_END;
  4200. });
  4201. if (it != result.end()) {
  4202. it = result.erase(it);
  4203. }
  4204. } else {
  4205. // host link options must be wrapped
  4206. std::vector<std::string> options;
  4207. cmSystemTools::ParseUnixCommandLine(it->Value.c_str(), options);
  4208. auto hostOptions = wrapOptions(options, it->Backtrace, wrapperFlag,
  4209. wrapperSep, concatFlagAndArgs);
  4210. it = result.erase(it);
  4211. // some compilers (like gcc 4.8 or Intel 19.0 or XLC 16) do not respect
  4212. // C++11 standard: 'std::vector::insert()' do not returns an iterator,
  4213. // so need to recompute the iterator after insertion.
  4214. if (it == result.end()) {
  4215. cm::append(result, hostOptions);
  4216. it = result.end();
  4217. } else {
  4218. auto index = it - result.begin();
  4219. result.insert(it, hostOptions.begin(), hostOptions.end());
  4220. it = result.begin() + index + hostOptions.size();
  4221. }
  4222. }
  4223. }
  4224. }
  4225. // Last step: replace "LINKER:" prefixed elements by
  4226. // actual linker wrapper
  4227. result = this->ResolveLinkerWrapper(result, language);
  4228. this->LinkOptionsCache.emplace(cacheKey, result);
  4229. return result;
  4230. }
  4231. std::vector<BT<std::string>>& cmGeneratorTarget::ResolveLinkerWrapper(
  4232. std::vector<BT<std::string>>& result, const std::string& language,
  4233. bool joinItems) const
  4234. {
  4235. // replace "LINKER:" prefixed elements by actual linker wrapper
  4236. const std::string wrapper(this->Makefile->GetSafeDefinition(
  4237. "CMAKE_" + language +
  4238. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG"
  4239. : "_LINKER_WRAPPER_FLAG")));
  4240. std::vector<std::string> wrapperFlag = cmExpandedList(wrapper);
  4241. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  4242. "CMAKE_" + language +
  4243. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG_SEP"
  4244. : "_LINKER_WRAPPER_FLAG_SEP")));
  4245. bool concatFlagAndArgs = true;
  4246. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  4247. concatFlagAndArgs = false;
  4248. wrapperFlag.pop_back();
  4249. }
  4250. const std::string LINKER{ "LINKER:" };
  4251. const std::string SHELL{ "SHELL:" };
  4252. const std::string LINKER_SHELL = LINKER + SHELL;
  4253. std::vector<BT<std::string>>::iterator entry;
  4254. while ((entry = std::find_if(result.begin(), result.end(),
  4255. [&LINKER](BT<std::string> const& item) -> bool {
  4256. return item.Value.compare(0, LINKER.length(),
  4257. LINKER) == 0;
  4258. })) != result.end()) {
  4259. std::string value = std::move(entry->Value);
  4260. cmListFileBacktrace bt = std::move(entry->Backtrace);
  4261. entry = result.erase(entry);
  4262. std::vector<std::string> linkerOptions;
  4263. if (value.compare(0, LINKER_SHELL.length(), LINKER_SHELL) == 0) {
  4264. cmSystemTools::ParseUnixCommandLine(
  4265. value.c_str() + LINKER_SHELL.length(), linkerOptions);
  4266. } else {
  4267. linkerOptions = cmTokenize(value.substr(LINKER.length()), ",");
  4268. }
  4269. if (linkerOptions.empty() ||
  4270. (linkerOptions.size() == 1 && linkerOptions.front().empty())) {
  4271. continue;
  4272. }
  4273. // for now, raise an error if prefix SHELL: is part of arguments
  4274. if (std::find_if(linkerOptions.begin(), linkerOptions.end(),
  4275. [&SHELL](const std::string& item) -> bool {
  4276. return item.find(SHELL) != std::string::npos;
  4277. }) != linkerOptions.end()) {
  4278. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4279. MessageType::FATAL_ERROR,
  4280. "'SHELL:' prefix is not supported as part of 'LINKER:' arguments.",
  4281. this->GetBacktrace());
  4282. return result;
  4283. }
  4284. std::vector<BT<std::string>> options = wrapOptions(
  4285. linkerOptions, bt, wrapperFlag, wrapperSep, concatFlagAndArgs);
  4286. if (joinItems) {
  4287. result.insert(entry,
  4288. cmJoin(cmRange<decltype(options.cbegin())>(
  4289. options.cbegin(), options.cend()),
  4290. " "_s));
  4291. } else {
  4292. result.insert(entry, options.begin(), options.end());
  4293. }
  4294. }
  4295. return result;
  4296. }
  4297. void cmGeneratorTarget::GetStaticLibraryLinkOptions(
  4298. std::vector<std::string>& result, const std::string& config,
  4299. const std::string& language) const
  4300. {
  4301. std::vector<BT<std::string>> tmp =
  4302. this->GetStaticLibraryLinkOptions(config, language);
  4303. result.reserve(tmp.size());
  4304. for (BT<std::string>& v : tmp) {
  4305. result.emplace_back(std::move(v.Value));
  4306. }
  4307. }
  4308. std::vector<BT<std::string>> cmGeneratorTarget::GetStaticLibraryLinkOptions(
  4309. std::string const& config, std::string const& language) const
  4310. {
  4311. std::vector<BT<std::string>> result;
  4312. std::unordered_set<std::string> uniqueOptions;
  4313. cmGeneratorExpressionDAGChecker dagChecker(this, "STATIC_LIBRARY_OPTIONS",
  4314. nullptr, nullptr);
  4315. EvaluatedTargetPropertyEntries entries;
  4316. if (cmValue linkOptions = this->GetProperty("STATIC_LIBRARY_OPTIONS")) {
  4317. std::unique_ptr<TargetPropertyEntry> entry = CreateTargetPropertyEntry(
  4318. *this->LocalGenerator->GetCMakeInstance(), *linkOptions);
  4319. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  4320. this, config, language, &dagChecker, *entry));
  4321. }
  4322. processOptions(this, entries, result, uniqueOptions, false,
  4323. "static library link options", OptionsParse::Shell);
  4324. return result;
  4325. }
  4326. namespace {
  4327. void processLinkDirectories(cmGeneratorTarget const* tgt,
  4328. EvaluatedTargetPropertyEntries& entries,
  4329. std::vector<BT<std::string>>& directories,
  4330. std::unordered_set<std::string>& uniqueDirectories,
  4331. bool debugDirectories)
  4332. {
  4333. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  4334. cmLinkImplItem const& item = entry.LinkImplItem;
  4335. std::string const& targetName = item.AsStr();
  4336. std::string usedDirectories;
  4337. for (std::string& entryDirectory : entry.Values) {
  4338. if (!cmSystemTools::FileIsFullPath(entryDirectory)) {
  4339. std::ostringstream e;
  4340. bool noMessage = false;
  4341. MessageType messageType = MessageType::FATAL_ERROR;
  4342. if (!targetName.empty()) {
  4343. /* clang-format off */
  4344. e << "Target \"" << targetName << "\" contains relative "
  4345. "path in its INTERFACE_LINK_DIRECTORIES:\n"
  4346. " \"" << entryDirectory << "\"";
  4347. /* clang-format on */
  4348. } else {
  4349. switch (tgt->GetPolicyStatusCMP0081()) {
  4350. case cmPolicies::WARN: {
  4351. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0081) << "\n";
  4352. messageType = MessageType::AUTHOR_WARNING;
  4353. } break;
  4354. case cmPolicies::OLD:
  4355. noMessage = true;
  4356. break;
  4357. case cmPolicies::REQUIRED_IF_USED:
  4358. case cmPolicies::REQUIRED_ALWAYS:
  4359. case cmPolicies::NEW:
  4360. // Issue the fatal message.
  4361. break;
  4362. }
  4363. e << "Found relative path while evaluating link directories of "
  4364. "\""
  4365. << tgt->GetName() << "\":\n \"" << entryDirectory << "\"\n";
  4366. }
  4367. if (!noMessage) {
  4368. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  4369. if (messageType == MessageType::FATAL_ERROR) {
  4370. return;
  4371. }
  4372. }
  4373. }
  4374. // Sanitize the path the same way the link_directories command does
  4375. // in case projects set the LINK_DIRECTORIES property directly.
  4376. cmSystemTools::ConvertToUnixSlashes(entryDirectory);
  4377. if (uniqueDirectories.insert(entryDirectory).second) {
  4378. directories.emplace_back(entryDirectory, entry.Backtrace);
  4379. if (debugDirectories) {
  4380. usedDirectories += " * " + entryDirectory + "\n";
  4381. }
  4382. }
  4383. }
  4384. if (!usedDirectories.empty()) {
  4385. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  4386. MessageType::LOG,
  4387. std::string("Used link directories for target ") + tgt->GetName() +
  4388. ":\n" + usedDirectories,
  4389. entry.Backtrace);
  4390. }
  4391. }
  4392. }
  4393. }
  4394. void cmGeneratorTarget::GetLinkDirectories(std::vector<std::string>& result,
  4395. const std::string& config,
  4396. const std::string& language) const
  4397. {
  4398. std::vector<BT<std::string>> tmp =
  4399. this->GetLinkDirectories(config, language);
  4400. result.reserve(tmp.size());
  4401. for (BT<std::string>& v : tmp) {
  4402. result.emplace_back(std::move(v.Value));
  4403. }
  4404. }
  4405. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDirectories(
  4406. std::string const& config, std::string const& language) const
  4407. {
  4408. ConfigAndLanguage cacheKey(
  4409. config, cmStrCat(language, this->IsDeviceLink() ? "-device" : ""));
  4410. {
  4411. auto it = this->LinkDirectoriesCache.find(cacheKey);
  4412. if (it != this->LinkDirectoriesCache.end()) {
  4413. return it->second;
  4414. }
  4415. }
  4416. std::vector<BT<std::string>> result;
  4417. std::unordered_set<std::string> uniqueDirectories;
  4418. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DIRECTORIES", nullptr,
  4419. nullptr);
  4420. std::vector<std::string> debugProperties;
  4421. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  4422. debugProperties);
  4423. bool debugDirectories = !this->DebugLinkDirectoriesDone &&
  4424. cm::contains(debugProperties, "LINK_DIRECTORIES");
  4425. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4426. this->DebugLinkDirectoriesDone = true;
  4427. }
  4428. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  4429. this, config, language, &dagChecker, this->LinkDirectoriesEntries);
  4430. AddInterfaceEntries(this, config, "INTERFACE_LINK_DIRECTORIES", language,
  4431. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  4432. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  4433. ? LinkInterfaceFor::Link
  4434. : LinkInterfaceFor::Usage);
  4435. processLinkDirectories(this, entries, result, uniqueDirectories,
  4436. debugDirectories);
  4437. this->LinkDirectoriesCache.emplace(cacheKey, result);
  4438. return result;
  4439. }
  4440. void cmGeneratorTarget::GetLinkDepends(std::vector<std::string>& result,
  4441. const std::string& config,
  4442. const std::string& language) const
  4443. {
  4444. std::vector<BT<std::string>> tmp = this->GetLinkDepends(config, language);
  4445. result.reserve(tmp.size());
  4446. for (BT<std::string>& v : tmp) {
  4447. result.emplace_back(std::move(v.Value));
  4448. }
  4449. }
  4450. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDepends(
  4451. std::string const& config, std::string const& language) const
  4452. {
  4453. std::vector<BT<std::string>> result;
  4454. std::unordered_set<std::string> uniqueOptions;
  4455. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DEPENDS", nullptr,
  4456. nullptr);
  4457. EvaluatedTargetPropertyEntries entries;
  4458. if (cmValue linkDepends = this->GetProperty("LINK_DEPENDS")) {
  4459. std::vector<std::string> depends = cmExpandedList(*linkDepends);
  4460. for (const auto& depend : depends) {
  4461. std::unique_ptr<TargetPropertyEntry> entry = CreateTargetPropertyEntry(
  4462. *this->LocalGenerator->GetCMakeInstance(), depend);
  4463. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  4464. this, config, language, &dagChecker, *entry));
  4465. }
  4466. }
  4467. AddInterfaceEntries(this, config, "INTERFACE_LINK_DEPENDS", language,
  4468. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  4469. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  4470. ? LinkInterfaceFor::Link
  4471. : LinkInterfaceFor::Usage);
  4472. processOptions(this, entries, result, uniqueOptions, false, "link depends",
  4473. OptionsParse::None);
  4474. return result;
  4475. }
  4476. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  4477. {
  4478. if (this->IsImported()) {
  4479. return;
  4480. }
  4481. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  4482. // Get the names.
  4483. cmGeneratorTarget::Names targetNames;
  4484. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4485. targetNames = this->GetExecutableNames(config);
  4486. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4487. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4488. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4489. targetNames = this->GetLibraryNames(config);
  4490. } else {
  4491. return;
  4492. }
  4493. // Get the directory.
  4494. std::string dir =
  4495. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  4496. // Add each name.
  4497. std::string f;
  4498. if (!targetNames.Output.empty()) {
  4499. f = cmStrCat(dir, '/', targetNames.Output);
  4500. gg->AddToManifest(f);
  4501. }
  4502. if (!targetNames.SharedObject.empty()) {
  4503. f = cmStrCat(dir, '/', targetNames.SharedObject);
  4504. gg->AddToManifest(f);
  4505. }
  4506. if (!targetNames.Real.empty()) {
  4507. f = cmStrCat(dir, '/', targetNames.Real);
  4508. gg->AddToManifest(f);
  4509. }
  4510. if (!targetNames.PDB.empty()) {
  4511. f = cmStrCat(dir, '/', targetNames.PDB);
  4512. gg->AddToManifest(f);
  4513. }
  4514. if (!targetNames.ImportLibrary.empty()) {
  4515. f =
  4516. cmStrCat(this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact),
  4517. '/', targetNames.ImportLibrary);
  4518. gg->AddToManifest(f);
  4519. }
  4520. }
  4521. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  4522. {
  4523. // Compute the language standard based on the compile features.
  4524. cmStandardLevelResolver standardResolver(this->Makefile);
  4525. std::vector<BT<std::string>> features = this->GetCompileFeatures(config);
  4526. for (BT<std::string> const& f : features) {
  4527. std::string lang;
  4528. if (!standardResolver.CompileFeatureKnown(this->Target->GetName(), f.Value,
  4529. lang, nullptr)) {
  4530. return false;
  4531. }
  4532. std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
  4533. cmValue currentLanguageStandard = this->GetLanguageStandard(lang, config);
  4534. std::string newRequiredStandard;
  4535. if (!standardResolver.GetNewRequiredStandard(
  4536. this->Target->GetName(), f.Value, currentLanguageStandard,
  4537. newRequiredStandard)) {
  4538. return false;
  4539. }
  4540. if (!newRequiredStandard.empty()) {
  4541. BTs<std::string>& languageStandardProperty =
  4542. this->LanguageStandardMap[key];
  4543. if (languageStandardProperty.Value != newRequiredStandard) {
  4544. languageStandardProperty.Value = newRequiredStandard;
  4545. languageStandardProperty.Backtraces.clear();
  4546. }
  4547. languageStandardProperty.Backtraces.emplace_back(f.Backtrace);
  4548. }
  4549. }
  4550. return true;
  4551. }
  4552. bool cmGeneratorTarget::ComputeCompileFeatures(
  4553. std::string const& config, std::set<LanguagePair> const& languagePairs) const
  4554. {
  4555. for (const auto& language : languagePairs) {
  4556. BTs<std::string> const* generatorTargetLanguageStandard =
  4557. this->GetLanguageStandardProperty(language.first, config);
  4558. if (!generatorTargetLanguageStandard) {
  4559. // If the standard isn't explicitly set we copy it over from the
  4560. // specified paired language.
  4561. std::string key =
  4562. cmStrCat(cmSystemTools::UpperCase(config), '-', language.first);
  4563. BTs<std::string> const* standardToCopy =
  4564. this->GetLanguageStandardProperty(language.second, config);
  4565. if (standardToCopy) {
  4566. this->LanguageStandardMap[key] = *standardToCopy;
  4567. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  4568. } else {
  4569. cmValue defaultStandard = this->Makefile->GetDefinition(
  4570. cmStrCat("CMAKE_", language.second, "_STANDARD_DEFAULT"));
  4571. if (defaultStandard) {
  4572. this->LanguageStandardMap[key] = BTs<std::string>(*defaultStandard);
  4573. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  4574. }
  4575. }
  4576. // Custom updates for the CUDA standard.
  4577. if (generatorTargetLanguageStandard != nullptr &&
  4578. (language.first == "CUDA")) {
  4579. if (generatorTargetLanguageStandard->Value == "98") {
  4580. this->LanguageStandardMap[key].Value = "03";
  4581. }
  4582. }
  4583. }
  4584. }
  4585. return true;
  4586. }
  4587. std::string cmGeneratorTarget::GetImportedLibName(
  4588. std::string const& config) const
  4589. {
  4590. if (cmGeneratorTarget::ImportInfo const* info =
  4591. this->GetImportInfo(config)) {
  4592. return info->LibName;
  4593. }
  4594. return std::string();
  4595. }
  4596. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  4597. cmStateEnums::ArtifactType artifact,
  4598. bool realname) const
  4599. {
  4600. if (this->IsImported()) {
  4601. return this->Target->ImportedGetFullPath(config, artifact);
  4602. }
  4603. return this->NormalGetFullPath(config, artifact, realname);
  4604. }
  4605. std::string cmGeneratorTarget::NormalGetFullPath(
  4606. const std::string& config, cmStateEnums::ArtifactType artifact,
  4607. bool realname) const
  4608. {
  4609. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  4610. if (this->IsAppBundleOnApple()) {
  4611. fpath =
  4612. cmStrCat(this->BuildBundleDirectory(fpath, config, FullLevel), '/');
  4613. }
  4614. // Add the full name of the target.
  4615. switch (artifact) {
  4616. case cmStateEnums::RuntimeBinaryArtifact:
  4617. if (realname) {
  4618. fpath += this->NormalGetRealName(config);
  4619. } else {
  4620. fpath +=
  4621. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  4622. }
  4623. break;
  4624. case cmStateEnums::ImportLibraryArtifact:
  4625. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  4626. break;
  4627. }
  4628. return fpath;
  4629. }
  4630. std::string cmGeneratorTarget::NormalGetRealName(
  4631. const std::string& config) const
  4632. {
  4633. // This should not be called for imported targets.
  4634. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4635. // enforcement of the limited imported target API.
  4636. if (this->IsImported()) {
  4637. std::string msg = cmStrCat("NormalGetRealName called on imported target: ",
  4638. this->GetName());
  4639. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4640. }
  4641. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4642. // Compute the real name that will be built.
  4643. return this->GetExecutableNames(config).Real;
  4644. }
  4645. // Compute the real name that will be built.
  4646. return this->GetLibraryNames(config).Real;
  4647. }
  4648. cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
  4649. const std::string& config) const
  4650. {
  4651. cmGeneratorTarget::Names targetNames;
  4652. // This should not be called for imported targets.
  4653. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4654. // enforcement of the limited imported target API.
  4655. if (this->IsImported()) {
  4656. std::string msg =
  4657. cmStrCat("GetLibraryNames called on imported target: ", this->GetName());
  4658. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4659. }
  4660. // Check for library version properties.
  4661. cmValue version = this->GetProperty("VERSION");
  4662. cmValue soversion = this->GetProperty("SOVERSION");
  4663. if (!this->HasSOName(config) ||
  4664. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  4665. this->IsFrameworkOnApple()) {
  4666. // Versioning is supported only for shared libraries and modules,
  4667. // and then only when the platform supports an soname flag.
  4668. version = nullptr;
  4669. soversion = nullptr;
  4670. }
  4671. if (version && !soversion) {
  4672. // The soversion must be set if the library version is set. Use
  4673. // the library version as the soversion.
  4674. soversion = version;
  4675. }
  4676. if (!version && soversion) {
  4677. // Use the soversion as the library version.
  4678. version = soversion;
  4679. }
  4680. // Get the components of the library name.
  4681. NameComponents const& components = this->GetFullNameInternalComponents(
  4682. config, cmStateEnums::RuntimeBinaryArtifact);
  4683. // The library name.
  4684. targetNames.Base = components.base;
  4685. targetNames.Output =
  4686. components.prefix + targetNames.Base + components.suffix;
  4687. if (this->IsFrameworkOnApple()) {
  4688. targetNames.Real = components.prefix;
  4689. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  4690. targetNames.Real += "Versions/";
  4691. targetNames.Real += this->GetFrameworkVersion();
  4692. targetNames.Real += "/";
  4693. }
  4694. targetNames.Real += targetNames.Base + components.suffix;
  4695. targetNames.SharedObject = targetNames.Real + components.suffix;
  4696. } else {
  4697. // The library's soname.
  4698. this->ComputeVersionedName(targetNames.SharedObject, components.prefix,
  4699. targetNames.Base, components.suffix,
  4700. targetNames.Output, soversion);
  4701. // The library's real name on disk.
  4702. this->ComputeVersionedName(targetNames.Real, components.prefix,
  4703. targetNames.Base, components.suffix,
  4704. targetNames.Output, version);
  4705. }
  4706. // The import library name.
  4707. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4708. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4709. targetNames.ImportLibrary =
  4710. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4711. }
  4712. // The program database file name.
  4713. targetNames.PDB = this->GetPDBName(config);
  4714. return targetNames;
  4715. }
  4716. cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
  4717. const std::string& config) const
  4718. {
  4719. cmGeneratorTarget::Names targetNames;
  4720. // This should not be called for imported targets.
  4721. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4722. // enforcement of the limited imported target API.
  4723. if (this->IsImported()) {
  4724. std::string msg = cmStrCat(
  4725. "GetExecutableNames called on imported target: ", this->GetName());
  4726. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4727. }
  4728. // This versioning is supported only for executables and then only
  4729. // when the platform supports symbolic links.
  4730. #if defined(_WIN32) && !defined(__CYGWIN__)
  4731. cmValue version;
  4732. #else
  4733. // Check for executable version properties.
  4734. cmValue version = this->GetProperty("VERSION");
  4735. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  4736. this->Makefile->IsOn("XCODE")) {
  4737. version = nullptr;
  4738. }
  4739. #endif
  4740. // Get the components of the executable name.
  4741. NameComponents const& components = this->GetFullNameInternalComponents(
  4742. config, cmStateEnums::RuntimeBinaryArtifact);
  4743. // The executable name.
  4744. targetNames.Base = components.base;
  4745. targetNames.Output =
  4746. components.prefix + targetNames.Base + components.suffix;
  4747. // The executable's real name on disk.
  4748. #if defined(__CYGWIN__)
  4749. targetNames.Real = components.prefix + targetNames.Base;
  4750. #else
  4751. targetNames.Real = targetNames.Output;
  4752. #endif
  4753. if (version) {
  4754. targetNames.Real += "-";
  4755. targetNames.Real += *version;
  4756. }
  4757. #if defined(__CYGWIN__)
  4758. targetNames.Real += components.suffix;
  4759. #endif
  4760. // The import library name.
  4761. targetNames.ImportLibrary =
  4762. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4763. // The program database file name.
  4764. targetNames.PDB = this->GetPDBName(config);
  4765. return targetNames;
  4766. }
  4767. std::string cmGeneratorTarget::GetFullNameInternal(
  4768. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4769. {
  4770. NameComponents const& components =
  4771. this->GetFullNameInternalComponents(config, artifact);
  4772. return components.prefix + components.base + components.suffix;
  4773. }
  4774. std::string cmGeneratorTarget::ImportedGetLocation(
  4775. const std::string& config) const
  4776. {
  4777. assert(this->IsImported());
  4778. return this->Target->ImportedGetFullPath(
  4779. config, cmStateEnums::RuntimeBinaryArtifact);
  4780. }
  4781. std::string cmGeneratorTarget::GetFullNameImported(
  4782. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4783. {
  4784. return cmSystemTools::GetFilenameName(
  4785. this->Target->ImportedGetFullPath(config, artifact));
  4786. }
  4787. cmGeneratorTarget::NameComponents const&
  4788. cmGeneratorTarget::GetFullNameInternalComponents(
  4789. std::string const& config, cmStateEnums::ArtifactType artifact) const
  4790. {
  4791. assert(artifact == cmStateEnums::RuntimeBinaryArtifact ||
  4792. artifact == cmStateEnums::ImportLibraryArtifact);
  4793. FullNameCache& cache = artifact == cmStateEnums::RuntimeBinaryArtifact
  4794. ? RuntimeBinaryFullNameCache
  4795. : ImportLibraryFullNameCache;
  4796. auto search = cache.find(config);
  4797. if (search != cache.end()) {
  4798. return search->second;
  4799. }
  4800. // Use just the target name for non-main target types.
  4801. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  4802. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4803. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4804. this->GetType() != cmStateEnums::EXECUTABLE) {
  4805. NameComponents components;
  4806. components.base = this->GetName();
  4807. return cache.emplace(config, std::move(components)).first->second;
  4808. }
  4809. const bool isImportedLibraryArtifact =
  4810. (artifact == cmStateEnums::ImportLibraryArtifact);
  4811. // Return an empty name for the import library if this platform
  4812. // does not support import libraries.
  4813. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  4814. return cache.emplace(config, NameComponents()).first->second;
  4815. }
  4816. NameComponents parts;
  4817. std::string& outPrefix = parts.prefix;
  4818. std::string& outBase = parts.base;
  4819. std::string& outSuffix = parts.suffix;
  4820. // retrieve prefix and suffix
  4821. std::string ll = this->GetLinkerLanguage(config);
  4822. cmValue targetPrefix = this->GetFilePrefixInternal(config, artifact, ll);
  4823. cmValue targetSuffix = this->GetFileSuffixInternal(config, artifact, ll);
  4824. // The implib option is only allowed for shared libraries, module
  4825. // libraries, and executables.
  4826. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4827. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4828. this->GetType() != cmStateEnums::EXECUTABLE) {
  4829. artifact = cmStateEnums::RuntimeBinaryArtifact;
  4830. }
  4831. // Compute the full name for main target types.
  4832. const std::string configPostfix = this->GetFilePostfix(config);
  4833. // frameworks have directory prefix but no suffix
  4834. std::string fw_prefix;
  4835. if (this->IsFrameworkOnApple()) {
  4836. fw_prefix =
  4837. cmStrCat(this->GetFrameworkDirectory(config, ContentLevel), '/');
  4838. targetPrefix = cmValue(fw_prefix);
  4839. targetSuffix = nullptr;
  4840. }
  4841. if (this->IsCFBundleOnApple()) {
  4842. fw_prefix = cmStrCat(this->GetCFBundleDirectory(config, FullLevel), '/');
  4843. targetPrefix = cmValue(fw_prefix);
  4844. targetSuffix = nullptr;
  4845. }
  4846. // Begin the final name with the prefix.
  4847. outPrefix = targetPrefix ? *targetPrefix : "";
  4848. // Append the target name or property-specified name.
  4849. outBase += this->GetOutputName(config, artifact);
  4850. // Append the per-configuration postfix.
  4851. // When using Xcode, the postfix should be part of the suffix rather than
  4852. // the base, because the suffix ends up being used in Xcode's
  4853. // EXECUTABLE_SUFFIX attribute.
  4854. if (this->IsFrameworkOnApple() &&
  4855. this->GetGlobalGenerator()->GetName() == "Xcode") {
  4856. targetSuffix = cmValue(configPostfix);
  4857. } else {
  4858. outBase += configPostfix;
  4859. }
  4860. // Name shared libraries with their version number on some platforms.
  4861. if (cmValue soversion = this->GetProperty("SOVERSION")) {
  4862. cmValue dllProp;
  4863. if (this->IsDLLPlatform()) {
  4864. dllProp = this->GetProperty("DLL_NAME_WITH_SOVERSION");
  4865. }
  4866. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  4867. !isImportedLibraryArtifact &&
  4868. (dllProp.IsOn() ||
  4869. (!dllProp.IsSet() &&
  4870. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")))) {
  4871. outBase += "-";
  4872. outBase += *soversion;
  4873. }
  4874. }
  4875. // Append the suffix.
  4876. outSuffix = targetSuffix ? *targetSuffix : "";
  4877. return cache.emplace(config, std::move(parts)).first->second;
  4878. }
  4879. std::string cmGeneratorTarget::GetLinkerLanguage(
  4880. const std::string& config) const
  4881. {
  4882. return this->GetLinkClosure(config)->LinkerLanguage;
  4883. }
  4884. std::string cmGeneratorTarget::GetPDBOutputName(
  4885. const std::string& config) const
  4886. {
  4887. std::string base =
  4888. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  4889. std::vector<std::string> props;
  4890. std::string configUpper = cmSystemTools::UpperCase(config);
  4891. if (!configUpper.empty()) {
  4892. // PDB_NAME_<CONFIG>
  4893. props.push_back("PDB_NAME_" + configUpper);
  4894. }
  4895. // PDB_NAME
  4896. props.emplace_back("PDB_NAME");
  4897. for (std::string const& p : props) {
  4898. if (cmValue outName = this->GetProperty(p)) {
  4899. base = *outName;
  4900. break;
  4901. }
  4902. }
  4903. return base;
  4904. }
  4905. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  4906. {
  4907. NameComponents const& parts = this->GetFullNameInternalComponents(
  4908. config, cmStateEnums::RuntimeBinaryArtifact);
  4909. std::vector<std::string> props;
  4910. std::string configUpper = cmSystemTools::UpperCase(config);
  4911. if (!configUpper.empty()) {
  4912. // PDB_NAME_<CONFIG>
  4913. props.push_back("PDB_NAME_" + configUpper);
  4914. }
  4915. // PDB_NAME
  4916. props.emplace_back("PDB_NAME");
  4917. for (std::string const& p : props) {
  4918. if (cmValue outName = this->GetProperty(p)) {
  4919. return parts.prefix + *outName + ".pdb";
  4920. }
  4921. }
  4922. return parts.prefix + parts.base + ".pdb";
  4923. }
  4924. std::string cmGeneratorTarget::GetObjectDirectory(
  4925. std::string const& config) const
  4926. {
  4927. std::string obj_dir =
  4928. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  4929. #if defined(__APPLE__)
  4930. // Replace Xcode's placeholder for the object file directory since
  4931. // installation and export scripts need to know the real directory.
  4932. // Xcode has build-time settings (e.g. for sanitizers) that affect this,
  4933. // but we use the default here. Users that want to enable sanitizers
  4934. // will do so at the cost of object library installation and export.
  4935. cmSystemTools::ReplaceString(obj_dir, "$(OBJECT_FILE_DIR_normal:base)",
  4936. "Objects-normal");
  4937. #endif
  4938. return obj_dir;
  4939. }
  4940. void cmGeneratorTarget::GetTargetObjectNames(
  4941. std::string const& config, std::vector<std::string>& objects) const
  4942. {
  4943. std::vector<cmSourceFile const*> objectSources;
  4944. this->GetObjectSources(objectSources, config);
  4945. std::map<cmSourceFile const*, std::string> mapping;
  4946. for (cmSourceFile const* sf : objectSources) {
  4947. mapping[sf];
  4948. }
  4949. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  4950. for (cmSourceFile const* src : objectSources) {
  4951. // Find the object file name corresponding to this source file.
  4952. auto map_it = mapping.find(src);
  4953. // It must exist because we populated the mapping just above.
  4954. assert(!map_it->second.empty());
  4955. objects.push_back(map_it->second);
  4956. }
  4957. // We need to compute the relative path from the root of
  4958. // of the object directory to handle subdirectory paths
  4959. std::string rootObjectDir = this->GetObjectDirectory(config);
  4960. rootObjectDir = cmSystemTools::CollapseFullPath(rootObjectDir);
  4961. auto ispcObjects = this->GetGeneratedISPCObjects(config);
  4962. for (std::string const& output : ispcObjects) {
  4963. auto relativePathFromObjectDir = output.substr(rootObjectDir.size());
  4964. objects.push_back(relativePathFromObjectDir);
  4965. }
  4966. }
  4967. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  4968. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  4969. {
  4970. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  4971. if (nameResult == 0) {
  4972. return strcmp(
  4973. t1->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str(),
  4974. t2->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str()) < 0;
  4975. }
  4976. return nameResult < 0;
  4977. }
  4978. struct cmGeneratorTarget::SourceFileFlags
  4979. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  4980. {
  4981. struct SourceFileFlags flags;
  4982. this->ConstructSourceFileFlags();
  4983. auto si = this->SourceFlagsMap.find(sf);
  4984. if (si != this->SourceFlagsMap.end()) {
  4985. flags = si->second;
  4986. } else {
  4987. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  4988. // were not listed in one of the other lists.
  4989. if (cmValue location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  4990. flags.MacFolder = location->c_str();
  4991. const bool stripResources =
  4992. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  4993. if (*location == "Resources") {
  4994. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4995. if (stripResources) {
  4996. flags.MacFolder = "";
  4997. }
  4998. } else if (cmHasLiteralPrefix(*location, "Resources/")) {
  4999. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  5000. if (stripResources) {
  5001. flags.MacFolder += cmStrLen("Resources/");
  5002. }
  5003. } else {
  5004. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  5005. }
  5006. }
  5007. }
  5008. return flags;
  5009. }
  5010. void cmGeneratorTarget::ConstructSourceFileFlags() const
  5011. {
  5012. if (this->SourceFileFlagsConstructed) {
  5013. return;
  5014. }
  5015. this->SourceFileFlagsConstructed = true;
  5016. // Process public headers to mark the source files.
  5017. if (cmValue files = this->GetProperty("PUBLIC_HEADER")) {
  5018. std::vector<std::string> relFiles = cmExpandedList(*files);
  5019. for (std::string const& relFile : relFiles) {
  5020. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  5021. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  5022. flags.MacFolder = "Headers";
  5023. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  5024. }
  5025. }
  5026. }
  5027. // Process private headers after public headers so that they take
  5028. // precedence if a file is listed in both.
  5029. if (cmValue files = this->GetProperty("PRIVATE_HEADER")) {
  5030. std::vector<std::string> relFiles = cmExpandedList(*files);
  5031. for (std::string const& relFile : relFiles) {
  5032. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  5033. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  5034. flags.MacFolder = "PrivateHeaders";
  5035. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  5036. }
  5037. }
  5038. }
  5039. // Mark sources listed as resources.
  5040. if (cmValue files = this->GetProperty("RESOURCE")) {
  5041. std::vector<std::string> relFiles = cmExpandedList(*files);
  5042. for (std::string const& relFile : relFiles) {
  5043. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  5044. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  5045. flags.MacFolder = "";
  5046. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  5047. flags.MacFolder = "Resources";
  5048. }
  5049. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  5050. }
  5051. }
  5052. }
  5053. }
  5054. const cmGeneratorTarget::CompatibleInterfacesBase&
  5055. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  5056. {
  5057. cmGeneratorTarget::CompatibleInterfaces& compat =
  5058. this->CompatibleInterfacesMap[config];
  5059. if (!compat.Done) {
  5060. compat.Done = true;
  5061. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  5062. compat.PropsString.insert("AUTOUIC_OPTIONS");
  5063. std::vector<cmGeneratorTarget const*> const& deps =
  5064. this->GetLinkImplementationClosure(config);
  5065. for (cmGeneratorTarget const* li : deps) {
  5066. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  5067. if (cmValue prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  5068. std::vector<std::string> props; \
  5069. cmExpandList(*prop, props); \
  5070. compat.Props##x.insert(props.begin(), props.end()); \
  5071. }
  5072. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  5073. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  5074. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  5075. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  5076. #undef CM_READ_COMPATIBLE_INTERFACE
  5077. }
  5078. }
  5079. return compat;
  5080. }
  5081. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  5082. const std::string& p, const std::string& config) const
  5083. {
  5084. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5085. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5086. return false;
  5087. }
  5088. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  5089. }
  5090. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  5091. const std::string& p, const std::string& config) const
  5092. {
  5093. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5094. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5095. return false;
  5096. }
  5097. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  5098. }
  5099. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  5100. const std::string& p, const std::string& config) const
  5101. {
  5102. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5103. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5104. return false;
  5105. }
  5106. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  5107. }
  5108. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  5109. const std::string& p, const std::string& config) const
  5110. {
  5111. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5112. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5113. return false;
  5114. }
  5115. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  5116. }
  5117. enum CompatibleType
  5118. {
  5119. BoolType,
  5120. StringType,
  5121. NumberMinType,
  5122. NumberMaxType
  5123. };
  5124. template <typename PropertyType>
  5125. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  5126. const std::string& prop,
  5127. const std::string& config,
  5128. CompatibleType, PropertyType*);
  5129. template <>
  5130. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  5131. const std::string& prop,
  5132. const std::string& config,
  5133. CompatibleType /*unused*/,
  5134. bool* /*unused*/)
  5135. {
  5136. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  5137. }
  5138. template <>
  5139. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  5140. const std::string& prop,
  5141. const std::string& config,
  5142. CompatibleType t,
  5143. const char** /*unused*/)
  5144. {
  5145. switch (t) {
  5146. case BoolType:
  5147. assert(false &&
  5148. "String compatibility check function called for boolean");
  5149. return nullptr;
  5150. case StringType:
  5151. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  5152. case NumberMinType:
  5153. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  5154. case NumberMaxType:
  5155. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  5156. }
  5157. assert(false && "Unreachable!");
  5158. return nullptr;
  5159. }
  5160. template <typename PropertyType>
  5161. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  5162. cmGeneratorTarget const* dependee,
  5163. const std::string& propName,
  5164. std::set<std::string>& emitted,
  5165. const std::string& config, CompatibleType t,
  5166. PropertyType* /*unused*/)
  5167. {
  5168. cmValue prop = dependee->GetProperty(propName);
  5169. if (!prop) {
  5170. return;
  5171. }
  5172. std::vector<std::string> props = cmExpandedList(*prop);
  5173. std::string pdir =
  5174. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Help/prop_tgt/");
  5175. for (std::string const& p : props) {
  5176. std::string pname = cmSystemTools::HelpFileName(p);
  5177. std::string pfile = pdir + pname + ".rst";
  5178. if (cmSystemTools::FileExists(pfile, true)) {
  5179. std::ostringstream e;
  5180. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  5181. << "\" listed in its " << propName
  5182. << " property. "
  5183. "This is not allowed. Only user-defined properties may appear "
  5184. "listed in the "
  5185. << propName << " property.";
  5186. depender->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  5187. e.str());
  5188. return;
  5189. }
  5190. if (emitted.insert(p).second) {
  5191. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  5192. nullptr);
  5193. if (cmSystemTools::GetErrorOccurredFlag()) {
  5194. return;
  5195. }
  5196. }
  5197. }
  5198. }
  5199. namespace {
  5200. std::string intersect(const std::set<std::string>& s1,
  5201. const std::set<std::string>& s2)
  5202. {
  5203. std::set<std::string> intersect;
  5204. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  5205. std::inserter(intersect, intersect.begin()));
  5206. if (!intersect.empty()) {
  5207. return *intersect.begin();
  5208. }
  5209. return "";
  5210. }
  5211. std::string intersect(const std::set<std::string>& s1,
  5212. const std::set<std::string>& s2,
  5213. const std::set<std::string>& s3)
  5214. {
  5215. std::string result;
  5216. result = intersect(s1, s2);
  5217. if (!result.empty()) {
  5218. return result;
  5219. }
  5220. result = intersect(s1, s3);
  5221. if (!result.empty()) {
  5222. return result;
  5223. }
  5224. return intersect(s2, s3);
  5225. }
  5226. std::string intersect(const std::set<std::string>& s1,
  5227. const std::set<std::string>& s2,
  5228. const std::set<std::string>& s3,
  5229. const std::set<std::string>& s4)
  5230. {
  5231. std::string result;
  5232. result = intersect(s1, s2);
  5233. if (!result.empty()) {
  5234. return result;
  5235. }
  5236. result = intersect(s1, s3);
  5237. if (!result.empty()) {
  5238. return result;
  5239. }
  5240. result = intersect(s1, s4);
  5241. if (!result.empty()) {
  5242. return result;
  5243. }
  5244. return intersect(s2, s3, s4);
  5245. }
  5246. }
  5247. void cmGeneratorTarget::CheckPropertyCompatibility(
  5248. cmComputeLinkInformation& info, const std::string& config) const
  5249. {
  5250. const cmComputeLinkInformation::ItemVector& deps = info.GetItems();
  5251. std::set<std::string> emittedBools;
  5252. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  5253. std::set<std::string> emittedStrings;
  5254. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  5255. std::set<std::string> emittedMinNumbers;
  5256. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  5257. std::set<std::string> emittedMaxNumbers;
  5258. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  5259. for (auto const& dep : deps) {
  5260. if (!dep.Target) {
  5261. continue;
  5262. }
  5263. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  5264. config, BoolType, nullptr);
  5265. if (cmSystemTools::GetErrorOccurredFlag()) {
  5266. return;
  5267. }
  5268. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  5269. emittedStrings, config, StringType,
  5270. nullptr);
  5271. if (cmSystemTools::GetErrorOccurredFlag()) {
  5272. return;
  5273. }
  5274. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  5275. emittedMinNumbers, config,
  5276. NumberMinType, nullptr);
  5277. if (cmSystemTools::GetErrorOccurredFlag()) {
  5278. return;
  5279. }
  5280. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  5281. emittedMaxNumbers, config,
  5282. NumberMaxType, nullptr);
  5283. if (cmSystemTools::GetErrorOccurredFlag()) {
  5284. return;
  5285. }
  5286. }
  5287. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  5288. emittedMaxNumbers);
  5289. if (!prop.empty()) {
  5290. // Use a sorted std::vector to keep the error message sorted.
  5291. std::vector<std::string> props;
  5292. auto i = emittedBools.find(prop);
  5293. if (i != emittedBools.end()) {
  5294. props.push_back(strBool);
  5295. }
  5296. i = emittedStrings.find(prop);
  5297. if (i != emittedStrings.end()) {
  5298. props.push_back(strString);
  5299. }
  5300. i = emittedMinNumbers.find(prop);
  5301. if (i != emittedMinNumbers.end()) {
  5302. props.push_back(strNumMin);
  5303. }
  5304. i = emittedMaxNumbers.find(prop);
  5305. if (i != emittedMaxNumbers.end()) {
  5306. props.push_back(strNumMax);
  5307. }
  5308. std::sort(props.begin(), props.end());
  5309. std::string propsString = cmStrCat(
  5310. cmJoin(cmMakeRange(props).retreat(1), ", "), " and the ", props.back());
  5311. std::ostringstream e;
  5312. e << "Property \"" << prop << "\" appears in both the " << propsString
  5313. << " property in the dependencies of target \"" << this->GetName()
  5314. << "\". This is not allowed. A property may only require "
  5315. "compatibility "
  5316. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  5317. "or a "
  5318. "string interpretation, but not a mixture.";
  5319. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  5320. }
  5321. }
  5322. template <typename PropertyType>
  5323. std::string valueAsString(PropertyType);
  5324. template <>
  5325. std::string valueAsString<bool>(bool value)
  5326. {
  5327. return value ? "TRUE" : "FALSE";
  5328. }
  5329. template <>
  5330. std::string valueAsString<const char*>(const char* value)
  5331. {
  5332. return value ? value : "(unset)";
  5333. }
  5334. template <>
  5335. std::string valueAsString<std::string>(std::string value)
  5336. {
  5337. return value;
  5338. }
  5339. template <>
  5340. std::string valueAsString<cmValue>(cmValue value)
  5341. {
  5342. return value ? *value : std::string("(unset)");
  5343. }
  5344. template <>
  5345. std::string valueAsString<std::nullptr_t>(std::nullptr_t /*unused*/)
  5346. {
  5347. return "(unset)";
  5348. }
  5349. static std::string compatibilityType(CompatibleType t)
  5350. {
  5351. switch (t) {
  5352. case BoolType:
  5353. return "Boolean compatibility";
  5354. case StringType:
  5355. return "String compatibility";
  5356. case NumberMaxType:
  5357. return "Numeric maximum compatibility";
  5358. case NumberMinType:
  5359. return "Numeric minimum compatibility";
  5360. }
  5361. assert(false && "Unreachable!");
  5362. return "";
  5363. }
  5364. static std::string compatibilityAgree(CompatibleType t, bool dominant)
  5365. {
  5366. switch (t) {
  5367. case BoolType:
  5368. case StringType:
  5369. return dominant ? "(Disagree)\n" : "(Agree)\n";
  5370. case NumberMaxType:
  5371. case NumberMinType:
  5372. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  5373. }
  5374. assert(false && "Unreachable!");
  5375. return "";
  5376. }
  5377. template <typename PropertyType>
  5378. PropertyType getTypedProperty(
  5379. cmGeneratorTarget const* tgt, const std::string& prop,
  5380. cmGeneratorExpressionInterpreter* genexInterpreter = nullptr);
  5381. template <>
  5382. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  5383. const std::string& prop,
  5384. cmGeneratorExpressionInterpreter* genexInterpreter)
  5385. {
  5386. if (genexInterpreter == nullptr) {
  5387. return tgt->GetPropertyAsBool(prop);
  5388. }
  5389. cmValue value = tgt->GetProperty(prop);
  5390. return cmIsOn(genexInterpreter->Evaluate(value ? *value : "", prop));
  5391. }
  5392. template <>
  5393. const char* getTypedProperty<const char*>(
  5394. cmGeneratorTarget const* tgt, const std::string& prop,
  5395. cmGeneratorExpressionInterpreter* genexInterpreter)
  5396. {
  5397. cmValue value = tgt->GetProperty(prop);
  5398. if (genexInterpreter == nullptr) {
  5399. return value.GetCStr();
  5400. }
  5401. return genexInterpreter->Evaluate(value ? *value : "", prop).c_str();
  5402. }
  5403. template <>
  5404. std::string getTypedProperty<std::string>(
  5405. cmGeneratorTarget const* tgt, const std::string& prop,
  5406. cmGeneratorExpressionInterpreter* genexInterpreter)
  5407. {
  5408. cmValue value = tgt->GetProperty(prop);
  5409. if (genexInterpreter == nullptr) {
  5410. return valueAsString(value);
  5411. }
  5412. return genexInterpreter->Evaluate(value ? *value : "", prop);
  5413. }
  5414. template <typename PropertyType>
  5415. PropertyType impliedValue(PropertyType);
  5416. template <>
  5417. bool impliedValue<bool>(bool /*unused*/)
  5418. {
  5419. return false;
  5420. }
  5421. template <>
  5422. const char* impliedValue<const char*>(const char* /*unused*/)
  5423. {
  5424. return "";
  5425. }
  5426. template <>
  5427. std::string impliedValue<std::string>(std::string /*unused*/) // NOLINT(*)
  5428. {
  5429. return std::string();
  5430. }
  5431. template <typename PropertyType>
  5432. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  5433. PropertyType rhs,
  5434. CompatibleType t);
  5435. template <>
  5436. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  5437. CompatibleType /*unused*/)
  5438. {
  5439. return { lhs == rhs, lhs };
  5440. }
  5441. static std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  5442. const char* rhs)
  5443. {
  5444. const bool b = strcmp(lhs, rhs) == 0;
  5445. return { b, b ? lhs : nullptr };
  5446. }
  5447. static std::pair<bool, std::string> consistentStringProperty(
  5448. const std::string& lhs, const std::string& rhs)
  5449. {
  5450. const bool b = lhs == rhs;
  5451. return { b, b ? lhs : valueAsString(nullptr) };
  5452. }
  5453. static std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  5454. const char* rhs,
  5455. CompatibleType t)
  5456. {
  5457. char* pEnd;
  5458. long lnum = strtol(lhs, &pEnd, 0);
  5459. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  5460. return { false, nullptr };
  5461. }
  5462. long rnum = strtol(rhs, &pEnd, 0);
  5463. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  5464. return { false, nullptr };
  5465. }
  5466. if (t == NumberMaxType) {
  5467. return { true, std::max(lnum, rnum) == lnum ? lhs : rhs };
  5468. }
  5469. return { true, std::min(lnum, rnum) == lnum ? lhs : rhs };
  5470. }
  5471. template <>
  5472. std::pair<bool, const char*> consistentProperty(const char* lhs,
  5473. const char* rhs,
  5474. CompatibleType t)
  5475. {
  5476. if (!lhs && !rhs) {
  5477. return { true, lhs };
  5478. }
  5479. if (!lhs) {
  5480. return { true, rhs };
  5481. }
  5482. if (!rhs) {
  5483. return { true, lhs };
  5484. }
  5485. switch (t) {
  5486. case BoolType: {
  5487. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  5488. return { same, same ? lhs : nullptr };
  5489. }
  5490. case StringType:
  5491. return consistentStringProperty(lhs, rhs);
  5492. case NumberMinType:
  5493. case NumberMaxType:
  5494. return consistentNumberProperty(lhs, rhs, t);
  5495. }
  5496. assert(false && "Unreachable!");
  5497. return { false, nullptr };
  5498. }
  5499. static std::pair<bool, std::string> consistentProperty(const std::string& lhs,
  5500. const std::string& rhs,
  5501. CompatibleType t)
  5502. {
  5503. const std::string null_ptr = valueAsString(nullptr);
  5504. if (lhs == null_ptr && rhs == null_ptr) {
  5505. return { true, lhs };
  5506. }
  5507. if (lhs == null_ptr) {
  5508. return { true, rhs };
  5509. }
  5510. if (rhs == null_ptr) {
  5511. return { true, lhs };
  5512. }
  5513. switch (t) {
  5514. case BoolType: {
  5515. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  5516. return { same, same ? lhs : null_ptr };
  5517. }
  5518. case StringType:
  5519. return consistentStringProperty(lhs, rhs);
  5520. case NumberMinType:
  5521. case NumberMaxType: {
  5522. auto value = consistentNumberProperty(lhs.c_str(), rhs.c_str(), t);
  5523. return { value.first,
  5524. value.first ? std::string(value.second) : null_ptr };
  5525. }
  5526. }
  5527. assert(false && "Unreachable!");
  5528. return { false, null_ptr };
  5529. }
  5530. template <typename PropertyType>
  5531. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  5532. const std::string& p,
  5533. const std::string& config,
  5534. const char* defaultValue,
  5535. CompatibleType t,
  5536. PropertyType* /*unused*/)
  5537. {
  5538. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  5539. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  5540. const bool explicitlySet = cm::contains(headPropKeys, p);
  5541. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  5542. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  5543. std::vector<cmGeneratorTarget const*> const& deps =
  5544. tgt->GetLinkImplementationClosure(config);
  5545. if (deps.empty()) {
  5546. return propContent;
  5547. }
  5548. bool propInitialized = explicitlySet;
  5549. std::string report = cmStrCat(" * Target \"", tgt->GetName());
  5550. if (explicitlySet) {
  5551. report += "\" has property content \"";
  5552. report += valueAsString<PropertyType>(propContent);
  5553. report += "\"\n";
  5554. } else if (impliedByUse) {
  5555. report += "\" property is implied by use.\n";
  5556. } else {
  5557. report += "\" property not set.\n";
  5558. }
  5559. std::string interfaceProperty = "INTERFACE_" + p;
  5560. std::unique_ptr<cmGeneratorExpressionInterpreter> genexInterpreter;
  5561. if (p == "POSITION_INDEPENDENT_CODE") {
  5562. genexInterpreter = cm::make_unique<cmGeneratorExpressionInterpreter>(
  5563. tgt->GetLocalGenerator(), config, tgt);
  5564. }
  5565. for (cmGeneratorTarget const* theTarget : deps) {
  5566. // An error should be reported if one dependency
  5567. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  5568. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  5569. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  5570. // with a dependency.
  5571. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  5572. const bool ifaceIsSet = cm::contains(propKeys, interfaceProperty);
  5573. PropertyType ifacePropContent = getTypedProperty<PropertyType>(
  5574. theTarget, interfaceProperty, genexInterpreter.get());
  5575. std::string reportEntry;
  5576. if (ifaceIsSet) {
  5577. reportEntry += " * Target \"";
  5578. reportEntry += theTarget->GetName();
  5579. reportEntry += "\" property value \"";
  5580. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  5581. reportEntry += "\" ";
  5582. }
  5583. if (explicitlySet) {
  5584. if (ifaceIsSet) {
  5585. std::pair<bool, PropertyType> consistent =
  5586. consistentProperty(propContent, ifacePropContent, t);
  5587. report += reportEntry;
  5588. report += compatibilityAgree(t, propContent != consistent.second);
  5589. if (!consistent.first) {
  5590. std::ostringstream e;
  5591. e << "Property " << p << " on target \"" << tgt->GetName()
  5592. << "\" does\nnot match the "
  5593. "INTERFACE_"
  5594. << p
  5595. << " property requirement\nof "
  5596. "dependency \""
  5597. << theTarget->GetName() << "\".\n";
  5598. cmSystemTools::Error(e.str());
  5599. break;
  5600. }
  5601. propContent = consistent.second;
  5602. continue;
  5603. }
  5604. // Explicitly set on target and not set in iface. Can't disagree.
  5605. continue;
  5606. }
  5607. if (impliedByUse) {
  5608. propContent = impliedValue<PropertyType>(propContent);
  5609. if (ifaceIsSet) {
  5610. std::pair<bool, PropertyType> consistent =
  5611. consistentProperty(propContent, ifacePropContent, t);
  5612. report += reportEntry;
  5613. report += compatibilityAgree(t, propContent != consistent.second);
  5614. if (!consistent.first) {
  5615. std::ostringstream e;
  5616. e << "Property " << p << " on target \"" << tgt->GetName()
  5617. << "\" is\nimplied to be " << defaultValue
  5618. << " because it was used to determine the link libraries\n"
  5619. "already. The INTERFACE_"
  5620. << p << " property on\ndependency \"" << theTarget->GetName()
  5621. << "\" is in conflict.\n";
  5622. cmSystemTools::Error(e.str());
  5623. break;
  5624. }
  5625. propContent = consistent.second;
  5626. continue;
  5627. }
  5628. // Implicitly set on target and not set in iface. Can't disagree.
  5629. continue;
  5630. }
  5631. if (ifaceIsSet) {
  5632. if (propInitialized) {
  5633. std::pair<bool, PropertyType> consistent =
  5634. consistentProperty(propContent, ifacePropContent, t);
  5635. report += reportEntry;
  5636. report += compatibilityAgree(t, propContent != consistent.second);
  5637. if (!consistent.first) {
  5638. std::ostringstream e;
  5639. e << "The INTERFACE_" << p << " property of \""
  5640. << theTarget->GetName() << "\" does\nnot agree with the value of "
  5641. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  5642. cmSystemTools::Error(e.str());
  5643. break;
  5644. }
  5645. propContent = consistent.second;
  5646. continue;
  5647. }
  5648. report += reportEntry + "(Interface set)\n";
  5649. propContent = ifacePropContent;
  5650. propInitialized = true;
  5651. } else {
  5652. // Not set. Nothing to agree on.
  5653. continue;
  5654. }
  5655. }
  5656. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  5657. report, compatibilityType(t));
  5658. return propContent;
  5659. }
  5660. bool cmGeneratorTarget::SetDeviceLink(bool deviceLink)
  5661. {
  5662. bool previous = this->DeviceLink;
  5663. this->DeviceLink = deviceLink;
  5664. return previous;
  5665. }
  5666. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  5667. const std::string& p, const std::string& config) const
  5668. {
  5669. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  5670. BoolType, nullptr);
  5671. }
  5672. std::string cmGeneratorTarget::GetLinkInterfaceDependentStringAsBoolProperty(
  5673. const std::string& p, const std::string& config) const
  5674. {
  5675. return checkInterfacePropertyCompatibility<std::string>(
  5676. this, p, config, "FALSE", BoolType, nullptr);
  5677. }
  5678. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  5679. const std::string& p, const std::string& config) const
  5680. {
  5681. return checkInterfacePropertyCompatibility<const char*>(
  5682. this, p, config, "empty", StringType, nullptr);
  5683. }
  5684. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  5685. const std::string& p, const std::string& config) const
  5686. {
  5687. return checkInterfacePropertyCompatibility<const char*>(
  5688. this, p, config, "empty", NumberMinType, nullptr);
  5689. }
  5690. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  5691. const std::string& p, const std::string& config) const
  5692. {
  5693. return checkInterfacePropertyCompatibility<const char*>(
  5694. this, p, config, "empty", NumberMaxType, nullptr);
  5695. }
  5696. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  5697. const std::string& config) const
  5698. {
  5699. // Lookup any existing information for this configuration.
  5700. std::string key(cmSystemTools::UpperCase(config));
  5701. auto i = this->LinkInformation.find(key);
  5702. if (i == this->LinkInformation.end()) {
  5703. // Compute information for this configuration.
  5704. auto info = cm::make_unique<cmComputeLinkInformation>(this, config);
  5705. if (info && !info->Compute()) {
  5706. info.reset();
  5707. }
  5708. // Store the information for this configuration.
  5709. i = this->LinkInformation.emplace(key, std::move(info)).first;
  5710. if (i->second) {
  5711. this->CheckPropertyCompatibility(*i->second, config);
  5712. }
  5713. }
  5714. return i->second.get();
  5715. }
  5716. void cmGeneratorTarget::CheckLinkLibraries() const
  5717. {
  5718. bool linkLibrariesOnlyTargets =
  5719. this->GetPropertyAsBool("LINK_LIBRARIES_ONLY_TARGETS");
  5720. // Evaluate the link interface of this target if needed for extra checks.
  5721. if (linkLibrariesOnlyTargets) {
  5722. std::vector<std::string> const& configs =
  5723. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  5724. for (std::string const& config : configs) {
  5725. this->GetLinkInterfaceLibraries(config, this, LinkInterfaceFor::Link);
  5726. }
  5727. }
  5728. // Check link the implementation for each generated configuration.
  5729. for (auto const& hmp : this->LinkImplMap) {
  5730. HeadToLinkImplementationMap const& hm = hmp.second;
  5731. // There could be several entries used when computing the pre-CMP0022
  5732. // default link interface. Check only the entry for our own link impl.
  5733. auto const hmi = hm.find(this);
  5734. if (hmi == hm.end() || !hmi->second.LibrariesDone) {
  5735. continue;
  5736. }
  5737. for (cmLinkImplItem const& item : hmi->second.Libraries) {
  5738. if (!this->VerifyLinkItemColons(LinkItemRole::Implementation, item)) {
  5739. return;
  5740. }
  5741. if (linkLibrariesOnlyTargets &&
  5742. !this->VerifyLinkItemIsTarget(LinkItemRole::Implementation, item)) {
  5743. return;
  5744. }
  5745. }
  5746. }
  5747. // Check link the interface for each generated combination of
  5748. // configuration and consuming head target. We should not need to
  5749. // consider LinkInterfaceUsageRequirementsOnlyMap because its entries
  5750. // should be a subset of LinkInterfaceMap (with LINK_ONLY left out).
  5751. for (auto const& hmp : this->LinkInterfaceMap) {
  5752. for (auto const& hmi : hmp.second) {
  5753. if (!hmi.second.LibrariesDone) {
  5754. continue;
  5755. }
  5756. for (cmLinkItem const& item : hmi.second.Libraries) {
  5757. if (!this->VerifyLinkItemColons(LinkItemRole::Interface, item)) {
  5758. return;
  5759. }
  5760. if (linkLibrariesOnlyTargets &&
  5761. !this->VerifyLinkItemIsTarget(LinkItemRole::Interface, item)) {
  5762. return;
  5763. }
  5764. }
  5765. }
  5766. }
  5767. }
  5768. namespace {
  5769. cm::string_view missingTargetPossibleReasons =
  5770. "Possible reasons include:\n"
  5771. " * There is a typo in the target name.\n"
  5772. " * A find_package call is missing for an IMPORTED target.\n"
  5773. " * An ALIAS target is missing.\n"_s;
  5774. }
  5775. bool cmGeneratorTarget::VerifyLinkItemColons(LinkItemRole role,
  5776. cmLinkItem const& item) const
  5777. {
  5778. if (item.Target || cmHasPrefix(item.AsStr(), "<LINK_GROUP:"_s) ||
  5779. item.AsStr().find("::") == std::string::npos) {
  5780. return true;
  5781. }
  5782. MessageType messageType = MessageType::FATAL_ERROR;
  5783. std::string e;
  5784. switch (this->GetLocalGenerator()->GetPolicyStatus(cmPolicies::CMP0028)) {
  5785. case cmPolicies::WARN: {
  5786. e = cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0028), "\n");
  5787. messageType = MessageType::AUTHOR_WARNING;
  5788. } break;
  5789. case cmPolicies::OLD:
  5790. return true;
  5791. case cmPolicies::REQUIRED_IF_USED:
  5792. case cmPolicies::REQUIRED_ALWAYS:
  5793. case cmPolicies::NEW:
  5794. // Issue the fatal message.
  5795. break;
  5796. }
  5797. if (role == LinkItemRole::Implementation) {
  5798. e = cmStrCat(e, "Target \"", this->GetName(), "\" links to");
  5799. } else {
  5800. e = cmStrCat(e, "The link interface of target \"", this->GetName(),
  5801. "\" contains");
  5802. }
  5803. e =
  5804. cmStrCat(e, ":\n ", item.AsStr(), "\n", "but the target was not found. ",
  5805. missingTargetPossibleReasons);
  5806. cmListFileBacktrace backtrace = item.Backtrace;
  5807. if (backtrace.Empty()) {
  5808. backtrace = this->GetBacktrace();
  5809. }
  5810. this->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(messageType, e,
  5811. backtrace);
  5812. return false;
  5813. }
  5814. bool cmGeneratorTarget::VerifyLinkItemIsTarget(LinkItemRole role,
  5815. cmLinkItem const& item) const
  5816. {
  5817. if (item.Target) {
  5818. return true;
  5819. }
  5820. std::string const& str = item.AsStr();
  5821. if (!str.empty() &&
  5822. (str[0] == '-' || str[0] == '$' || str[0] == '`' ||
  5823. str.find_first_of("/\\") != std::string::npos ||
  5824. cmHasPrefix(str, "<LINK_LIBRARY:"_s) ||
  5825. cmHasPrefix(str, "<LINK_GROUP:"_s))) {
  5826. return true;
  5827. }
  5828. std::string e = cmStrCat("Target \"", this->GetName(),
  5829. "\" has LINK_LIBRARIES_ONLY_TARGETS enabled, but ",
  5830. role == LinkItemRole::Implementation
  5831. ? "it links to"
  5832. : "its link interface contains",
  5833. ":\n ", item.AsStr(), "\nwhich is not a target. ",
  5834. missingTargetPossibleReasons);
  5835. cmListFileBacktrace backtrace = item.Backtrace;
  5836. if (backtrace.Empty()) {
  5837. backtrace = this->GetBacktrace();
  5838. }
  5839. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  5840. MessageType::FATAL_ERROR, e, backtrace);
  5841. return false;
  5842. }
  5843. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  5844. {
  5845. int patch;
  5846. this->GetTargetVersion("VERSION", major, minor, patch);
  5847. }
  5848. void cmGeneratorTarget::GetTargetVersionFallback(
  5849. const std::string& property, const std::string& fallback_property,
  5850. int& major, int& minor, int& patch) const
  5851. {
  5852. if (this->GetProperty(property)) {
  5853. this->GetTargetVersion(property, major, minor, patch);
  5854. } else {
  5855. this->GetTargetVersion(fallback_property, major, minor, patch);
  5856. }
  5857. }
  5858. void cmGeneratorTarget::GetTargetVersion(const std::string& property,
  5859. int& major, int& minor,
  5860. int& patch) const
  5861. {
  5862. // Set the default values.
  5863. major = 0;
  5864. minor = 0;
  5865. patch = 0;
  5866. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5867. if (cmValue version = this->GetProperty(property)) {
  5868. // Try to parse the version number and store the results that were
  5869. // successfully parsed.
  5870. int parsed_major;
  5871. int parsed_minor;
  5872. int parsed_patch;
  5873. switch (sscanf(version->c_str(), "%d.%d.%d", &parsed_major, &parsed_minor,
  5874. &parsed_patch)) {
  5875. case 3:
  5876. patch = parsed_patch;
  5877. CM_FALLTHROUGH;
  5878. case 2:
  5879. minor = parsed_minor;
  5880. CM_FALLTHROUGH;
  5881. case 1:
  5882. major = parsed_major;
  5883. CM_FALLTHROUGH;
  5884. default:
  5885. break;
  5886. }
  5887. }
  5888. }
  5889. std::string cmGeneratorTarget::GetRuntimeLinkLibrary(
  5890. std::string const& lang, std::string const& config) const
  5891. {
  5892. // This is activated by the presence of a default selection whether or
  5893. // not it is overridden by a property.
  5894. cmValue runtimeLibraryDefault = this->Makefile->GetDefinition(
  5895. cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT"));
  5896. if (!cmNonempty(runtimeLibraryDefault)) {
  5897. return std::string();
  5898. }
  5899. cmValue runtimeLibraryValue =
  5900. this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
  5901. if (!runtimeLibraryValue) {
  5902. runtimeLibraryValue = runtimeLibraryDefault;
  5903. }
  5904. return cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
  5905. *runtimeLibraryValue, this->LocalGenerator, config, this));
  5906. }
  5907. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  5908. std::string const& working_dir) const
  5909. {
  5910. if (!this->FortranModuleDirectoryCreated) {
  5911. this->FortranModuleDirectory =
  5912. this->CreateFortranModuleDirectory(working_dir);
  5913. this->FortranModuleDirectoryCreated = true;
  5914. }
  5915. return this->FortranModuleDirectory;
  5916. }
  5917. bool cmGeneratorTarget::IsFortranBuildingInstrinsicModules() const
  5918. {
  5919. if (cmValue prop =
  5920. this->GetProperty("Fortran_BUILDING_INSTRINSIC_MODULES")) {
  5921. return cmIsOn(*prop);
  5922. }
  5923. return false;
  5924. }
  5925. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  5926. std::string const& working_dir) const
  5927. {
  5928. std::string mod_dir;
  5929. std::string target_mod_dir;
  5930. if (cmValue prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  5931. target_mod_dir = *prop;
  5932. } else {
  5933. std::string const& default_mod_dir =
  5934. this->LocalGenerator->GetCurrentBinaryDirectory();
  5935. if (default_mod_dir != working_dir) {
  5936. target_mod_dir = default_mod_dir;
  5937. }
  5938. }
  5939. cmValue moddir_flag =
  5940. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  5941. if (!target_mod_dir.empty() && moddir_flag) {
  5942. // Compute the full path to the module directory.
  5943. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  5944. // Already a full path.
  5945. mod_dir = target_mod_dir;
  5946. } else {
  5947. // Interpret relative to the current output directory.
  5948. mod_dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  5949. '/', target_mod_dir);
  5950. }
  5951. // Make sure the module output directory exists.
  5952. cmSystemTools::MakeDirectory(mod_dir);
  5953. }
  5954. return mod_dir;
  5955. }
  5956. void cmGeneratorTarget::AddISPCGeneratedHeader(std::string const& header,
  5957. std::string const& config)
  5958. {
  5959. std::string config_upper;
  5960. if (!config.empty()) {
  5961. config_upper = cmSystemTools::UpperCase(config);
  5962. }
  5963. auto iter = this->ISPCGeneratedHeaders.find(config_upper);
  5964. if (iter == this->ISPCGeneratedHeaders.end()) {
  5965. std::vector<std::string> headers;
  5966. headers.emplace_back(header);
  5967. this->ISPCGeneratedHeaders.insert({ config_upper, headers });
  5968. } else {
  5969. iter->second.emplace_back(header);
  5970. }
  5971. }
  5972. std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCHeaders(
  5973. std::string const& config) const
  5974. {
  5975. std::string config_upper;
  5976. if (!config.empty()) {
  5977. config_upper = cmSystemTools::UpperCase(config);
  5978. }
  5979. auto iter = this->ISPCGeneratedHeaders.find(config_upper);
  5980. if (iter == this->ISPCGeneratedHeaders.end()) {
  5981. return std::vector<std::string>{};
  5982. }
  5983. return iter->second;
  5984. }
  5985. void cmGeneratorTarget::AddISPCGeneratedObject(std::vector<std::string>&& objs,
  5986. std::string const& config)
  5987. {
  5988. std::string config_upper;
  5989. if (!config.empty()) {
  5990. config_upper = cmSystemTools::UpperCase(config);
  5991. }
  5992. auto iter = this->ISPCGeneratedObjects.find(config_upper);
  5993. if (iter == this->ISPCGeneratedObjects.end()) {
  5994. this->ISPCGeneratedObjects.insert({ config_upper, objs });
  5995. } else {
  5996. iter->second.insert(iter->second.end(), objs.begin(), objs.end());
  5997. }
  5998. }
  5999. std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCObjects(
  6000. std::string const& config) const
  6001. {
  6002. std::string config_upper;
  6003. if (!config.empty()) {
  6004. config_upper = cmSystemTools::UpperCase(config);
  6005. }
  6006. auto iter = this->ISPCGeneratedObjects.find(config_upper);
  6007. if (iter == this->ISPCGeneratedObjects.end()) {
  6008. return std::vector<std::string>{};
  6009. }
  6010. return iter->second;
  6011. }
  6012. std::string cmGeneratorTarget::GetFrameworkVersion() const
  6013. {
  6014. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  6015. if (cmValue fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  6016. return *fversion;
  6017. }
  6018. if (cmValue tversion = this->GetProperty("VERSION")) {
  6019. return *tversion;
  6020. }
  6021. return "A";
  6022. }
  6023. void cmGeneratorTarget::ComputeVersionedName(
  6024. std::string& vName, std::string const& prefix, std::string const& base,
  6025. std::string const& suffix, std::string const& name, cmValue version) const
  6026. {
  6027. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  6028. if (version) {
  6029. vName += ".";
  6030. vName += *version;
  6031. }
  6032. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  6033. }
  6034. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  6035. {
  6036. return this->Target->GetProperties().GetKeys();
  6037. }
  6038. void cmGeneratorTarget::ReportPropertyOrigin(
  6039. const std::string& p, const std::string& result, const std::string& report,
  6040. const std::string& compatibilityType) const
  6041. {
  6042. std::vector<std::string> debugProperties;
  6043. this->Target->GetMakefile()->GetDefExpandList(
  6044. "CMAKE_DEBUG_TARGET_PROPERTIES", debugProperties);
  6045. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  6046. cm::contains(debugProperties, p);
  6047. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  6048. this->DebugCompatiblePropertiesDone[p] = true;
  6049. }
  6050. if (!debugOrigin) {
  6051. return;
  6052. }
  6053. std::string areport =
  6054. cmStrCat(compatibilityType, " of property \"", p, "\" for target \"",
  6055. this->GetName(), "\" (result: \"", result, "\"):\n", report);
  6056. this->LocalGenerator->GetCMakeInstance()->IssueMessage(MessageType::LOG,
  6057. areport);
  6058. }
  6059. bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n,
  6060. cmLocalGenerator const*& lg) const
  6061. {
  6062. if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) {
  6063. cmDirectoryId const dirId = n.substr(cmStrLen(CMAKE_DIRECTORY_ID_SEP));
  6064. if (dirId.String.empty()) {
  6065. lg = this->LocalGenerator;
  6066. return true;
  6067. }
  6068. if (cmLocalGenerator const* otherLG =
  6069. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  6070. lg = otherLG;
  6071. return true;
  6072. }
  6073. }
  6074. return false;
  6075. }
  6076. cm::optional<cmLinkItem> cmGeneratorTarget::LookupLinkItem(
  6077. std::string const& n, cmListFileBacktrace const& bt,
  6078. LookupLinkItemScope* scope, LookupSelf lookupSelf) const
  6079. {
  6080. cm::optional<cmLinkItem> maybeItem;
  6081. if (this->IsLinkLookupScope(n, scope->LG)) {
  6082. return maybeItem;
  6083. }
  6084. std::string name = this->CheckCMP0004(n);
  6085. if (name.empty() ||
  6086. (lookupSelf == LookupSelf::No && name == this->GetName())) {
  6087. return maybeItem;
  6088. }
  6089. maybeItem = this->ResolveLinkItem(BT<std::string>(name, bt), scope->LG);
  6090. return maybeItem;
  6091. }
  6092. void cmGeneratorTarget::ExpandLinkItems(
  6093. std::string const& prop, cmBTStringRange entries, std::string const& config,
  6094. cmGeneratorTarget const* headTarget, LinkInterfaceFor interfaceFor,
  6095. LinkInterfaceField field, cmLinkInterface& iface) const
  6096. {
  6097. if (entries.empty()) {
  6098. return;
  6099. }
  6100. // Keep this logic in sync with ComputeLinkImplementationLibraries.
  6101. cmGeneratorExpressionDAGChecker dagChecker(this, prop, nullptr, nullptr);
  6102. // The $<LINK_ONLY> expression may be in a link interface to specify
  6103. // private link dependencies that are otherwise excluded from usage
  6104. // requirements.
  6105. if (interfaceFor == LinkInterfaceFor::Usage) {
  6106. dagChecker.SetTransitivePropertiesOnly();
  6107. }
  6108. cmMakefile const* mf = this->LocalGenerator->GetMakefile();
  6109. LookupLinkItemScope scope{ this->LocalGenerator };
  6110. for (BT<std::string> const& entry : entries) {
  6111. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(),
  6112. entry.Backtrace);
  6113. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(entry.Value);
  6114. cge->SetEvaluateForBuildsystem(true);
  6115. std::vector<std::string> libs = cmExpandedList(
  6116. cge->Evaluate(this->LocalGenerator, config, headTarget, &dagChecker,
  6117. this, headTarget->LinkerLanguage));
  6118. for (std::string const& lib : libs) {
  6119. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  6120. lib, cge->GetBacktrace(), &scope,
  6121. field == LinkInterfaceField::Libraries ? LookupSelf::No
  6122. : LookupSelf::Yes)) {
  6123. cmLinkItem item = std::move(*maybeItem);
  6124. if (field == LinkInterfaceField::HeadInclude) {
  6125. iface.HeadInclude.emplace_back(std::move(item));
  6126. continue;
  6127. }
  6128. if (field == LinkInterfaceField::HeadExclude) {
  6129. iface.HeadExclude.emplace_back(std::move(item));
  6130. continue;
  6131. }
  6132. if (!item.Target) {
  6133. // Report explicitly linked object files separately.
  6134. std::string const& maybeObj = item.AsStr();
  6135. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  6136. cmSourceFile const* sf =
  6137. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  6138. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  6139. iface.Objects.emplace_back(std::move(item));
  6140. continue;
  6141. }
  6142. }
  6143. }
  6144. iface.Libraries.emplace_back(std::move(item));
  6145. }
  6146. }
  6147. if (cge->GetHadHeadSensitiveCondition()) {
  6148. iface.HadHeadSensitiveCondition = true;
  6149. }
  6150. if (cge->GetHadContextSensitiveCondition()) {
  6151. iface.HadContextSensitiveCondition = true;
  6152. }
  6153. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  6154. iface.HadLinkLanguageSensitiveCondition = true;
  6155. }
  6156. }
  6157. }
  6158. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  6159. const std::string& config, cmGeneratorTarget const* head) const
  6160. {
  6161. return this->GetLinkInterface(config, head, false);
  6162. }
  6163. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  6164. const std::string& config, cmGeneratorTarget const* head,
  6165. bool secondPass) const
  6166. {
  6167. // Imported targets have their own link interface.
  6168. if (this->IsImported()) {
  6169. return this->GetImportLinkInterface(config, head, LinkInterfaceFor::Link,
  6170. secondPass);
  6171. }
  6172. // Link interfaces are not supported for executables that do not
  6173. // export symbols.
  6174. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  6175. !this->IsExecutableWithExports()) {
  6176. return nullptr;
  6177. }
  6178. // Lookup any existing link interface for this configuration.
  6179. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  6180. // If the link interface does not depend on the head target
  6181. // then re-use the one from the head we computed first.
  6182. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6183. head = hm.begin()->first;
  6184. }
  6185. cmOptionalLinkInterface& iface = hm[head];
  6186. if (secondPass) {
  6187. iface = cmOptionalLinkInterface();
  6188. }
  6189. if (!iface.LibrariesDone) {
  6190. iface.LibrariesDone = true;
  6191. this->ComputeLinkInterfaceLibraries(config, iface, head,
  6192. LinkInterfaceFor::Link);
  6193. }
  6194. if (!iface.AllDone) {
  6195. iface.AllDone = true;
  6196. if (iface.Exists) {
  6197. this->ComputeLinkInterface(config, iface, head, secondPass);
  6198. this->ComputeLinkInterfaceRuntimeLibraries(config, iface);
  6199. }
  6200. }
  6201. return iface.Exists ? &iface : nullptr;
  6202. }
  6203. void cmGeneratorTarget::ComputeLinkInterface(
  6204. const std::string& config, cmOptionalLinkInterface& iface,
  6205. cmGeneratorTarget const* headTarget) const
  6206. {
  6207. this->ComputeLinkInterface(config, iface, headTarget, false);
  6208. }
  6209. void cmGeneratorTarget::ComputeLinkInterface(
  6210. const std::string& config, cmOptionalLinkInterface& iface,
  6211. cmGeneratorTarget const* headTarget, bool secondPass) const
  6212. {
  6213. if (iface.Explicit) {
  6214. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6215. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  6216. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6217. // Shared libraries may have runtime implementation dependencies
  6218. // on other shared libraries that are not in the interface.
  6219. std::set<cmLinkItem> emitted;
  6220. for (cmLinkItem const& lib : iface.Libraries) {
  6221. emitted.insert(lib);
  6222. }
  6223. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  6224. cmLinkImplementation const* impl = this->GetLinkImplementation(
  6225. config, LinkInterfaceFor::Link, secondPass);
  6226. for (cmLinkImplItem const& lib : impl->Libraries) {
  6227. if (emitted.insert(lib).second) {
  6228. if (lib.Target) {
  6229. // This is a runtime dependency on another shared library.
  6230. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6231. iface.SharedDeps.push_back(lib);
  6232. }
  6233. } else {
  6234. // TODO: Recognize shared library file names. Perhaps this
  6235. // should be moved to cmComputeLinkInformation, but that
  6236. // creates a chicken-and-egg problem since this list is needed
  6237. // for its construction.
  6238. }
  6239. }
  6240. }
  6241. }
  6242. }
  6243. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  6244. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  6245. // The link implementation is the default link interface.
  6246. cmLinkImplementationLibraries const* impl =
  6247. this->GetLinkImplementationLibrariesInternal(config, headTarget,
  6248. LinkInterfaceFor::Link);
  6249. iface.ImplementationIsInterface = true;
  6250. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  6251. }
  6252. if (this->LinkLanguagePropagatesToDependents()) {
  6253. // Targets using this archive need its language runtime libraries.
  6254. if (cmLinkImplementation const* impl = this->GetLinkImplementation(
  6255. config, LinkInterfaceFor::Link, secondPass)) {
  6256. iface.Languages = impl->Languages;
  6257. }
  6258. }
  6259. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6260. // Construct the property name suffix for this configuration.
  6261. std::string suffix = "_";
  6262. if (!config.empty()) {
  6263. suffix += cmSystemTools::UpperCase(config);
  6264. } else {
  6265. suffix += "NOCONFIG";
  6266. }
  6267. // How many repetitions are needed if this library has cyclic
  6268. // dependencies?
  6269. std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix);
  6270. if (cmValue config_reps = this->GetProperty(propName)) {
  6271. sscanf(config_reps->c_str(), "%u", &iface.Multiplicity);
  6272. } else if (cmValue reps =
  6273. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  6274. sscanf(reps->c_str(), "%u", &iface.Multiplicity);
  6275. }
  6276. }
  6277. }
  6278. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  6279. const std::string& config, cmGeneratorTarget const* head,
  6280. LinkInterfaceFor interfaceFor) const
  6281. {
  6282. // Imported targets have their own link interface.
  6283. if (this->IsImported()) {
  6284. return this->GetImportLinkInterface(config, head, interfaceFor);
  6285. }
  6286. // Link interfaces are not supported for executables that do not
  6287. // export symbols.
  6288. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  6289. !this->IsExecutableWithExports()) {
  6290. return nullptr;
  6291. }
  6292. // Lookup any existing link interface for this configuration.
  6293. cmHeadToLinkInterfaceMap& hm =
  6294. (interfaceFor == LinkInterfaceFor::Usage
  6295. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  6296. : this->GetHeadToLinkInterfaceMap(config));
  6297. // If the link interface does not depend on the head target
  6298. // then re-use the one from the head we computed first.
  6299. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6300. head = hm.begin()->first;
  6301. }
  6302. cmOptionalLinkInterface& iface = hm[head];
  6303. if (!iface.LibrariesDone) {
  6304. iface.LibrariesDone = true;
  6305. this->ComputeLinkInterfaceLibraries(config, iface, head, interfaceFor);
  6306. }
  6307. return iface.Exists ? &iface : nullptr;
  6308. }
  6309. std::string cmGeneratorTarget::GetDirectory(
  6310. const std::string& config, cmStateEnums::ArtifactType artifact) const
  6311. {
  6312. if (this->IsImported()) {
  6313. auto fullPath = this->Target->ImportedGetFullPath(config, artifact);
  6314. if (this->IsFrameworkOnApple()) {
  6315. cmsys::RegularExpressionMatch match;
  6316. if (FrameworkRegularExpression.find(fullPath.c_str(), match)) {
  6317. auto path = match.match(1);
  6318. if (!path.empty()) {
  6319. path.erase(path.length() - 1);
  6320. }
  6321. return path;
  6322. }
  6323. }
  6324. // Return the directory from which the target is imported.
  6325. return cmSystemTools::GetFilenamePath(fullPath);
  6326. }
  6327. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  6328. // Return the directory in which the target will be built.
  6329. switch (artifact) {
  6330. case cmStateEnums::RuntimeBinaryArtifact:
  6331. return info->OutDir;
  6332. case cmStateEnums::ImportLibraryArtifact:
  6333. return info->ImpDir;
  6334. }
  6335. }
  6336. return "";
  6337. }
  6338. bool cmGeneratorTarget::UsesDefaultOutputDir(
  6339. const std::string& config, cmStateEnums::ArtifactType artifact) const
  6340. {
  6341. std::string dir;
  6342. return this->ComputeOutputDir(config, artifact, dir);
  6343. }
  6344. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  6345. const std::string& config) const
  6346. {
  6347. // There is no output information for imported targets.
  6348. if (this->IsImported()) {
  6349. return nullptr;
  6350. }
  6351. // Synthetic targets don't have output.
  6352. if (this->IsSynthetic()) {
  6353. return nullptr;
  6354. }
  6355. // Only libraries and executables have well-defined output files.
  6356. if (!this->HaveWellDefinedOutputFiles()) {
  6357. std::string msg = cmStrCat("cmGeneratorTarget::GetOutputInfo called for ",
  6358. this->GetName(), " which has type ",
  6359. cmState::GetTargetTypeName(this->GetType()));
  6360. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  6361. return nullptr;
  6362. }
  6363. // Lookup/compute/cache the output information for this configuration.
  6364. std::string config_upper;
  6365. if (!config.empty()) {
  6366. config_upper = cmSystemTools::UpperCase(config);
  6367. }
  6368. auto i = this->OutputInfoMap.find(config_upper);
  6369. if (i == this->OutputInfoMap.end()) {
  6370. // Add empty info in map to detect potential recursion.
  6371. OutputInfo info;
  6372. OutputInfoMapType::value_type entry(config_upper, info);
  6373. i = this->OutputInfoMap.insert(entry).first;
  6374. // Compute output directories.
  6375. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  6376. info.OutDir);
  6377. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  6378. info.ImpDir);
  6379. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  6380. info.PdbDir = info.OutDir;
  6381. }
  6382. // Now update the previously-prepared map entry.
  6383. i->second = info;
  6384. } else if (i->second.empty()) {
  6385. // An empty map entry indicates we have been called recursively
  6386. // from the above block.
  6387. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6388. MessageType::FATAL_ERROR,
  6389. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  6390. this->GetBacktrace());
  6391. return nullptr;
  6392. }
  6393. return &i->second;
  6394. }
  6395. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  6396. cmStateEnums::ArtifactType artifact,
  6397. std::string& out) const
  6398. {
  6399. bool usesDefaultOutputDir = false;
  6400. std::string conf = config;
  6401. // Look for a target property defining the target output directory
  6402. // based on the target type.
  6403. std::string targetTypeName = this->GetOutputTargetType(artifact);
  6404. std::string propertyName;
  6405. if (!targetTypeName.empty()) {
  6406. propertyName = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY");
  6407. }
  6408. // Check for a per-configuration output directory target property.
  6409. std::string configUpper = cmSystemTools::UpperCase(conf);
  6410. std::string configProp;
  6411. if (!targetTypeName.empty()) {
  6412. configProp = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY_", configUpper);
  6413. }
  6414. // Select an output directory.
  6415. if (cmValue config_outdir = this->GetProperty(configProp)) {
  6416. // Use the user-specified per-configuration output directory.
  6417. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  6418. config, this);
  6419. // Skip per-configuration subdirectory.
  6420. conf.clear();
  6421. } else if (cmValue outdir = this->GetProperty(propertyName)) {
  6422. // Use the user-specified output directory.
  6423. out = cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator,
  6424. config, this);
  6425. // Skip per-configuration subdirectory if the value contained a
  6426. // generator expression.
  6427. if (out != *outdir) {
  6428. conf.clear();
  6429. }
  6430. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  6431. // Lookup the output path for executables.
  6432. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  6433. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  6434. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6435. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  6436. // Lookup the output path for libraries.
  6437. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  6438. }
  6439. if (out.empty()) {
  6440. // Default to the current output directory.
  6441. usesDefaultOutputDir = true;
  6442. out = ".";
  6443. }
  6444. // Convert the output path to a full path in case it is
  6445. // specified as a relative path. Treat a relative path as
  6446. // relative to the current output directory for this makefile.
  6447. out = (cmSystemTools::CollapseFullPath(
  6448. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  6449. // The generator may add the configuration's subdirectory.
  6450. if (!conf.empty()) {
  6451. bool useEPN =
  6452. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  6453. std::string suffix =
  6454. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  6455. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  6456. "/", conf, suffix, out);
  6457. }
  6458. return usesDefaultOutputDir;
  6459. }
  6460. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  6461. const std::string& config,
  6462. std::string& out) const
  6463. {
  6464. // Look for a target property defining the target output directory
  6465. // based on the target type.
  6466. std::string propertyName;
  6467. if (!kind.empty()) {
  6468. propertyName = cmStrCat(kind, "_OUTPUT_DIRECTORY");
  6469. }
  6470. std::string conf = config;
  6471. // Check for a per-configuration output directory target property.
  6472. std::string configUpper = cmSystemTools::UpperCase(conf);
  6473. std::string configProp;
  6474. if (!kind.empty()) {
  6475. configProp = cmStrCat(kind, "_OUTPUT_DIRECTORY_", configUpper);
  6476. }
  6477. // Select an output directory.
  6478. if (cmValue config_outdir = this->GetProperty(configProp)) {
  6479. // Use the user-specified per-configuration output directory.
  6480. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  6481. config);
  6482. // Skip per-configuration subdirectory.
  6483. conf.clear();
  6484. } else if (cmValue outdir = this->GetProperty(propertyName)) {
  6485. // Use the user-specified output directory.
  6486. out =
  6487. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  6488. // Skip per-configuration subdirectory if the value contained a
  6489. // generator expression.
  6490. if (out != *outdir) {
  6491. conf.clear();
  6492. }
  6493. }
  6494. if (out.empty()) {
  6495. return false;
  6496. }
  6497. // Convert the output path to a full path in case it is
  6498. // specified as a relative path. Treat a relative path as
  6499. // relative to the current output directory for this makefile.
  6500. out = (cmSystemTools::CollapseFullPath(
  6501. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  6502. // The generator may add the configuration's subdirectory.
  6503. if (!conf.empty()) {
  6504. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  6505. "/", conf, "", out);
  6506. }
  6507. return true;
  6508. }
  6509. bool cmGeneratorTarget::HaveInstallTreeRPATH(const std::string& config) const
  6510. {
  6511. std::string install_rpath;
  6512. this->GetInstallRPATH(config, install_rpath);
  6513. return !install_rpath.empty() &&
  6514. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  6515. }
  6516. bool cmGeneratorTarget::GetBuildRPATH(const std::string& config,
  6517. std::string& rpath) const
  6518. {
  6519. return this->GetRPATH(config, "BUILD_RPATH", rpath);
  6520. }
  6521. bool cmGeneratorTarget::GetInstallRPATH(const std::string& config,
  6522. std::string& rpath) const
  6523. {
  6524. return this->GetRPATH(config, "INSTALL_RPATH", rpath);
  6525. }
  6526. bool cmGeneratorTarget::GetRPATH(const std::string& config,
  6527. const std::string& prop,
  6528. std::string& rpath) const
  6529. {
  6530. cmValue value = this->GetProperty(prop);
  6531. if (!value) {
  6532. return false;
  6533. }
  6534. rpath =
  6535. cmGeneratorExpression::Evaluate(*value, this->LocalGenerator, config);
  6536. return true;
  6537. }
  6538. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  6539. const std::string& config, cmOptionalLinkInterface& iface,
  6540. cmGeneratorTarget const* headTarget, LinkInterfaceFor interfaceFor) const
  6541. {
  6542. // Construct the property name suffix for this configuration.
  6543. std::string suffix = "_";
  6544. if (!config.empty()) {
  6545. suffix += cmSystemTools::UpperCase(config);
  6546. } else {
  6547. suffix += "NOCONFIG";
  6548. }
  6549. // An explicit list of interface libraries may be set for shared
  6550. // libraries and executables that export symbols.
  6551. bool haveExplicitLibraries = false;
  6552. cmValue explicitLibrariesCMP0022OLD;
  6553. std::string linkIfacePropCMP0022OLD;
  6554. bool const cmp0022NEW = (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  6555. this->GetPolicyStatusCMP0022() != cmPolicies::WARN);
  6556. if (cmp0022NEW) {
  6557. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  6558. haveExplicitLibraries = !this->Target->GetLinkInterfaceEntries().empty() ||
  6559. !this->Target->GetLinkInterfaceDirectEntries().empty() ||
  6560. !this->Target->GetLinkInterfaceDirectExcludeEntries().empty();
  6561. } else {
  6562. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  6563. // shared lib or executable.
  6564. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6565. this->IsExecutableWithExports()) {
  6566. // Lookup the per-configuration property.
  6567. linkIfacePropCMP0022OLD = cmStrCat("LINK_INTERFACE_LIBRARIES", suffix);
  6568. explicitLibrariesCMP0022OLD = this->GetProperty(linkIfacePropCMP0022OLD);
  6569. // If not set, try the generic property.
  6570. if (!explicitLibrariesCMP0022OLD) {
  6571. linkIfacePropCMP0022OLD = "LINK_INTERFACE_LIBRARIES";
  6572. explicitLibrariesCMP0022OLD =
  6573. this->GetProperty(linkIfacePropCMP0022OLD);
  6574. }
  6575. }
  6576. if (explicitLibrariesCMP0022OLD &&
  6577. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  6578. !this->PolicyWarnedCMP0022) {
  6579. // Compare the explicitly set old link interface properties to the
  6580. // preferred new link interface property one and warn if different.
  6581. cmValue newExplicitLibraries =
  6582. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  6583. if (newExplicitLibraries &&
  6584. (*newExplicitLibraries != *explicitLibrariesCMP0022OLD)) {
  6585. std::ostringstream w;
  6586. /* clang-format off */
  6587. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  6588. "Target \"" << this->GetName() << "\" has an "
  6589. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  6590. linkIfacePropCMP0022OLD << " properties."
  6591. "\n"
  6592. "INTERFACE_LINK_LIBRARIES:\n"
  6593. " " << *newExplicitLibraries << "\n" <<
  6594. linkIfacePropCMP0022OLD << ":\n"
  6595. " " << *explicitLibrariesCMP0022OLD << "\n";
  6596. /* clang-format on */
  6597. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  6598. w.str());
  6599. this->PolicyWarnedCMP0022 = true;
  6600. }
  6601. }
  6602. haveExplicitLibraries = static_cast<bool>(explicitLibrariesCMP0022OLD);
  6603. }
  6604. // There is no implicit link interface for executables or modules
  6605. // so if none was explicitly set then there is no link interface.
  6606. if (!haveExplicitLibraries &&
  6607. (this->GetType() == cmStateEnums::EXECUTABLE ||
  6608. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  6609. return;
  6610. }
  6611. iface.Exists = true;
  6612. // If CMP0022 is NEW then the plain tll signature sets the
  6613. // INTERFACE_LINK_LIBRARIES property. Even if the project
  6614. // clears it, the link interface is still explicit.
  6615. iface.Explicit = cmp0022NEW || explicitLibrariesCMP0022OLD;
  6616. if (cmp0022NEW) {
  6617. // The interface libraries are specified by INTERFACE_LINK_LIBRARIES.
  6618. // Use its special representation directly to get backtraces.
  6619. this->ExpandLinkItems(
  6620. kINTERFACE_LINK_LIBRARIES, this->Target->GetLinkInterfaceEntries(),
  6621. config, headTarget, interfaceFor, LinkInterfaceField::Libraries, iface);
  6622. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  6623. this->Target->GetLinkInterfaceDirectEntries(),
  6624. config, headTarget, interfaceFor,
  6625. LinkInterfaceField::HeadInclude, iface);
  6626. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  6627. this->Target->GetLinkInterfaceDirectExcludeEntries(),
  6628. config, headTarget, interfaceFor,
  6629. LinkInterfaceField::HeadExclude, iface);
  6630. } else if (explicitLibrariesCMP0022OLD) {
  6631. // The interface libraries have been explicitly set in pre-CMP0022 style.
  6632. std::vector<BT<std::string>> entries;
  6633. entries.emplace_back(*explicitLibrariesCMP0022OLD);
  6634. this->ExpandLinkItems(linkIfacePropCMP0022OLD, cmMakeRange(entries),
  6635. config, headTarget, interfaceFor,
  6636. LinkInterfaceField::Libraries, iface);
  6637. }
  6638. // If the link interface is explicit, do not fall back to the link impl.
  6639. if (iface.Explicit) {
  6640. return;
  6641. }
  6642. // The link implementation is the default link interface.
  6643. if (cmLinkImplementationLibraries const* impl =
  6644. this->GetLinkImplementationLibrariesInternal(config, headTarget,
  6645. interfaceFor)) {
  6646. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  6647. impl->Libraries.end());
  6648. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  6649. !this->PolicyWarnedCMP0022 && interfaceFor == LinkInterfaceFor::Link) {
  6650. // Compare the link implementation fallback link interface to the
  6651. // preferred new link interface property and warn if different.
  6652. cmLinkInterface ifaceNew;
  6653. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES,
  6654. this->Target->GetLinkInterfaceEntries(), config,
  6655. headTarget, interfaceFor,
  6656. LinkInterfaceField::Libraries, ifaceNew);
  6657. if (ifaceNew.Libraries != iface.Libraries) {
  6658. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  6659. std::string newLibraries = cmJoin(ifaceNew.Libraries, ";");
  6660. if (oldLibraries.empty()) {
  6661. oldLibraries = "(empty)";
  6662. }
  6663. if (newLibraries.empty()) {
  6664. newLibraries = "(empty)";
  6665. }
  6666. std::ostringstream w;
  6667. /* clang-format off */
  6668. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  6669. "Target \"" << this->GetName() << "\" has an "
  6670. "INTERFACE_LINK_LIBRARIES property. "
  6671. "This should be preferred as the source of the link interface "
  6672. "for this library but because CMP0022 is not set CMake is "
  6673. "ignoring the property and using the link implementation "
  6674. "as the link interface instead."
  6675. "\n"
  6676. "INTERFACE_LINK_LIBRARIES:\n"
  6677. " " << newLibraries << "\n"
  6678. "Link implementation:\n"
  6679. " " << oldLibraries << "\n";
  6680. /* clang-format on */
  6681. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  6682. w.str());
  6683. this->PolicyWarnedCMP0022 = true;
  6684. }
  6685. }
  6686. }
  6687. }
  6688. namespace {
  6689. template <typename ReturnType>
  6690. ReturnType constructItem(cmGeneratorTarget* target,
  6691. cmListFileBacktrace const& bt);
  6692. template <>
  6693. inline cmLinkImplItem constructItem(cmGeneratorTarget* target,
  6694. cmListFileBacktrace const& bt)
  6695. {
  6696. return cmLinkImplItem(cmLinkItem(target, false, bt), false);
  6697. }
  6698. template <>
  6699. inline cmLinkItem constructItem(cmGeneratorTarget* target,
  6700. cmListFileBacktrace const& bt)
  6701. {
  6702. return cmLinkItem(target, false, bt);
  6703. }
  6704. template <typename ValueType>
  6705. std::vector<ValueType> computeImplicitLanguageTargets(
  6706. std::string const& lang, std::string const& config,
  6707. cmGeneratorTarget const* currentTarget)
  6708. {
  6709. cmListFileBacktrace bt;
  6710. std::vector<ValueType> result;
  6711. cmLocalGenerator* lg = currentTarget->GetLocalGenerator();
  6712. std::string const& runtimeLibrary =
  6713. currentTarget->GetRuntimeLinkLibrary(lang, config);
  6714. if (cmValue runtimeLinkOptions = currentTarget->Makefile->GetDefinition(
  6715. "CMAKE_" + lang + "_RUNTIME_LIBRARIES_" + runtimeLibrary)) {
  6716. std::vector<std::string> libsVec = cmExpandedList(*runtimeLinkOptions);
  6717. result.reserve(libsVec.size());
  6718. for (std::string const& i : libsVec) {
  6719. cmGeneratorTarget::TargetOrString resolved =
  6720. currentTarget->ResolveTargetReference(i, lg);
  6721. if (resolved.Target) {
  6722. result.emplace_back(constructItem<ValueType>(resolved.Target, bt));
  6723. }
  6724. }
  6725. }
  6726. return result;
  6727. }
  6728. }
  6729. void cmGeneratorTarget::ComputeLinkInterfaceRuntimeLibraries(
  6730. const std::string& config, cmOptionalLinkInterface& iface) const
  6731. {
  6732. for (std::string const& lang : iface.Languages) {
  6733. if ((lang == "CUDA" || lang == "HIP") &&
  6734. iface.LanguageRuntimeLibraries.find(lang) ==
  6735. iface.LanguageRuntimeLibraries.end()) {
  6736. auto implicitTargets =
  6737. computeImplicitLanguageTargets<cmLinkItem>(lang, config, this);
  6738. iface.LanguageRuntimeLibraries[lang] = std::move(implicitTargets);
  6739. }
  6740. }
  6741. }
  6742. void cmGeneratorTarget::ComputeLinkImplementationRuntimeLibraries(
  6743. const std::string& config, cmOptionalLinkImplementation& impl) const
  6744. {
  6745. for (std::string const& lang : impl.Languages) {
  6746. if ((lang == "CUDA" || lang == "HIP") &&
  6747. impl.LanguageRuntimeLibraries.find(lang) ==
  6748. impl.LanguageRuntimeLibraries.end()) {
  6749. auto implicitTargets =
  6750. computeImplicitLanguageTargets<cmLinkImplItem>(lang, config, this);
  6751. impl.LanguageRuntimeLibraries[lang] = std::move(implicitTargets);
  6752. }
  6753. }
  6754. }
  6755. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  6756. const std::string& config, cmGeneratorTarget const* headTarget,
  6757. LinkInterfaceFor interfaceFor, bool secondPass) const
  6758. {
  6759. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  6760. if (!info) {
  6761. return nullptr;
  6762. }
  6763. cmHeadToLinkInterfaceMap& hm =
  6764. (interfaceFor == LinkInterfaceFor::Usage
  6765. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  6766. : this->GetHeadToLinkInterfaceMap(config));
  6767. // If the link interface does not depend on the head target
  6768. // then re-use the one from the head we computed first.
  6769. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6770. headTarget = hm.begin()->first;
  6771. }
  6772. cmOptionalLinkInterface& iface = hm[headTarget];
  6773. if (secondPass) {
  6774. iface = cmOptionalLinkInterface();
  6775. }
  6776. if (!iface.AllDone) {
  6777. iface.AllDone = true;
  6778. iface.LibrariesDone = true;
  6779. iface.Multiplicity = info->Multiplicity;
  6780. cmExpandList(info->Languages, iface.Languages);
  6781. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  6782. cmMakeRange(info->LibrariesHeadInclude), config,
  6783. headTarget, interfaceFor,
  6784. LinkInterfaceField::HeadInclude, iface);
  6785. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  6786. cmMakeRange(info->LibrariesHeadExclude), config,
  6787. headTarget, interfaceFor,
  6788. LinkInterfaceField::HeadExclude, iface);
  6789. this->ExpandLinkItems(info->LibrariesProp, cmMakeRange(info->Libraries),
  6790. config, headTarget, interfaceFor,
  6791. LinkInterfaceField::Libraries, iface);
  6792. std::vector<std::string> deps = cmExpandedList(info->SharedDeps);
  6793. LookupLinkItemScope scope{ this->LocalGenerator };
  6794. for (std::string const& dep : deps) {
  6795. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  6796. dep, cmListFileBacktrace(), &scope, LookupSelf::No)) {
  6797. iface.SharedDeps.emplace_back(std::move(*maybeItem));
  6798. }
  6799. }
  6800. }
  6801. return &iface;
  6802. }
  6803. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  6804. const std::string& config) const
  6805. {
  6806. // There is no imported information for non-imported targets.
  6807. if (!this->IsImported()) {
  6808. return nullptr;
  6809. }
  6810. // Lookup/compute/cache the import information for this
  6811. // configuration.
  6812. std::string config_upper;
  6813. if (!config.empty()) {
  6814. config_upper = cmSystemTools::UpperCase(config);
  6815. } else {
  6816. config_upper = "NOCONFIG";
  6817. }
  6818. auto i = this->ImportInfoMap.find(config_upper);
  6819. if (i == this->ImportInfoMap.end()) {
  6820. ImportInfo info;
  6821. this->ComputeImportInfo(config_upper, info);
  6822. ImportInfoMapType::value_type entry(config_upper, info);
  6823. i = this->ImportInfoMap.insert(entry).first;
  6824. }
  6825. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6826. return &i->second;
  6827. }
  6828. // If the location is empty then the target is not available for
  6829. // this configuration.
  6830. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  6831. return nullptr;
  6832. }
  6833. // Return the import information.
  6834. return &i->second;
  6835. }
  6836. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  6837. ImportInfo& info) const
  6838. {
  6839. // This method finds information about an imported target from its
  6840. // properties. The "IMPORTED_" namespace is reserved for properties
  6841. // defined by the project exporting the target.
  6842. // Initialize members.
  6843. info.NoSOName = false;
  6844. cmValue loc = nullptr;
  6845. cmValue imp = nullptr;
  6846. std::string suffix;
  6847. if (!this->Target->GetMappedConfig(desired_config, loc, imp, suffix)) {
  6848. return;
  6849. }
  6850. // Get the link interface.
  6851. {
  6852. // Use the INTERFACE_LINK_LIBRARIES special representation directly
  6853. // to get backtraces.
  6854. cmBTStringRange entries = this->Target->GetLinkInterfaceEntries();
  6855. if (!entries.empty()) {
  6856. info.LibrariesProp = "INTERFACE_LINK_LIBRARIES";
  6857. for (BT<std::string> const& entry : entries) {
  6858. info.Libraries.emplace_back(entry);
  6859. }
  6860. } else if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  6861. std::string linkProp =
  6862. cmStrCat("IMPORTED_LINK_INTERFACE_LIBRARIES", suffix);
  6863. cmValue propertyLibs = this->GetProperty(linkProp);
  6864. if (!propertyLibs) {
  6865. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  6866. propertyLibs = this->GetProperty(linkProp);
  6867. }
  6868. if (propertyLibs) {
  6869. info.LibrariesProp = linkProp;
  6870. info.Libraries.emplace_back(*propertyLibs);
  6871. }
  6872. }
  6873. }
  6874. for (BT<std::string> const& entry :
  6875. this->Target->GetLinkInterfaceDirectEntries()) {
  6876. info.LibrariesHeadInclude.emplace_back(entry);
  6877. }
  6878. for (BT<std::string> const& entry :
  6879. this->Target->GetLinkInterfaceDirectExcludeEntries()) {
  6880. info.LibrariesHeadExclude.emplace_back(entry);
  6881. }
  6882. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6883. if (loc) {
  6884. info.LibName = *loc;
  6885. }
  6886. return;
  6887. }
  6888. // A provided configuration has been chosen. Load the
  6889. // configuration's properties.
  6890. // Get the location.
  6891. if (loc) {
  6892. info.Location = *loc;
  6893. } else {
  6894. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  6895. if (cmValue config_location = this->GetProperty(impProp)) {
  6896. info.Location = *config_location;
  6897. } else if (cmValue location = this->GetProperty("IMPORTED_LOCATION")) {
  6898. info.Location = *location;
  6899. }
  6900. }
  6901. // Get the soname.
  6902. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6903. std::string soProp = cmStrCat("IMPORTED_SONAME", suffix);
  6904. if (cmValue config_soname = this->GetProperty(soProp)) {
  6905. info.SOName = *config_soname;
  6906. } else if (cmValue soname = this->GetProperty("IMPORTED_SONAME")) {
  6907. info.SOName = *soname;
  6908. }
  6909. }
  6910. // Get the "no-soname" mark.
  6911. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6912. std::string soProp = cmStrCat("IMPORTED_NO_SONAME", suffix);
  6913. if (cmValue config_no_soname = this->GetProperty(soProp)) {
  6914. info.NoSOName = cmIsOn(*config_no_soname);
  6915. } else if (cmValue no_soname = this->GetProperty("IMPORTED_NO_SONAME")) {
  6916. info.NoSOName = cmIsOn(*no_soname);
  6917. }
  6918. }
  6919. // Get the import library.
  6920. if (imp) {
  6921. info.ImportLibrary = *imp;
  6922. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6923. this->IsExecutableWithExports()) {
  6924. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  6925. if (cmValue config_implib = this->GetProperty(impProp)) {
  6926. info.ImportLibrary = *config_implib;
  6927. } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
  6928. info.ImportLibrary = *implib;
  6929. }
  6930. }
  6931. // Get the link dependencies.
  6932. {
  6933. std::string linkProp =
  6934. cmStrCat("IMPORTED_LINK_DEPENDENT_LIBRARIES", suffix);
  6935. if (cmValue config_libs = this->GetProperty(linkProp)) {
  6936. info.SharedDeps = *config_libs;
  6937. } else if (cmValue libs =
  6938. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  6939. info.SharedDeps = *libs;
  6940. }
  6941. }
  6942. // Get the link languages.
  6943. if (this->LinkLanguagePropagatesToDependents()) {
  6944. std::string linkProp =
  6945. cmStrCat("IMPORTED_LINK_INTERFACE_LANGUAGES", suffix);
  6946. if (cmValue config_libs = this->GetProperty(linkProp)) {
  6947. info.Languages = *config_libs;
  6948. } else if (cmValue libs =
  6949. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  6950. info.Languages = *libs;
  6951. }
  6952. }
  6953. // Get information if target is managed assembly.
  6954. {
  6955. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  6956. if (cmValue pc = this->GetProperty(linkProp + suffix)) {
  6957. info.Managed = this->CheckManagedType(*pc);
  6958. } else if (cmValue p = this->GetProperty(linkProp)) {
  6959. info.Managed = this->CheckManagedType(*p);
  6960. }
  6961. }
  6962. // Get the cyclic repetition count.
  6963. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6964. std::string linkProp =
  6965. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  6966. if (cmValue config_reps = this->GetProperty(linkProp)) {
  6967. sscanf(config_reps->c_str(), "%u", &info.Multiplicity);
  6968. } else if (cmValue reps =
  6969. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  6970. sscanf(reps->c_str(), "%u", &info.Multiplicity);
  6971. }
  6972. }
  6973. }
  6974. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  6975. const std::string& config) const
  6976. {
  6977. return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)];
  6978. }
  6979. cmHeadToLinkInterfaceMap&
  6980. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  6981. const std::string& config) const
  6982. {
  6983. return this
  6984. ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  6985. }
  6986. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6987. const std::string& config, LinkInterfaceFor implFor) const
  6988. {
  6989. return this->GetLinkImplementation(config, implFor, false);
  6990. }
  6991. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6992. const std::string& config, LinkInterfaceFor implFor, bool secondPass) const
  6993. {
  6994. // There is no link implementation for targets that cannot compile sources.
  6995. if (!this->CanCompileSources()) {
  6996. return nullptr;
  6997. }
  6998. HeadToLinkImplementationMap& hm =
  6999. (implFor == LinkInterfaceFor::Usage
  7000. ? this->GetHeadToLinkImplementationUsageRequirementsMap(config)
  7001. : this->GetHeadToLinkImplementationMap(config));
  7002. cmOptionalLinkImplementation& impl = hm[this];
  7003. if (secondPass) {
  7004. impl = cmOptionalLinkImplementation();
  7005. }
  7006. if (!impl.LibrariesDone) {
  7007. impl.LibrariesDone = true;
  7008. this->ComputeLinkImplementationLibraries(config, impl, this, implFor);
  7009. }
  7010. if (!impl.LanguagesDone) {
  7011. impl.LanguagesDone = true;
  7012. this->ComputeLinkImplementationLanguages(config, impl);
  7013. this->ComputeLinkImplementationRuntimeLibraries(config, impl);
  7014. }
  7015. return &impl;
  7016. }
  7017. cmGeneratorTarget::HeadToLinkImplementationMap&
  7018. cmGeneratorTarget::GetHeadToLinkImplementationMap(
  7019. std::string const& config) const
  7020. {
  7021. return this->LinkImplMap[cmSystemTools::UpperCase(config)];
  7022. }
  7023. cmGeneratorTarget::HeadToLinkImplementationMap&
  7024. cmGeneratorTarget::GetHeadToLinkImplementationUsageRequirementsMap(
  7025. std::string const& config) const
  7026. {
  7027. return this
  7028. ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  7029. }
  7030. bool cmGeneratorTarget::GetConfigCommonSourceFilesForXcode(
  7031. std::vector<cmSourceFile*>& files) const
  7032. {
  7033. std::vector<std::string> const& configs =
  7034. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  7035. auto it = configs.begin();
  7036. const std::string& firstConfig = *it;
  7037. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  7038. for (; it != configs.end(); ++it) {
  7039. std::vector<cmSourceFile*> configFiles;
  7040. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  7041. if (configFiles != files) {
  7042. std::string firstConfigFiles;
  7043. const char* sep = "";
  7044. for (cmSourceFile* f : files) {
  7045. firstConfigFiles += sep;
  7046. firstConfigFiles += f->ResolveFullPath();
  7047. sep = "\n ";
  7048. }
  7049. std::string thisConfigFiles;
  7050. sep = "";
  7051. for (cmSourceFile* f : configFiles) {
  7052. thisConfigFiles += sep;
  7053. thisConfigFiles += f->ResolveFullPath();
  7054. sep = "\n ";
  7055. }
  7056. std::ostringstream e;
  7057. /* clang-format off */
  7058. e << "Target \"" << this->GetName()
  7059. << "\" has source files which vary by "
  7060. "configuration. This is not supported by the \""
  7061. << this->GlobalGenerator->GetName()
  7062. << "\" generator.\n"
  7063. "Config \"" << firstConfig << "\":\n"
  7064. " " << firstConfigFiles << "\n"
  7065. "Config \"" << *it << "\":\n"
  7066. " " << thisConfigFiles << "\n";
  7067. /* clang-format on */
  7068. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  7069. return false;
  7070. }
  7071. }
  7072. return true;
  7073. }
  7074. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  7075. std::vector<cmGeneratorTarget*>& objlibs) const
  7076. {
  7077. // At configure-time, this method can be called as part of getting the
  7078. // LOCATION property or to export() a file to be include()d. However
  7079. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  7080. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  7081. // behavior of CMP0024 and CMP0026 only.
  7082. cmBTStringRange rng = this->Target->GetSourceEntries();
  7083. for (auto const& entry : rng) {
  7084. std::vector<std::string> files = cmExpandedList(entry.Value);
  7085. for (std::string const& li : files) {
  7086. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") && li.back() == '>') {
  7087. std::string objLibName = li.substr(17, li.size() - 18);
  7088. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  7089. continue;
  7090. }
  7091. cmGeneratorTarget* objLib =
  7092. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  7093. if (objLib) {
  7094. objlibs.push_back(objLib);
  7095. }
  7096. }
  7097. }
  7098. }
  7099. }
  7100. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  7101. {
  7102. // Strip whitespace off the library names because we used to do this
  7103. // in case variables were expanded at generate time. We no longer
  7104. // do the expansion but users link to libraries like " ${VAR} ".
  7105. std::string lib = item;
  7106. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  7107. if (pos != std::string::npos) {
  7108. lib = lib.substr(pos);
  7109. }
  7110. pos = lib.find_last_not_of(" \t\r\n");
  7111. if (pos != std::string::npos) {
  7112. lib = lib.substr(0, pos + 1);
  7113. }
  7114. if (lib != item) {
  7115. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  7116. switch (this->GetPolicyStatusCMP0004()) {
  7117. case cmPolicies::WARN: {
  7118. std::ostringstream w;
  7119. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  7120. << "Target \"" << this->GetName() << "\" links to item \"" << item
  7121. << "\" which has leading or trailing whitespace.";
  7122. cm->IssueMessage(MessageType::AUTHOR_WARNING, w.str(),
  7123. this->GetBacktrace());
  7124. }
  7125. CM_FALLTHROUGH;
  7126. case cmPolicies::OLD:
  7127. break;
  7128. case cmPolicies::NEW: {
  7129. std::ostringstream e;
  7130. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  7131. << "\" which has leading or trailing whitespace. "
  7132. << "This is now an error according to policy CMP0004.";
  7133. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  7134. this->GetBacktrace());
  7135. } break;
  7136. case cmPolicies::REQUIRED_IF_USED:
  7137. case cmPolicies::REQUIRED_ALWAYS: {
  7138. std::ostringstream e;
  7139. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  7140. << "Target \"" << this->GetName() << "\" links to item \"" << item
  7141. << "\" which has leading or trailing whitespace.";
  7142. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  7143. this->GetBacktrace());
  7144. } break;
  7145. }
  7146. }
  7147. return lib;
  7148. }
  7149. bool cmGeneratorTarget::IsDeprecated() const
  7150. {
  7151. cmValue deprecation = this->GetProperty("DEPRECATION");
  7152. return cmNonempty(deprecation);
  7153. }
  7154. std::string cmGeneratorTarget::GetDeprecation() const
  7155. {
  7156. // find DEPRECATION property
  7157. if (cmValue deprecation = this->GetProperty("DEPRECATION")) {
  7158. return *deprecation;
  7159. }
  7160. return std::string();
  7161. }
  7162. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  7163. const std::string& config) const
  7164. {
  7165. // Targets that do not compile anything have no languages.
  7166. if (!this->CanCompileSources()) {
  7167. return;
  7168. }
  7169. std::vector<cmSourceFile*> sourceFiles;
  7170. this->GetSourceFiles(sourceFiles, config);
  7171. for (cmSourceFile* src : sourceFiles) {
  7172. const std::string& lang = src->GetOrDetermineLanguage();
  7173. if (!lang.empty()) {
  7174. languages.insert(lang);
  7175. }
  7176. }
  7177. std::vector<cmGeneratorTarget*> objectLibraries;
  7178. std::vector<cmSourceFile const*> externalObjects;
  7179. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  7180. std::vector<cmGeneratorTarget*> objectTargets;
  7181. this->GetObjectLibrariesCMP0026(objectTargets);
  7182. objectLibraries.reserve(objectTargets.size());
  7183. for (cmGeneratorTarget* gt : objectTargets) {
  7184. objectLibraries.push_back(gt);
  7185. }
  7186. } else {
  7187. this->GetExternalObjects(externalObjects, config);
  7188. for (cmSourceFile const* extObj : externalObjects) {
  7189. std::string objLib = extObj->GetObjectLibrary();
  7190. if (cmGeneratorTarget* tgt =
  7191. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  7192. auto const objLibIt =
  7193. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  7194. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  7195. if (objectLibraries.cend() == objLibIt) {
  7196. objectLibraries.push_back(tgt);
  7197. }
  7198. }
  7199. }
  7200. }
  7201. for (cmGeneratorTarget* objLib : objectLibraries) {
  7202. objLib->GetLanguages(languages, config);
  7203. }
  7204. }
  7205. bool cmGeneratorTarget::IsLanguageUsed(std::string const& language,
  7206. std::string const& config) const
  7207. {
  7208. std::set<std::string> languages;
  7209. this->GetLanguages(languages, config);
  7210. return languages.count(language);
  7211. }
  7212. bool cmGeneratorTarget::IsCSharpOnly() const
  7213. {
  7214. // Only certain target types may compile CSharp.
  7215. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  7216. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  7217. this->GetType() != cmStateEnums::EXECUTABLE) {
  7218. return false;
  7219. }
  7220. std::set<std::string> languages = this->GetAllConfigCompileLanguages();
  7221. // Consider an explicit linker language property, but *not* the
  7222. // computed linker language that may depend on linked targets.
  7223. cmValue linkLang = this->GetProperty("LINKER_LANGUAGE");
  7224. if (cmNonempty(linkLang)) {
  7225. languages.insert(*linkLang);
  7226. }
  7227. return languages.size() == 1 && languages.count("CSharp") > 0;
  7228. }
  7229. bool cmGeneratorTarget::IsDotNetSdkTarget() const
  7230. {
  7231. return !this->GetProperty("DOTNET_SDK").IsEmpty();
  7232. }
  7233. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  7234. const std::string& config, cmOptionalLinkImplementation& impl) const
  7235. {
  7236. // This target needs runtime libraries for its source languages.
  7237. std::set<std::string> languages;
  7238. // Get languages used in our source files.
  7239. this->GetLanguages(languages, config);
  7240. // Copy the set of languages to the link implementation.
  7241. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  7242. languages.end());
  7243. }
  7244. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  7245. {
  7246. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  7247. return false;
  7248. }
  7249. std::string build_rpath;
  7250. if (this->GetBuildRPATH(config, build_rpath)) {
  7251. return true;
  7252. }
  7253. if (cmLinkImplementationLibraries const* impl =
  7254. this->GetLinkImplementationLibraries(config, LinkInterfaceFor::Link)) {
  7255. return !impl->Libraries.empty();
  7256. }
  7257. return false;
  7258. }
  7259. cmLinkImplementationLibraries const*
  7260. cmGeneratorTarget::GetLinkImplementationLibraries(
  7261. const std::string& config, LinkInterfaceFor implFor) const
  7262. {
  7263. return this->GetLinkImplementationLibrariesInternal(config, this, implFor);
  7264. }
  7265. cmLinkImplementationLibraries const*
  7266. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  7267. const std::string& config, cmGeneratorTarget const* head,
  7268. LinkInterfaceFor implFor) const
  7269. {
  7270. // There is no link implementation for targets that cannot compile sources.
  7271. if (!this->CanCompileSources()) {
  7272. return nullptr;
  7273. }
  7274. // Populate the link implementation libraries for this configuration.
  7275. HeadToLinkImplementationMap& hm =
  7276. (implFor == LinkInterfaceFor::Usage
  7277. ? this->GetHeadToLinkImplementationUsageRequirementsMap(config)
  7278. : this->GetHeadToLinkImplementationMap(config));
  7279. // If the link implementation does not depend on the head target
  7280. // then re-use the one from the head we computed first.
  7281. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  7282. head = hm.begin()->first;
  7283. }
  7284. cmOptionalLinkImplementation& impl = hm[head];
  7285. if (!impl.LibrariesDone) {
  7286. impl.LibrariesDone = true;
  7287. this->ComputeLinkImplementationLibraries(config, impl, head, implFor);
  7288. }
  7289. return &impl;
  7290. }
  7291. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  7292. const std::string& p) const
  7293. {
  7294. return cm::contains(this->LinkImplicitNullProperties, p);
  7295. }
  7296. namespace {
  7297. class TransitiveLinkImpl
  7298. {
  7299. cmGeneratorTarget const* Self;
  7300. std::string const& Config;
  7301. LinkInterfaceFor ImplFor;
  7302. cmLinkImplementation& Impl;
  7303. std::set<cmLinkItem> Emitted;
  7304. std::set<cmLinkItem> Excluded;
  7305. std::unordered_set<cmGeneratorTarget const*> Followed;
  7306. void Follow(cmGeneratorTarget const* target);
  7307. public:
  7308. TransitiveLinkImpl(cmGeneratorTarget const* self, std::string const& config,
  7309. LinkInterfaceFor implFor, cmLinkImplementation& impl)
  7310. : Self(self)
  7311. , Config(config)
  7312. , ImplFor(implFor)
  7313. , Impl(impl)
  7314. {
  7315. }
  7316. void Compute();
  7317. };
  7318. void TransitiveLinkImpl::Follow(cmGeneratorTarget const* target)
  7319. {
  7320. if (!target || !this->Followed.insert(target).second ||
  7321. target->GetPolicyStatusCMP0022() == cmPolicies::OLD ||
  7322. target->GetPolicyStatusCMP0022() == cmPolicies::WARN) {
  7323. return;
  7324. }
  7325. // Get this target's usage requirements.
  7326. cmLinkInterfaceLibraries const* iface =
  7327. target->GetLinkInterfaceLibraries(this->Config, this->Self, this->ImplFor);
  7328. if (!iface) {
  7329. return;
  7330. }
  7331. if (iface->HadContextSensitiveCondition) {
  7332. this->Impl.HadContextSensitiveCondition = true;
  7333. }
  7334. // Process 'INTERFACE_LINK_LIBRARIES_DIRECT' usage requirements.
  7335. for (cmLinkItem const& item : iface->HeadInclude) {
  7336. // Inject direct dependencies from the item's usage requirements
  7337. // before the item itself.
  7338. this->Follow(item.Target);
  7339. // Add the item itself, but at most once.
  7340. if (this->Emitted.insert(item).second) {
  7341. this->Impl.Libraries.emplace_back(item, /* checkCMP0027= */ false);
  7342. }
  7343. }
  7344. // Follow transitive dependencies.
  7345. for (cmLinkItem const& item : iface->Libraries) {
  7346. this->Follow(item.Target);
  7347. }
  7348. // Record exclusions from 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  7349. // usage requirements.
  7350. for (cmLinkItem const& item : iface->HeadExclude) {
  7351. this->Excluded.insert(item);
  7352. }
  7353. }
  7354. void TransitiveLinkImpl::Compute()
  7355. {
  7356. // Save the original items and start with an empty list.
  7357. std::vector<cmLinkImplItem> original = std::move(this->Impl.Libraries);
  7358. // Avoid injecting any original items as usage requirements.
  7359. // This gives LINK_LIBRARIES final control over the order
  7360. // if it explicitly lists everything.
  7361. this->Emitted.insert(original.cbegin(), original.cend());
  7362. // Process each original item.
  7363. for (cmLinkImplItem& item : original) {
  7364. // Inject direct dependencies listed in 'INTERFACE_LINK_LIBRARIES_DIRECT'
  7365. // usage requirements before the item itself.
  7366. this->Follow(item.Target);
  7367. // Add the item itself.
  7368. this->Impl.Libraries.emplace_back(std::move(item));
  7369. }
  7370. // Remove items listed in 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  7371. // usage requirements found through any dependency above.
  7372. this->Impl.Libraries.erase(
  7373. std::remove_if(this->Impl.Libraries.begin(), this->Impl.Libraries.end(),
  7374. [this](cmLinkImplItem const& item) {
  7375. return this->Excluded.find(item) != this->Excluded.end();
  7376. }),
  7377. this->Impl.Libraries.end());
  7378. }
  7379. void ComputeLinkImplTransitive(cmGeneratorTarget const* self,
  7380. std::string const& config,
  7381. LinkInterfaceFor implFor,
  7382. cmLinkImplementation& impl)
  7383. {
  7384. TransitiveLinkImpl transitiveLinkImpl(self, config, implFor, impl);
  7385. transitiveLinkImpl.Compute();
  7386. }
  7387. }
  7388. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  7389. const std::string& config, cmOptionalLinkImplementation& impl,
  7390. cmGeneratorTarget const* head, LinkInterfaceFor implFor) const
  7391. {
  7392. cmLocalGenerator const* lg = this->LocalGenerator;
  7393. cmMakefile const* mf = lg->GetMakefile();
  7394. cmBTStringRange entryRange = this->Target->GetLinkImplementationEntries();
  7395. // Collect libraries directly linked in this configuration.
  7396. for (auto const& entry : entryRange) {
  7397. std::vector<std::string> llibs;
  7398. // Keep this logic in sync with ExpandLinkItems.
  7399. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_LIBRARIES", nullptr,
  7400. nullptr);
  7401. // The $<LINK_ONLY> expression may be used to specify link dependencies
  7402. // that are otherwise excluded from usage requirements.
  7403. if (implFor == LinkInterfaceFor::Usage) {
  7404. switch (this->GetPolicyStatusCMP0131()) {
  7405. case cmPolicies::WARN:
  7406. case cmPolicies::OLD:
  7407. break;
  7408. case cmPolicies::REQUIRED_IF_USED:
  7409. case cmPolicies::REQUIRED_ALWAYS:
  7410. case cmPolicies::NEW:
  7411. dagChecker.SetTransitivePropertiesOnly();
  7412. break;
  7413. }
  7414. }
  7415. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(),
  7416. entry.Backtrace);
  7417. std::unique_ptr<cmCompiledGeneratorExpression> const cge =
  7418. ge.Parse(entry.Value);
  7419. cge->SetEvaluateForBuildsystem(true);
  7420. std::string const& evaluated =
  7421. cge->Evaluate(this->LocalGenerator, config, head, &dagChecker, nullptr,
  7422. this->LinkerLanguage);
  7423. bool const checkCMP0027 = evaluated != entry.Value;
  7424. cmExpandList(evaluated, llibs);
  7425. if (cge->GetHadHeadSensitiveCondition()) {
  7426. impl.HadHeadSensitiveCondition = true;
  7427. }
  7428. if (cge->GetHadContextSensitiveCondition()) {
  7429. impl.HadContextSensitiveCondition = true;
  7430. }
  7431. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  7432. impl.HadLinkLanguageSensitiveCondition = true;
  7433. }
  7434. for (std::string const& lib : llibs) {
  7435. if (this->IsLinkLookupScope(lib, lg)) {
  7436. continue;
  7437. }
  7438. // Skip entries that resolve to the target itself or are empty.
  7439. std::string name = this->CheckCMP0004(lib);
  7440. if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) {
  7441. // resolve alias name
  7442. auto* target = this->Makefile->FindTargetToUse(name);
  7443. if (target) {
  7444. name = target->GetName();
  7445. }
  7446. }
  7447. if (name == this->GetName() || name.empty()) {
  7448. if (name == this->GetName()) {
  7449. bool noMessage = false;
  7450. MessageType messageType = MessageType::FATAL_ERROR;
  7451. std::ostringstream e;
  7452. switch (this->GetPolicyStatusCMP0038()) {
  7453. case cmPolicies::WARN: {
  7454. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  7455. messageType = MessageType::AUTHOR_WARNING;
  7456. } break;
  7457. case cmPolicies::OLD:
  7458. noMessage = true;
  7459. break;
  7460. case cmPolicies::REQUIRED_IF_USED:
  7461. case cmPolicies::REQUIRED_ALWAYS:
  7462. case cmPolicies::NEW:
  7463. // Issue the fatal message.
  7464. break;
  7465. }
  7466. if (!noMessage) {
  7467. e << "Target \"" << this->GetName() << "\" links to itself.";
  7468. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  7469. messageType, e.str(), this->GetBacktrace());
  7470. if (messageType == MessageType::FATAL_ERROR) {
  7471. return;
  7472. }
  7473. }
  7474. }
  7475. continue;
  7476. }
  7477. // The entry is meant for this configuration.
  7478. cmLinkItem item =
  7479. this->ResolveLinkItem(BT<std::string>(name, entry.Backtrace), lg);
  7480. if (!item.Target) {
  7481. // Report explicitly linked object files separately.
  7482. std::string const& maybeObj = item.AsStr();
  7483. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  7484. cmSourceFile const* sf =
  7485. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  7486. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  7487. impl.Objects.emplace_back(std::move(item));
  7488. continue;
  7489. }
  7490. }
  7491. }
  7492. impl.Libraries.emplace_back(std::move(item), checkCMP0027);
  7493. }
  7494. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  7495. for (std::string const& sp : seenProps) {
  7496. if (!this->GetProperty(sp)) {
  7497. this->LinkImplicitNullProperties.insert(sp);
  7498. }
  7499. }
  7500. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  7501. }
  7502. // Update the list of direct link dependencies from usage requirements.
  7503. if (head == this) {
  7504. ComputeLinkImplTransitive(this, config, implFor, impl);
  7505. }
  7506. // Get the list of configurations considered to be DEBUG.
  7507. std::vector<std::string> debugConfigs =
  7508. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  7509. cmTargetLinkLibraryType linkType =
  7510. CMP0003_ComputeLinkType(config, debugConfigs);
  7511. cmTarget::LinkLibraryVectorType const& oldllibs =
  7512. this->Target->GetOriginalLinkLibraries();
  7513. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  7514. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  7515. std::string name = this->CheckCMP0004(oldllib.first);
  7516. if (name == this->GetName() || name.empty()) {
  7517. continue;
  7518. }
  7519. // Support OLD behavior for CMP0003.
  7520. impl.WrongConfigLibraries.push_back(
  7521. this->ResolveLinkItem(BT<std::string>(name)));
  7522. }
  7523. }
  7524. }
  7525. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  7526. std::string const& name) const
  7527. {
  7528. return this->ResolveTargetReference(name, this->LocalGenerator);
  7529. }
  7530. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  7531. std::string const& name, cmLocalGenerator const* lg) const
  7532. {
  7533. TargetOrString resolved;
  7534. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  7535. resolved.Target = tgt;
  7536. } else {
  7537. resolved.String = name;
  7538. }
  7539. return resolved;
  7540. }
  7541. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  7542. BT<std::string> const& name) const
  7543. {
  7544. return this->ResolveLinkItem(name, this->LocalGenerator);
  7545. }
  7546. cmLinkItem cmGeneratorTarget::ResolveLinkItem(BT<std::string> const& name,
  7547. cmLocalGenerator const* lg) const
  7548. {
  7549. auto bt = name.Backtrace;
  7550. TargetOrString resolved = this->ResolveTargetReference(name.Value, lg);
  7551. if (!resolved.Target) {
  7552. return cmLinkItem(resolved.String, false, bt);
  7553. }
  7554. // Check deprecation, issue message with `bt` backtrace.
  7555. if (resolved.Target->IsDeprecated()) {
  7556. std::ostringstream w;
  7557. /* clang-format off */
  7558. w <<
  7559. "The library that is being linked to, " << resolved.Target->GetName() <<
  7560. ", is marked as being deprecated by the owner. The message provided by "
  7561. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  7562. /* clang-format on */
  7563. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  7564. MessageType::AUTHOR_WARNING, w.str(), bt);
  7565. }
  7566. // Skip targets that will not really be linked. This is probably a
  7567. // name conflict between an external library and an executable
  7568. // within the project.
  7569. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  7570. !resolved.Target->IsExecutableWithExports()) {
  7571. return cmLinkItem(resolved.Target->GetName(), false, bt);
  7572. }
  7573. return cmLinkItem(resolved.Target, false, bt);
  7574. }
  7575. bool cmGeneratorTarget::HasPackageReferences() const
  7576. {
  7577. return this->IsInBuildSystem() &&
  7578. !this->GetProperty("VS_PACKAGE_REFERENCES")->empty();
  7579. }
  7580. std::vector<std::string> cmGeneratorTarget::GetPackageReferences() const
  7581. {
  7582. std::vector<std::string> packageReferences;
  7583. if (this->IsInBuildSystem()) {
  7584. if (cmValue vsPackageReferences =
  7585. this->GetProperty("VS_PACKAGE_REFERENCES")) {
  7586. cmExpandList(*vsPackageReferences, packageReferences);
  7587. }
  7588. }
  7589. return packageReferences;
  7590. }
  7591. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  7592. {
  7593. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  7594. // Return the directory in which the target will be built.
  7595. return info->PdbDir;
  7596. }
  7597. return "";
  7598. }
  7599. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  7600. {
  7601. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  7602. }
  7603. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  7604. std::string const& gnuName,
  7605. std::string& out,
  7606. const char* newExt) const
  7607. {
  7608. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  7609. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  7610. out = cmStrCat(cm::string_view(gnuName).substr(0, gnuName.size() - 6),
  7611. newExt ? newExt : ".lib");
  7612. return true;
  7613. }
  7614. return false;
  7615. }
  7616. bool cmGeneratorTarget::HasContextDependentSources() const
  7617. {
  7618. return this->SourcesAreContextDependent == Tribool::True;
  7619. }
  7620. bool cmGeneratorTarget::IsExecutableWithExports() const
  7621. {
  7622. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  7623. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  7624. }
  7625. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  7626. {
  7627. return (this->IsDLLPlatform() &&
  7628. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  7629. this->IsExecutableWithExports()) &&
  7630. // Assemblies which have only managed code do not have
  7631. // import libraries.
  7632. this->GetManagedType(config) != ManagedType::Managed) ||
  7633. (this->Target->IsAIX() && this->IsExecutableWithExports());
  7634. }
  7635. bool cmGeneratorTarget::NeedImportLibraryName(std::string const& config) const
  7636. {
  7637. return this->HasImportLibrary(config) ||
  7638. // On DLL platforms we always generate the import library name
  7639. // just in case the sources have export markup.
  7640. (this->IsDLLPlatform() &&
  7641. (this->GetType() == cmStateEnums::EXECUTABLE ||
  7642. this->GetType() == cmStateEnums::MODULE_LIBRARY));
  7643. }
  7644. std::string cmGeneratorTarget::GetSupportDirectory() const
  7645. {
  7646. std::string dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  7647. "/CMakeFiles/", this->GetName());
  7648. #if defined(__VMS)
  7649. dir += "_dir";
  7650. #else
  7651. dir += ".dir";
  7652. #endif
  7653. return dir;
  7654. }
  7655. bool cmGeneratorTarget::IsLinkable() const
  7656. {
  7657. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  7658. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  7659. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  7660. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  7661. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  7662. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  7663. this->IsExecutableWithExports());
  7664. }
  7665. bool cmGeneratorTarget::IsFrameworkOnApple() const
  7666. {
  7667. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  7668. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  7669. this->Makefile->IsOn("APPLE") &&
  7670. this->GetPropertyAsBool("FRAMEWORK"));
  7671. }
  7672. bool cmGeneratorTarget::IsAppBundleOnApple() const
  7673. {
  7674. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  7675. this->Makefile->IsOn("APPLE") &&
  7676. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  7677. }
  7678. bool cmGeneratorTarget::IsXCTestOnApple() const
  7679. {
  7680. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  7681. }
  7682. bool cmGeneratorTarget::IsCFBundleOnApple() const
  7683. {
  7684. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  7685. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  7686. }
  7687. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  7688. std::string const& propval) const
  7689. {
  7690. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  7691. // or only C++/CLI) does only depend on whether the property is an empty
  7692. // string or contains any value at all. In Visual Studio generators
  7693. // this propval is prepended with /clr[:] which results in:
  7694. //
  7695. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  7696. // lib
  7697. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  7698. // target, has import lib
  7699. // 3. netcore propval: add /clr:netcore as flag, mixed
  7700. // unmanaged/managed target, has import lib.
  7701. // 4. any value (safe,pure): add /clr:[propval] as flag, target with
  7702. // managed code only, no import lib
  7703. if (propval.empty() || propval == "netcore") {
  7704. return ManagedType::Mixed;
  7705. }
  7706. return ManagedType::Managed;
  7707. }
  7708. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  7709. const std::string& config) const
  7710. {
  7711. // Only libraries and executables can be managed targets.
  7712. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  7713. return ManagedType::Undefined;
  7714. }
  7715. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  7716. return ManagedType::Native;
  7717. }
  7718. // Check imported target.
  7719. if (this->IsImported()) {
  7720. if (cmGeneratorTarget::ImportInfo const* info =
  7721. this->GetImportInfo(config)) {
  7722. return info->Managed;
  7723. }
  7724. return ManagedType::Undefined;
  7725. }
  7726. // Check for explicitly set clr target property.
  7727. if (cmValue clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  7728. return this->CheckManagedType(*clr);
  7729. }
  7730. // C# targets are always managed. This language specific check
  7731. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  7732. // has to be set manually for C# targets.
  7733. return this->IsCSharpOnly() ? ManagedType::Managed : ManagedType::Native;
  7734. }
  7735. bool cmGeneratorTarget::AddHeaderSetVerification()
  7736. {
  7737. if (!this->GetPropertyAsBool("VERIFY_INTERFACE_HEADER_SETS")) {
  7738. return true;
  7739. }
  7740. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  7741. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  7742. this->GetType() != cmStateEnums::UNKNOWN_LIBRARY &&
  7743. this->GetType() != cmStateEnums::OBJECT_LIBRARY &&
  7744. this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  7745. !this->IsExecutableWithExports()) {
  7746. return true;
  7747. }
  7748. auto verifyValue = this->GetProperty("INTERFACE_HEADER_SETS_TO_VERIFY");
  7749. const bool all = verifyValue.IsEmpty();
  7750. std::set<std::string> verifySet;
  7751. if (!all) {
  7752. auto verifyList = cmExpandedList(verifyValue);
  7753. verifySet.insert(verifyList.begin(), verifyList.end());
  7754. }
  7755. cmTarget* verifyTarget = nullptr;
  7756. cmTarget* allVerifyTarget =
  7757. this->GlobalGenerator->GetMakefiles().front()->FindTargetToUse(
  7758. "all_verify_interface_header_sets", true);
  7759. auto interfaceFileSetEntries = this->Target->GetInterfaceHeaderSetsEntries();
  7760. std::set<cmFileSet*> fileSets;
  7761. for (auto const& entry : interfaceFileSetEntries) {
  7762. for (auto const& name : cmExpandedList(entry.Value)) {
  7763. if (all || verifySet.count(name)) {
  7764. fileSets.insert(this->Target->GetFileSet(name));
  7765. verifySet.erase(name);
  7766. }
  7767. }
  7768. }
  7769. if (!verifySet.empty()) {
  7770. this->Makefile->IssueMessage(
  7771. MessageType::FATAL_ERROR,
  7772. cmStrCat("Property INTERFACE_HEADER_SETS_TO_VERIFY of target \"",
  7773. this->GetName(),
  7774. "\" contained the following header sets that are nonexistent "
  7775. "or not INTERFACE:\n ",
  7776. cmJoin(verifySet, "\n ")));
  7777. return false;
  7778. }
  7779. cm::optional<std::set<std::string>> languages;
  7780. for (auto* fileSet : fileSets) {
  7781. auto dirCges = fileSet->CompileDirectoryEntries();
  7782. auto fileCges = fileSet->CompileFileEntries();
  7783. static auto const contextSensitive =
  7784. [](const std::unique_ptr<cmCompiledGeneratorExpression>& cge) {
  7785. return cge->GetHadContextSensitiveCondition();
  7786. };
  7787. bool dirCgesContextSensitive = false;
  7788. bool fileCgesContextSensitive = false;
  7789. std::vector<std::string> dirs;
  7790. std::map<std::string, std::vector<std::string>> filesPerDir;
  7791. bool first = true;
  7792. for (auto const& config : this->Makefile->GetGeneratorConfigs(
  7793. cmMakefile::GeneratorConfigQuery::IncludeEmptyConfig)) {
  7794. if (first || dirCgesContextSensitive) {
  7795. dirs = fileSet->EvaluateDirectoryEntries(dirCges, this->LocalGenerator,
  7796. config, this);
  7797. dirCgesContextSensitive =
  7798. std::any_of(dirCges.begin(), dirCges.end(), contextSensitive);
  7799. }
  7800. if (first || fileCgesContextSensitive) {
  7801. filesPerDir.clear();
  7802. for (auto const& fileCge : fileCges) {
  7803. fileSet->EvaluateFileEntry(dirs, filesPerDir, fileCge,
  7804. this->LocalGenerator, config, this);
  7805. if (fileCge->GetHadContextSensitiveCondition()) {
  7806. fileCgesContextSensitive = true;
  7807. }
  7808. }
  7809. }
  7810. for (auto const& files : filesPerDir) {
  7811. for (auto const& file : files.second) {
  7812. std::string filename = this->GenerateHeaderSetVerificationFile(
  7813. *this->Makefile->GetOrCreateSource(file), files.first, languages);
  7814. if (filename.empty()) {
  7815. continue;
  7816. }
  7817. if (!verifyTarget) {
  7818. {
  7819. cmMakefile::PolicyPushPop polScope(this->Makefile);
  7820. this->Makefile->SetPolicy(cmPolicies::CMP0119, cmPolicies::NEW);
  7821. verifyTarget = this->Makefile->AddLibrary(
  7822. cmStrCat(this->GetName(), "_verify_interface_header_sets"),
  7823. cmStateEnums::OBJECT_LIBRARY, {}, true);
  7824. }
  7825. verifyTarget->AddLinkLibrary(
  7826. *this->Makefile, this->GetName(),
  7827. cmTargetLinkLibraryType::GENERAL_LibraryType);
  7828. verifyTarget->SetProperty("AUTOMOC", "OFF");
  7829. verifyTarget->SetProperty("AUTORCC", "OFF");
  7830. verifyTarget->SetProperty("AUTOUIC", "OFF");
  7831. verifyTarget->SetProperty("DISABLE_PRECOMPILE_HEADERS", "ON");
  7832. verifyTarget->SetProperty("UNITY_BUILD", "OFF");
  7833. cm::optional<std::map<std::string, cmValue>>
  7834. perConfigCompileDefinitions;
  7835. verifyTarget->FinalizeTargetConfiguration(
  7836. this->Makefile->GetCompileDefinitionsEntries(),
  7837. perConfigCompileDefinitions);
  7838. if (!allVerifyTarget) {
  7839. allVerifyTarget = this->GlobalGenerator->GetMakefiles()
  7840. .front()
  7841. ->AddNewUtilityTarget(
  7842. "all_verify_interface_header_sets", true);
  7843. }
  7844. allVerifyTarget->AddUtility(verifyTarget->GetName(), false);
  7845. }
  7846. if (fileCgesContextSensitive) {
  7847. filename = cmStrCat("$<$<CONFIG:", config, ">:", filename, ">");
  7848. }
  7849. verifyTarget->AddSource(filename);
  7850. }
  7851. }
  7852. if (!dirCgesContextSensitive && !fileCgesContextSensitive) {
  7853. break;
  7854. }
  7855. first = false;
  7856. }
  7857. }
  7858. if (verifyTarget) {
  7859. this->LocalGenerator->AddGeneratorTarget(
  7860. cm::make_unique<cmGeneratorTarget>(verifyTarget, this->LocalGenerator));
  7861. }
  7862. return true;
  7863. }
  7864. std::string cmGeneratorTarget::GenerateHeaderSetVerificationFile(
  7865. cmSourceFile& source, const std::string& dir,
  7866. cm::optional<std::set<std::string>>& languages) const
  7867. {
  7868. std::string extension;
  7869. std::string language = source.GetOrDetermineLanguage();
  7870. if (language.empty()) {
  7871. if (!languages) {
  7872. languages.emplace();
  7873. for (auto const& tgtSource : this->GetAllConfigSources()) {
  7874. auto const& tgtSourceLanguage =
  7875. tgtSource.Source->GetOrDetermineLanguage();
  7876. if (tgtSourceLanguage == "CXX") {
  7877. languages->insert("CXX");
  7878. break; // C++ overrides everything else, so we don't need to keep
  7879. // checking.
  7880. }
  7881. if (tgtSourceLanguage == "C") {
  7882. languages->insert("C");
  7883. }
  7884. }
  7885. if (languages->empty()) {
  7886. std::vector<std::string> languagesVector;
  7887. this->GlobalGenerator->GetEnabledLanguages(languagesVector);
  7888. languages->insert(languagesVector.begin(), languagesVector.end());
  7889. }
  7890. }
  7891. if (languages->count("CXX")) {
  7892. language = "CXX";
  7893. } else if (languages->count("C")) {
  7894. language = "C";
  7895. }
  7896. }
  7897. if (language == "C") {
  7898. extension = ".c";
  7899. } else if (language == "CXX") {
  7900. extension = ".cxx";
  7901. } else {
  7902. return "";
  7903. }
  7904. std::string headerFilename = dir;
  7905. if (!headerFilename.empty()) {
  7906. headerFilename += '/';
  7907. }
  7908. headerFilename += source.GetLocation().GetName();
  7909. auto filename = cmStrCat(
  7910. this->LocalGenerator->GetCurrentBinaryDirectory(), '/', this->GetName(),
  7911. "_verify_interface_header_sets/", headerFilename, extension);
  7912. auto* verificationSource = this->Makefile->GetOrCreateSource(filename);
  7913. verificationSource->SetProperty("LANGUAGE", language);
  7914. cmSystemTools::MakeDirectory(cmSystemTools::GetFilenamePath(filename));
  7915. cmGeneratedFileStream fout(filename);
  7916. fout.SetCopyIfDifferent(true);
  7917. // IWYU pragma: associated allows include what you use to
  7918. // consider the headerFile as part of the entire language
  7919. // unit within include-what-you-use and as a result allows
  7920. // one to get IWYU advice for headers :)
  7921. fout << "#include <" << headerFilename << "> // IWYU pragma: associated\n";
  7922. fout.close();
  7923. return filename;
  7924. }
  7925. bool cmGeneratorTarget::HaveFortranSources(std::string const& config) const
  7926. {
  7927. auto sources = cmGeneratorTarget::GetSourceFiles(config);
  7928. return std::any_of(sources.begin(), sources.end(),
  7929. [](BT<cmSourceFile*> const& sf) -> bool {
  7930. return sf.Value->GetLanguage() == "Fortran"_s;
  7931. });
  7932. }
  7933. bool cmGeneratorTarget::HaveCxx20ModuleSources() const
  7934. {
  7935. auto const& fs_names = this->Target->GetAllFileSetNames();
  7936. return std::any_of(fs_names.begin(), fs_names.end(),
  7937. [this](std::string const& name) -> bool {
  7938. auto const* file_set = this->Target->GetFileSet(name);
  7939. if (!file_set) {
  7940. this->Makefile->IssueMessage(
  7941. MessageType::INTERNAL_ERROR,
  7942. cmStrCat("Target \"", this->Target->GetName(),
  7943. "\" is tracked to have file set \"", name,
  7944. "\", but it was not found."));
  7945. return false;
  7946. }
  7947. auto const& fs_type = file_set->GetType();
  7948. return fs_type == "CXX_MODULES"_s ||
  7949. fs_type == "CXX_MODULE_HEADER_UNITS"_s;
  7950. });
  7951. }
  7952. cmGeneratorTarget::Cxx20SupportLevel cmGeneratorTarget::HaveCxxModuleSupport(
  7953. std::string const& config) const
  7954. {
  7955. auto const* state = this->Makefile->GetState();
  7956. if (!state->GetLanguageEnabled("CXX")) {
  7957. return Cxx20SupportLevel::MissingCxx;
  7958. }
  7959. cmValue standardDefault =
  7960. this->Target->GetMakefile()->GetDefinition("CMAKE_CXX_STANDARD_DEFAULT");
  7961. if (standardDefault && !standardDefault->empty()) {
  7962. cmStandardLevelResolver standardResolver(this->Makefile);
  7963. if (!standardResolver.HaveStandardAvailable(this, "CXX", config,
  7964. "cxx_std_20")) {
  7965. return Cxx20SupportLevel::NoCxx20;
  7966. }
  7967. }
  7968. // Else, an empty CMAKE_CXX_STANDARD_DEFAULT means CMake does not detect and
  7969. // set a default standard level for this compiler, so assume all standards
  7970. // are available.
  7971. if (!this->Makefile->IsOn("CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP")) {
  7972. return Cxx20SupportLevel::MissingExperimentalFlag;
  7973. }
  7974. return Cxx20SupportLevel::Supported;
  7975. }
  7976. void cmGeneratorTarget::CheckCxxModuleStatus(std::string const& config) const
  7977. {
  7978. // Check for `CXX_MODULE*` file sets and a lack of support.
  7979. if (this->HaveCxx20ModuleSources()) {
  7980. switch (this->HaveCxxModuleSupport(config)) {
  7981. case cmGeneratorTarget::Cxx20SupportLevel::MissingCxx:
  7982. this->Makefile->IssueMessage(
  7983. MessageType::FATAL_ERROR,
  7984. cmStrCat("The \"", this->GetName(),
  7985. "\" target has C++ module sources but the \"CXX\" language "
  7986. "has not been enabled"));
  7987. break;
  7988. case cmGeneratorTarget::Cxx20SupportLevel::MissingExperimentalFlag:
  7989. this->Makefile->IssueMessage(
  7990. MessageType::FATAL_ERROR,
  7991. cmStrCat("The \"", this->GetName(),
  7992. "\" target has C++ module sources but its experimental "
  7993. "support has not been requested"));
  7994. break;
  7995. case cmGeneratorTarget::Cxx20SupportLevel::NoCxx20:
  7996. this->Makefile->IssueMessage(
  7997. MessageType::FATAL_ERROR,
  7998. cmStrCat(
  7999. "The \"", this->GetName(),
  8000. "\" target has C++ module sources but is not using at least "
  8001. "\"cxx_std_20\""));
  8002. break;
  8003. case cmGeneratorTarget::Cxx20SupportLevel::Supported:
  8004. // All is well.
  8005. break;
  8006. }
  8007. }
  8008. }
  8009. bool cmGeneratorTarget::NeedCxxModuleSupport(std::string const& lang,
  8010. std::string const& config) const
  8011. {
  8012. if (lang != "CXX"_s) {
  8013. return false;
  8014. }
  8015. return this->HaveCxxModuleSupport(config) == Cxx20SupportLevel::Supported &&
  8016. this->GetGlobalGenerator()->CheckCxxModuleSupport();
  8017. }
  8018. bool cmGeneratorTarget::NeedDyndep(std::string const& lang,
  8019. std::string const& config) const
  8020. {
  8021. return lang == "Fortran"_s || this->NeedCxxModuleSupport(lang, config);
  8022. }
  8023. cmFileSet const* cmGeneratorTarget::GetFileSetForSource(
  8024. std::string const& config, cmSourceFile const* sf) const
  8025. {
  8026. this->BuildFileSetInfoCache(config);
  8027. auto const& path = sf->GetFullPath();
  8028. auto const& per_config = this->Configs[config];
  8029. auto const fsit = per_config.FileSetCache.find(path);
  8030. if (fsit == per_config.FileSetCache.end()) {
  8031. return nullptr;
  8032. }
  8033. return fsit->second;
  8034. }
  8035. bool cmGeneratorTarget::NeedDyndepForSource(std::string const& lang,
  8036. std::string const& config,
  8037. cmSourceFile const* sf) const
  8038. {
  8039. bool const needDyndep = this->NeedDyndep(lang, config);
  8040. if (!needDyndep) {
  8041. return false;
  8042. }
  8043. auto const* fs = this->GetFileSetForSource(config, sf);
  8044. if (fs &&
  8045. (fs->GetType() == "CXX_MODULES"_s ||
  8046. fs->GetType() == "CXX_MODULE_HEADER_UNITS"_s)) {
  8047. return true;
  8048. }
  8049. auto const sfProp = sf->GetProperty("CXX_SCAN_FOR_MODULES");
  8050. if (sfProp.IsSet()) {
  8051. return sfProp.IsOn();
  8052. }
  8053. auto const tgtProp = this->GetProperty("CXX_SCAN_FOR_MODULES");
  8054. if (tgtProp.IsSet()) {
  8055. return tgtProp.IsOn();
  8056. }
  8057. return true;
  8058. }
  8059. void cmGeneratorTarget::BuildFileSetInfoCache(std::string const& config) const
  8060. {
  8061. auto& per_config = this->Configs[config];
  8062. if (per_config.BuiltFileSetCache) {
  8063. return;
  8064. }
  8065. auto const* tgt = this->Target;
  8066. for (auto const& name : tgt->GetAllFileSetNames()) {
  8067. auto const* file_set = tgt->GetFileSet(name);
  8068. if (!file_set) {
  8069. tgt->GetMakefile()->IssueMessage(
  8070. MessageType::INTERNAL_ERROR,
  8071. cmStrCat("Target \"", tgt->GetName(),
  8072. "\" is tracked to have file set \"", name,
  8073. "\", but it was not found."));
  8074. continue;
  8075. }
  8076. auto fileEntries = file_set->CompileFileEntries();
  8077. auto directoryEntries = file_set->CompileDirectoryEntries();
  8078. auto directories = file_set->EvaluateDirectoryEntries(
  8079. directoryEntries, this->LocalGenerator, config, this);
  8080. std::map<std::string, std::vector<std::string>> files;
  8081. for (auto const& entry : fileEntries) {
  8082. file_set->EvaluateFileEntry(directories, files, entry,
  8083. this->LocalGenerator, config, this);
  8084. }
  8085. for (auto const& it : files) {
  8086. for (auto const& filename : it.second) {
  8087. per_config.FileSetCache[filename] = file_set;
  8088. }
  8089. }
  8090. }
  8091. per_config.BuiltFileSetCache = true;
  8092. }