cmGeneratorTarget.cxx 249 KB

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