cmGeneratorTarget.cxx 276 KB

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