cmGeneratorTarget.cxx 258 KB

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