cmGeneratorTarget.cxx 249 KB

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