cmGeneratorTarget.cxx 297 KB

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