cmGeneratorTarget.cxx 315 KB

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