cmGeneratorTarget.cxx 216 KB

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