cmGeneratorTarget.cxx 203 KB

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