cmGeneratorTarget.cxx 193 KB

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