cmGeneratorTarget.cxx 302 KB

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