cmGeneratorTarget.cxx 208 KB

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