cmGeneratorTarget.cxx 302 KB

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