cmGeneratorTarget.cxx 290 KB

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