cmGeneratorTarget.cxx 303 KB

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