cmGeneratorTarget.cxx 320 KB

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