cmGeneratorTarget.cxx 253 KB

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