cmGeneratorTarget.cxx 213 KB

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