cmGeneratorTarget.cxx 317 KB

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