cmGeneratorTarget.cxx 275 KB

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