cmGeneratorTarget.cxx 208 KB

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