cmGeneratorTarget.cxx 294 KB

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