cmGeneratorTarget.cxx 199 KB

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