cmGeneratorTarget.cxx 315 KB

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