cmGeneratorTarget.cxx 193 KB

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