cmGeneratorTarget.cxx 207 KB

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