cmGeneratorTarget.cxx 215 KB

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