cmGeneratorTarget.cxx 244 KB

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