cmGeneratorTarget.cxx 247 KB

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