cmGeneratorTarget.cxx 226 KB

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