cmGeneratorTarget.cxx 301 KB

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