cmGeneratorTarget.cxx 310 KB

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