cmGeneratorTarget.cxx 270 KB

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