cmGeneratorTarget.cxx 327 KB

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