cmGeneratorTarget.cxx 237 KB

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