cmGeneratorTarget.cxx 218 KB

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