cmGeneratorTarget.cxx 284 KB

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