cmGeneratorTarget.cxx 281 KB

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