cmGeneratorTarget.cxx 216 KB

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