cmGeneratorTarget.cxx 254 KB

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