cmGeneratorTarget.cxx 304 KB

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