cmGeneratorTarget.cxx 325 KB

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