cmGeneratorTarget.cxx 232 KB

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