cmGeneratorTarget.cxx 255 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655
  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. if (archVec.empty()) {
  2835. this->Makefile->GetDefExpandList("_CMAKE_APPLE_ARCHS_DEFAULT", archVec);
  2836. }
  2837. }
  2838. void cmGeneratorTarget::AddCUDAArchitectureFlags(std::string& flags) const
  2839. {
  2840. const std::string& property = this->GetSafeProperty("CUDA_ARCHITECTURES");
  2841. if (property.empty()) {
  2842. switch (this->GetPolicyStatusCMP0104()) {
  2843. case cmPolicies::WARN:
  2844. if (!this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile()) {
  2845. this->Makefile->IssueMessage(
  2846. MessageType::AUTHOR_WARNING,
  2847. cmPolicies::GetPolicyWarning(cmPolicies::CMP0104) +
  2848. "\nCUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  2849. "\".");
  2850. }
  2851. CM_FALLTHROUGH;
  2852. case cmPolicies::OLD:
  2853. break;
  2854. default:
  2855. this->Makefile->IssueMessage(
  2856. MessageType::FATAL_ERROR,
  2857. "CUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  2858. "\".");
  2859. }
  2860. }
  2861. // If CUDA_ARCHITECTURES is false we don't add any architectures.
  2862. if (cmIsOff(property)) {
  2863. return;
  2864. }
  2865. struct CudaArchitecture
  2866. {
  2867. std::string name;
  2868. bool real{ true };
  2869. bool virtual_{ true };
  2870. };
  2871. std::vector<CudaArchitecture> architectures;
  2872. {
  2873. std::vector<std::string> options;
  2874. cmExpandList(property, options);
  2875. for (std::string& option : options) {
  2876. CudaArchitecture architecture;
  2877. // Architecture name is up to the first specifier.
  2878. std::size_t pos = option.find_first_of('-');
  2879. architecture.name = option.substr(0, pos);
  2880. if (pos != std::string::npos) {
  2881. cm::string_view specifier{ option.c_str() + pos + 1,
  2882. option.length() - pos - 1 };
  2883. if (specifier == "real") {
  2884. architecture.real = true;
  2885. architecture.virtual_ = false;
  2886. } else if (specifier == "virtual") {
  2887. architecture.real = false;
  2888. architecture.virtual_ = true;
  2889. } else {
  2890. this->Makefile->IssueMessage(
  2891. MessageType::FATAL_ERROR,
  2892. "Unknown CUDA architecture specifier \"" + std::string(specifier) +
  2893. "\".");
  2894. }
  2895. }
  2896. architectures.emplace_back(architecture);
  2897. }
  2898. }
  2899. std::string const& compiler =
  2900. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  2901. if (compiler == "NVIDIA") {
  2902. for (CudaArchitecture& architecture : architectures) {
  2903. flags +=
  2904. " --generate-code=arch=compute_" + architecture.name + ",code=[";
  2905. if (architecture.virtual_) {
  2906. flags += "compute_" + architecture.name;
  2907. if (architecture.real) {
  2908. flags += ",";
  2909. }
  2910. }
  2911. if (architecture.real) {
  2912. flags += "sm_" + architecture.name;
  2913. }
  2914. flags += "]";
  2915. }
  2916. } else if (compiler == "Clang") {
  2917. for (CudaArchitecture& architecture : architectures) {
  2918. flags += " --cuda-gpu-arch=sm_" + architecture.name;
  2919. if (!architecture.real) {
  2920. Makefile->IssueMessage(
  2921. MessageType::WARNING,
  2922. "Clang doesn't support disabling CUDA real code generation.");
  2923. }
  2924. if (!architecture.virtual_) {
  2925. flags += " --no-cuda-include-ptx=sm_" + architecture.name;
  2926. }
  2927. }
  2928. }
  2929. }
  2930. void cmGeneratorTarget::AddISPCTargetFlags(std::string& flags) const
  2931. {
  2932. const std::string& property = this->GetSafeProperty("ISPC_INSTRUCTION_SETS");
  2933. // If ISPC_TARGET is false we don't add any architectures.
  2934. if (cmIsOff(property)) {
  2935. return;
  2936. }
  2937. std::string const& compiler =
  2938. this->Makefile->GetSafeDefinition("CMAKE_ISPC_COMPILER_ID");
  2939. if (compiler == "Intel") {
  2940. std::vector<std::string> targets;
  2941. cmExpandList(property, targets);
  2942. if (!targets.empty()) {
  2943. flags += cmStrCat(" --target=", cmWrap("", targets, "", ","));
  2944. }
  2945. }
  2946. }
  2947. void cmGeneratorTarget::AddCUDAToolkitFlags(std::string& flags) const
  2948. {
  2949. std::string const& compiler =
  2950. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  2951. if (compiler == "Clang") {
  2952. // Pass CUDA toolkit explicitly to Clang.
  2953. // Clang's searching for the system CUDA toolkit isn't very good and it's
  2954. // expected the user will explicitly pass the toolkit path.
  2955. // This also avoids Clang having to search for the toolkit on every
  2956. // invocation.
  2957. std::string toolkitRoot =
  2958. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_LIBRARY_ROOT");
  2959. if (!toolkitRoot.empty()) {
  2960. flags += " --cuda-path=" +
  2961. this->LocalGenerator->ConvertToOutputFormat(toolkitRoot,
  2962. cmOutputConverter::SHELL);
  2963. }
  2964. }
  2965. }
  2966. //----------------------------------------------------------------------------
  2967. std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
  2968. std::string const& var, std::string const& lang,
  2969. std::string const& config) const
  2970. {
  2971. if (this->IsIPOEnabled(lang, config)) {
  2972. std::string varIPO = var + "_IPO";
  2973. if (this->Makefile->IsDefinitionSet(varIPO)) {
  2974. return varIPO;
  2975. }
  2976. }
  2977. return var;
  2978. }
  2979. //----------------------------------------------------------------------------
  2980. std::string cmGeneratorTarget::GetCreateRuleVariable(
  2981. std::string const& lang, std::string const& config) const
  2982. {
  2983. switch (this->GetType()) {
  2984. case cmStateEnums::STATIC_LIBRARY: {
  2985. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  2986. return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
  2987. }
  2988. case cmStateEnums::SHARED_LIBRARY:
  2989. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  2990. case cmStateEnums::MODULE_LIBRARY:
  2991. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  2992. case cmStateEnums::EXECUTABLE:
  2993. if (this->IsExecutableWithExports()) {
  2994. std::string linkExeWithExports =
  2995. "CMAKE_" + lang + "_LINK_EXECUTABLE_WITH_EXPORTS";
  2996. if (this->Makefile->IsDefinitionSet(linkExeWithExports)) {
  2997. return linkExeWithExports;
  2998. }
  2999. }
  3000. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  3001. default:
  3002. break;
  3003. }
  3004. return "";
  3005. }
  3006. namespace {
  3007. void processIncludeDirectories(cmGeneratorTarget const* tgt,
  3008. EvaluatedTargetPropertyEntries& entries,
  3009. std::vector<BT<std::string>>& includes,
  3010. std::unordered_set<std::string>& uniqueIncludes,
  3011. bool debugIncludes)
  3012. {
  3013. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  3014. cmLinkImplItem const& item = entry.LinkImplItem;
  3015. std::string const& targetName = item.AsStr();
  3016. bool const fromImported = item.Target && item.Target->IsImported();
  3017. bool const checkCMP0027 = item.FromGenex;
  3018. std::string usedIncludes;
  3019. for (std::string& entryInclude : entry.Values) {
  3020. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  3021. std::ostringstream e;
  3022. MessageType messageType = MessageType::FATAL_ERROR;
  3023. if (checkCMP0027) {
  3024. switch (tgt->GetPolicyStatusCMP0027()) {
  3025. case cmPolicies::WARN:
  3026. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0027) << "\n";
  3027. CM_FALLTHROUGH;
  3028. case cmPolicies::OLD:
  3029. messageType = MessageType::AUTHOR_WARNING;
  3030. break;
  3031. case cmPolicies::REQUIRED_ALWAYS:
  3032. case cmPolicies::REQUIRED_IF_USED:
  3033. case cmPolicies::NEW:
  3034. break;
  3035. }
  3036. }
  3037. /* clang-format off */
  3038. e << "Imported target \"" << targetName << "\" includes "
  3039. "non-existent path\n \"" << entryInclude << "\"\nin its "
  3040. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  3041. "* The path was deleted, renamed, or moved to another "
  3042. "location.\n"
  3043. "* An install or uninstall procedure did not complete "
  3044. "successfully.\n"
  3045. "* The installation package was faulty and references files it "
  3046. "does not provide.\n";
  3047. /* clang-format on */
  3048. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3049. return;
  3050. }
  3051. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  3052. std::ostringstream e;
  3053. bool noMessage = false;
  3054. MessageType messageType = MessageType::FATAL_ERROR;
  3055. if (!targetName.empty()) {
  3056. /* clang-format off */
  3057. e << "Target \"" << targetName << "\" contains relative "
  3058. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  3059. " \"" << entryInclude << "\"";
  3060. /* clang-format on */
  3061. } else {
  3062. switch (tgt->GetPolicyStatusCMP0021()) {
  3063. case cmPolicies::WARN: {
  3064. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  3065. messageType = MessageType::AUTHOR_WARNING;
  3066. } break;
  3067. case cmPolicies::OLD:
  3068. noMessage = true;
  3069. case cmPolicies::REQUIRED_IF_USED:
  3070. case cmPolicies::REQUIRED_ALWAYS:
  3071. case cmPolicies::NEW:
  3072. // Issue the fatal message.
  3073. break;
  3074. }
  3075. e << "Found relative path while evaluating include directories of "
  3076. "\""
  3077. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  3078. }
  3079. if (!noMessage) {
  3080. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3081. if (messageType == MessageType::FATAL_ERROR) {
  3082. return;
  3083. }
  3084. }
  3085. }
  3086. if (!cmIsOff(entryInclude)) {
  3087. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  3088. }
  3089. if (uniqueIncludes.insert(entryInclude).second) {
  3090. includes.emplace_back(entryInclude, entry.Backtrace);
  3091. if (debugIncludes) {
  3092. usedIncludes += " * " + entryInclude + "\n";
  3093. }
  3094. }
  3095. }
  3096. if (!usedIncludes.empty()) {
  3097. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3098. MessageType::LOG,
  3099. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  3100. usedIncludes,
  3101. entry.Backtrace);
  3102. }
  3103. }
  3104. }
  3105. }
  3106. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  3107. const std::string& config, const std::string& lang) const
  3108. {
  3109. std::vector<BT<std::string>> includes;
  3110. std::unordered_set<std::string> uniqueIncludes;
  3111. cmGeneratorExpressionDAGChecker dagChecker(this, "INCLUDE_DIRECTORIES",
  3112. nullptr, nullptr);
  3113. std::vector<std::string> debugProperties;
  3114. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3115. debugProperties);
  3116. bool debugIncludes = !this->DebugIncludesDone &&
  3117. cm::contains(debugProperties, "INCLUDE_DIRECTORIES");
  3118. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3119. this->DebugIncludesDone = true;
  3120. }
  3121. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3122. this, config, lang, &dagChecker, this->IncludeDirectoriesEntries);
  3123. if (lang == "Swift") {
  3124. AddLangSpecificImplicitIncludeDirectories(
  3125. this, lang, config, "Swift_MODULE_DIRECTORY",
  3126. IncludeDirectoryFallBack::BINARY, entries);
  3127. }
  3128. if (this->CanCompileSources() && (lang != "Swift" && lang != "Fortran")) {
  3129. const std::string propertyName = "ISPC_HEADER_DIRECTORY";
  3130. // If this target has ISPC sources make sure to add the header
  3131. // directory to other compilation units
  3132. if (cm::contains(this->GetAllConfigCompileLanguages(), "ISPC")) {
  3133. if (cmProp val = this->GetProperty(propertyName)) {
  3134. includes.emplace_back(*val);
  3135. } else {
  3136. includes.emplace_back(this->GetObjectDirectory(config));
  3137. }
  3138. }
  3139. AddLangSpecificImplicitIncludeDirectories(
  3140. this, "ISPC", config, propertyName, IncludeDirectoryFallBack::OBJECT,
  3141. entries);
  3142. }
  3143. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES", lang,
  3144. &dagChecker, entries);
  3145. if (this->Makefile->IsOn("APPLE")) {
  3146. if (cmLinkImplementationLibraries const* impl =
  3147. this->GetLinkImplementationLibraries(config)) {
  3148. for (cmLinkImplItem const& lib : impl->Libraries) {
  3149. std::string libDir = cmSystemTools::CollapseFullPath(
  3150. lib.AsStr(), this->Makefile->GetHomeOutputDirectory());
  3151. static cmsys::RegularExpression frameworkCheck(
  3152. "(.*\\.framework)(/Versions/[^/]+)?/[^/]+$");
  3153. if (!frameworkCheck.find(libDir)) {
  3154. continue;
  3155. }
  3156. libDir = frameworkCheck.match(1);
  3157. EvaluatedTargetPropertyEntry ee(lib, cmListFileBacktrace());
  3158. ee.Values.emplace_back(std::move(libDir));
  3159. entries.Entries.emplace_back(std::move(ee));
  3160. }
  3161. }
  3162. }
  3163. processIncludeDirectories(this, entries, includes, uniqueIncludes,
  3164. debugIncludes);
  3165. return includes;
  3166. }
  3167. enum class OptionsParse
  3168. {
  3169. None,
  3170. Shell
  3171. };
  3172. namespace {
  3173. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  3174. const auto DL_END = "</DEVICE_LINK>"_s;
  3175. void processOptions(cmGeneratorTarget const* tgt,
  3176. EvaluatedTargetPropertyEntries const& entries,
  3177. std::vector<BT<std::string>>& options,
  3178. std::unordered_set<std::string>& uniqueOptions,
  3179. bool debugOptions, const char* logName, OptionsParse parse,
  3180. bool processDeviceOptions = false)
  3181. {
  3182. bool splitOption = !processDeviceOptions;
  3183. for (EvaluatedTargetPropertyEntry const& entry : entries.Entries) {
  3184. std::string usedOptions;
  3185. for (std::string const& opt : entry.Values) {
  3186. if (processDeviceOptions && (opt == DL_BEGIN || opt == DL_END)) {
  3187. options.emplace_back(opt, entry.Backtrace);
  3188. splitOption = opt == DL_BEGIN;
  3189. continue;
  3190. }
  3191. if (uniqueOptions.insert(opt).second) {
  3192. if (parse == OptionsParse::Shell &&
  3193. cmHasLiteralPrefix(opt, "SHELL:")) {
  3194. if (splitOption) {
  3195. std::vector<std::string> tmp;
  3196. cmSystemTools::ParseUnixCommandLine(opt.c_str() + 6, tmp);
  3197. for (std::string& o : tmp) {
  3198. options.emplace_back(std::move(o), entry.Backtrace);
  3199. }
  3200. } else {
  3201. options.emplace_back(std::string(opt.c_str() + 6),
  3202. entry.Backtrace);
  3203. }
  3204. } else {
  3205. options.emplace_back(opt, entry.Backtrace);
  3206. }
  3207. if (debugOptions) {
  3208. usedOptions += " * " + opt + "\n";
  3209. }
  3210. }
  3211. }
  3212. if (!usedOptions.empty()) {
  3213. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3214. MessageType::LOG,
  3215. std::string("Used ") + logName + std::string(" for target ") +
  3216. tgt->GetName() + ":\n" + usedOptions,
  3217. entry.Backtrace);
  3218. }
  3219. }
  3220. }
  3221. std::vector<BT<std::string>> wrapOptions(
  3222. std::vector<std::string>& options, const cmListFileBacktrace& bt,
  3223. const std::vector<std::string>& wrapperFlag, const std::string& wrapperSep,
  3224. bool concatFlagAndArgs)
  3225. {
  3226. std::vector<BT<std::string>> result;
  3227. if (options.empty()) {
  3228. return result;
  3229. }
  3230. if (wrapperFlag.empty() || cmHasLiteralPrefix(options.front(), "LINKER:")) {
  3231. // nothing specified or LINKER wrapper, insert elements as is
  3232. result.reserve(options.size());
  3233. for (std::string& o : options) {
  3234. result.emplace_back(std::move(o), bt);
  3235. }
  3236. } else {
  3237. if (!wrapperSep.empty()) {
  3238. if (concatFlagAndArgs) {
  3239. // insert flag elements except last one
  3240. for (auto i = wrapperFlag.begin(); i != wrapperFlag.end() - 1; ++i) {
  3241. result.emplace_back(*i, bt);
  3242. }
  3243. // concatenate last flag element and all list values
  3244. // in one option
  3245. result.emplace_back(wrapperFlag.back() + cmJoin(options, wrapperSep),
  3246. bt);
  3247. } else {
  3248. for (std::string const& i : wrapperFlag) {
  3249. result.emplace_back(i, bt);
  3250. }
  3251. // concatenate all list values in one option
  3252. result.emplace_back(cmJoin(options, wrapperSep), bt);
  3253. }
  3254. } else {
  3255. // prefix each element of list with wrapper
  3256. if (concatFlagAndArgs) {
  3257. std::transform(options.begin(), options.end(), options.begin(),
  3258. [&wrapperFlag](std::string const& o) -> std::string {
  3259. return wrapperFlag.back() + o;
  3260. });
  3261. }
  3262. for (std::string& o : options) {
  3263. for (auto i = wrapperFlag.begin(),
  3264. e = concatFlagAndArgs ? wrapperFlag.end() - 1
  3265. : wrapperFlag.end();
  3266. i != e; ++i) {
  3267. result.emplace_back(*i, bt);
  3268. }
  3269. result.emplace_back(std::move(o), bt);
  3270. }
  3271. }
  3272. }
  3273. return result;
  3274. }
  3275. }
  3276. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  3277. const std::string& config,
  3278. const std::string& language) const
  3279. {
  3280. std::vector<BT<std::string>> tmp = this->GetCompileOptions(config, language);
  3281. result.reserve(tmp.size());
  3282. for (BT<std::string>& v : tmp) {
  3283. result.emplace_back(std::move(v.Value));
  3284. }
  3285. }
  3286. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileOptions(
  3287. std::string const& config, std::string const& language) const
  3288. {
  3289. std::vector<BT<std::string>> result;
  3290. std::unordered_set<std::string> uniqueOptions;
  3291. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_OPTIONS", nullptr,
  3292. nullptr);
  3293. std::vector<std::string> debugProperties;
  3294. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3295. debugProperties);
  3296. bool debugOptions = !this->DebugCompileOptionsDone &&
  3297. cm::contains(debugProperties, "COMPILE_OPTIONS");
  3298. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3299. this->DebugCompileOptionsDone = true;
  3300. }
  3301. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3302. this, config, language, &dagChecker, this->CompileOptionsEntries);
  3303. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS", language,
  3304. &dagChecker, entries);
  3305. processOptions(this, entries, result, uniqueOptions, debugOptions,
  3306. "compile options", OptionsParse::Shell);
  3307. return result;
  3308. }
  3309. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  3310. const std::string& config) const
  3311. {
  3312. std::vector<BT<std::string>> tmp = this->GetCompileFeatures(config);
  3313. result.reserve(tmp.size());
  3314. for (BT<std::string>& v : tmp) {
  3315. result.emplace_back(std::move(v.Value));
  3316. }
  3317. }
  3318. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileFeatures(
  3319. std::string const& config) const
  3320. {
  3321. std::vector<BT<std::string>> result;
  3322. std::unordered_set<std::string> uniqueFeatures;
  3323. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_FEATURES", nullptr,
  3324. nullptr);
  3325. std::vector<std::string> debugProperties;
  3326. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3327. debugProperties);
  3328. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  3329. cm::contains(debugProperties, "COMPILE_FEATURES");
  3330. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3331. this->DebugCompileFeaturesDone = true;
  3332. }
  3333. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3334. this, config, std::string(), &dagChecker, this->CompileFeaturesEntries);
  3335. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  3336. std::string(), &dagChecker, entries);
  3337. processOptions(this, entries, result, uniqueFeatures, debugFeatures,
  3338. "compile features", OptionsParse::None);
  3339. return result;
  3340. }
  3341. void cmGeneratorTarget::GetCompileDefinitions(
  3342. std::vector<std::string>& result, const std::string& config,
  3343. const std::string& language) const
  3344. {
  3345. std::vector<BT<std::string>> tmp =
  3346. this->GetCompileDefinitions(config, language);
  3347. result.reserve(tmp.size());
  3348. for (BT<std::string>& v : tmp) {
  3349. result.emplace_back(std::move(v.Value));
  3350. }
  3351. }
  3352. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileDefinitions(
  3353. std::string const& config, std::string const& language) const
  3354. {
  3355. std::vector<BT<std::string>> list;
  3356. std::unordered_set<std::string> uniqueOptions;
  3357. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_DEFINITIONS",
  3358. nullptr, nullptr);
  3359. std::vector<std::string> debugProperties;
  3360. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3361. debugProperties);
  3362. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  3363. cm::contains(debugProperties, "COMPILE_DEFINITIONS");
  3364. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3365. this->DebugCompileDefinitionsDone = true;
  3366. }
  3367. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3368. this, config, language, &dagChecker, this->CompileDefinitionsEntries);
  3369. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS", language,
  3370. &dagChecker, entries);
  3371. if (!config.empty()) {
  3372. std::string configPropName =
  3373. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  3374. cmProp configProp = this->GetProperty(configPropName);
  3375. if (configProp) {
  3376. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  3377. case cmPolicies::WARN: {
  3378. this->LocalGenerator->IssueMessage(
  3379. MessageType::AUTHOR_WARNING,
  3380. cmPolicies::GetPolicyWarning(cmPolicies::CMP0043));
  3381. CM_FALLTHROUGH;
  3382. }
  3383. case cmPolicies::OLD: {
  3384. std::unique_ptr<TargetPropertyEntry> entry =
  3385. CreateTargetPropertyEntry(*configProp);
  3386. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  3387. this, config, language, &dagChecker, *entry));
  3388. } break;
  3389. case cmPolicies::NEW:
  3390. case cmPolicies::REQUIRED_ALWAYS:
  3391. case cmPolicies::REQUIRED_IF_USED:
  3392. break;
  3393. }
  3394. }
  3395. }
  3396. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3397. "compile definitions", OptionsParse::None);
  3398. return list;
  3399. }
  3400. std::vector<BT<std::string>> cmGeneratorTarget::GetPrecompileHeaders(
  3401. const std::string& config, const std::string& language) const
  3402. {
  3403. std::unordered_set<std::string> uniqueOptions;
  3404. cmGeneratorExpressionDAGChecker dagChecker(this, "PRECOMPILE_HEADERS",
  3405. nullptr, nullptr);
  3406. std::vector<std::string> debugProperties;
  3407. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3408. debugProperties);
  3409. bool debugDefines = !this->DebugPrecompileHeadersDone &&
  3410. std::find(debugProperties.begin(), debugProperties.end(),
  3411. "PRECOMPILE_HEADERS") != debugProperties.end();
  3412. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3413. this->DebugPrecompileHeadersDone = true;
  3414. }
  3415. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3416. this, config, language, &dagChecker, this->PrecompileHeadersEntries);
  3417. AddInterfaceEntries(this, config, "INTERFACE_PRECOMPILE_HEADERS", language,
  3418. &dagChecker, entries);
  3419. std::vector<BT<std::string>> list;
  3420. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3421. "precompile headers", OptionsParse::None);
  3422. return list;
  3423. }
  3424. std::string cmGeneratorTarget::GetPchHeader(const std::string& config,
  3425. const std::string& language,
  3426. const std::string& arch) const
  3427. {
  3428. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3429. language != "OBJCXX") {
  3430. return std::string();
  3431. }
  3432. if (this->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
  3433. return std::string();
  3434. }
  3435. const cmGeneratorTarget* generatorTarget = this;
  3436. cmProp pchReuseFrom =
  3437. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3438. const auto inserted =
  3439. this->PchHeaders.insert(std::make_pair(language + config + arch, ""));
  3440. if (inserted.second) {
  3441. const std::vector<BT<std::string>> headers =
  3442. this->GetPrecompileHeaders(config, language);
  3443. if (headers.empty() && !pchReuseFrom) {
  3444. return std::string();
  3445. }
  3446. std::string& filename = inserted.first->second;
  3447. if (pchReuseFrom) {
  3448. generatorTarget =
  3449. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3450. }
  3451. filename = cmStrCat(
  3452. generatorTarget->LocalGenerator->GetCurrentBinaryDirectory(), "/");
  3453. const std::map<std::string, std::string> languageToExtension = {
  3454. { "C", ".h" },
  3455. { "CXX", ".hxx" },
  3456. { "OBJC", ".objc.h" },
  3457. { "OBJCXX", ".objcxx.hxx" }
  3458. };
  3459. filename =
  3460. cmStrCat(filename, "CMakeFiles/", generatorTarget->GetName(), ".dir");
  3461. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  3462. filename = cmStrCat(filename, "/", config);
  3463. }
  3464. filename =
  3465. cmStrCat(filename, "/cmake_pch", arch.empty() ? "" : cmStrCat("_", arch),
  3466. languageToExtension.at(language));
  3467. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3468. if (!pchReuseFrom) {
  3469. cmProp pchPrologue = this->Makefile->GetDefinition("CMAKE_PCH_PROLOGUE");
  3470. cmProp pchEpilogue = this->Makefile->GetDefinition("CMAKE_PCH_EPILOGUE");
  3471. std::string firstHeaderOnDisk;
  3472. {
  3473. cmGeneratedFileStream file(
  3474. filename_tmp, false,
  3475. this->GetGlobalGenerator()->GetMakefileEncoding());
  3476. file << "/* generated by CMake */\n\n";
  3477. if (pchPrologue) {
  3478. file << *pchPrologue << "\n";
  3479. }
  3480. if (this->GetGlobalGenerator()->IsXcode()) {
  3481. file << "#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3482. }
  3483. if (language == "CXX") {
  3484. file << "#ifdef __cplusplus\n";
  3485. }
  3486. for (auto const& header_bt : headers) {
  3487. if (header_bt.Value.empty()) {
  3488. continue;
  3489. }
  3490. if (header_bt.Value[0] == '<' || header_bt.Value[0] == '\"') {
  3491. file << "#include " << header_bt.Value << "\n";
  3492. } else {
  3493. file << "#include \"" << header_bt.Value << "\"\n";
  3494. }
  3495. if (cmSystemTools::FileExists(header_bt.Value) &&
  3496. firstHeaderOnDisk.empty()) {
  3497. firstHeaderOnDisk = header_bt.Value;
  3498. }
  3499. }
  3500. if (language == "CXX") {
  3501. file << "#endif // __cplusplus\n";
  3502. }
  3503. if (this->GetGlobalGenerator()->IsXcode()) {
  3504. file << "#endif // CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3505. }
  3506. if (pchEpilogue) {
  3507. file << *pchEpilogue << "\n";
  3508. }
  3509. }
  3510. if (!firstHeaderOnDisk.empty()) {
  3511. cmFileTimes::Copy(firstHeaderOnDisk, filename_tmp);
  3512. }
  3513. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3514. }
  3515. }
  3516. return inserted.first->second;
  3517. }
  3518. std::string cmGeneratorTarget::GetPchSource(const std::string& config,
  3519. const std::string& language,
  3520. const std::string& arch) const
  3521. {
  3522. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3523. language != "OBJCXX") {
  3524. return std::string();
  3525. }
  3526. const auto inserted =
  3527. this->PchSources.insert(std::make_pair(language + config + arch, ""));
  3528. if (inserted.second) {
  3529. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3530. if (pchHeader.empty()) {
  3531. return std::string();
  3532. }
  3533. std::string& filename = inserted.first->second;
  3534. const cmGeneratorTarget* generatorTarget = this;
  3535. cmProp pchReuseFrom =
  3536. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3537. if (pchReuseFrom) {
  3538. generatorTarget =
  3539. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3540. }
  3541. filename =
  3542. cmStrCat(generatorTarget->LocalGenerator->GetCurrentBinaryDirectory(),
  3543. "/CMakeFiles/", generatorTarget->GetName(), ".dir/cmake_pch");
  3544. // For GCC the source extension will be transformed into .h[xx].gch
  3545. if (!this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  3546. const std::map<std::string, std::string> languageToExtension = {
  3547. { "C", ".h.c" },
  3548. { "CXX", ".hxx.cxx" },
  3549. { "OBJC", ".objc.h.m" },
  3550. { "OBJCXX", ".objcxx.hxx.mm" }
  3551. };
  3552. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3553. languageToExtension.at(language));
  3554. } else {
  3555. const std::map<std::string, std::string> languageToExtension = {
  3556. { "C", ".c" }, { "CXX", ".cxx" }, { "OBJC", ".m" }, { "OBJCXX", ".mm" }
  3557. };
  3558. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3559. languageToExtension.at(language));
  3560. }
  3561. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3562. if (!pchReuseFrom) {
  3563. {
  3564. cmGeneratedFileStream file(filename_tmp);
  3565. file << "/* generated by CMake */\n";
  3566. }
  3567. cmFileTimes::Copy(pchHeader, filename_tmp);
  3568. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3569. }
  3570. }
  3571. return inserted.first->second;
  3572. }
  3573. std::string cmGeneratorTarget::GetPchFileObject(const std::string& config,
  3574. const std::string& language,
  3575. const std::string& arch)
  3576. {
  3577. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3578. language != "OBJCXX") {
  3579. return std::string();
  3580. }
  3581. const auto inserted =
  3582. this->PchObjectFiles.insert(std::make_pair(language + config + arch, ""));
  3583. if (inserted.second) {
  3584. const std::string pchSource = this->GetPchSource(config, language, arch);
  3585. if (pchSource.empty()) {
  3586. return std::string();
  3587. }
  3588. std::string& filename = inserted.first->second;
  3589. auto pchSf = this->Makefile->GetOrCreateSource(
  3590. pchSource, false, cmSourceFileLocationKind::Known);
  3591. filename = cmStrCat(this->ObjectDirectory, this->GetObjectName(pchSf));
  3592. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  3593. cmSystemTools::ReplaceString(
  3594. filename, this->GetGlobalGenerator()->GetCMakeCFGIntDir(), config);
  3595. }
  3596. }
  3597. return inserted.first->second;
  3598. }
  3599. std::string cmGeneratorTarget::GetPchFile(const std::string& config,
  3600. const std::string& language,
  3601. const std::string& arch)
  3602. {
  3603. const auto inserted =
  3604. this->PchFiles.insert(std::make_pair(language + config + arch, ""));
  3605. if (inserted.second) {
  3606. std::string& pchFile = inserted.first->second;
  3607. const std::string pchExtension =
  3608. this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
  3609. if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  3610. auto replaceExtension = [](const std::string& str,
  3611. const std::string& ext) -> std::string {
  3612. auto dot_pos = str.rfind('.');
  3613. std::string result;
  3614. if (dot_pos != std::string::npos) {
  3615. result = str.substr(0, dot_pos);
  3616. }
  3617. result += ext;
  3618. return result;
  3619. };
  3620. cmGeneratorTarget* generatorTarget = this;
  3621. cmProp pchReuseFrom =
  3622. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3623. if (pchReuseFrom) {
  3624. generatorTarget =
  3625. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3626. }
  3627. const std::string pchFileObject =
  3628. generatorTarget->GetPchFileObject(config, language, arch);
  3629. if (!pchExtension.empty()) {
  3630. pchFile = replaceExtension(pchFileObject, pchExtension);
  3631. }
  3632. } else {
  3633. pchFile = this->GetPchHeader(config, language, arch);
  3634. pchFile += pchExtension;
  3635. }
  3636. }
  3637. return inserted.first->second;
  3638. }
  3639. std::string cmGeneratorTarget::GetPchCreateCompileOptions(
  3640. const std::string& config, const std::string& language,
  3641. const std::string& arch)
  3642. {
  3643. const auto inserted = this->PchCreateCompileOptions.insert(
  3644. std::make_pair(language + config + arch, ""));
  3645. if (inserted.second) {
  3646. std::string& createOptionList = inserted.first->second;
  3647. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  3648. createOptionList = this->Makefile->GetSafeDefinition(
  3649. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  3650. }
  3651. if (this->GetPropertyAsBool("PCH_INSTANTIATE_TEMPLATES")) {
  3652. std::string varName = cmStrCat(
  3653. "CMAKE_", language, "_COMPILE_OPTIONS_INSTANTIATE_TEMPLATES_PCH");
  3654. std::string instantiateOption =
  3655. this->Makefile->GetSafeDefinition(varName);
  3656. if (!instantiateOption.empty()) {
  3657. createOptionList = cmStrCat(createOptionList, ";", instantiateOption);
  3658. }
  3659. }
  3660. const std::string createOptVar =
  3661. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_CREATE_PCH");
  3662. createOptionList = cmStrCat(
  3663. createOptionList, ";", this->Makefile->GetSafeDefinition(createOptVar));
  3664. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3665. const std::string pchFile = this->GetPchFile(config, language, arch);
  3666. cmSystemTools::ReplaceString(createOptionList, "<PCH_HEADER>", pchHeader);
  3667. cmSystemTools::ReplaceString(createOptionList, "<PCH_FILE>", pchFile);
  3668. }
  3669. return inserted.first->second;
  3670. }
  3671. std::string cmGeneratorTarget::GetPchUseCompileOptions(
  3672. const std::string& config, const std::string& language,
  3673. const std::string& arch)
  3674. {
  3675. const auto inserted = this->PchUseCompileOptions.insert(
  3676. std::make_pair(language + config + arch, ""));
  3677. if (inserted.second) {
  3678. std::string& useOptionList = inserted.first->second;
  3679. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  3680. useOptionList = this->Makefile->GetSafeDefinition(
  3681. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  3682. }
  3683. const std::string useOptVar =
  3684. cmStrCat(language, "_COMPILE_OPTIONS_USE_PCH");
  3685. std::string const& useOptionListProperty =
  3686. this->GetSafeProperty(useOptVar);
  3687. useOptionList = cmStrCat(
  3688. useOptionList, ";",
  3689. useOptionListProperty.empty()
  3690. ? this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_", useOptVar))
  3691. : useOptionListProperty);
  3692. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3693. const std::string pchFile = this->GetPchFile(config, language, arch);
  3694. cmSystemTools::ReplaceString(useOptionList, "<PCH_HEADER>", pchHeader);
  3695. cmSystemTools::ReplaceString(useOptionList, "<PCH_FILE>", pchFile);
  3696. }
  3697. return inserted.first->second;
  3698. }
  3699. void cmGeneratorTarget::AddSourceFileToUnityBatch(
  3700. const std::string& sourceFilename)
  3701. {
  3702. this->UnityBatchedSourceFiles.insert(sourceFilename);
  3703. }
  3704. bool cmGeneratorTarget::IsSourceFilePartOfUnityBatch(
  3705. const std::string& sourceFilename) const
  3706. {
  3707. if (!this->GetPropertyAsBool("UNITY_BUILD")) {
  3708. return false;
  3709. }
  3710. return this->UnityBatchedSourceFiles.find(sourceFilename) !=
  3711. this->UnityBatchedSourceFiles.end();
  3712. }
  3713. void cmGeneratorTarget::GetLinkOptions(std::vector<std::string>& result,
  3714. const std::string& config,
  3715. const std::string& language) const
  3716. {
  3717. if (this->IsDeviceLink() &&
  3718. this->GetPolicyStatusCMP0105() != cmPolicies::NEW) {
  3719. // link options are not propagated to the device link step
  3720. return;
  3721. }
  3722. std::vector<BT<std::string>> tmp = this->GetLinkOptions(config, language);
  3723. result.reserve(tmp.size());
  3724. for (BT<std::string>& v : tmp) {
  3725. result.emplace_back(std::move(v.Value));
  3726. }
  3727. }
  3728. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkOptions(
  3729. std::string const& config, std::string const& language) const
  3730. {
  3731. std::vector<BT<std::string>> result;
  3732. std::unordered_set<std::string> uniqueOptions;
  3733. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_OPTIONS", nullptr,
  3734. nullptr);
  3735. std::vector<std::string> debugProperties;
  3736. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3737. debugProperties);
  3738. bool debugOptions = !this->DebugLinkOptionsDone &&
  3739. cm::contains(debugProperties, "LINK_OPTIONS");
  3740. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3741. this->DebugLinkOptionsDone = true;
  3742. }
  3743. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3744. this, config, language, &dagChecker, this->LinkOptionsEntries);
  3745. AddInterfaceEntries(this, config, "INTERFACE_LINK_OPTIONS", language,
  3746. &dagChecker, entries,
  3747. this->GetPolicyStatusCMP0099() != cmPolicies::NEW);
  3748. processOptions(this, entries, result, uniqueOptions, debugOptions,
  3749. "link options", OptionsParse::Shell, this->IsDeviceLink());
  3750. if (this->IsDeviceLink()) {
  3751. // wrap host link options
  3752. const std::string wrapper(this->Makefile->GetSafeDefinition(
  3753. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG"));
  3754. std::vector<std::string> wrapperFlag = cmExpandedList(wrapper);
  3755. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  3756. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG_SEP"));
  3757. bool concatFlagAndArgs = true;
  3758. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  3759. concatFlagAndArgs = false;
  3760. wrapperFlag.pop_back();
  3761. }
  3762. auto it = result.begin();
  3763. while (it != result.end()) {
  3764. if (it->Value == DL_BEGIN) {
  3765. // device link options, no treatment
  3766. it = result.erase(it);
  3767. it = std::find_if(it, result.end(), [](const BT<std::string>& item) {
  3768. return item.Value == DL_END;
  3769. });
  3770. if (it != result.end()) {
  3771. it = result.erase(it);
  3772. }
  3773. } else {
  3774. // host link options must be wrapped
  3775. std::vector<std::string> options;
  3776. cmSystemTools::ParseUnixCommandLine(it->Value.c_str(), options);
  3777. auto hostOptions = wrapOptions(options, it->Backtrace, wrapperFlag,
  3778. wrapperSep, concatFlagAndArgs);
  3779. it = result.erase(it);
  3780. // some compilers (like gcc 4.8 or Intel 19.0 or XLC 16) do not respect
  3781. // C++11 standard: 'std::vector::insert()' do not returns an iterator,
  3782. // so need to recompute the iterator after insertion.
  3783. if (it == result.end()) {
  3784. cm::append(result, hostOptions);
  3785. it = result.end();
  3786. } else {
  3787. auto index = it - result.begin();
  3788. result.insert(it, hostOptions.begin(), hostOptions.end());
  3789. it = result.begin() + index + hostOptions.size();
  3790. }
  3791. }
  3792. }
  3793. }
  3794. // Last step: replace "LINKER:" prefixed elements by
  3795. // actual linker wrapper
  3796. const std::string wrapper(this->Makefile->GetSafeDefinition(
  3797. "CMAKE_" + language +
  3798. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG"
  3799. : "_LINKER_WRAPPER_FLAG")));
  3800. std::vector<std::string> wrapperFlag = cmExpandedList(wrapper);
  3801. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  3802. "CMAKE_" + language +
  3803. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG_SEP"
  3804. : "_LINKER_WRAPPER_FLAG_SEP")));
  3805. bool concatFlagAndArgs = true;
  3806. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  3807. concatFlagAndArgs = false;
  3808. wrapperFlag.pop_back();
  3809. }
  3810. const std::string LINKER{ "LINKER:" };
  3811. const std::string SHELL{ "SHELL:" };
  3812. const std::string LINKER_SHELL = LINKER + SHELL;
  3813. std::vector<BT<std::string>>::iterator entry;
  3814. while ((entry = std::find_if(result.begin(), result.end(),
  3815. [&LINKER](BT<std::string> const& item) -> bool {
  3816. return item.Value.compare(0, LINKER.length(),
  3817. LINKER) == 0;
  3818. })) != result.end()) {
  3819. std::string value = std::move(entry->Value);
  3820. cmListFileBacktrace bt = std::move(entry->Backtrace);
  3821. entry = result.erase(entry);
  3822. std::vector<std::string> linkerOptions;
  3823. if (value.compare(0, LINKER_SHELL.length(), LINKER_SHELL) == 0) {
  3824. cmSystemTools::ParseUnixCommandLine(
  3825. value.c_str() + LINKER_SHELL.length(), linkerOptions);
  3826. } else {
  3827. linkerOptions = cmTokenize(value.substr(LINKER.length()), ",");
  3828. }
  3829. if (linkerOptions.empty() ||
  3830. (linkerOptions.size() == 1 && linkerOptions.front().empty())) {
  3831. continue;
  3832. }
  3833. // for now, raise an error if prefix SHELL: is part of arguments
  3834. if (std::find_if(linkerOptions.begin(), linkerOptions.end(),
  3835. [&SHELL](const std::string& item) -> bool {
  3836. return item.find(SHELL) != std::string::npos;
  3837. }) != linkerOptions.end()) {
  3838. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  3839. MessageType::FATAL_ERROR,
  3840. "'SHELL:' prefix is not supported as part of 'LINKER:' arguments.",
  3841. this->GetBacktrace());
  3842. return result;
  3843. }
  3844. std::vector<BT<std::string>> options = wrapOptions(
  3845. linkerOptions, bt, wrapperFlag, wrapperSep, concatFlagAndArgs);
  3846. result.insert(entry, options.begin(), options.end());
  3847. }
  3848. return result;
  3849. }
  3850. void cmGeneratorTarget::GetStaticLibraryLinkOptions(
  3851. std::vector<std::string>& result, const std::string& config,
  3852. const std::string& language) const
  3853. {
  3854. std::vector<BT<std::string>> tmp =
  3855. this->GetStaticLibraryLinkOptions(config, language);
  3856. result.reserve(tmp.size());
  3857. for (BT<std::string>& v : tmp) {
  3858. result.emplace_back(std::move(v.Value));
  3859. }
  3860. }
  3861. std::vector<BT<std::string>> cmGeneratorTarget::GetStaticLibraryLinkOptions(
  3862. std::string const& config, std::string const& language) const
  3863. {
  3864. std::vector<BT<std::string>> result;
  3865. std::unordered_set<std::string> uniqueOptions;
  3866. cmGeneratorExpressionDAGChecker dagChecker(this, "STATIC_LIBRARY_OPTIONS",
  3867. nullptr, nullptr);
  3868. EvaluatedTargetPropertyEntries entries;
  3869. if (cmProp linkOptions = this->GetProperty("STATIC_LIBRARY_OPTIONS")) {
  3870. std::vector<std::string> options = cmExpandedList(*linkOptions);
  3871. for (const auto& option : options) {
  3872. std::unique_ptr<TargetPropertyEntry> entry =
  3873. CreateTargetPropertyEntry(option);
  3874. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  3875. this, config, language, &dagChecker, *entry));
  3876. }
  3877. }
  3878. processOptions(this, entries, result, uniqueOptions, false,
  3879. "static library link options", OptionsParse::Shell);
  3880. return result;
  3881. }
  3882. namespace {
  3883. void processLinkDirectories(cmGeneratorTarget const* tgt,
  3884. EvaluatedTargetPropertyEntries& entries,
  3885. std::vector<BT<std::string>>& directories,
  3886. std::unordered_set<std::string>& uniqueDirectories,
  3887. bool debugDirectories)
  3888. {
  3889. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  3890. cmLinkImplItem const& item = entry.LinkImplItem;
  3891. std::string const& targetName = item.AsStr();
  3892. std::string usedDirectories;
  3893. for (std::string& entryDirectory : entry.Values) {
  3894. if (!cmSystemTools::FileIsFullPath(entryDirectory)) {
  3895. std::ostringstream e;
  3896. bool noMessage = false;
  3897. MessageType messageType = MessageType::FATAL_ERROR;
  3898. if (!targetName.empty()) {
  3899. /* clang-format off */
  3900. e << "Target \"" << targetName << "\" contains relative "
  3901. "path in its INTERFACE_LINK_DIRECTORIES:\n"
  3902. " \"" << entryDirectory << "\"";
  3903. /* clang-format on */
  3904. } else {
  3905. switch (tgt->GetPolicyStatusCMP0081()) {
  3906. case cmPolicies::WARN: {
  3907. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0081) << "\n";
  3908. messageType = MessageType::AUTHOR_WARNING;
  3909. } break;
  3910. case cmPolicies::OLD:
  3911. noMessage = true;
  3912. break;
  3913. case cmPolicies::REQUIRED_IF_USED:
  3914. case cmPolicies::REQUIRED_ALWAYS:
  3915. case cmPolicies::NEW:
  3916. // Issue the fatal message.
  3917. break;
  3918. }
  3919. e << "Found relative path while evaluating link directories of "
  3920. "\""
  3921. << tgt->GetName() << "\":\n \"" << entryDirectory << "\"\n";
  3922. }
  3923. if (!noMessage) {
  3924. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3925. if (messageType == MessageType::FATAL_ERROR) {
  3926. return;
  3927. }
  3928. }
  3929. }
  3930. // Sanitize the path the same way the link_directories command does
  3931. // in case projects set the LINK_DIRECTORIES property directly.
  3932. cmSystemTools::ConvertToUnixSlashes(entryDirectory);
  3933. if (uniqueDirectories.insert(entryDirectory).second) {
  3934. directories.emplace_back(entryDirectory, entry.Backtrace);
  3935. if (debugDirectories) {
  3936. usedDirectories += " * " + entryDirectory + "\n";
  3937. }
  3938. }
  3939. }
  3940. if (!usedDirectories.empty()) {
  3941. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3942. MessageType::LOG,
  3943. std::string("Used link directories for target ") + tgt->GetName() +
  3944. ":\n" + usedDirectories,
  3945. entry.Backtrace);
  3946. }
  3947. }
  3948. }
  3949. }
  3950. void cmGeneratorTarget::GetLinkDirectories(std::vector<std::string>& result,
  3951. const std::string& config,
  3952. const std::string& language) const
  3953. {
  3954. std::vector<BT<std::string>> tmp =
  3955. this->GetLinkDirectories(config, language);
  3956. result.reserve(tmp.size());
  3957. for (BT<std::string>& v : tmp) {
  3958. result.emplace_back(std::move(v.Value));
  3959. }
  3960. }
  3961. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDirectories(
  3962. std::string const& config, std::string const& language) const
  3963. {
  3964. std::vector<BT<std::string>> result;
  3965. std::unordered_set<std::string> uniqueDirectories;
  3966. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DIRECTORIES", nullptr,
  3967. nullptr);
  3968. std::vector<std::string> debugProperties;
  3969. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3970. debugProperties);
  3971. bool debugDirectories = !this->DebugLinkDirectoriesDone &&
  3972. cm::contains(debugProperties, "LINK_DIRECTORIES");
  3973. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3974. this->DebugLinkDirectoriesDone = true;
  3975. }
  3976. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3977. this, config, language, &dagChecker, this->LinkDirectoriesEntries);
  3978. AddInterfaceEntries(this, config, "INTERFACE_LINK_DIRECTORIES", language,
  3979. &dagChecker, entries,
  3980. this->GetPolicyStatusCMP0099() != cmPolicies::NEW);
  3981. processLinkDirectories(this, entries, result, uniqueDirectories,
  3982. debugDirectories);
  3983. return result;
  3984. }
  3985. void cmGeneratorTarget::GetLinkDepends(std::vector<std::string>& result,
  3986. const std::string& config,
  3987. const std::string& language) const
  3988. {
  3989. std::vector<BT<std::string>> tmp = this->GetLinkDepends(config, language);
  3990. result.reserve(tmp.size());
  3991. for (BT<std::string>& v : tmp) {
  3992. result.emplace_back(std::move(v.Value));
  3993. }
  3994. }
  3995. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDepends(
  3996. std::string const& config, std::string const& language) const
  3997. {
  3998. std::vector<BT<std::string>> result;
  3999. std::unordered_set<std::string> uniqueOptions;
  4000. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DEPENDS", nullptr,
  4001. nullptr);
  4002. EvaluatedTargetPropertyEntries entries;
  4003. if (cmProp linkDepends = this->GetProperty("LINK_DEPENDS")) {
  4004. std::vector<std::string> depends = cmExpandedList(*linkDepends);
  4005. for (const auto& depend : depends) {
  4006. std::unique_ptr<TargetPropertyEntry> entry =
  4007. CreateTargetPropertyEntry(depend);
  4008. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  4009. this, config, language, &dagChecker, *entry));
  4010. }
  4011. }
  4012. AddInterfaceEntries(this, config, "INTERFACE_LINK_DEPENDS", language,
  4013. &dagChecker, entries,
  4014. this->GetPolicyStatusCMP0099() != cmPolicies::NEW);
  4015. processOptions(this, entries, result, uniqueOptions, false, "link depends",
  4016. OptionsParse::None);
  4017. return result;
  4018. }
  4019. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  4020. {
  4021. if (this->IsImported()) {
  4022. return;
  4023. }
  4024. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  4025. // Get the names.
  4026. cmGeneratorTarget::Names targetNames;
  4027. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4028. targetNames = this->GetExecutableNames(config);
  4029. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4030. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4031. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4032. targetNames = this->GetLibraryNames(config);
  4033. } else {
  4034. return;
  4035. }
  4036. // Get the directory.
  4037. std::string dir =
  4038. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  4039. // Add each name.
  4040. std::string f;
  4041. if (!targetNames.Output.empty()) {
  4042. f = cmStrCat(dir, '/', targetNames.Output);
  4043. gg->AddToManifest(f);
  4044. }
  4045. if (!targetNames.SharedObject.empty()) {
  4046. f = cmStrCat(dir, '/', targetNames.SharedObject);
  4047. gg->AddToManifest(f);
  4048. }
  4049. if (!targetNames.Real.empty()) {
  4050. f = cmStrCat(dir, '/', targetNames.Real);
  4051. gg->AddToManifest(f);
  4052. }
  4053. if (!targetNames.PDB.empty()) {
  4054. f = cmStrCat(dir, '/', targetNames.PDB);
  4055. gg->AddToManifest(f);
  4056. }
  4057. if (!targetNames.ImportLibrary.empty()) {
  4058. f =
  4059. cmStrCat(this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact),
  4060. '/', targetNames.ImportLibrary);
  4061. gg->AddToManifest(f);
  4062. }
  4063. }
  4064. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  4065. {
  4066. // Compute the language standard based on the compile features.
  4067. cmStandardLevelResolver standardResolver(this->Makefile);
  4068. std::vector<BT<std::string>> features = this->GetCompileFeatures(config);
  4069. for (BT<std::string> const& f : features) {
  4070. std::string lang;
  4071. if (!standardResolver.CompileFeatureKnown(this->Target->GetName(), f.Value,
  4072. lang, nullptr)) {
  4073. return false;
  4074. }
  4075. std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
  4076. cmProp currentLanguageStandard = this->GetLanguageStandard(lang, config);
  4077. std::string newRequiredStandard;
  4078. if (!standardResolver.GetNewRequiredStandard(
  4079. this->Target->GetName(), f.Value, currentLanguageStandard,
  4080. newRequiredStandard)) {
  4081. return false;
  4082. }
  4083. if (!newRequiredStandard.empty()) {
  4084. BTs<std::string>& languageStandardProperty =
  4085. this->LanguageStandardMap[key];
  4086. if (languageStandardProperty.Value != newRequiredStandard) {
  4087. languageStandardProperty.Value = newRequiredStandard;
  4088. languageStandardProperty.Backtraces.clear();
  4089. }
  4090. languageStandardProperty.Backtraces.emplace_back(f.Backtrace);
  4091. }
  4092. }
  4093. return true;
  4094. }
  4095. bool cmGeneratorTarget::ComputeCompileFeatures(
  4096. std::string const& config, std::set<LanguagePair> const& languagePairs) const
  4097. {
  4098. for (const auto& language : languagePairs) {
  4099. BTs<std::string> const* generatorTargetLanguageStandard =
  4100. this->GetLanguageStandardProperty(language.first, config);
  4101. if (!generatorTargetLanguageStandard) {
  4102. // If the standard isn't explicitly set we copy it over from the
  4103. // specified paired language.
  4104. std::string key =
  4105. cmStrCat(cmSystemTools::UpperCase(config), '-', language.first);
  4106. BTs<std::string> const* standardToCopy =
  4107. this->GetLanguageStandardProperty(language.second, config);
  4108. if (standardToCopy != nullptr) {
  4109. this->LanguageStandardMap[key] = *standardToCopy;
  4110. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  4111. } else {
  4112. cmProp defaultStandard = this->Makefile->GetDefinition(
  4113. cmStrCat("CMAKE_", language.second, "_STANDARD_DEFAULT"));
  4114. if (defaultStandard != nullptr) {
  4115. this->LanguageStandardMap[key] = BTs<std::string>(*defaultStandard);
  4116. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  4117. }
  4118. }
  4119. // Custom updates for the CUDA standard.
  4120. if (generatorTargetLanguageStandard != nullptr &&
  4121. language.first == "CUDA") {
  4122. if (generatorTargetLanguageStandard->Value == "98") {
  4123. this->LanguageStandardMap[key].Value = "03";
  4124. }
  4125. }
  4126. }
  4127. }
  4128. return true;
  4129. }
  4130. std::string cmGeneratorTarget::GetImportedLibName(
  4131. std::string const& config) const
  4132. {
  4133. if (cmGeneratorTarget::ImportInfo const* info =
  4134. this->GetImportInfo(config)) {
  4135. return info->LibName;
  4136. }
  4137. return std::string();
  4138. }
  4139. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  4140. cmStateEnums::ArtifactType artifact,
  4141. bool realname) const
  4142. {
  4143. if (this->IsImported()) {
  4144. return this->Target->ImportedGetFullPath(config, artifact);
  4145. }
  4146. return this->NormalGetFullPath(config, artifact, realname);
  4147. }
  4148. std::string cmGeneratorTarget::NormalGetFullPath(
  4149. const std::string& config, cmStateEnums::ArtifactType artifact,
  4150. bool realname) const
  4151. {
  4152. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  4153. if (this->IsAppBundleOnApple()) {
  4154. fpath =
  4155. cmStrCat(this->BuildBundleDirectory(fpath, config, FullLevel), '/');
  4156. }
  4157. // Add the full name of the target.
  4158. switch (artifact) {
  4159. case cmStateEnums::RuntimeBinaryArtifact:
  4160. if (realname) {
  4161. fpath += this->NormalGetRealName(config);
  4162. } else {
  4163. fpath +=
  4164. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  4165. }
  4166. break;
  4167. case cmStateEnums::ImportLibraryArtifact:
  4168. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  4169. break;
  4170. }
  4171. return fpath;
  4172. }
  4173. std::string cmGeneratorTarget::NormalGetRealName(
  4174. const std::string& config) const
  4175. {
  4176. // This should not be called for imported targets.
  4177. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4178. // enforcement of the limited imported target API.
  4179. if (this->IsImported()) {
  4180. std::string msg = cmStrCat("NormalGetRealName called on imported target: ",
  4181. this->GetName());
  4182. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4183. }
  4184. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4185. // Compute the real name that will be built.
  4186. return this->GetExecutableNames(config).Real;
  4187. }
  4188. // Compute the real name that will be built.
  4189. return this->GetLibraryNames(config).Real;
  4190. }
  4191. cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
  4192. const std::string& config) const
  4193. {
  4194. cmGeneratorTarget::Names targetNames;
  4195. // This should not be called for imported targets.
  4196. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4197. // enforcement of the limited imported target API.
  4198. if (this->IsImported()) {
  4199. std::string msg =
  4200. cmStrCat("GetLibraryNames called on imported target: ", this->GetName());
  4201. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4202. }
  4203. // Check for library version properties.
  4204. cmProp version = this->GetProperty("VERSION");
  4205. cmProp soversion = this->GetProperty("SOVERSION");
  4206. if (!this->HasSOName(config) ||
  4207. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  4208. this->IsFrameworkOnApple()) {
  4209. // Versioning is supported only for shared libraries and modules,
  4210. // and then only when the platform supports an soname flag.
  4211. version = nullptr;
  4212. soversion = nullptr;
  4213. }
  4214. if (version && !soversion) {
  4215. // The soversion must be set if the library version is set. Use
  4216. // the library version as the soversion.
  4217. soversion = version;
  4218. }
  4219. if (!version && soversion) {
  4220. // Use the soversion as the library version.
  4221. version = soversion;
  4222. }
  4223. // Get the components of the library name.
  4224. std::string prefix;
  4225. std::string suffix;
  4226. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4227. prefix, targetNames.Base, suffix);
  4228. // The library name.
  4229. targetNames.Output = prefix + targetNames.Base + suffix;
  4230. if (this->IsFrameworkOnApple()) {
  4231. targetNames.Real = prefix;
  4232. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  4233. targetNames.Real += "Versions/";
  4234. targetNames.Real += this->GetFrameworkVersion();
  4235. targetNames.Real += "/";
  4236. }
  4237. targetNames.Real += targetNames.Base + suffix;
  4238. targetNames.SharedObject = targetNames.Real + suffix;
  4239. } else {
  4240. // The library's soname.
  4241. this->ComputeVersionedName(targetNames.SharedObject, prefix,
  4242. targetNames.Base, suffix, targetNames.Output,
  4243. cmToCStr(soversion));
  4244. // The library's real name on disk.
  4245. this->ComputeVersionedName(targetNames.Real, prefix, targetNames.Base,
  4246. suffix, targetNames.Output, cmToCStr(version));
  4247. }
  4248. // The import library name.
  4249. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4250. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4251. targetNames.ImportLibrary =
  4252. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4253. }
  4254. // The program database file name.
  4255. targetNames.PDB = this->GetPDBName(config);
  4256. return targetNames;
  4257. }
  4258. cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
  4259. const std::string& config) const
  4260. {
  4261. cmGeneratorTarget::Names targetNames;
  4262. // This should not be called for imported targets.
  4263. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4264. // enforcement of the limited imported target API.
  4265. if (this->IsImported()) {
  4266. std::string msg = cmStrCat(
  4267. "GetExecutableNames called on imported target: ", this->GetName());
  4268. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4269. }
  4270. // This versioning is supported only for executables and then only
  4271. // when the platform supports symbolic links.
  4272. #if defined(_WIN32) && !defined(__CYGWIN__)
  4273. const char* version = nullptr;
  4274. #else
  4275. // Check for executable version properties.
  4276. const char* version = cmToCStr(this->GetProperty("VERSION"));
  4277. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  4278. this->Makefile->IsOn("XCODE")) {
  4279. version = nullptr;
  4280. }
  4281. #endif
  4282. // Get the components of the executable name.
  4283. std::string prefix;
  4284. std::string suffix;
  4285. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4286. prefix, targetNames.Base, suffix);
  4287. // The executable name.
  4288. targetNames.Output = prefix + targetNames.Base + suffix;
  4289. // The executable's real name on disk.
  4290. #if defined(__CYGWIN__)
  4291. targetNames.Real = prefix + targetNames.Base;
  4292. #else
  4293. targetNames.Real = targetNames.Output;
  4294. #endif
  4295. if (version) {
  4296. targetNames.Real += "-";
  4297. targetNames.Real += version;
  4298. }
  4299. #if defined(__CYGWIN__)
  4300. targetNames.Real += suffix;
  4301. #endif
  4302. // The import library name.
  4303. targetNames.ImportLibrary =
  4304. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4305. // The program database file name.
  4306. targetNames.PDB = this->GetPDBName(config);
  4307. return targetNames;
  4308. }
  4309. std::string cmGeneratorTarget::GetFullNameInternal(
  4310. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4311. {
  4312. std::string prefix;
  4313. std::string base;
  4314. std::string suffix;
  4315. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  4316. return prefix + base + suffix;
  4317. }
  4318. std::string cmGeneratorTarget::ImportedGetLocation(
  4319. const std::string& config) const
  4320. {
  4321. assert(this->IsImported());
  4322. return this->Target->ImportedGetFullPath(
  4323. config, cmStateEnums::RuntimeBinaryArtifact);
  4324. }
  4325. std::string cmGeneratorTarget::GetFullNameImported(
  4326. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4327. {
  4328. return cmSystemTools::GetFilenameName(
  4329. this->Target->ImportedGetFullPath(config, artifact));
  4330. }
  4331. void cmGeneratorTarget::GetFullNameInternal(
  4332. const std::string& config, cmStateEnums::ArtifactType artifact,
  4333. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  4334. {
  4335. // Use just the target name for non-main target types.
  4336. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  4337. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4338. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4339. this->GetType() != cmStateEnums::EXECUTABLE) {
  4340. outPrefix.clear();
  4341. outBase = this->GetName();
  4342. outSuffix.clear();
  4343. return;
  4344. }
  4345. const bool isImportedLibraryArtifact =
  4346. (artifact == cmStateEnums::ImportLibraryArtifact);
  4347. // Return an empty name for the import library if this platform
  4348. // does not support import libraries.
  4349. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  4350. outPrefix.clear();
  4351. outBase.clear();
  4352. outSuffix.clear();
  4353. return;
  4354. }
  4355. // retrieve prefix and suffix
  4356. std::string ll = this->GetLinkerLanguage(config);
  4357. const char* targetPrefix = this->GetFilePrefixInternal(config, artifact, ll);
  4358. const char* targetSuffix = this->GetFileSuffixInternal(config, artifact, ll);
  4359. // The implib option is only allowed for shared libraries, module
  4360. // libraries, and executables.
  4361. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4362. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4363. this->GetType() != cmStateEnums::EXECUTABLE) {
  4364. artifact = cmStateEnums::RuntimeBinaryArtifact;
  4365. }
  4366. // Compute the full name for main target types.
  4367. const std::string configPostfix = this->GetFilePostfix(config);
  4368. // frameworks have directory prefix but no suffix
  4369. std::string fw_prefix;
  4370. if (this->IsFrameworkOnApple()) {
  4371. fw_prefix =
  4372. cmStrCat(this->GetFrameworkDirectory(config, ContentLevel), '/');
  4373. targetPrefix = fw_prefix.c_str();
  4374. targetSuffix = nullptr;
  4375. }
  4376. if (this->IsCFBundleOnApple()) {
  4377. fw_prefix = cmStrCat(this->GetCFBundleDirectory(config, FullLevel), '/');
  4378. targetPrefix = fw_prefix.c_str();
  4379. targetSuffix = nullptr;
  4380. }
  4381. // Begin the final name with the prefix.
  4382. outPrefix = targetPrefix ? targetPrefix : "";
  4383. // Append the target name or property-specified name.
  4384. outBase += this->GetOutputName(config, artifact);
  4385. // Append the per-configuration postfix.
  4386. // When using Xcode, the postfix should be part of the suffix rather than
  4387. // the base, because the suffix ends up being used in Xcode's
  4388. // EXECUTABLE_SUFFIX attribute.
  4389. if (this->IsFrameworkOnApple() &&
  4390. GetGlobalGenerator()->GetName() == "Xcode") {
  4391. targetSuffix = configPostfix.c_str();
  4392. } else {
  4393. outBase += configPostfix;
  4394. }
  4395. // Name shared libraries with their version number on some platforms.
  4396. if (cmProp soversion = this->GetProperty("SOVERSION")) {
  4397. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  4398. !isImportedLibraryArtifact &&
  4399. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  4400. outBase += "-";
  4401. outBase += *soversion;
  4402. }
  4403. }
  4404. // Append the suffix.
  4405. outSuffix = targetSuffix ? targetSuffix : "";
  4406. }
  4407. std::string cmGeneratorTarget::GetLinkerLanguage(
  4408. const std::string& config) const
  4409. {
  4410. return this->GetLinkClosure(config)->LinkerLanguage;
  4411. }
  4412. std::string cmGeneratorTarget::GetPDBOutputName(
  4413. const std::string& config) const
  4414. {
  4415. std::string base =
  4416. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  4417. std::vector<std::string> props;
  4418. std::string configUpper = cmSystemTools::UpperCase(config);
  4419. if (!configUpper.empty()) {
  4420. // PDB_NAME_<CONFIG>
  4421. props.push_back("PDB_NAME_" + configUpper);
  4422. }
  4423. // PDB_NAME
  4424. props.emplace_back("PDB_NAME");
  4425. for (std::string const& p : props) {
  4426. if (cmProp outName = this->GetProperty(p)) {
  4427. base = *outName;
  4428. break;
  4429. }
  4430. }
  4431. return base;
  4432. }
  4433. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  4434. {
  4435. std::string prefix;
  4436. std::string base;
  4437. std::string suffix;
  4438. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4439. prefix, base, suffix);
  4440. std::vector<std::string> props;
  4441. std::string configUpper = cmSystemTools::UpperCase(config);
  4442. if (!configUpper.empty()) {
  4443. // PDB_NAME_<CONFIG>
  4444. props.push_back("PDB_NAME_" + configUpper);
  4445. }
  4446. // PDB_NAME
  4447. props.emplace_back("PDB_NAME");
  4448. for (std::string const& p : props) {
  4449. if (cmProp outName = this->GetProperty(p)) {
  4450. base = *outName;
  4451. break;
  4452. }
  4453. }
  4454. return prefix + base + ".pdb";
  4455. }
  4456. std::string cmGeneratorTarget::GetObjectDirectory(
  4457. std::string const& config) const
  4458. {
  4459. std::string obj_dir =
  4460. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  4461. #if defined(__APPLE__)
  4462. // find and replace $(PROJECT_NAME) xcode placeholder
  4463. const std::string projectName = this->LocalGenerator->GetProjectName();
  4464. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  4465. // Replace Xcode's placeholder for the object file directory since
  4466. // installation and export scripts need to know the real directory.
  4467. // Xcode has build-time settings (e.g. for sanitizers) that affect this,
  4468. // but we use the default here. Users that want to enable sanitizers
  4469. // will do so at the cost of object library installation and export.
  4470. cmSystemTools::ReplaceString(obj_dir, "$(OBJECT_FILE_DIR_normal:base)",
  4471. "Objects-normal");
  4472. #endif
  4473. return obj_dir;
  4474. }
  4475. void cmGeneratorTarget::GetTargetObjectNames(
  4476. std::string const& config, std::vector<std::string>& objects) const
  4477. {
  4478. std::vector<cmSourceFile const*> objectSources;
  4479. this->GetObjectSources(objectSources, config);
  4480. std::map<cmSourceFile const*, std::string> mapping;
  4481. for (cmSourceFile const* sf : objectSources) {
  4482. mapping[sf];
  4483. }
  4484. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  4485. for (cmSourceFile const* src : objectSources) {
  4486. // Find the object file name corresponding to this source file.
  4487. auto map_it = mapping.find(src);
  4488. // It must exist because we populated the mapping just above.
  4489. assert(!map_it->second.empty());
  4490. objects.push_back(map_it->second);
  4491. }
  4492. auto ispcObjects = this->GetGeneratedISPCObjects(config);
  4493. for (std::string const& output : ispcObjects) {
  4494. objects.push_back(cmSystemTools::GetFilenameName(output));
  4495. }
  4496. }
  4497. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  4498. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  4499. {
  4500. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  4501. if (nameResult == 0) {
  4502. return strcmp(
  4503. t1->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str(),
  4504. t2->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str()) < 0;
  4505. }
  4506. return nameResult < 0;
  4507. }
  4508. struct cmGeneratorTarget::SourceFileFlags
  4509. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  4510. {
  4511. struct SourceFileFlags flags;
  4512. this->ConstructSourceFileFlags();
  4513. auto si = this->SourceFlagsMap.find(sf);
  4514. if (si != this->SourceFlagsMap.end()) {
  4515. flags = si->second;
  4516. } else {
  4517. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  4518. // were not listed in one of the other lists.
  4519. if (cmProp location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  4520. flags.MacFolder = location->c_str();
  4521. const bool stripResources =
  4522. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  4523. if (*location == "Resources") {
  4524. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4525. if (stripResources) {
  4526. flags.MacFolder = "";
  4527. }
  4528. } else if (cmHasLiteralPrefix(*location, "Resources/")) {
  4529. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  4530. if (stripResources) {
  4531. flags.MacFolder += strlen("Resources/");
  4532. }
  4533. } else {
  4534. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  4535. }
  4536. }
  4537. }
  4538. return flags;
  4539. }
  4540. void cmGeneratorTarget::ConstructSourceFileFlags() const
  4541. {
  4542. if (this->SourceFileFlagsConstructed) {
  4543. return;
  4544. }
  4545. this->SourceFileFlagsConstructed = true;
  4546. // Process public headers to mark the source files.
  4547. if (cmProp files = this->GetProperty("PUBLIC_HEADER")) {
  4548. std::vector<std::string> relFiles = cmExpandedList(*files);
  4549. for (std::string const& relFile : relFiles) {
  4550. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4551. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4552. flags.MacFolder = "Headers";
  4553. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  4554. }
  4555. }
  4556. }
  4557. // Process private headers after public headers so that they take
  4558. // precedence if a file is listed in both.
  4559. if (cmProp files = this->GetProperty("PRIVATE_HEADER")) {
  4560. std::vector<std::string> relFiles = cmExpandedList(*files);
  4561. for (std::string const& relFile : relFiles) {
  4562. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4563. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4564. flags.MacFolder = "PrivateHeaders";
  4565. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  4566. }
  4567. }
  4568. }
  4569. // Mark sources listed as resources.
  4570. if (cmProp files = this->GetProperty("RESOURCE")) {
  4571. std::vector<std::string> relFiles = cmExpandedList(*files);
  4572. for (std::string const& relFile : relFiles) {
  4573. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4574. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4575. flags.MacFolder = "";
  4576. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  4577. flags.MacFolder = "Resources";
  4578. }
  4579. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4580. }
  4581. }
  4582. }
  4583. }
  4584. const cmGeneratorTarget::CompatibleInterfacesBase&
  4585. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  4586. {
  4587. cmGeneratorTarget::CompatibleInterfaces& compat =
  4588. this->CompatibleInterfacesMap[config];
  4589. if (!compat.Done) {
  4590. compat.Done = true;
  4591. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  4592. compat.PropsString.insert("AUTOUIC_OPTIONS");
  4593. std::vector<cmGeneratorTarget const*> const& deps =
  4594. this->GetLinkImplementationClosure(config);
  4595. for (cmGeneratorTarget const* li : deps) {
  4596. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  4597. if (cmProp prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  4598. std::vector<std::string> props; \
  4599. cmExpandList(*prop, props); \
  4600. compat.Props##x.insert(props.begin(), props.end()); \
  4601. }
  4602. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  4603. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  4604. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  4605. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  4606. #undef CM_READ_COMPATIBLE_INTERFACE
  4607. }
  4608. }
  4609. return compat;
  4610. }
  4611. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  4612. const std::string& p, const std::string& config) const
  4613. {
  4614. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4615. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4616. return false;
  4617. }
  4618. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  4619. }
  4620. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  4621. const std::string& p, const std::string& config) const
  4622. {
  4623. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4624. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4625. return false;
  4626. }
  4627. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  4628. }
  4629. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  4630. const std::string& p, const std::string& config) const
  4631. {
  4632. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4633. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4634. return false;
  4635. }
  4636. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  4637. }
  4638. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  4639. const std::string& p, const std::string& config) const
  4640. {
  4641. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4642. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4643. return false;
  4644. }
  4645. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  4646. }
  4647. enum CompatibleType
  4648. {
  4649. BoolType,
  4650. StringType,
  4651. NumberMinType,
  4652. NumberMaxType
  4653. };
  4654. template <typename PropertyType>
  4655. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4656. const std::string& prop,
  4657. const std::string& config,
  4658. CompatibleType, PropertyType*);
  4659. template <>
  4660. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4661. const std::string& prop,
  4662. const std::string& config,
  4663. CompatibleType /*unused*/,
  4664. bool* /*unused*/)
  4665. {
  4666. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  4667. }
  4668. template <>
  4669. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4670. const std::string& prop,
  4671. const std::string& config,
  4672. CompatibleType t,
  4673. const char** /*unused*/)
  4674. {
  4675. switch (t) {
  4676. case BoolType:
  4677. assert(false &&
  4678. "String compatibility check function called for boolean");
  4679. return nullptr;
  4680. case StringType:
  4681. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  4682. case NumberMinType:
  4683. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  4684. case NumberMaxType:
  4685. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  4686. }
  4687. assert(false && "Unreachable!");
  4688. return nullptr;
  4689. }
  4690. template <typename PropertyType>
  4691. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  4692. cmGeneratorTarget const* dependee,
  4693. const std::string& propName,
  4694. std::set<std::string>& emitted,
  4695. const std::string& config, CompatibleType t,
  4696. PropertyType* /*unused*/)
  4697. {
  4698. cmProp prop = dependee->GetProperty(propName);
  4699. if (!prop) {
  4700. return;
  4701. }
  4702. std::vector<std::string> props = cmExpandedList(*prop);
  4703. std::string pdir =
  4704. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Help/prop_tgt/");
  4705. for (std::string const& p : props) {
  4706. std::string pname = cmSystemTools::HelpFileName(p);
  4707. std::string pfile = pdir + pname + ".rst";
  4708. if (cmSystemTools::FileExists(pfile, true)) {
  4709. std::ostringstream e;
  4710. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  4711. << "\" listed in its " << propName
  4712. << " property. "
  4713. "This is not allowed. Only user-defined properties may appear "
  4714. "listed in the "
  4715. << propName << " property.";
  4716. depender->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  4717. e.str());
  4718. return;
  4719. }
  4720. if (emitted.insert(p).second) {
  4721. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  4722. nullptr);
  4723. if (cmSystemTools::GetErrorOccuredFlag()) {
  4724. return;
  4725. }
  4726. }
  4727. }
  4728. }
  4729. namespace {
  4730. std::string intersect(const std::set<std::string>& s1,
  4731. const std::set<std::string>& s2)
  4732. {
  4733. std::set<std::string> intersect;
  4734. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  4735. std::inserter(intersect, intersect.begin()));
  4736. if (!intersect.empty()) {
  4737. return *intersect.begin();
  4738. }
  4739. return "";
  4740. }
  4741. std::string intersect(const std::set<std::string>& s1,
  4742. const std::set<std::string>& s2,
  4743. const std::set<std::string>& s3)
  4744. {
  4745. std::string result;
  4746. result = intersect(s1, s2);
  4747. if (!result.empty()) {
  4748. return result;
  4749. }
  4750. result = intersect(s1, s3);
  4751. if (!result.empty()) {
  4752. return result;
  4753. }
  4754. return intersect(s2, s3);
  4755. }
  4756. std::string intersect(const std::set<std::string>& s1,
  4757. const std::set<std::string>& s2,
  4758. const std::set<std::string>& s3,
  4759. const std::set<std::string>& s4)
  4760. {
  4761. std::string result;
  4762. result = intersect(s1, s2);
  4763. if (!result.empty()) {
  4764. return result;
  4765. }
  4766. result = intersect(s1, s3);
  4767. if (!result.empty()) {
  4768. return result;
  4769. }
  4770. result = intersect(s1, s4);
  4771. if (!result.empty()) {
  4772. return result;
  4773. }
  4774. return intersect(s2, s3, s4);
  4775. }
  4776. }
  4777. void cmGeneratorTarget::CheckPropertyCompatibility(
  4778. cmComputeLinkInformation& info, const std::string& config) const
  4779. {
  4780. const cmComputeLinkInformation::ItemVector& deps = info.GetItems();
  4781. std::set<std::string> emittedBools;
  4782. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  4783. std::set<std::string> emittedStrings;
  4784. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  4785. std::set<std::string> emittedMinNumbers;
  4786. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  4787. std::set<std::string> emittedMaxNumbers;
  4788. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  4789. for (auto const& dep : deps) {
  4790. if (!dep.Target) {
  4791. continue;
  4792. }
  4793. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  4794. config, BoolType, nullptr);
  4795. if (cmSystemTools::GetErrorOccuredFlag()) {
  4796. return;
  4797. }
  4798. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  4799. emittedStrings, config, StringType,
  4800. nullptr);
  4801. if (cmSystemTools::GetErrorOccuredFlag()) {
  4802. return;
  4803. }
  4804. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  4805. emittedMinNumbers, config,
  4806. NumberMinType, nullptr);
  4807. if (cmSystemTools::GetErrorOccuredFlag()) {
  4808. return;
  4809. }
  4810. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  4811. emittedMaxNumbers, config,
  4812. NumberMaxType, nullptr);
  4813. if (cmSystemTools::GetErrorOccuredFlag()) {
  4814. return;
  4815. }
  4816. }
  4817. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  4818. emittedMaxNumbers);
  4819. if (!prop.empty()) {
  4820. // Use a sorted std::vector to keep the error message sorted.
  4821. std::vector<std::string> props;
  4822. auto i = emittedBools.find(prop);
  4823. if (i != emittedBools.end()) {
  4824. props.push_back(strBool);
  4825. }
  4826. i = emittedStrings.find(prop);
  4827. if (i != emittedStrings.end()) {
  4828. props.push_back(strString);
  4829. }
  4830. i = emittedMinNumbers.find(prop);
  4831. if (i != emittedMinNumbers.end()) {
  4832. props.push_back(strNumMin);
  4833. }
  4834. i = emittedMaxNumbers.find(prop);
  4835. if (i != emittedMaxNumbers.end()) {
  4836. props.push_back(strNumMax);
  4837. }
  4838. std::sort(props.begin(), props.end());
  4839. std::string propsString = cmStrCat(
  4840. cmJoin(cmMakeRange(props).retreat(1), ", "), " and the ", props.back());
  4841. std::ostringstream e;
  4842. e << "Property \"" << prop << "\" appears in both the " << propsString
  4843. << " property in the dependencies of target \"" << this->GetName()
  4844. << "\". This is not allowed. A property may only require "
  4845. "compatibility "
  4846. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  4847. "or a "
  4848. "string interpretation, but not a mixture.";
  4849. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  4850. }
  4851. }
  4852. template <typename PropertyType>
  4853. std::string valueAsString(PropertyType);
  4854. template <>
  4855. std::string valueAsString<bool>(bool value)
  4856. {
  4857. return value ? "TRUE" : "FALSE";
  4858. }
  4859. template <>
  4860. std::string valueAsString<const char*>(const char* value)
  4861. {
  4862. return value ? value : "(unset)";
  4863. }
  4864. template <>
  4865. std::string valueAsString<std::string>(std::string value)
  4866. {
  4867. return value;
  4868. }
  4869. template <>
  4870. std::string valueAsString<std::nullptr_t>(std::nullptr_t /*unused*/)
  4871. {
  4872. return "(unset)";
  4873. }
  4874. std::string compatibilityType(CompatibleType t)
  4875. {
  4876. switch (t) {
  4877. case BoolType:
  4878. return "Boolean compatibility";
  4879. case StringType:
  4880. return "String compatibility";
  4881. case NumberMaxType:
  4882. return "Numeric maximum compatibility";
  4883. case NumberMinType:
  4884. return "Numeric minimum compatibility";
  4885. }
  4886. assert(false && "Unreachable!");
  4887. return "";
  4888. }
  4889. std::string compatibilityAgree(CompatibleType t, bool dominant)
  4890. {
  4891. switch (t) {
  4892. case BoolType:
  4893. case StringType:
  4894. return dominant ? "(Disagree)\n" : "(Agree)\n";
  4895. case NumberMaxType:
  4896. case NumberMinType:
  4897. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  4898. }
  4899. assert(false && "Unreachable!");
  4900. return "";
  4901. }
  4902. template <typename PropertyType>
  4903. PropertyType getTypedProperty(
  4904. cmGeneratorTarget const* tgt, const std::string& prop,
  4905. cmGeneratorExpressionInterpreter* genexInterpreter = nullptr);
  4906. template <>
  4907. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  4908. const std::string& prop,
  4909. cmGeneratorExpressionInterpreter* genexInterpreter)
  4910. {
  4911. if (genexInterpreter == nullptr) {
  4912. return tgt->GetPropertyAsBool(prop);
  4913. }
  4914. cmProp value = tgt->GetProperty(prop);
  4915. return cmIsOn(genexInterpreter->Evaluate(value ? *value : "", prop));
  4916. }
  4917. template <>
  4918. const char* getTypedProperty<const char*>(
  4919. cmGeneratorTarget const* tgt, const std::string& prop,
  4920. cmGeneratorExpressionInterpreter* genexInterpreter)
  4921. {
  4922. cmProp value = tgt->GetProperty(prop);
  4923. if (genexInterpreter == nullptr) {
  4924. return cmToCStr(value);
  4925. }
  4926. return genexInterpreter->Evaluate(value ? *value : "", prop).c_str();
  4927. }
  4928. template <>
  4929. std::string getTypedProperty<std::string>(
  4930. cmGeneratorTarget const* tgt, const std::string& prop,
  4931. cmGeneratorExpressionInterpreter* genexInterpreter)
  4932. {
  4933. cmProp value = tgt->GetProperty(prop);
  4934. if (genexInterpreter == nullptr) {
  4935. return valueAsString(cmToCStr(value));
  4936. }
  4937. return genexInterpreter->Evaluate(value ? *value : "", prop);
  4938. }
  4939. template <typename PropertyType>
  4940. PropertyType impliedValue(PropertyType);
  4941. template <>
  4942. bool impliedValue<bool>(bool /*unused*/)
  4943. {
  4944. return false;
  4945. }
  4946. template <>
  4947. const char* impliedValue<const char*>(const char* /*unused*/)
  4948. {
  4949. return "";
  4950. }
  4951. template <>
  4952. std::string impliedValue<std::string>(std::string /*unused*/) // NOLINT(*)
  4953. {
  4954. return std::string();
  4955. }
  4956. template <typename PropertyType>
  4957. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  4958. PropertyType rhs,
  4959. CompatibleType t);
  4960. template <>
  4961. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  4962. CompatibleType /*unused*/)
  4963. {
  4964. return { lhs == rhs, lhs };
  4965. }
  4966. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  4967. const char* rhs)
  4968. {
  4969. const bool b = strcmp(lhs, rhs) == 0;
  4970. return { b, b ? lhs : nullptr };
  4971. }
  4972. std::pair<bool, std::string> consistentStringProperty(const std::string& lhs,
  4973. const std::string& rhs)
  4974. {
  4975. const bool b = lhs == rhs;
  4976. return { b, b ? lhs : valueAsString(nullptr) };
  4977. }
  4978. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  4979. const char* rhs,
  4980. CompatibleType t)
  4981. {
  4982. char* pEnd;
  4983. long lnum = strtol(lhs, &pEnd, 0);
  4984. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  4985. return { false, nullptr };
  4986. }
  4987. long rnum = strtol(rhs, &pEnd, 0);
  4988. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  4989. return { false, nullptr };
  4990. }
  4991. if (t == NumberMaxType) {
  4992. return { true, std::max(lnum, rnum) == lnum ? lhs : rhs };
  4993. }
  4994. return { true, std::min(lnum, rnum) == lnum ? lhs : rhs };
  4995. }
  4996. template <>
  4997. std::pair<bool, const char*> consistentProperty(const char* lhs,
  4998. const char* rhs,
  4999. CompatibleType t)
  5000. {
  5001. if (!lhs && !rhs) {
  5002. return { true, lhs };
  5003. }
  5004. if (!lhs) {
  5005. return { true, rhs };
  5006. }
  5007. if (!rhs) {
  5008. return { true, lhs };
  5009. }
  5010. switch (t) {
  5011. case BoolType: {
  5012. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  5013. return { same, same ? lhs : nullptr };
  5014. }
  5015. case StringType:
  5016. return consistentStringProperty(lhs, rhs);
  5017. case NumberMinType:
  5018. case NumberMaxType:
  5019. return consistentNumberProperty(lhs, rhs, t);
  5020. }
  5021. assert(false && "Unreachable!");
  5022. return { false, nullptr };
  5023. }
  5024. std::pair<bool, std::string> consistentProperty(const std::string& lhs,
  5025. const std::string& rhs,
  5026. CompatibleType t)
  5027. {
  5028. const std::string null_ptr = valueAsString(nullptr);
  5029. if (lhs == null_ptr && rhs == null_ptr) {
  5030. return { true, lhs };
  5031. }
  5032. if (lhs == null_ptr) {
  5033. return { true, rhs };
  5034. }
  5035. if (rhs == null_ptr) {
  5036. return { true, lhs };
  5037. }
  5038. switch (t) {
  5039. case BoolType: {
  5040. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  5041. return { same, same ? lhs : null_ptr };
  5042. }
  5043. case StringType:
  5044. return consistentStringProperty(lhs, rhs);
  5045. case NumberMinType:
  5046. case NumberMaxType: {
  5047. auto value = consistentNumberProperty(lhs.c_str(), rhs.c_str(), t);
  5048. return { value.first,
  5049. value.first ? std::string(value.second) : null_ptr };
  5050. }
  5051. }
  5052. assert(false && "Unreachable!");
  5053. return { false, null_ptr };
  5054. }
  5055. template <typename PropertyType>
  5056. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  5057. const std::string& p,
  5058. const std::string& config,
  5059. const char* defaultValue,
  5060. CompatibleType t,
  5061. PropertyType* /*unused*/)
  5062. {
  5063. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  5064. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  5065. const bool explicitlySet = cm::contains(headPropKeys, p);
  5066. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  5067. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  5068. std::vector<cmGeneratorTarget const*> const& deps =
  5069. tgt->GetLinkImplementationClosure(config);
  5070. if (deps.empty()) {
  5071. return propContent;
  5072. }
  5073. bool propInitialized = explicitlySet;
  5074. std::string report = cmStrCat(" * Target \"", tgt->GetName());
  5075. if (explicitlySet) {
  5076. report += "\" has property content \"";
  5077. report += valueAsString<PropertyType>(propContent);
  5078. report += "\"\n";
  5079. } else if (impliedByUse) {
  5080. report += "\" property is implied by use.\n";
  5081. } else {
  5082. report += "\" property not set.\n";
  5083. }
  5084. std::string interfaceProperty = "INTERFACE_" + p;
  5085. std::unique_ptr<cmGeneratorExpressionInterpreter> genexInterpreter;
  5086. if (p == "POSITION_INDEPENDENT_CODE") {
  5087. genexInterpreter = cm::make_unique<cmGeneratorExpressionInterpreter>(
  5088. tgt->GetLocalGenerator(), config, tgt);
  5089. }
  5090. for (cmGeneratorTarget const* theTarget : deps) {
  5091. // An error should be reported if one dependency
  5092. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  5093. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  5094. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  5095. // with a dependency.
  5096. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  5097. const bool ifaceIsSet = cm::contains(propKeys, interfaceProperty);
  5098. PropertyType ifacePropContent = getTypedProperty<PropertyType>(
  5099. theTarget, interfaceProperty, genexInterpreter.get());
  5100. std::string reportEntry;
  5101. if (ifaceIsSet) {
  5102. reportEntry += " * Target \"";
  5103. reportEntry += theTarget->GetName();
  5104. reportEntry += "\" property value \"";
  5105. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  5106. reportEntry += "\" ";
  5107. }
  5108. if (explicitlySet) {
  5109. if (ifaceIsSet) {
  5110. std::pair<bool, PropertyType> consistent =
  5111. consistentProperty(propContent, ifacePropContent, t);
  5112. report += reportEntry;
  5113. report += compatibilityAgree(t, propContent != consistent.second);
  5114. if (!consistent.first) {
  5115. std::ostringstream e;
  5116. e << "Property " << p << " on target \"" << tgt->GetName()
  5117. << "\" does\nnot match the "
  5118. "INTERFACE_"
  5119. << p
  5120. << " property requirement\nof "
  5121. "dependency \""
  5122. << theTarget->GetName() << "\".\n";
  5123. cmSystemTools::Error(e.str());
  5124. break;
  5125. }
  5126. propContent = consistent.second;
  5127. continue;
  5128. }
  5129. // Explicitly set on target and not set in iface. Can't disagree.
  5130. continue;
  5131. }
  5132. if (impliedByUse) {
  5133. propContent = impliedValue<PropertyType>(propContent);
  5134. if (ifaceIsSet) {
  5135. std::pair<bool, PropertyType> consistent =
  5136. consistentProperty(propContent, ifacePropContent, t);
  5137. report += reportEntry;
  5138. report += compatibilityAgree(t, propContent != consistent.second);
  5139. if (!consistent.first) {
  5140. std::ostringstream e;
  5141. e << "Property " << p << " on target \"" << tgt->GetName()
  5142. << "\" is\nimplied to be " << defaultValue
  5143. << " because it was used to determine the link libraries\n"
  5144. "already. The INTERFACE_"
  5145. << p << " property on\ndependency \"" << theTarget->GetName()
  5146. << "\" is in conflict.\n";
  5147. cmSystemTools::Error(e.str());
  5148. break;
  5149. }
  5150. propContent = consistent.second;
  5151. continue;
  5152. }
  5153. // Implicitly set on target and not set in iface. Can't disagree.
  5154. continue;
  5155. }
  5156. if (ifaceIsSet) {
  5157. if (propInitialized) {
  5158. std::pair<bool, PropertyType> consistent =
  5159. consistentProperty(propContent, ifacePropContent, t);
  5160. report += reportEntry;
  5161. report += compatibilityAgree(t, propContent != consistent.second);
  5162. if (!consistent.first) {
  5163. std::ostringstream e;
  5164. e << "The INTERFACE_" << p << " property of \""
  5165. << theTarget->GetName() << "\" does\nnot agree with the value of "
  5166. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  5167. cmSystemTools::Error(e.str());
  5168. break;
  5169. }
  5170. propContent = consistent.second;
  5171. continue;
  5172. }
  5173. report += reportEntry + "(Interface set)\n";
  5174. propContent = ifacePropContent;
  5175. propInitialized = true;
  5176. } else {
  5177. // Not set. Nothing to agree on.
  5178. continue;
  5179. }
  5180. }
  5181. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  5182. report, compatibilityType(t));
  5183. return propContent;
  5184. }
  5185. bool cmGeneratorTarget::SetDeviceLink(bool deviceLink)
  5186. {
  5187. bool previous = this->DeviceLink;
  5188. this->DeviceLink = deviceLink;
  5189. return previous;
  5190. }
  5191. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  5192. const std::string& p, const std::string& config) const
  5193. {
  5194. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  5195. BoolType, nullptr);
  5196. }
  5197. std::string cmGeneratorTarget::GetLinkInterfaceDependentStringAsBoolProperty(
  5198. const std::string& p, const std::string& config) const
  5199. {
  5200. return checkInterfacePropertyCompatibility<std::string>(
  5201. this, p, config, "FALSE", BoolType, nullptr);
  5202. }
  5203. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  5204. const std::string& p, const std::string& config) const
  5205. {
  5206. return checkInterfacePropertyCompatibility<const char*>(
  5207. this, p, config, "empty", StringType, nullptr);
  5208. }
  5209. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  5210. const std::string& p, const std::string& config) const
  5211. {
  5212. return checkInterfacePropertyCompatibility<const char*>(
  5213. this, p, config, "empty", NumberMinType, nullptr);
  5214. }
  5215. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  5216. const std::string& p, const std::string& config) const
  5217. {
  5218. return checkInterfacePropertyCompatibility<const char*>(
  5219. this, p, config, "empty", NumberMaxType, nullptr);
  5220. }
  5221. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  5222. const std::string& config) const
  5223. {
  5224. // Lookup any existing information for this configuration.
  5225. std::string key(cmSystemTools::UpperCase(config));
  5226. auto i = this->LinkInformation.find(key);
  5227. if (i == this->LinkInformation.end()) {
  5228. // Compute information for this configuration.
  5229. auto info = cm::make_unique<cmComputeLinkInformation>(this, config);
  5230. if (info && !info->Compute()) {
  5231. info.reset();
  5232. }
  5233. // Store the information for this configuration.
  5234. i = this->LinkInformation.emplace(key, std::move(info)).first;
  5235. if (i->second) {
  5236. this->CheckPropertyCompatibility(*i->second, config);
  5237. }
  5238. }
  5239. return i->second.get();
  5240. }
  5241. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  5242. {
  5243. int patch;
  5244. this->GetTargetVersion("VERSION", major, minor, patch);
  5245. }
  5246. void cmGeneratorTarget::GetTargetVersionFallback(
  5247. const std::string& property, const std::string& fallback_property,
  5248. int& major, int& minor, int& patch) const
  5249. {
  5250. if (this->GetProperty(property)) {
  5251. this->GetTargetVersion(property, major, minor, patch);
  5252. } else {
  5253. this->GetTargetVersion(fallback_property, major, minor, patch);
  5254. }
  5255. }
  5256. void cmGeneratorTarget::GetTargetVersion(const std::string& property,
  5257. int& major, int& minor,
  5258. int& patch) const
  5259. {
  5260. // Set the default values.
  5261. major = 0;
  5262. minor = 0;
  5263. patch = 0;
  5264. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5265. if (cmProp version = this->GetProperty(property)) {
  5266. // Try to parse the version number and store the results that were
  5267. // successfully parsed.
  5268. int parsed_major;
  5269. int parsed_minor;
  5270. int parsed_patch;
  5271. switch (sscanf(version->c_str(), "%d.%d.%d", &parsed_major, &parsed_minor,
  5272. &parsed_patch)) {
  5273. case 3:
  5274. patch = parsed_patch;
  5275. CM_FALLTHROUGH;
  5276. case 2:
  5277. minor = parsed_minor;
  5278. CM_FALLTHROUGH;
  5279. case 1:
  5280. major = parsed_major;
  5281. CM_FALLTHROUGH;
  5282. default:
  5283. break;
  5284. }
  5285. }
  5286. }
  5287. std::string cmGeneratorTarget::GetRuntimeLinkLibrary(
  5288. std::string const& lang, std::string const& config) const
  5289. {
  5290. // This is activated by the presence of a default selection whether or
  5291. // not it is overridden by a property.
  5292. cmProp runtimeLibraryDefault = this->Makefile->GetDefinition(
  5293. cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT"));
  5294. if (!cmNonempty(runtimeLibraryDefault)) {
  5295. return std::string();
  5296. }
  5297. cmProp runtimeLibraryValue =
  5298. this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
  5299. if (!runtimeLibraryValue) {
  5300. runtimeLibraryValue = runtimeLibraryDefault;
  5301. }
  5302. return cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
  5303. *runtimeLibraryValue, this->LocalGenerator, config, this));
  5304. }
  5305. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  5306. std::string const& working_dir) const
  5307. {
  5308. if (!this->FortranModuleDirectoryCreated) {
  5309. this->FortranModuleDirectory =
  5310. this->CreateFortranModuleDirectory(working_dir);
  5311. this->FortranModuleDirectoryCreated = true;
  5312. }
  5313. return this->FortranModuleDirectory;
  5314. }
  5315. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  5316. std::string const& working_dir) const
  5317. {
  5318. std::string mod_dir;
  5319. std::string target_mod_dir;
  5320. if (cmProp prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  5321. target_mod_dir = *prop;
  5322. } else {
  5323. std::string const& default_mod_dir =
  5324. this->LocalGenerator->GetCurrentBinaryDirectory();
  5325. if (default_mod_dir != working_dir) {
  5326. target_mod_dir = default_mod_dir;
  5327. }
  5328. }
  5329. cmProp moddir_flag =
  5330. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  5331. if (!target_mod_dir.empty() && moddir_flag) {
  5332. // Compute the full path to the module directory.
  5333. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  5334. // Already a full path.
  5335. mod_dir = target_mod_dir;
  5336. } else {
  5337. // Interpret relative to the current output directory.
  5338. mod_dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  5339. '/', target_mod_dir);
  5340. }
  5341. // Make sure the module output directory exists.
  5342. cmSystemTools::MakeDirectory(mod_dir);
  5343. }
  5344. return mod_dir;
  5345. }
  5346. void cmGeneratorTarget::AddISPCGeneratedHeader(std::string const& header,
  5347. std::string const& config)
  5348. {
  5349. std::string config_upper;
  5350. if (!config.empty()) {
  5351. config_upper = cmSystemTools::UpperCase(config);
  5352. }
  5353. auto iter = this->ISPCGeneratedHeaders.find(config_upper);
  5354. if (iter == this->ISPCGeneratedHeaders.end()) {
  5355. std::vector<std::string> headers;
  5356. headers.emplace_back(header);
  5357. this->ISPCGeneratedHeaders.insert({ config_upper, headers });
  5358. } else {
  5359. iter->second.emplace_back(header);
  5360. }
  5361. }
  5362. std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCHeaders(
  5363. std::string const& config) const
  5364. {
  5365. std::string config_upper;
  5366. if (!config.empty()) {
  5367. config_upper = cmSystemTools::UpperCase(config);
  5368. }
  5369. auto iter = this->ISPCGeneratedHeaders.find(config_upper);
  5370. if (iter == this->ISPCGeneratedHeaders.end()) {
  5371. return std::vector<std::string>{};
  5372. }
  5373. return iter->second;
  5374. }
  5375. void cmGeneratorTarget::AddISPCGeneratedObject(std::vector<std::string>&& objs,
  5376. std::string const& config)
  5377. {
  5378. std::string config_upper;
  5379. if (!config.empty()) {
  5380. config_upper = cmSystemTools::UpperCase(config);
  5381. }
  5382. auto iter = this->ISPCGeneratedObjects.find(config_upper);
  5383. if (iter == this->ISPCGeneratedObjects.end()) {
  5384. this->ISPCGeneratedObjects.insert({ config_upper, objs });
  5385. } else {
  5386. iter->second.insert(iter->second.end(), objs.begin(), objs.end());
  5387. }
  5388. }
  5389. std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCObjects(
  5390. std::string const& config) const
  5391. {
  5392. std::string config_upper;
  5393. if (!config.empty()) {
  5394. config_upper = cmSystemTools::UpperCase(config);
  5395. }
  5396. auto iter = this->ISPCGeneratedObjects.find(config_upper);
  5397. if (iter == this->ISPCGeneratedObjects.end()) {
  5398. return std::vector<std::string>{};
  5399. }
  5400. return iter->second;
  5401. }
  5402. std::string cmGeneratorTarget::GetFrameworkVersion() const
  5403. {
  5404. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5405. if (cmProp fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  5406. return *fversion;
  5407. }
  5408. if (cmProp tversion = this->GetProperty("VERSION")) {
  5409. return *tversion;
  5410. }
  5411. return "A";
  5412. }
  5413. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  5414. std::string const& prefix,
  5415. std::string const& base,
  5416. std::string const& suffix,
  5417. std::string const& name,
  5418. const char* version) const
  5419. {
  5420. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  5421. if (version) {
  5422. vName += ".";
  5423. vName += version;
  5424. }
  5425. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  5426. }
  5427. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  5428. {
  5429. return this->Target->GetProperties().GetKeys();
  5430. }
  5431. void cmGeneratorTarget::ReportPropertyOrigin(
  5432. const std::string& p, const std::string& result, const std::string& report,
  5433. const std::string& compatibilityType) const
  5434. {
  5435. std::vector<std::string> debugProperties;
  5436. this->Target->GetMakefile()->GetDefExpandList(
  5437. "CMAKE_DEBUG_TARGET_PROPERTIES", debugProperties);
  5438. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  5439. cm::contains(debugProperties, p);
  5440. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  5441. this->DebugCompatiblePropertiesDone[p] = true;
  5442. }
  5443. if (!debugOrigin) {
  5444. return;
  5445. }
  5446. std::string areport =
  5447. cmStrCat(compatibilityType, " of property \"", p, "\" for target \"",
  5448. this->GetName(), "\" (result: \"", result, "\"):\n", report);
  5449. this->LocalGenerator->GetCMakeInstance()->IssueMessage(MessageType::LOG,
  5450. areport);
  5451. }
  5452. bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n,
  5453. cmLocalGenerator const*& lg) const
  5454. {
  5455. if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) {
  5456. cmDirectoryId const dirId = n.substr(sizeof(CMAKE_DIRECTORY_ID_SEP) - 1);
  5457. if (dirId.String.empty()) {
  5458. lg = this->LocalGenerator;
  5459. return true;
  5460. }
  5461. if (cmLocalGenerator const* otherLG =
  5462. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  5463. lg = otherLG;
  5464. return true;
  5465. }
  5466. }
  5467. return false;
  5468. }
  5469. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  5470. cmListFileBacktrace const& bt,
  5471. std::vector<cmLinkItem>& items) const
  5472. {
  5473. cmLocalGenerator const* lg = this->LocalGenerator;
  5474. for (std::string const& n : names) {
  5475. if (this->IsLinkLookupScope(n, lg)) {
  5476. continue;
  5477. }
  5478. std::string name = this->CheckCMP0004(n);
  5479. if (name == this->GetName() || name.empty()) {
  5480. continue;
  5481. }
  5482. items.push_back(this->ResolveLinkItem(name, bt, lg));
  5483. }
  5484. }
  5485. void cmGeneratorTarget::ExpandLinkItems(
  5486. std::string const& prop, std::string const& value, std::string const& config,
  5487. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  5488. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition,
  5489. bool& hadContextSensitiveCondition,
  5490. bool& hadLinkLanguageSensitiveCondition) const
  5491. {
  5492. // Keep this logic in sync with ComputeLinkImplementationLibraries.
  5493. cmGeneratorExpression ge;
  5494. cmGeneratorExpressionDAGChecker dagChecker(this, prop, nullptr, nullptr);
  5495. // The $<LINK_ONLY> expression may be in a link interface to specify
  5496. // private link dependencies that are otherwise excluded from usage
  5497. // requirements.
  5498. if (usage_requirements_only) {
  5499. dagChecker.SetTransitivePropertiesOnly();
  5500. }
  5501. std::vector<std::string> libs;
  5502. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  5503. cmExpandList(cge->Evaluate(this->LocalGenerator, config, headTarget,
  5504. &dagChecker, this, headTarget->LinkerLanguage),
  5505. libs);
  5506. this->LookupLinkItems(libs, cge->GetBacktrace(), items);
  5507. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  5508. hadContextSensitiveCondition = cge->GetHadContextSensitiveCondition();
  5509. hadLinkLanguageSensitiveCondition =
  5510. cge->GetHadLinkLanguageSensitiveCondition();
  5511. }
  5512. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  5513. const std::string& config, cmGeneratorTarget const* head) const
  5514. {
  5515. return this->GetLinkInterface(config, head, false);
  5516. }
  5517. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  5518. const std::string& config, cmGeneratorTarget const* head,
  5519. bool secondPass) const
  5520. {
  5521. // Imported targets have their own link interface.
  5522. if (this->IsImported()) {
  5523. return this->GetImportLinkInterface(config, head, false, secondPass);
  5524. }
  5525. // Link interfaces are not supported for executables that do not
  5526. // export symbols.
  5527. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  5528. !this->IsExecutableWithExports()) {
  5529. return nullptr;
  5530. }
  5531. // Lookup any existing link interface for this configuration.
  5532. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  5533. if (secondPass) {
  5534. hm.erase(head);
  5535. }
  5536. // If the link interface does not depend on the head target
  5537. // then return the one we computed first.
  5538. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5539. return &hm.begin()->second;
  5540. }
  5541. cmOptionalLinkInterface& iface = hm[head];
  5542. if (!iface.LibrariesDone) {
  5543. iface.LibrariesDone = true;
  5544. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  5545. }
  5546. if (!iface.AllDone) {
  5547. iface.AllDone = true;
  5548. if (iface.Exists) {
  5549. this->ComputeLinkInterface(config, iface, head, secondPass);
  5550. }
  5551. }
  5552. return iface.Exists ? &iface : nullptr;
  5553. }
  5554. void cmGeneratorTarget::ComputeLinkInterface(
  5555. const std::string& config, cmOptionalLinkInterface& iface,
  5556. cmGeneratorTarget const* headTarget) const
  5557. {
  5558. this->ComputeLinkInterface(config, iface, headTarget, false);
  5559. }
  5560. void cmGeneratorTarget::ComputeLinkInterface(
  5561. const std::string& config, cmOptionalLinkInterface& iface,
  5562. cmGeneratorTarget const* headTarget, bool secondPass) const
  5563. {
  5564. if (iface.Explicit) {
  5565. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5566. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5567. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5568. // Shared libraries may have runtime implementation dependencies
  5569. // on other shared libraries that are not in the interface.
  5570. std::set<cmLinkItem> emitted;
  5571. for (cmLinkItem const& lib : iface.Libraries) {
  5572. emitted.insert(lib);
  5573. }
  5574. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  5575. cmLinkImplementation const* impl =
  5576. this->GetLinkImplementation(config, secondPass);
  5577. for (cmLinkImplItem const& lib : impl->Libraries) {
  5578. if (emitted.insert(lib).second) {
  5579. if (lib.Target) {
  5580. // This is a runtime dependency on another shared library.
  5581. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5582. iface.SharedDeps.push_back(lib);
  5583. }
  5584. } else {
  5585. // TODO: Recognize shared library file names. Perhaps this
  5586. // should be moved to cmComputeLinkInformation, but that
  5587. // creates a chicken-and-egg problem since this list is needed
  5588. // for its construction.
  5589. }
  5590. }
  5591. }
  5592. }
  5593. }
  5594. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  5595. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  5596. // The link implementation is the default link interface.
  5597. cmLinkImplementationLibraries const* impl =
  5598. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  5599. iface.ImplementationIsInterface = true;
  5600. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  5601. }
  5602. if (this->LinkLanguagePropagatesToDependents()) {
  5603. // Targets using this archive need its language runtime libraries.
  5604. if (cmLinkImplementation const* impl =
  5605. this->GetLinkImplementation(config, secondPass)) {
  5606. iface.Languages = impl->Languages;
  5607. }
  5608. }
  5609. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5610. // Construct the property name suffix for this configuration.
  5611. std::string suffix = "_";
  5612. if (!config.empty()) {
  5613. suffix += cmSystemTools::UpperCase(config);
  5614. } else {
  5615. suffix += "NOCONFIG";
  5616. }
  5617. // How many repetitions are needed if this library has cyclic
  5618. // dependencies?
  5619. std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix);
  5620. if (cmProp config_reps = this->GetProperty(propName)) {
  5621. sscanf(config_reps->c_str(), "%u", &iface.Multiplicity);
  5622. } else if (cmProp reps =
  5623. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  5624. sscanf(reps->c_str(), "%u", &iface.Multiplicity);
  5625. }
  5626. }
  5627. }
  5628. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  5629. const std::string& config, cmGeneratorTarget const* head,
  5630. bool usage_requirements_only) const
  5631. {
  5632. // Imported targets have their own link interface.
  5633. if (this->IsImported()) {
  5634. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  5635. }
  5636. // Link interfaces are not supported for executables that do not
  5637. // export symbols.
  5638. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  5639. !this->IsExecutableWithExports()) {
  5640. return nullptr;
  5641. }
  5642. // Lookup any existing link interface for this configuration.
  5643. cmHeadToLinkInterfaceMap& hm =
  5644. (usage_requirements_only
  5645. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  5646. : this->GetHeadToLinkInterfaceMap(config));
  5647. // If the link interface does not depend on the head target
  5648. // then return the one we computed first.
  5649. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5650. return &hm.begin()->second;
  5651. }
  5652. cmOptionalLinkInterface& iface = hm[head];
  5653. if (!iface.LibrariesDone) {
  5654. iface.LibrariesDone = true;
  5655. this->ComputeLinkInterfaceLibraries(config, iface, head,
  5656. usage_requirements_only);
  5657. }
  5658. return iface.Exists ? &iface : nullptr;
  5659. }
  5660. std::string cmGeneratorTarget::GetDirectory(
  5661. const std::string& config, cmStateEnums::ArtifactType artifact) const
  5662. {
  5663. if (this->IsImported()) {
  5664. // Return the directory from which the target is imported.
  5665. return cmSystemTools::GetFilenamePath(
  5666. this->Target->ImportedGetFullPath(config, artifact));
  5667. }
  5668. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  5669. // Return the directory in which the target will be built.
  5670. switch (artifact) {
  5671. case cmStateEnums::RuntimeBinaryArtifact:
  5672. return info->OutDir;
  5673. case cmStateEnums::ImportLibraryArtifact:
  5674. return info->ImpDir;
  5675. }
  5676. }
  5677. return "";
  5678. }
  5679. bool cmGeneratorTarget::UsesDefaultOutputDir(
  5680. const std::string& config, cmStateEnums::ArtifactType artifact) const
  5681. {
  5682. std::string dir;
  5683. return this->ComputeOutputDir(config, artifact, dir);
  5684. }
  5685. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  5686. const std::string& config) const
  5687. {
  5688. // There is no output information for imported targets.
  5689. if (this->IsImported()) {
  5690. return nullptr;
  5691. }
  5692. // Only libraries and executables have well-defined output files.
  5693. if (!this->HaveWellDefinedOutputFiles()) {
  5694. std::string msg = cmStrCat("cmGeneratorTarget::GetOutputInfo called for ",
  5695. this->GetName(), " which has type ",
  5696. cmState::GetTargetTypeName(this->GetType()));
  5697. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  5698. return nullptr;
  5699. }
  5700. // Lookup/compute/cache the output information for this configuration.
  5701. std::string config_upper;
  5702. if (!config.empty()) {
  5703. config_upper = cmSystemTools::UpperCase(config);
  5704. }
  5705. auto i = this->OutputInfoMap.find(config_upper);
  5706. if (i == this->OutputInfoMap.end()) {
  5707. // Add empty info in map to detect potential recursion.
  5708. OutputInfo info;
  5709. OutputInfoMapType::value_type entry(config_upper, info);
  5710. i = this->OutputInfoMap.insert(entry).first;
  5711. // Compute output directories.
  5712. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  5713. info.OutDir);
  5714. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  5715. info.ImpDir);
  5716. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  5717. info.PdbDir = info.OutDir;
  5718. }
  5719. // Now update the previously-prepared map entry.
  5720. i->second = info;
  5721. } else if (i->second.empty()) {
  5722. // An empty map entry indicates we have been called recursively
  5723. // from the above block.
  5724. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  5725. MessageType::FATAL_ERROR,
  5726. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  5727. this->GetBacktrace());
  5728. return nullptr;
  5729. }
  5730. return &i->second;
  5731. }
  5732. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  5733. cmStateEnums::ArtifactType artifact,
  5734. std::string& out) const
  5735. {
  5736. bool usesDefaultOutputDir = false;
  5737. std::string conf = config;
  5738. // Look for a target property defining the target output directory
  5739. // based on the target type.
  5740. std::string targetTypeName = this->GetOutputTargetType(artifact);
  5741. std::string propertyName;
  5742. if (!targetTypeName.empty()) {
  5743. propertyName = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY");
  5744. }
  5745. // Check for a per-configuration output directory target property.
  5746. std::string configUpper = cmSystemTools::UpperCase(conf);
  5747. std::string configProp;
  5748. if (!targetTypeName.empty()) {
  5749. configProp = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY_", configUpper);
  5750. }
  5751. // Select an output directory.
  5752. if (cmProp config_outdir = this->GetProperty(configProp)) {
  5753. // Use the user-specified per-configuration output directory.
  5754. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  5755. config);
  5756. // Skip per-configuration subdirectory.
  5757. conf.clear();
  5758. } else if (cmProp outdir = this->GetProperty(propertyName)) {
  5759. // Use the user-specified output directory.
  5760. out =
  5761. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  5762. // Skip per-configuration subdirectory if the value contained a
  5763. // generator expression.
  5764. if (out != *outdir) {
  5765. conf.clear();
  5766. }
  5767. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  5768. // Lookup the output path for executables.
  5769. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  5770. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5771. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5772. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  5773. // Lookup the output path for libraries.
  5774. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  5775. }
  5776. if (out.empty()) {
  5777. // Default to the current output directory.
  5778. usesDefaultOutputDir = true;
  5779. out = ".";
  5780. }
  5781. // Convert the output path to a full path in case it is
  5782. // specified as a relative path. Treat a relative path as
  5783. // relative to the current output directory for this makefile.
  5784. out = (cmSystemTools::CollapseFullPath(
  5785. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  5786. // The generator may add the configuration's subdirectory.
  5787. if (!conf.empty()) {
  5788. bool useEPN =
  5789. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  5790. std::string suffix =
  5791. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  5792. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  5793. "/", conf, suffix, out);
  5794. }
  5795. return usesDefaultOutputDir;
  5796. }
  5797. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  5798. const std::string& config,
  5799. std::string& out) const
  5800. {
  5801. // Look for a target property defining the target output directory
  5802. // based on the target type.
  5803. std::string propertyName;
  5804. if (!kind.empty()) {
  5805. propertyName = cmStrCat(kind, "_OUTPUT_DIRECTORY");
  5806. }
  5807. std::string conf = config;
  5808. // Check for a per-configuration output directory target property.
  5809. std::string configUpper = cmSystemTools::UpperCase(conf);
  5810. std::string configProp;
  5811. if (!kind.empty()) {
  5812. configProp = cmStrCat(kind, "_OUTPUT_DIRECTORY_", configUpper);
  5813. }
  5814. // Select an output directory.
  5815. if (cmProp config_outdir = this->GetProperty(configProp)) {
  5816. // Use the user-specified per-configuration output directory.
  5817. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  5818. config);
  5819. // Skip per-configuration subdirectory.
  5820. conf.clear();
  5821. } else if (cmProp outdir = this->GetProperty(propertyName)) {
  5822. // Use the user-specified output directory.
  5823. out =
  5824. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  5825. // Skip per-configuration subdirectory if the value contained a
  5826. // generator expression.
  5827. if (out != *outdir) {
  5828. conf.clear();
  5829. }
  5830. }
  5831. if (out.empty()) {
  5832. return false;
  5833. }
  5834. // Convert the output path to a full path in case it is
  5835. // specified as a relative path. Treat a relative path as
  5836. // relative to the current output directory for this makefile.
  5837. out = (cmSystemTools::CollapseFullPath(
  5838. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  5839. // The generator may add the configuration's subdirectory.
  5840. if (!conf.empty()) {
  5841. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  5842. "/", conf, "", out);
  5843. }
  5844. return true;
  5845. }
  5846. bool cmGeneratorTarget::HaveInstallTreeRPATH(const std::string& config) const
  5847. {
  5848. std::string install_rpath;
  5849. this->GetInstallRPATH(config, install_rpath);
  5850. return !install_rpath.empty() &&
  5851. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  5852. }
  5853. bool cmGeneratorTarget::GetBuildRPATH(const std::string& config,
  5854. std::string& rpath) const
  5855. {
  5856. return this->GetRPATH(config, "BUILD_RPATH", rpath);
  5857. }
  5858. bool cmGeneratorTarget::GetInstallRPATH(const std::string& config,
  5859. std::string& rpath) const
  5860. {
  5861. return this->GetRPATH(config, "INSTALL_RPATH", rpath);
  5862. }
  5863. bool cmGeneratorTarget::GetRPATH(const std::string& config,
  5864. const std::string& prop,
  5865. std::string& rpath) const
  5866. {
  5867. cmProp value = this->GetProperty(prop);
  5868. if (!value) {
  5869. return false;
  5870. }
  5871. rpath =
  5872. cmGeneratorExpression::Evaluate(*value, this->LocalGenerator, config);
  5873. return true;
  5874. }
  5875. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  5876. const std::string& config, cmOptionalLinkInterface& iface,
  5877. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  5878. {
  5879. // Construct the property name suffix for this configuration.
  5880. std::string suffix = "_";
  5881. if (!config.empty()) {
  5882. suffix += cmSystemTools::UpperCase(config);
  5883. } else {
  5884. suffix += "NOCONFIG";
  5885. }
  5886. // An explicit list of interface libraries may be set for shared
  5887. // libraries and executables that export symbols.
  5888. cmProp explicitLibraries = nullptr;
  5889. std::string linkIfaceProp;
  5890. bool const cmp0022NEW = (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  5891. this->GetPolicyStatusCMP0022() != cmPolicies::WARN);
  5892. if (cmp0022NEW) {
  5893. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  5894. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  5895. explicitLibraries = this->GetProperty(linkIfaceProp);
  5896. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5897. this->IsExecutableWithExports()) {
  5898. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  5899. // shared lib or executable.
  5900. // Lookup the per-configuration property.
  5901. linkIfaceProp = cmStrCat("LINK_INTERFACE_LIBRARIES", suffix);
  5902. explicitLibraries = this->GetProperty(linkIfaceProp);
  5903. // If not set, try the generic property.
  5904. if (!explicitLibraries) {
  5905. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  5906. explicitLibraries = this->GetProperty(linkIfaceProp);
  5907. }
  5908. }
  5909. if (explicitLibraries &&
  5910. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  5911. !this->PolicyWarnedCMP0022) {
  5912. // Compare the explicitly set old link interface properties to the
  5913. // preferred new link interface property one and warn if different.
  5914. cmProp newExplicitLibraries =
  5915. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  5916. if (newExplicitLibraries &&
  5917. (*newExplicitLibraries != *explicitLibraries)) {
  5918. std::ostringstream w;
  5919. /* clang-format off */
  5920. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  5921. "Target \"" << this->GetName() << "\" has an "
  5922. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  5923. linkIfaceProp << " properties."
  5924. "\n"
  5925. "INTERFACE_LINK_LIBRARIES:\n"
  5926. " " << *newExplicitLibraries << "\n" <<
  5927. linkIfaceProp << ":\n"
  5928. " " << *explicitLibraries << "\n";
  5929. /* clang-format on */
  5930. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  5931. this->PolicyWarnedCMP0022 = true;
  5932. }
  5933. }
  5934. // There is no implicit link interface for executables or modules
  5935. // so if none was explicitly set then there is no link interface.
  5936. if (!explicitLibraries &&
  5937. (this->GetType() == cmStateEnums::EXECUTABLE ||
  5938. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  5939. return;
  5940. }
  5941. iface.Exists = true;
  5942. iface.Explicit = cmp0022NEW || explicitLibraries != nullptr;
  5943. if (explicitLibraries) {
  5944. // The interface libraries have been explicitly set.
  5945. this->ExpandLinkItems(linkIfaceProp, *explicitLibraries, config,
  5946. headTarget, usage_requirements_only, iface.Libraries,
  5947. iface.HadHeadSensitiveCondition,
  5948. iface.HadContextSensitiveCondition,
  5949. iface.HadLinkLanguageSensitiveCondition);
  5950. return;
  5951. }
  5952. // If CMP0022 is NEW then the plain tll signature sets the
  5953. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  5954. // cleared the property explicitly and we should not fall back
  5955. // to the link implementation.
  5956. if (cmp0022NEW) {
  5957. return;
  5958. }
  5959. // The link implementation is the default link interface.
  5960. if (cmLinkImplementationLibraries const* impl =
  5961. this->GetLinkImplementationLibrariesInternal(config, headTarget)) {
  5962. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  5963. impl->Libraries.end());
  5964. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  5965. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  5966. // Compare the link implementation fallback link interface to the
  5967. // preferred new link interface property and warn if different.
  5968. std::vector<cmLinkItem> ifaceLibs;
  5969. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  5970. if (cmProp newExplicitLibraries = this->GetProperty(newProp)) {
  5971. bool hadHeadSensitiveConditionDummy = false;
  5972. bool hadContextSensitiveConditionDummy = false;
  5973. bool hadLinkLanguageSensitiveConditionDummy = false;
  5974. this->ExpandLinkItems(newProp, *newExplicitLibraries, config,
  5975. headTarget, usage_requirements_only, ifaceLibs,
  5976. hadHeadSensitiveConditionDummy,
  5977. hadContextSensitiveConditionDummy,
  5978. hadLinkLanguageSensitiveConditionDummy);
  5979. }
  5980. if (ifaceLibs != iface.Libraries) {
  5981. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  5982. std::string newLibraries = cmJoin(ifaceLibs, ";");
  5983. if (oldLibraries.empty()) {
  5984. oldLibraries = "(empty)";
  5985. }
  5986. if (newLibraries.empty()) {
  5987. newLibraries = "(empty)";
  5988. }
  5989. std::ostringstream w;
  5990. /* clang-format off */
  5991. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  5992. "Target \"" << this->GetName() << "\" has an "
  5993. "INTERFACE_LINK_LIBRARIES property. "
  5994. "This should be preferred as the source of the link interface "
  5995. "for this library but because CMP0022 is not set CMake is "
  5996. "ignoring the property and using the link implementation "
  5997. "as the link interface instead."
  5998. "\n"
  5999. "INTERFACE_LINK_LIBRARIES:\n"
  6000. " " << newLibraries << "\n"
  6001. "Link implementation:\n"
  6002. " " << oldLibraries << "\n";
  6003. /* clang-format on */
  6004. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  6005. w.str());
  6006. this->PolicyWarnedCMP0022 = true;
  6007. }
  6008. }
  6009. }
  6010. }
  6011. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  6012. const std::string& config, cmGeneratorTarget const* headTarget,
  6013. bool usage_requirements_only, bool secondPass) const
  6014. {
  6015. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  6016. if (!info) {
  6017. return nullptr;
  6018. }
  6019. cmHeadToLinkInterfaceMap& hm =
  6020. (usage_requirements_only
  6021. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  6022. : this->GetHeadToLinkInterfaceMap(config));
  6023. if (secondPass) {
  6024. hm.erase(headTarget);
  6025. }
  6026. // If the link interface does not depend on the head target
  6027. // then return the one we computed first.
  6028. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6029. return &hm.begin()->second;
  6030. }
  6031. cmOptionalLinkInterface& iface = hm[headTarget];
  6032. if (!iface.AllDone) {
  6033. iface.AllDone = true;
  6034. iface.Multiplicity = info->Multiplicity;
  6035. cmExpandList(info->Languages, iface.Languages);
  6036. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  6037. headTarget, usage_requirements_only, iface.Libraries,
  6038. iface.HadHeadSensitiveCondition,
  6039. iface.HadContextSensitiveCondition,
  6040. iface.HadLinkLanguageSensitiveCondition);
  6041. std::vector<std::string> deps = cmExpandedList(info->SharedDeps);
  6042. this->LookupLinkItems(deps, cmListFileBacktrace(), iface.SharedDeps);
  6043. }
  6044. return &iface;
  6045. }
  6046. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  6047. const std::string& config) const
  6048. {
  6049. // There is no imported information for non-imported targets.
  6050. if (!this->IsImported()) {
  6051. return nullptr;
  6052. }
  6053. // Lookup/compute/cache the import information for this
  6054. // configuration.
  6055. std::string config_upper;
  6056. if (!config.empty()) {
  6057. config_upper = cmSystemTools::UpperCase(config);
  6058. } else {
  6059. config_upper = "NOCONFIG";
  6060. }
  6061. auto i = this->ImportInfoMap.find(config_upper);
  6062. if (i == this->ImportInfoMap.end()) {
  6063. ImportInfo info;
  6064. this->ComputeImportInfo(config_upper, info);
  6065. ImportInfoMapType::value_type entry(config_upper, info);
  6066. i = this->ImportInfoMap.insert(entry).first;
  6067. }
  6068. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6069. return &i->second;
  6070. }
  6071. // If the location is empty then the target is not available for
  6072. // this configuration.
  6073. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  6074. return nullptr;
  6075. }
  6076. // Return the import information.
  6077. return &i->second;
  6078. }
  6079. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  6080. ImportInfo& info) const
  6081. {
  6082. // This method finds information about an imported target from its
  6083. // properties. The "IMPORTED_" namespace is reserved for properties
  6084. // defined by the project exporting the target.
  6085. // Initialize members.
  6086. info.NoSOName = false;
  6087. cmProp loc = nullptr;
  6088. cmProp imp = nullptr;
  6089. std::string suffix;
  6090. if (!this->Target->GetMappedConfig(desired_config, loc, imp, suffix)) {
  6091. return;
  6092. }
  6093. // Get the link interface.
  6094. {
  6095. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  6096. cmProp propertyLibs = this->GetProperty(linkProp);
  6097. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  6098. if (!propertyLibs) {
  6099. linkProp = cmStrCat("IMPORTED_LINK_INTERFACE_LIBRARIES", suffix);
  6100. propertyLibs = this->GetProperty(linkProp);
  6101. }
  6102. if (!propertyLibs) {
  6103. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  6104. propertyLibs = this->GetProperty(linkProp);
  6105. }
  6106. }
  6107. if (propertyLibs) {
  6108. info.LibrariesProp = linkProp;
  6109. info.Libraries = *propertyLibs;
  6110. }
  6111. }
  6112. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6113. if (loc) {
  6114. info.LibName = *loc;
  6115. }
  6116. return;
  6117. }
  6118. // A provided configuration has been chosen. Load the
  6119. // configuration's properties.
  6120. // Get the location.
  6121. if (loc) {
  6122. info.Location = *loc;
  6123. } else {
  6124. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  6125. if (cmProp config_location = this->GetProperty(impProp)) {
  6126. info.Location = *config_location;
  6127. } else if (cmProp location = this->GetProperty("IMPORTED_LOCATION")) {
  6128. info.Location = *location;
  6129. }
  6130. }
  6131. // Get the soname.
  6132. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6133. std::string soProp = cmStrCat("IMPORTED_SONAME", suffix);
  6134. if (cmProp config_soname = this->GetProperty(soProp)) {
  6135. info.SOName = *config_soname;
  6136. } else if (cmProp soname = this->GetProperty("IMPORTED_SONAME")) {
  6137. info.SOName = *soname;
  6138. }
  6139. }
  6140. // Get the "no-soname" mark.
  6141. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6142. std::string soProp = cmStrCat("IMPORTED_NO_SONAME", suffix);
  6143. if (cmProp config_no_soname = this->GetProperty(soProp)) {
  6144. info.NoSOName = cmIsOn(*config_no_soname);
  6145. } else if (cmProp no_soname = this->GetProperty("IMPORTED_NO_SONAME")) {
  6146. info.NoSOName = cmIsOn(*no_soname);
  6147. }
  6148. }
  6149. // Get the import library.
  6150. if (imp) {
  6151. info.ImportLibrary = *imp;
  6152. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6153. this->IsExecutableWithExports()) {
  6154. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  6155. if (cmProp config_implib = this->GetProperty(impProp)) {
  6156. info.ImportLibrary = *config_implib;
  6157. } else if (cmProp implib = this->GetProperty("IMPORTED_IMPLIB")) {
  6158. info.ImportLibrary = *implib;
  6159. }
  6160. }
  6161. // Get the link dependencies.
  6162. {
  6163. std::string linkProp =
  6164. cmStrCat("IMPORTED_LINK_DEPENDENT_LIBRARIES", suffix);
  6165. if (cmProp config_libs = this->GetProperty(linkProp)) {
  6166. info.SharedDeps = *config_libs;
  6167. } else if (cmProp libs =
  6168. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  6169. info.SharedDeps = *libs;
  6170. }
  6171. }
  6172. // Get the link languages.
  6173. if (this->LinkLanguagePropagatesToDependents()) {
  6174. std::string linkProp =
  6175. cmStrCat("IMPORTED_LINK_INTERFACE_LANGUAGES", suffix);
  6176. if (cmProp config_libs = this->GetProperty(linkProp)) {
  6177. info.Languages = *config_libs;
  6178. } else if (cmProp libs =
  6179. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  6180. info.Languages = *libs;
  6181. }
  6182. }
  6183. // Get information if target is managed assembly.
  6184. {
  6185. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  6186. if (cmProp pc = this->GetProperty(linkProp + suffix)) {
  6187. info.Managed = this->CheckManagedType(*pc);
  6188. } else if (cmProp p = this->GetProperty(linkProp)) {
  6189. info.Managed = this->CheckManagedType(*p);
  6190. }
  6191. }
  6192. // Get the cyclic repetition count.
  6193. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6194. std::string linkProp =
  6195. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  6196. if (cmProp config_reps = this->GetProperty(linkProp)) {
  6197. sscanf(config_reps->c_str(), "%u", &info.Multiplicity);
  6198. } else if (cmProp reps =
  6199. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  6200. sscanf(reps->c_str(), "%u", &info.Multiplicity);
  6201. }
  6202. }
  6203. }
  6204. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  6205. const std::string& config) const
  6206. {
  6207. return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)];
  6208. }
  6209. cmHeadToLinkInterfaceMap&
  6210. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  6211. const std::string& config) const
  6212. {
  6213. return this
  6214. ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  6215. }
  6216. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6217. const std::string& config) const
  6218. {
  6219. return this->GetLinkImplementation(config, false);
  6220. }
  6221. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6222. const std::string& config, bool secondPass) const
  6223. {
  6224. // There is no link implementation for targets that cannot compile sources.
  6225. if (!this->CanCompileSources()) {
  6226. return nullptr;
  6227. }
  6228. cmOptionalLinkImplementation& impl =
  6229. this->LinkImplMap[cmSystemTools::UpperCase(config)][this];
  6230. if (secondPass) {
  6231. impl = cmOptionalLinkImplementation();
  6232. }
  6233. if (!impl.LibrariesDone) {
  6234. impl.LibrariesDone = true;
  6235. this->ComputeLinkImplementationLibraries(config, impl, this);
  6236. }
  6237. if (!impl.LanguagesDone) {
  6238. impl.LanguagesDone = true;
  6239. this->ComputeLinkImplementationLanguages(config, impl);
  6240. }
  6241. return &impl;
  6242. }
  6243. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  6244. std::vector<cmSourceFile*>& files) const
  6245. {
  6246. std::vector<std::string> const& configs =
  6247. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  6248. auto it = configs.begin();
  6249. const std::string& firstConfig = *it;
  6250. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  6251. for (; it != configs.end(); ++it) {
  6252. std::vector<cmSourceFile*> configFiles;
  6253. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  6254. if (configFiles != files) {
  6255. std::string firstConfigFiles;
  6256. const char* sep = "";
  6257. for (cmSourceFile* f : files) {
  6258. firstConfigFiles += sep;
  6259. firstConfigFiles += f->ResolveFullPath();
  6260. sep = "\n ";
  6261. }
  6262. std::string thisConfigFiles;
  6263. sep = "";
  6264. for (cmSourceFile* f : configFiles) {
  6265. thisConfigFiles += sep;
  6266. thisConfigFiles += f->ResolveFullPath();
  6267. sep = "\n ";
  6268. }
  6269. std::ostringstream e;
  6270. /* clang-format off */
  6271. e << "Target \"" << this->GetName()
  6272. << "\" has source files which vary by "
  6273. "configuration. This is not supported by the \""
  6274. << this->GlobalGenerator->GetName()
  6275. << "\" generator.\n"
  6276. "Config \"" << firstConfig << "\":\n"
  6277. " " << firstConfigFiles << "\n"
  6278. "Config \"" << *it << "\":\n"
  6279. " " << thisConfigFiles << "\n";
  6280. /* clang-format on */
  6281. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  6282. return false;
  6283. }
  6284. }
  6285. return true;
  6286. }
  6287. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  6288. std::vector<cmGeneratorTarget*>& objlibs) const
  6289. {
  6290. // At configure-time, this method can be called as part of getting the
  6291. // LOCATION property or to export() a file to be include()d. However
  6292. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  6293. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  6294. // behavior of CMP0024 and CMP0026 only.
  6295. cmStringRange rng = this->Target->GetSourceEntries();
  6296. for (std::string const& entry : rng) {
  6297. std::vector<std::string> files = cmExpandedList(entry);
  6298. for (std::string const& li : files) {
  6299. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") && li.back() == '>') {
  6300. std::string objLibName = li.substr(17, li.size() - 18);
  6301. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  6302. continue;
  6303. }
  6304. cmGeneratorTarget* objLib =
  6305. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  6306. if (objLib) {
  6307. objlibs.push_back(objLib);
  6308. }
  6309. }
  6310. }
  6311. }
  6312. }
  6313. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  6314. {
  6315. // Strip whitespace off the library names because we used to do this
  6316. // in case variables were expanded at generate time. We no longer
  6317. // do the expansion but users link to libraries like " ${VAR} ".
  6318. std::string lib = item;
  6319. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  6320. if (pos != std::string::npos) {
  6321. lib = lib.substr(pos);
  6322. }
  6323. pos = lib.find_last_not_of(" \t\r\n");
  6324. if (pos != std::string::npos) {
  6325. lib = lib.substr(0, pos + 1);
  6326. }
  6327. if (lib != item) {
  6328. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  6329. switch (this->GetPolicyStatusCMP0004()) {
  6330. case cmPolicies::WARN: {
  6331. std::ostringstream w;
  6332. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  6333. << "Target \"" << this->GetName() << "\" links to item \"" << item
  6334. << "\" which has leading or trailing whitespace.";
  6335. cm->IssueMessage(MessageType::AUTHOR_WARNING, w.str(),
  6336. this->GetBacktrace());
  6337. }
  6338. case cmPolicies::OLD:
  6339. break;
  6340. case cmPolicies::NEW: {
  6341. std::ostringstream e;
  6342. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  6343. << "\" which has leading or trailing whitespace. "
  6344. << "This is now an error according to policy CMP0004.";
  6345. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  6346. this->GetBacktrace());
  6347. } break;
  6348. case cmPolicies::REQUIRED_IF_USED:
  6349. case cmPolicies::REQUIRED_ALWAYS: {
  6350. std::ostringstream e;
  6351. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  6352. << "Target \"" << this->GetName() << "\" links to item \"" << item
  6353. << "\" which has leading or trailing whitespace.";
  6354. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  6355. this->GetBacktrace());
  6356. } break;
  6357. }
  6358. }
  6359. return lib;
  6360. }
  6361. bool cmGeneratorTarget::IsDeprecated() const
  6362. {
  6363. cmProp deprecation = this->GetProperty("DEPRECATION");
  6364. return cmNonempty(deprecation);
  6365. }
  6366. std::string cmGeneratorTarget::GetDeprecation() const
  6367. {
  6368. // find DEPRECATION property
  6369. if (cmProp deprecation = this->GetProperty("DEPRECATION")) {
  6370. return *deprecation;
  6371. }
  6372. return std::string();
  6373. }
  6374. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  6375. const std::string& config) const
  6376. {
  6377. // Targets that do not compile anything have no languages.
  6378. if (!this->CanCompileSources()) {
  6379. return;
  6380. }
  6381. std::vector<cmSourceFile*> sourceFiles;
  6382. this->GetSourceFiles(sourceFiles, config);
  6383. for (cmSourceFile* src : sourceFiles) {
  6384. const std::string& lang = src->GetOrDetermineLanguage();
  6385. if (!lang.empty()) {
  6386. languages.insert(lang);
  6387. }
  6388. }
  6389. std::vector<cmGeneratorTarget*> objectLibraries;
  6390. std::vector<cmSourceFile const*> externalObjects;
  6391. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  6392. std::vector<cmGeneratorTarget*> objectTargets;
  6393. this->GetObjectLibrariesCMP0026(objectTargets);
  6394. objectLibraries.reserve(objectTargets.size());
  6395. for (cmGeneratorTarget* gt : objectTargets) {
  6396. objectLibraries.push_back(gt);
  6397. }
  6398. } else {
  6399. this->GetExternalObjects(externalObjects, config);
  6400. for (cmSourceFile const* extObj : externalObjects) {
  6401. std::string objLib = extObj->GetObjectLibrary();
  6402. if (cmGeneratorTarget* tgt =
  6403. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  6404. auto const objLibIt =
  6405. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  6406. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  6407. if (objectLibraries.cend() == objLibIt) {
  6408. objectLibraries.push_back(tgt);
  6409. }
  6410. }
  6411. }
  6412. }
  6413. for (cmGeneratorTarget* objLib : objectLibraries) {
  6414. objLib->GetLanguages(languages, config);
  6415. }
  6416. }
  6417. bool cmGeneratorTarget::IsCSharpOnly() const
  6418. {
  6419. // Only certain target types may compile CSharp.
  6420. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  6421. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  6422. this->GetType() != cmStateEnums::EXECUTABLE) {
  6423. return false;
  6424. }
  6425. std::set<std::string> languages = this->GetAllConfigCompileLanguages();
  6426. // Consider an explicit linker language property, but *not* the
  6427. // computed linker language that may depend on linked targets.
  6428. cmProp linkLang = this->GetProperty("LINKER_LANGUAGE");
  6429. if (cmNonempty(linkLang)) {
  6430. languages.insert(*linkLang);
  6431. }
  6432. return languages.size() == 1 && languages.count("CSharp") > 0;
  6433. }
  6434. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  6435. const std::string& config, cmOptionalLinkImplementation& impl) const
  6436. {
  6437. // This target needs runtime libraries for its source languages.
  6438. std::set<std::string> languages;
  6439. // Get languages used in our source files.
  6440. this->GetLanguages(languages, config);
  6441. // Copy the set of languages to the link implementation.
  6442. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  6443. languages.end());
  6444. }
  6445. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  6446. {
  6447. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  6448. return false;
  6449. }
  6450. std::string build_rpath;
  6451. if (this->GetBuildRPATH(config, build_rpath)) {
  6452. return true;
  6453. }
  6454. if (cmLinkImplementationLibraries const* impl =
  6455. this->GetLinkImplementationLibraries(config)) {
  6456. return !impl->Libraries.empty();
  6457. }
  6458. return false;
  6459. }
  6460. cmLinkImplementationLibraries const*
  6461. cmGeneratorTarget::GetLinkImplementationLibraries(
  6462. const std::string& config) const
  6463. {
  6464. return this->GetLinkImplementationLibrariesInternal(config, this);
  6465. }
  6466. cmLinkImplementationLibraries const*
  6467. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  6468. const std::string& config, cmGeneratorTarget const* head) const
  6469. {
  6470. // There is no link implementation for targets that cannot compile sources.
  6471. if (!this->CanCompileSources()) {
  6472. return nullptr;
  6473. }
  6474. // Populate the link implementation libraries for this configuration.
  6475. HeadToLinkImplementationMap& hm =
  6476. this->LinkImplMap[cmSystemTools::UpperCase(config)];
  6477. // If the link implementation does not depend on the head target
  6478. // then return the one we computed first.
  6479. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6480. return &hm.begin()->second;
  6481. }
  6482. cmOptionalLinkImplementation& impl = hm[head];
  6483. if (!impl.LibrariesDone) {
  6484. impl.LibrariesDone = true;
  6485. this->ComputeLinkImplementationLibraries(config, impl, head);
  6486. }
  6487. return &impl;
  6488. }
  6489. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  6490. const std::string& p) const
  6491. {
  6492. return cm::contains(this->LinkImplicitNullProperties, p);
  6493. }
  6494. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  6495. const std::string& config, cmOptionalLinkImplementation& impl,
  6496. cmGeneratorTarget const* head) const
  6497. {
  6498. cmLocalGenerator const* lg = this->LocalGenerator;
  6499. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  6500. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  6501. cmBacktraceRange::const_iterator btIt = btRange.begin();
  6502. // Collect libraries directly linked in this configuration.
  6503. for (cmStringRange::const_iterator le = entryRange.begin(),
  6504. end = entryRange.end();
  6505. le != end; ++le, ++btIt) {
  6506. std::vector<std::string> llibs;
  6507. // Keep this logic in sync with ExpandLinkItems.
  6508. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_LIBRARIES", nullptr,
  6509. nullptr);
  6510. cmGeneratorExpression ge(*btIt);
  6511. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  6512. std::string const& evaluated =
  6513. cge->Evaluate(this->LocalGenerator, config, head, &dagChecker, nullptr,
  6514. this->LinkerLanguage);
  6515. cmExpandList(evaluated, llibs);
  6516. if (cge->GetHadHeadSensitiveCondition()) {
  6517. impl.HadHeadSensitiveCondition = true;
  6518. }
  6519. if (cge->GetHadContextSensitiveCondition()) {
  6520. impl.HadContextSensitiveCondition = true;
  6521. }
  6522. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  6523. impl.HadLinkLanguageSensitiveCondition = true;
  6524. }
  6525. for (std::string const& lib : llibs) {
  6526. if (this->IsLinkLookupScope(lib, lg)) {
  6527. continue;
  6528. }
  6529. // Skip entries that resolve to the target itself or are empty.
  6530. std::string name = this->CheckCMP0004(lib);
  6531. if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) {
  6532. // resolve alias name
  6533. auto target = this->Makefile->FindTargetToUse(name);
  6534. if (target) {
  6535. name = target->GetName();
  6536. }
  6537. }
  6538. if (name == this->GetName() || name.empty()) {
  6539. if (name == this->GetName()) {
  6540. bool noMessage = false;
  6541. MessageType messageType = MessageType::FATAL_ERROR;
  6542. std::ostringstream e;
  6543. switch (this->GetPolicyStatusCMP0038()) {
  6544. case cmPolicies::WARN: {
  6545. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  6546. messageType = MessageType::AUTHOR_WARNING;
  6547. } break;
  6548. case cmPolicies::OLD:
  6549. noMessage = true;
  6550. case cmPolicies::REQUIRED_IF_USED:
  6551. case cmPolicies::REQUIRED_ALWAYS:
  6552. case cmPolicies::NEW:
  6553. // Issue the fatal message.
  6554. break;
  6555. }
  6556. if (!noMessage) {
  6557. e << "Target \"" << this->GetName() << "\" links to itself.";
  6558. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6559. messageType, e.str(), this->GetBacktrace());
  6560. if (messageType == MessageType::FATAL_ERROR) {
  6561. return;
  6562. }
  6563. }
  6564. }
  6565. continue;
  6566. }
  6567. // The entry is meant for this configuration.
  6568. impl.Libraries.emplace_back(this->ResolveLinkItem(name, *btIt, lg),
  6569. evaluated != *le);
  6570. }
  6571. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  6572. for (std::string const& sp : seenProps) {
  6573. if (!this->GetProperty(sp)) {
  6574. this->LinkImplicitNullProperties.insert(sp);
  6575. }
  6576. }
  6577. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  6578. }
  6579. // Get the list of configurations considered to be DEBUG.
  6580. std::vector<std::string> debugConfigs =
  6581. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  6582. cmTargetLinkLibraryType linkType =
  6583. CMP0003_ComputeLinkType(config, debugConfigs);
  6584. cmTarget::LinkLibraryVectorType const& oldllibs =
  6585. this->Target->GetOriginalLinkLibraries();
  6586. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  6587. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  6588. std::string name = this->CheckCMP0004(oldllib.first);
  6589. if (name == this->GetName() || name.empty()) {
  6590. continue;
  6591. }
  6592. // Support OLD behavior for CMP0003.
  6593. impl.WrongConfigLibraries.push_back(
  6594. this->ResolveLinkItem(name, cmListFileBacktrace()));
  6595. }
  6596. }
  6597. }
  6598. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  6599. std::string const& name) const
  6600. {
  6601. return this->ResolveTargetReference(name, this->LocalGenerator);
  6602. }
  6603. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  6604. std::string const& name, cmLocalGenerator const* lg) const
  6605. {
  6606. TargetOrString resolved;
  6607. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  6608. resolved.Target = tgt;
  6609. } else {
  6610. resolved.String = name;
  6611. }
  6612. return resolved;
  6613. }
  6614. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  6615. std::string const& name, cmListFileBacktrace const& bt) const
  6616. {
  6617. return this->ResolveLinkItem(name, bt, this->LocalGenerator);
  6618. }
  6619. cmLinkItem cmGeneratorTarget::ResolveLinkItem(std::string const& name,
  6620. cmListFileBacktrace const& bt,
  6621. cmLocalGenerator const* lg) const
  6622. {
  6623. TargetOrString resolved = this->ResolveTargetReference(name, lg);
  6624. if (!resolved.Target) {
  6625. return cmLinkItem(resolved.String, false, bt);
  6626. }
  6627. // Check deprecation, issue message with `bt` backtrace.
  6628. if (resolved.Target->IsDeprecated()) {
  6629. std::ostringstream w;
  6630. /* clang-format off */
  6631. w <<
  6632. "The library that is being linked to, " << resolved.Target->GetName() <<
  6633. ", is marked as being deprecated by the owner. The message provided by "
  6634. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  6635. /* clang-format on */
  6636. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6637. MessageType::AUTHOR_WARNING, w.str(), bt);
  6638. }
  6639. // Skip targets that will not really be linked. This is probably a
  6640. // name conflict between an external library and an executable
  6641. // within the project.
  6642. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  6643. !resolved.Target->IsExecutableWithExports()) {
  6644. return cmLinkItem(resolved.Target->GetName(), false, bt);
  6645. }
  6646. return cmLinkItem(resolved.Target, false, bt);
  6647. }
  6648. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  6649. {
  6650. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  6651. // Return the directory in which the target will be built.
  6652. return info->PdbDir;
  6653. }
  6654. return "";
  6655. }
  6656. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  6657. {
  6658. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  6659. }
  6660. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  6661. std::string const& gnuName,
  6662. std::string& out,
  6663. const char* newExt) const
  6664. {
  6665. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  6666. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  6667. out = cmStrCat(cm::string_view(gnuName).substr(0, gnuName.size() - 6),
  6668. newExt ? newExt : ".lib");
  6669. return true;
  6670. }
  6671. return false;
  6672. }
  6673. bool cmGeneratorTarget::HasContextDependentSources() const
  6674. {
  6675. return this->SourcesAreContextDependent == Tribool::True;
  6676. }
  6677. bool cmGeneratorTarget::IsExecutableWithExports() const
  6678. {
  6679. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  6680. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  6681. }
  6682. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  6683. {
  6684. return (this->IsDLLPlatform() &&
  6685. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6686. this->IsExecutableWithExports()) &&
  6687. // Assemblies which have only managed code do not have
  6688. // import libraries.
  6689. this->GetManagedType(config) != ManagedType::Managed) ||
  6690. (this->Target->IsAIX() && this->IsExecutableWithExports());
  6691. }
  6692. bool cmGeneratorTarget::NeedImportLibraryName(std::string const& config) const
  6693. {
  6694. return this->HasImportLibrary(config) ||
  6695. // On DLL platforms we always generate the import library name
  6696. // just in case the sources have export markup.
  6697. (this->IsDLLPlatform() &&
  6698. (this->GetType() == cmStateEnums::EXECUTABLE ||
  6699. this->GetType() == cmStateEnums::MODULE_LIBRARY));
  6700. }
  6701. std::string cmGeneratorTarget::GetSupportDirectory() const
  6702. {
  6703. std::string dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  6704. "/CMakeFiles/", this->GetName());
  6705. #if defined(__VMS)
  6706. dir += "_dir";
  6707. #else
  6708. dir += ".dir";
  6709. #endif
  6710. return dir;
  6711. }
  6712. bool cmGeneratorTarget::IsLinkable() const
  6713. {
  6714. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  6715. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6716. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  6717. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  6718. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  6719. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  6720. this->IsExecutableWithExports());
  6721. }
  6722. bool cmGeneratorTarget::IsFrameworkOnApple() const
  6723. {
  6724. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6725. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  6726. this->Makefile->IsOn("APPLE") &&
  6727. this->GetPropertyAsBool("FRAMEWORK"));
  6728. }
  6729. bool cmGeneratorTarget::IsAppBundleOnApple() const
  6730. {
  6731. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  6732. this->Makefile->IsOn("APPLE") &&
  6733. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  6734. }
  6735. bool cmGeneratorTarget::IsXCTestOnApple() const
  6736. {
  6737. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  6738. }
  6739. bool cmGeneratorTarget::IsCFBundleOnApple() const
  6740. {
  6741. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  6742. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  6743. }
  6744. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  6745. std::string const& propval) const
  6746. {
  6747. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  6748. // or only C++/CLI) does only depend on whether the property is an empty
  6749. // string or contains any value at all. In Visual Studio generators
  6750. // this propval is prepended with /clr[:] which results in:
  6751. //
  6752. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  6753. // lib
  6754. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  6755. // target, has import lib
  6756. // 3. any value (safe,pure): add /clr:[propval] as flag, target with
  6757. // managed code only, no import lib
  6758. return propval.empty() ? ManagedType::Mixed : ManagedType::Managed;
  6759. }
  6760. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  6761. const std::string& config) const
  6762. {
  6763. // Only libraries and executables can be managed targets.
  6764. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  6765. return ManagedType::Undefined;
  6766. }
  6767. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6768. return ManagedType::Native;
  6769. }
  6770. // Check imported target.
  6771. if (this->IsImported()) {
  6772. if (cmGeneratorTarget::ImportInfo const* info =
  6773. this->GetImportInfo(config)) {
  6774. return info->Managed;
  6775. }
  6776. return ManagedType::Undefined;
  6777. }
  6778. // Check for explicitly set clr target property.
  6779. if (cmProp clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  6780. return this->CheckManagedType(*clr);
  6781. }
  6782. // C# targets are always managed. This language specific check
  6783. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  6784. // has to be set manually for C# targets.
  6785. return this->IsCSharpOnly() ? ManagedType::Managed : ManagedType::Native;
  6786. }