cmGeneratorTarget.cxx 229 KB

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