cmGeneratorTarget.cxx 236 KB

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