cmGeneratorTarget.cxx 207 KB

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