cmGeneratorTarget.cxx 216 KB

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