cmGeneratorTarget.cxx 216 KB

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