cmGeneratorTarget.cxx 233 KB

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