cmGeneratorTarget.cxx 290 KB

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