cmGeneratorTarget.cxx 216 KB

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