cmGeneratorTarget.cxx 307 KB

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