cmGeneratorTarget.cxx 258 KB

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