cmGeneratorTarget.cxx 208 KB

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