cmGeneratorTarget.cxx 222 KB

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