cmGeneratorTarget.cxx 208 KB

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