cmGeneratorTarget.cxx 307 KB

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