cmGeneratorTarget.cxx 281 KB

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