cmGeneratorTarget.cxx 254 KB

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