cmGeneratorTarget.cxx 191 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758
  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 processLinkDepends(
  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, "link depends", language,
  2691. OptionsParse::None);
  2692. }
  2693. }
  2694. void cmGeneratorTarget::GetLinkDepends(std::vector<std::string>& result,
  2695. const std::string& config,
  2696. const std::string& language) const
  2697. {
  2698. std::vector<cmGeneratorTarget::TargetPropertyEntry*> linkDependsEntries;
  2699. std::unordered_set<std::string> uniqueOptions;
  2700. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), "LINK_DEPENDS",
  2701. nullptr, nullptr);
  2702. if (const char* linkDepends = this->GetProperty("LINK_DEPENDS")) {
  2703. std::vector<std::string> depends;
  2704. cmGeneratorExpression ge;
  2705. cmSystemTools::ExpandListArgument(linkDepends, depends);
  2706. for (const auto& depend : depends) {
  2707. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depend);
  2708. linkDependsEntries.push_back(
  2709. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge)));
  2710. }
  2711. }
  2712. AddInterfaceEntries(this, config, "INTERFACE_LINK_DEPENDS",
  2713. linkDependsEntries);
  2714. processLinkDepends(this, linkDependsEntries, result, uniqueOptions,
  2715. &dagChecker, config, language);
  2716. cmDeleteAll(linkDependsEntries);
  2717. }
  2718. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  2719. {
  2720. if (this->IsImported()) {
  2721. return;
  2722. }
  2723. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  2724. // Get the names.
  2725. std::string name;
  2726. std::string soName;
  2727. std::string realName;
  2728. std::string impName;
  2729. std::string pdbName;
  2730. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2731. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2732. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2733. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2734. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2735. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2736. } else {
  2737. return;
  2738. }
  2739. // Get the directory.
  2740. std::string dir =
  2741. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  2742. // Add each name.
  2743. std::string f;
  2744. if (!name.empty()) {
  2745. f = dir;
  2746. f += "/";
  2747. f += name;
  2748. gg->AddToManifest(f);
  2749. }
  2750. if (!soName.empty()) {
  2751. f = dir;
  2752. f += "/";
  2753. f += soName;
  2754. gg->AddToManifest(f);
  2755. }
  2756. if (!realName.empty()) {
  2757. f = dir;
  2758. f += "/";
  2759. f += realName;
  2760. gg->AddToManifest(f);
  2761. }
  2762. if (!pdbName.empty()) {
  2763. f = dir;
  2764. f += "/";
  2765. f += pdbName;
  2766. gg->AddToManifest(f);
  2767. }
  2768. if (!impName.empty()) {
  2769. f = this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  2770. f += "/";
  2771. f += impName;
  2772. gg->AddToManifest(f);
  2773. }
  2774. }
  2775. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  2776. {
  2777. std::vector<std::string> features;
  2778. this->GetCompileFeatures(features, config);
  2779. for (std::string const& f : features) {
  2780. if (!this->Makefile->AddRequiredTargetFeature(this->Target, f)) {
  2781. return false;
  2782. }
  2783. }
  2784. return true;
  2785. }
  2786. std::string cmGeneratorTarget::GetImportedLibName(
  2787. std::string const& config) const
  2788. {
  2789. if (cmGeneratorTarget::ImportInfo const* info =
  2790. this->GetImportInfo(config)) {
  2791. return info->LibName;
  2792. }
  2793. return std::string();
  2794. }
  2795. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  2796. cmStateEnums::ArtifactType artifact,
  2797. bool realname) const
  2798. {
  2799. if (this->IsImported()) {
  2800. return this->Target->ImportedGetFullPath(config, artifact);
  2801. }
  2802. return this->NormalGetFullPath(config, artifact, realname);
  2803. }
  2804. std::string cmGeneratorTarget::NormalGetFullPath(
  2805. const std::string& config, cmStateEnums::ArtifactType artifact,
  2806. bool realname) const
  2807. {
  2808. std::string fpath = this->GetDirectory(config, artifact);
  2809. fpath += "/";
  2810. if (this->IsAppBundleOnApple()) {
  2811. fpath = this->BuildBundleDirectory(fpath, config, FullLevel);
  2812. fpath += "/";
  2813. }
  2814. // Add the full name of the target.
  2815. switch (artifact) {
  2816. case cmStateEnums::RuntimeBinaryArtifact:
  2817. if (realname) {
  2818. fpath += this->NormalGetRealName(config);
  2819. } else {
  2820. fpath +=
  2821. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  2822. }
  2823. break;
  2824. case cmStateEnums::ImportLibraryArtifact:
  2825. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  2826. break;
  2827. }
  2828. return fpath;
  2829. }
  2830. std::string cmGeneratorTarget::NormalGetRealName(
  2831. const std::string& config) const
  2832. {
  2833. // This should not be called for imported targets.
  2834. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2835. // enforcement of the limited imported target API.
  2836. if (this->IsImported()) {
  2837. std::string msg = "NormalGetRealName called on imported target: ";
  2838. msg += this->GetName();
  2839. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2840. }
  2841. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2842. // Compute the real name that will be built.
  2843. std::string name;
  2844. std::string realName;
  2845. std::string impName;
  2846. std::string pdbName;
  2847. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2848. return realName;
  2849. }
  2850. // Compute the real name that will be built.
  2851. std::string name;
  2852. std::string soName;
  2853. std::string realName;
  2854. std::string impName;
  2855. std::string pdbName;
  2856. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2857. return realName;
  2858. }
  2859. void cmGeneratorTarget::GetLibraryNames(std::string& name, std::string& soName,
  2860. std::string& realName,
  2861. std::string& impName,
  2862. std::string& pdbName,
  2863. const std::string& config) const
  2864. {
  2865. // This should not be called for imported targets.
  2866. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2867. // enforcement of the limited imported target API.
  2868. if (this->IsImported()) {
  2869. std::string msg = "GetLibraryNames called on imported target: ";
  2870. msg += this->GetName();
  2871. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2872. return;
  2873. }
  2874. // Check for library version properties.
  2875. const char* version = this->GetProperty("VERSION");
  2876. const char* soversion = this->GetProperty("SOVERSION");
  2877. if (!this->HasSOName(config) ||
  2878. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  2879. this->IsFrameworkOnApple()) {
  2880. // Versioning is supported only for shared libraries and modules,
  2881. // and then only when the platform supports an soname flag.
  2882. version = nullptr;
  2883. soversion = nullptr;
  2884. }
  2885. if (version && !soversion) {
  2886. // The soversion must be set if the library version is set. Use
  2887. // the library version as the soversion.
  2888. soversion = version;
  2889. }
  2890. if (!version && soversion) {
  2891. // Use the soversion as the library version.
  2892. version = soversion;
  2893. }
  2894. // Get the components of the library name.
  2895. std::string prefix;
  2896. std::string base;
  2897. std::string suffix;
  2898. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2899. prefix, base, suffix);
  2900. // The library name.
  2901. name = prefix + base + suffix;
  2902. if (this->IsFrameworkOnApple()) {
  2903. realName = prefix;
  2904. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  2905. realName += "Versions/";
  2906. realName += this->GetFrameworkVersion();
  2907. realName += "/";
  2908. }
  2909. realName += base;
  2910. soName = realName;
  2911. } else {
  2912. // The library's soname.
  2913. this->ComputeVersionedName(soName, prefix, base, suffix, name, soversion);
  2914. // The library's real name on disk.
  2915. this->ComputeVersionedName(realName, prefix, base, suffix, name, version);
  2916. }
  2917. // The import library name.
  2918. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2919. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2920. impName =
  2921. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2922. } else {
  2923. impName.clear();
  2924. }
  2925. // The program database file name.
  2926. pdbName = this->GetPDBName(config);
  2927. }
  2928. void cmGeneratorTarget::GetExecutableNames(std::string& name,
  2929. std::string& realName,
  2930. std::string& impName,
  2931. std::string& pdbName,
  2932. const std::string& config) const
  2933. {
  2934. // This should not be called for imported targets.
  2935. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2936. // enforcement of the limited imported target API.
  2937. if (this->IsImported()) {
  2938. std::string msg = "GetExecutableNames called on imported target: ";
  2939. msg += this->GetName();
  2940. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2941. }
  2942. // This versioning is supported only for executables and then only
  2943. // when the platform supports symbolic links.
  2944. #if defined(_WIN32) && !defined(__CYGWIN__)
  2945. const char* version = 0;
  2946. #else
  2947. // Check for executable version properties.
  2948. const char* version = this->GetProperty("VERSION");
  2949. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  2950. this->Makefile->IsOn("XCODE")) {
  2951. version = nullptr;
  2952. }
  2953. #endif
  2954. // Get the components of the executable name.
  2955. std::string prefix;
  2956. std::string base;
  2957. std::string suffix;
  2958. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2959. prefix, base, suffix);
  2960. // The executable name.
  2961. name = prefix + base + suffix;
  2962. // The executable's real name on disk.
  2963. #if defined(__CYGWIN__)
  2964. realName = prefix + base;
  2965. #else
  2966. realName = name;
  2967. #endif
  2968. if (version) {
  2969. realName += "-";
  2970. realName += version;
  2971. }
  2972. #if defined(__CYGWIN__)
  2973. realName += suffix;
  2974. #endif
  2975. // The import library name.
  2976. impName =
  2977. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2978. // The program database file name.
  2979. pdbName = this->GetPDBName(config);
  2980. }
  2981. std::string cmGeneratorTarget::GetFullNameInternal(
  2982. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2983. {
  2984. std::string prefix;
  2985. std::string base;
  2986. std::string suffix;
  2987. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  2988. return prefix + base + suffix;
  2989. }
  2990. std::string cmGeneratorTarget::ImportedGetLocation(
  2991. const std::string& config) const
  2992. {
  2993. assert(this->IsImported());
  2994. return this->Target->ImportedGetFullPath(
  2995. config, cmStateEnums::RuntimeBinaryArtifact);
  2996. }
  2997. std::string cmGeneratorTarget::GetFullNameImported(
  2998. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2999. {
  3000. return cmSystemTools::GetFilenameName(
  3001. this->Target->ImportedGetFullPath(config, artifact));
  3002. }
  3003. void cmGeneratorTarget::GetFullNameInternal(
  3004. const std::string& config, cmStateEnums::ArtifactType artifact,
  3005. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  3006. {
  3007. // Use just the target name for non-main target types.
  3008. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  3009. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3010. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  3011. this->GetType() != cmStateEnums::EXECUTABLE) {
  3012. outPrefix.clear();
  3013. outBase = this->GetName();
  3014. outSuffix.clear();
  3015. return;
  3016. }
  3017. const bool isImportedLibraryArtifact =
  3018. (artifact == cmStateEnums::ImportLibraryArtifact);
  3019. // Return an empty name for the import library if this platform
  3020. // does not support import libraries.
  3021. if (isImportedLibraryArtifact &&
  3022. !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  3023. outPrefix.clear();
  3024. outBase.clear();
  3025. outSuffix.clear();
  3026. return;
  3027. }
  3028. // The implib option is only allowed for shared libraries, module
  3029. // libraries, and executables.
  3030. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3031. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  3032. this->GetType() != cmStateEnums::EXECUTABLE) {
  3033. artifact = cmStateEnums::RuntimeBinaryArtifact;
  3034. }
  3035. // Compute the full name for main target types.
  3036. const char* targetPrefix =
  3037. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
  3038. : this->GetProperty("PREFIX"));
  3039. const char* targetSuffix =
  3040. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
  3041. : this->GetProperty("SUFFIX"));
  3042. const char* configPostfix = nullptr;
  3043. if (!config.empty()) {
  3044. std::string configProp = cmSystemTools::UpperCase(config);
  3045. configProp += "_POSTFIX";
  3046. configPostfix = this->GetProperty(configProp);
  3047. // Mac application bundles and frameworks have no postfix.
  3048. if (configPostfix &&
  3049. (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
  3050. configPostfix = nullptr;
  3051. }
  3052. }
  3053. const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
  3054. const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
  3055. // Check for language-specific default prefix and suffix.
  3056. std::string ll = this->GetLinkerLanguage(config);
  3057. if (!ll.empty()) {
  3058. if (!targetSuffix && suffixVar && *suffixVar) {
  3059. std::string langSuff = suffixVar + std::string("_") + ll;
  3060. targetSuffix = this->Makefile->GetDefinition(langSuff);
  3061. }
  3062. if (!targetPrefix && prefixVar && *prefixVar) {
  3063. std::string langPrefix = prefixVar + std::string("_") + ll;
  3064. targetPrefix = this->Makefile->GetDefinition(langPrefix);
  3065. }
  3066. }
  3067. // if there is no prefix on the target use the cmake definition
  3068. if (!targetPrefix && prefixVar) {
  3069. targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
  3070. }
  3071. // if there is no suffix on the target use the cmake definition
  3072. if (!targetSuffix && suffixVar) {
  3073. targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
  3074. }
  3075. // frameworks have directory prefix but no suffix
  3076. std::string fw_prefix;
  3077. if (this->IsFrameworkOnApple()) {
  3078. fw_prefix = this->GetFrameworkDirectory(config, ContentLevel);
  3079. fw_prefix += "/";
  3080. targetPrefix = fw_prefix.c_str();
  3081. targetSuffix = nullptr;
  3082. }
  3083. if (this->IsCFBundleOnApple()) {
  3084. fw_prefix = this->GetCFBundleDirectory(config, FullLevel);
  3085. fw_prefix += "/";
  3086. targetPrefix = fw_prefix.c_str();
  3087. targetSuffix = nullptr;
  3088. }
  3089. // Begin the final name with the prefix.
  3090. outPrefix = targetPrefix ? targetPrefix : "";
  3091. // Append the target name or property-specified name.
  3092. outBase += this->GetOutputName(config, artifact);
  3093. // Append the per-configuration postfix.
  3094. outBase += configPostfix ? configPostfix : "";
  3095. // Name shared libraries with their version number on some platforms.
  3096. if (const char* soversion = this->GetProperty("SOVERSION")) {
  3097. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  3098. !isImportedLibraryArtifact &&
  3099. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  3100. outBase += "-";
  3101. outBase += soversion;
  3102. }
  3103. }
  3104. // Append the suffix.
  3105. outSuffix = targetSuffix ? targetSuffix : "";
  3106. }
  3107. std::string cmGeneratorTarget::GetLinkerLanguage(
  3108. const std::string& config) const
  3109. {
  3110. return this->GetLinkClosure(config)->LinkerLanguage;
  3111. }
  3112. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  3113. {
  3114. std::string prefix;
  3115. std::string base;
  3116. std::string suffix;
  3117. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  3118. prefix, base, suffix);
  3119. std::vector<std::string> props;
  3120. std::string configUpper = cmSystemTools::UpperCase(config);
  3121. if (!configUpper.empty()) {
  3122. // PDB_NAME_<CONFIG>
  3123. props.push_back("PDB_NAME_" + configUpper);
  3124. }
  3125. // PDB_NAME
  3126. props.push_back("PDB_NAME");
  3127. for (std::string const& p : props) {
  3128. if (const char* outName = this->GetProperty(p)) {
  3129. base = outName;
  3130. break;
  3131. }
  3132. }
  3133. return prefix + base + ".pdb";
  3134. }
  3135. std::string cmGeneratorTarget::GetObjectDirectory(
  3136. std::string const& config) const
  3137. {
  3138. std::string obj_dir =
  3139. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  3140. #if defined(__APPLE__)
  3141. // find and replace $(PROJECT_NAME) xcode placeholder
  3142. const std::string projectName = this->LocalGenerator->GetProjectName();
  3143. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  3144. #endif
  3145. return obj_dir;
  3146. }
  3147. void cmGeneratorTarget::GetTargetObjectNames(
  3148. std::string const& config, std::vector<std::string>& objects) const
  3149. {
  3150. std::vector<cmSourceFile const*> objectSources;
  3151. this->GetObjectSources(objectSources, config);
  3152. std::map<cmSourceFile const*, std::string> mapping;
  3153. for (cmSourceFile const* sf : objectSources) {
  3154. mapping[sf];
  3155. }
  3156. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  3157. for (cmSourceFile const* src : objectSources) {
  3158. // Find the object file name corresponding to this source file.
  3159. std::map<cmSourceFile const*, std::string>::const_iterator map_it =
  3160. mapping.find(src);
  3161. // It must exist because we populated the mapping just above.
  3162. assert(!map_it->second.empty());
  3163. objects.push_back(map_it->second);
  3164. }
  3165. }
  3166. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  3167. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  3168. {
  3169. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  3170. if (nameResult == 0) {
  3171. return strcmp(t1->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  3172. t2->GetLocalGenerator()->GetCurrentBinaryDirectory()) < 0;
  3173. }
  3174. return nameResult < 0;
  3175. }
  3176. struct cmGeneratorTarget::SourceFileFlags
  3177. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  3178. {
  3179. struct SourceFileFlags flags;
  3180. this->ConstructSourceFileFlags();
  3181. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  3182. this->SourceFlagsMap.find(sf);
  3183. if (si != this->SourceFlagsMap.end()) {
  3184. flags = si->second;
  3185. } else {
  3186. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  3187. // were not listed in one of the other lists.
  3188. if (const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  3189. flags.MacFolder = location;
  3190. const bool stripResources =
  3191. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  3192. if (strcmp(location, "Resources") == 0) {
  3193. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3194. if (stripResources) {
  3195. flags.MacFolder = "";
  3196. }
  3197. } else if (cmSystemTools::StringStartsWith(location, "Resources/")) {
  3198. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  3199. if (stripResources) {
  3200. flags.MacFolder += strlen("Resources/");
  3201. }
  3202. } else {
  3203. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  3204. }
  3205. }
  3206. }
  3207. return flags;
  3208. }
  3209. void cmGeneratorTarget::ConstructSourceFileFlags() const
  3210. {
  3211. if (this->SourceFileFlagsConstructed) {
  3212. return;
  3213. }
  3214. this->SourceFileFlagsConstructed = true;
  3215. // Process public headers to mark the source files.
  3216. if (const char* files = this->GetProperty("PUBLIC_HEADER")) {
  3217. std::vector<std::string> relFiles;
  3218. cmSystemTools::ExpandListArgument(files, relFiles);
  3219. for (std::string const& relFile : relFiles) {
  3220. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3221. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3222. flags.MacFolder = "Headers";
  3223. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  3224. }
  3225. }
  3226. }
  3227. // Process private headers after public headers so that they take
  3228. // precedence if a file is listed in both.
  3229. if (const char* files = this->GetProperty("PRIVATE_HEADER")) {
  3230. std::vector<std::string> relFiles;
  3231. cmSystemTools::ExpandListArgument(files, relFiles);
  3232. for (std::string const& relFile : relFiles) {
  3233. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3234. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3235. flags.MacFolder = "PrivateHeaders";
  3236. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  3237. }
  3238. }
  3239. }
  3240. // Mark sources listed as resources.
  3241. if (const char* files = this->GetProperty("RESOURCE")) {
  3242. std::vector<std::string> relFiles;
  3243. cmSystemTools::ExpandListArgument(files, relFiles);
  3244. for (std::string const& relFile : relFiles) {
  3245. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3246. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3247. flags.MacFolder = "";
  3248. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  3249. flags.MacFolder = "Resources";
  3250. }
  3251. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3252. }
  3253. }
  3254. }
  3255. }
  3256. const cmGeneratorTarget::CompatibleInterfacesBase&
  3257. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  3258. {
  3259. cmGeneratorTarget::CompatibleInterfaces& compat =
  3260. this->CompatibleInterfacesMap[config];
  3261. if (!compat.Done) {
  3262. compat.Done = true;
  3263. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  3264. compat.PropsString.insert("AUTOUIC_OPTIONS");
  3265. std::vector<cmGeneratorTarget const*> const& deps =
  3266. this->GetLinkImplementationClosure(config);
  3267. for (cmGeneratorTarget const* li : deps) {
  3268. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  3269. if (const char* prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  3270. std::vector<std::string> props; \
  3271. cmSystemTools::ExpandListArgument(prop, props); \
  3272. compat.Props##x.insert(props.begin(), props.end()); \
  3273. }
  3274. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  3275. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  3276. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  3277. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  3278. #undef CM_READ_COMPATIBLE_INTERFACE
  3279. }
  3280. }
  3281. return compat;
  3282. }
  3283. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  3284. const std::string& p, const std::string& config) const
  3285. {
  3286. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3287. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3288. return false;
  3289. }
  3290. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  3291. }
  3292. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  3293. const std::string& p, const std::string& config) const
  3294. {
  3295. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3296. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3297. return false;
  3298. }
  3299. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  3300. }
  3301. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  3302. const std::string& p, const std::string& config) const
  3303. {
  3304. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3305. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3306. return false;
  3307. }
  3308. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  3309. }
  3310. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  3311. const std::string& p, const std::string& config) const
  3312. {
  3313. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3314. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3315. return false;
  3316. }
  3317. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  3318. }
  3319. enum CompatibleType
  3320. {
  3321. BoolType,
  3322. StringType,
  3323. NumberMinType,
  3324. NumberMaxType
  3325. };
  3326. template <typename PropertyType>
  3327. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3328. const std::string& prop,
  3329. const std::string& config,
  3330. CompatibleType, PropertyType*);
  3331. template <>
  3332. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3333. const std::string& prop,
  3334. const std::string& config,
  3335. CompatibleType /*unused*/,
  3336. bool* /*unused*/)
  3337. {
  3338. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  3339. }
  3340. template <>
  3341. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3342. const std::string& prop,
  3343. const std::string& config,
  3344. CompatibleType t,
  3345. const char** /*unused*/)
  3346. {
  3347. switch (t) {
  3348. case BoolType:
  3349. assert(false &&
  3350. "String compatibility check function called for boolean");
  3351. return nullptr;
  3352. case StringType:
  3353. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  3354. case NumberMinType:
  3355. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  3356. case NumberMaxType:
  3357. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  3358. }
  3359. assert(false && "Unreachable!");
  3360. return nullptr;
  3361. }
  3362. template <typename PropertyType>
  3363. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  3364. cmGeneratorTarget const* dependee,
  3365. const std::string& propName,
  3366. std::set<std::string>& emitted,
  3367. const std::string& config, CompatibleType t,
  3368. PropertyType* /*unused*/)
  3369. {
  3370. const char* prop = dependee->GetProperty(propName);
  3371. if (!prop) {
  3372. return;
  3373. }
  3374. std::vector<std::string> props;
  3375. cmSystemTools::ExpandListArgument(prop, props);
  3376. std::string pdir = cmSystemTools::GetCMakeRoot();
  3377. pdir += "/Help/prop_tgt/";
  3378. for (std::string const& p : props) {
  3379. std::string pname = cmSystemTools::HelpFileName(p);
  3380. std::string pfile = pdir + pname + ".rst";
  3381. if (cmSystemTools::FileExists(pfile, true)) {
  3382. std::ostringstream e;
  3383. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  3384. << "\" listed in its " << propName
  3385. << " property. "
  3386. "This is not allowed. Only user-defined properties may appear "
  3387. "listed in the "
  3388. << propName << " property.";
  3389. depender->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, e.str());
  3390. return;
  3391. }
  3392. if (emitted.insert(p).second) {
  3393. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  3394. nullptr);
  3395. if (cmSystemTools::GetErrorOccuredFlag()) {
  3396. return;
  3397. }
  3398. }
  3399. }
  3400. }
  3401. static std::string intersect(const std::set<std::string>& s1,
  3402. const std::set<std::string>& s2)
  3403. {
  3404. std::set<std::string> intersect;
  3405. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  3406. std::inserter(intersect, intersect.begin()));
  3407. if (!intersect.empty()) {
  3408. return *intersect.begin();
  3409. }
  3410. return "";
  3411. }
  3412. static std::string intersect(const std::set<std::string>& s1,
  3413. const std::set<std::string>& s2,
  3414. const std::set<std::string>& s3)
  3415. {
  3416. std::string result;
  3417. result = intersect(s1, s2);
  3418. if (!result.empty()) {
  3419. return result;
  3420. }
  3421. result = intersect(s1, s3);
  3422. if (!result.empty()) {
  3423. return result;
  3424. }
  3425. return intersect(s2, s3);
  3426. }
  3427. static std::string intersect(const std::set<std::string>& s1,
  3428. const std::set<std::string>& s2,
  3429. const std::set<std::string>& s3,
  3430. const std::set<std::string>& s4)
  3431. {
  3432. std::string result;
  3433. result = intersect(s1, s2);
  3434. if (!result.empty()) {
  3435. return result;
  3436. }
  3437. result = intersect(s1, s3);
  3438. if (!result.empty()) {
  3439. return result;
  3440. }
  3441. result = intersect(s1, s4);
  3442. if (!result.empty()) {
  3443. return result;
  3444. }
  3445. return intersect(s2, s3, s4);
  3446. }
  3447. void cmGeneratorTarget::CheckPropertyCompatibility(
  3448. cmComputeLinkInformation* info, const std::string& config) const
  3449. {
  3450. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  3451. std::set<std::string> emittedBools;
  3452. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  3453. std::set<std::string> emittedStrings;
  3454. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  3455. std::set<std::string> emittedMinNumbers;
  3456. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  3457. std::set<std::string> emittedMaxNumbers;
  3458. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  3459. for (auto const& dep : deps) {
  3460. if (!dep.Target) {
  3461. continue;
  3462. }
  3463. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  3464. config, BoolType, nullptr);
  3465. if (cmSystemTools::GetErrorOccuredFlag()) {
  3466. return;
  3467. }
  3468. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  3469. emittedStrings, config, StringType,
  3470. nullptr);
  3471. if (cmSystemTools::GetErrorOccuredFlag()) {
  3472. return;
  3473. }
  3474. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  3475. emittedMinNumbers, config,
  3476. NumberMinType, nullptr);
  3477. if (cmSystemTools::GetErrorOccuredFlag()) {
  3478. return;
  3479. }
  3480. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  3481. emittedMaxNumbers, config,
  3482. NumberMaxType, nullptr);
  3483. if (cmSystemTools::GetErrorOccuredFlag()) {
  3484. return;
  3485. }
  3486. }
  3487. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  3488. emittedMaxNumbers);
  3489. if (!prop.empty()) {
  3490. // Use a sorted std::vector to keep the error message sorted.
  3491. std::vector<std::string> props;
  3492. std::set<std::string>::const_iterator i = emittedBools.find(prop);
  3493. if (i != emittedBools.end()) {
  3494. props.push_back(strBool);
  3495. }
  3496. i = emittedStrings.find(prop);
  3497. if (i != emittedStrings.end()) {
  3498. props.push_back(strString);
  3499. }
  3500. i = emittedMinNumbers.find(prop);
  3501. if (i != emittedMinNumbers.end()) {
  3502. props.push_back(strNumMin);
  3503. }
  3504. i = emittedMaxNumbers.find(prop);
  3505. if (i != emittedMaxNumbers.end()) {
  3506. props.push_back(strNumMax);
  3507. }
  3508. std::sort(props.begin(), props.end());
  3509. std::string propsString = cmJoin(cmMakeRange(props).retreat(1), ", ");
  3510. propsString += " and the " + props.back();
  3511. std::ostringstream e;
  3512. e << "Property \"" << prop << "\" appears in both the " << propsString
  3513. << " property in the dependencies of target \"" << this->GetName()
  3514. << "\". This is not allowed. A property may only require compatibility "
  3515. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  3516. "or a "
  3517. "string interpretation, but not a mixture.";
  3518. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  3519. }
  3520. }
  3521. std::string compatibilityType(CompatibleType t)
  3522. {
  3523. switch (t) {
  3524. case BoolType:
  3525. return "Boolean compatibility";
  3526. case StringType:
  3527. return "String compatibility";
  3528. case NumberMaxType:
  3529. return "Numeric maximum compatibility";
  3530. case NumberMinType:
  3531. return "Numeric minimum compatibility";
  3532. }
  3533. assert(false && "Unreachable!");
  3534. return "";
  3535. }
  3536. std::string compatibilityAgree(CompatibleType t, bool dominant)
  3537. {
  3538. switch (t) {
  3539. case BoolType:
  3540. case StringType:
  3541. return dominant ? "(Disagree)\n" : "(Agree)\n";
  3542. case NumberMaxType:
  3543. case NumberMinType:
  3544. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  3545. }
  3546. assert(false && "Unreachable!");
  3547. return "";
  3548. }
  3549. template <typename PropertyType>
  3550. PropertyType getTypedProperty(cmGeneratorTarget const* tgt,
  3551. const std::string& prop);
  3552. template <>
  3553. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  3554. const std::string& prop)
  3555. {
  3556. return tgt->GetPropertyAsBool(prop);
  3557. }
  3558. template <>
  3559. const char* getTypedProperty<const char*>(cmGeneratorTarget const* tgt,
  3560. const std::string& prop)
  3561. {
  3562. return tgt->GetProperty(prop);
  3563. }
  3564. template <typename PropertyType>
  3565. std::string valueAsString(PropertyType);
  3566. template <>
  3567. std::string valueAsString<bool>(bool value)
  3568. {
  3569. return value ? "TRUE" : "FALSE";
  3570. }
  3571. template <>
  3572. std::string valueAsString<const char*>(const char* value)
  3573. {
  3574. return value ? value : "(unset)";
  3575. }
  3576. template <typename PropertyType>
  3577. PropertyType impliedValue(PropertyType);
  3578. template <>
  3579. bool impliedValue<bool>(bool /*unused*/)
  3580. {
  3581. return false;
  3582. }
  3583. template <>
  3584. const char* impliedValue<const char*>(const char* /*unused*/)
  3585. {
  3586. return "";
  3587. }
  3588. template <typename PropertyType>
  3589. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  3590. PropertyType rhs,
  3591. CompatibleType t);
  3592. template <>
  3593. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  3594. CompatibleType /*unused*/)
  3595. {
  3596. return std::make_pair(lhs == rhs, lhs);
  3597. }
  3598. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  3599. const char* rhs)
  3600. {
  3601. const bool b = strcmp(lhs, rhs) == 0;
  3602. return std::make_pair(b, b ? lhs : nullptr);
  3603. }
  3604. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  3605. const char* rhs,
  3606. CompatibleType t)
  3607. {
  3608. char* pEnd;
  3609. const char* const null_ptr = nullptr;
  3610. long lnum = strtol(lhs, &pEnd, 0);
  3611. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  3612. return std::pair<bool, const char*>(false, null_ptr);
  3613. }
  3614. long rnum = strtol(rhs, &pEnd, 0);
  3615. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  3616. return std::pair<bool, const char*>(false, null_ptr);
  3617. }
  3618. if (t == NumberMaxType) {
  3619. return std::make_pair(true, std::max(lnum, rnum) == lnum ? lhs : rhs);
  3620. }
  3621. return std::make_pair(true, std::min(lnum, rnum) == lnum ? lhs : rhs);
  3622. }
  3623. template <>
  3624. std::pair<bool, const char*> consistentProperty(const char* lhs,
  3625. const char* rhs,
  3626. CompatibleType t)
  3627. {
  3628. if (!lhs && !rhs) {
  3629. return std::make_pair(true, lhs);
  3630. }
  3631. if (!lhs) {
  3632. return std::make_pair(true, rhs);
  3633. }
  3634. if (!rhs) {
  3635. return std::make_pair(true, lhs);
  3636. }
  3637. const char* const null_ptr = nullptr;
  3638. switch (t) {
  3639. case BoolType:
  3640. assert(false && "consistentProperty for strings called with BoolType");
  3641. return std::pair<bool, const char*>(false, null_ptr);
  3642. case StringType:
  3643. return consistentStringProperty(lhs, rhs);
  3644. case NumberMinType:
  3645. case NumberMaxType:
  3646. return consistentNumberProperty(lhs, rhs, t);
  3647. }
  3648. assert(false && "Unreachable!");
  3649. return std::pair<bool, const char*>(false, null_ptr);
  3650. }
  3651. template <typename PropertyType>
  3652. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  3653. const std::string& p,
  3654. const std::string& config,
  3655. const char* defaultValue,
  3656. CompatibleType t,
  3657. PropertyType* /*unused*/)
  3658. {
  3659. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  3660. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  3661. const bool explicitlySet =
  3662. std::find(headPropKeys.begin(), headPropKeys.end(), p) !=
  3663. headPropKeys.end();
  3664. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  3665. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  3666. std::vector<cmGeneratorTarget const*> const& deps =
  3667. tgt->GetLinkImplementationClosure(config);
  3668. if (deps.empty()) {
  3669. return propContent;
  3670. }
  3671. bool propInitialized = explicitlySet;
  3672. std::string report = " * Target \"";
  3673. report += tgt->GetName();
  3674. if (explicitlySet) {
  3675. report += "\" has property content \"";
  3676. report += valueAsString<PropertyType>(propContent);
  3677. report += "\"\n";
  3678. } else if (impliedByUse) {
  3679. report += "\" property is implied by use.\n";
  3680. } else {
  3681. report += "\" property not set.\n";
  3682. }
  3683. std::string interfaceProperty = "INTERFACE_" + p;
  3684. for (cmGeneratorTarget const* theTarget : deps) {
  3685. // An error should be reported if one dependency
  3686. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  3687. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  3688. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  3689. // with a dependency.
  3690. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  3691. const bool ifaceIsSet = std::find(propKeys.begin(), propKeys.end(),
  3692. interfaceProperty) != propKeys.end();
  3693. PropertyType ifacePropContent =
  3694. getTypedProperty<PropertyType>(theTarget, interfaceProperty);
  3695. std::string reportEntry;
  3696. if (ifaceIsSet) {
  3697. reportEntry += " * Target \"";
  3698. reportEntry += theTarget->GetName();
  3699. reportEntry += "\" property value \"";
  3700. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  3701. reportEntry += "\" ";
  3702. }
  3703. if (explicitlySet) {
  3704. if (ifaceIsSet) {
  3705. std::pair<bool, PropertyType> consistent =
  3706. consistentProperty(propContent, ifacePropContent, t);
  3707. report += reportEntry;
  3708. report += compatibilityAgree(t, propContent != consistent.second);
  3709. if (!consistent.first) {
  3710. std::ostringstream e;
  3711. e << "Property " << p << " on target \"" << tgt->GetName()
  3712. << "\" does\nnot match the "
  3713. "INTERFACE_"
  3714. << p
  3715. << " property requirement\nof "
  3716. "dependency \""
  3717. << theTarget->GetName() << "\".\n";
  3718. cmSystemTools::Error(e.str().c_str());
  3719. break;
  3720. }
  3721. propContent = consistent.second;
  3722. continue;
  3723. }
  3724. // Explicitly set on target and not set in iface. Can't disagree.
  3725. continue;
  3726. }
  3727. if (impliedByUse) {
  3728. propContent = impliedValue<PropertyType>(propContent);
  3729. if (ifaceIsSet) {
  3730. std::pair<bool, PropertyType> consistent =
  3731. consistentProperty(propContent, ifacePropContent, t);
  3732. report += reportEntry;
  3733. report += compatibilityAgree(t, propContent != consistent.second);
  3734. if (!consistent.first) {
  3735. std::ostringstream e;
  3736. e << "Property " << p << " on target \"" << tgt->GetName()
  3737. << "\" is\nimplied to be " << defaultValue
  3738. << " because it was used to determine the link libraries\n"
  3739. "already. The INTERFACE_"
  3740. << p << " property on\ndependency \"" << theTarget->GetName()
  3741. << "\" is in conflict.\n";
  3742. cmSystemTools::Error(e.str().c_str());
  3743. break;
  3744. }
  3745. propContent = consistent.second;
  3746. continue;
  3747. }
  3748. // Implicitly set on target and not set in iface. Can't disagree.
  3749. continue;
  3750. }
  3751. if (ifaceIsSet) {
  3752. if (propInitialized) {
  3753. std::pair<bool, PropertyType> consistent =
  3754. consistentProperty(propContent, ifacePropContent, t);
  3755. report += reportEntry;
  3756. report += compatibilityAgree(t, propContent != consistent.second);
  3757. if (!consistent.first) {
  3758. std::ostringstream e;
  3759. e << "The INTERFACE_" << p << " property of \""
  3760. << theTarget->GetName() << "\" does\nnot agree with the value of "
  3761. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  3762. cmSystemTools::Error(e.str().c_str());
  3763. break;
  3764. }
  3765. propContent = consistent.second;
  3766. continue;
  3767. }
  3768. report += reportEntry + "(Interface set)\n";
  3769. propContent = ifacePropContent;
  3770. propInitialized = true;
  3771. } else {
  3772. // Not set. Nothing to agree on.
  3773. continue;
  3774. }
  3775. }
  3776. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  3777. report, compatibilityType(t));
  3778. return propContent;
  3779. }
  3780. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  3781. const std::string& p, const std::string& config) const
  3782. {
  3783. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  3784. BoolType, nullptr);
  3785. }
  3786. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  3787. const std::string& p, const std::string& config) const
  3788. {
  3789. return checkInterfacePropertyCompatibility<const char*>(
  3790. this, p, config, "empty", StringType, nullptr);
  3791. }
  3792. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  3793. const std::string& p, const std::string& config) const
  3794. {
  3795. return checkInterfacePropertyCompatibility<const char*>(
  3796. this, p, config, "empty", NumberMinType, nullptr);
  3797. }
  3798. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  3799. const std::string& p, const std::string& config) const
  3800. {
  3801. return checkInterfacePropertyCompatibility<const char*>(
  3802. this, p, config, "empty", NumberMaxType, nullptr);
  3803. }
  3804. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  3805. const std::string& config) const
  3806. {
  3807. // Lookup any existing information for this configuration.
  3808. std::string key(cmSystemTools::UpperCase(config));
  3809. cmTargetLinkInformationMap::iterator i = this->LinkInformation.find(key);
  3810. if (i == this->LinkInformation.end()) {
  3811. // Compute information for this configuration.
  3812. cmComputeLinkInformation* info =
  3813. new cmComputeLinkInformation(this, config);
  3814. if (!info || !info->Compute()) {
  3815. delete info;
  3816. info = nullptr;
  3817. }
  3818. // Store the information for this configuration.
  3819. cmTargetLinkInformationMap::value_type entry(key, info);
  3820. i = this->LinkInformation.insert(entry).first;
  3821. if (info) {
  3822. this->CheckPropertyCompatibility(info, config);
  3823. }
  3824. }
  3825. return i->second;
  3826. }
  3827. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  3828. {
  3829. int patch;
  3830. this->GetTargetVersion(false, major, minor, patch);
  3831. }
  3832. void cmGeneratorTarget::GetTargetVersion(bool soversion, int& major,
  3833. int& minor, int& patch) const
  3834. {
  3835. // Set the default values.
  3836. major = 0;
  3837. minor = 0;
  3838. patch = 0;
  3839. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3840. // Look for a VERSION or SOVERSION property.
  3841. const char* prop = soversion ? "SOVERSION" : "VERSION";
  3842. if (const char* version = this->GetProperty(prop)) {
  3843. // Try to parse the version number and store the results that were
  3844. // successfully parsed.
  3845. int parsed_major;
  3846. int parsed_minor;
  3847. int parsed_patch;
  3848. switch (sscanf(version, "%d.%d.%d", &parsed_major, &parsed_minor,
  3849. &parsed_patch)) {
  3850. case 3:
  3851. patch = parsed_patch;
  3852. CM_FALLTHROUGH;
  3853. case 2:
  3854. minor = parsed_minor;
  3855. CM_FALLTHROUGH;
  3856. case 1:
  3857. major = parsed_major;
  3858. CM_FALLTHROUGH;
  3859. default:
  3860. break;
  3861. }
  3862. }
  3863. }
  3864. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  3865. std::string const& working_dir) const
  3866. {
  3867. if (!this->FortranModuleDirectoryCreated) {
  3868. this->FortranModuleDirectory =
  3869. this->CreateFortranModuleDirectory(working_dir);
  3870. this->FortranModuleDirectoryCreated = true;
  3871. }
  3872. return this->FortranModuleDirectory;
  3873. }
  3874. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  3875. std::string const& working_dir) const
  3876. {
  3877. std::string mod_dir;
  3878. std::string target_mod_dir;
  3879. if (const char* prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  3880. target_mod_dir = prop;
  3881. } else {
  3882. std::string const& default_mod_dir =
  3883. this->LocalGenerator->GetCurrentBinaryDirectory();
  3884. if (default_mod_dir != working_dir) {
  3885. target_mod_dir = default_mod_dir;
  3886. }
  3887. }
  3888. const char* moddir_flag =
  3889. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  3890. if (!target_mod_dir.empty() && moddir_flag) {
  3891. // Compute the full path to the module directory.
  3892. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  3893. // Already a full path.
  3894. mod_dir = target_mod_dir;
  3895. } else {
  3896. // Interpret relative to the current output directory.
  3897. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  3898. mod_dir += "/";
  3899. mod_dir += target_mod_dir;
  3900. }
  3901. // Make sure the module output directory exists.
  3902. cmSystemTools::MakeDirectory(mod_dir);
  3903. }
  3904. return mod_dir;
  3905. }
  3906. std::string cmGeneratorTarget::GetFrameworkVersion() const
  3907. {
  3908. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3909. if (const char* fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  3910. return fversion;
  3911. }
  3912. if (const char* tversion = this->GetProperty("VERSION")) {
  3913. return tversion;
  3914. }
  3915. return "A";
  3916. }
  3917. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  3918. std::string const& prefix,
  3919. std::string const& base,
  3920. std::string const& suffix,
  3921. std::string const& name,
  3922. const char* version) const
  3923. {
  3924. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  3925. if (version) {
  3926. vName += ".";
  3927. vName += version;
  3928. }
  3929. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  3930. }
  3931. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  3932. {
  3933. cmPropertyMap const& propsObject = this->Target->GetProperties();
  3934. std::vector<std::string> props;
  3935. props.reserve(propsObject.size());
  3936. for (auto const& it : propsObject) {
  3937. props.push_back(it.first);
  3938. }
  3939. return props;
  3940. }
  3941. void cmGeneratorTarget::ReportPropertyOrigin(
  3942. const std::string& p, const std::string& result, const std::string& report,
  3943. const std::string& compatibilityType) const
  3944. {
  3945. std::vector<std::string> debugProperties;
  3946. const char* debugProp = this->Target->GetMakefile()->GetDefinition(
  3947. "CMAKE_DEBUG_TARGET_PROPERTIES");
  3948. if (debugProp) {
  3949. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  3950. }
  3951. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  3952. std::find(debugProperties.begin(), debugProperties.end(), p) !=
  3953. debugProperties.end();
  3954. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3955. this->DebugCompatiblePropertiesDone[p] = true;
  3956. }
  3957. if (!debugOrigin) {
  3958. return;
  3959. }
  3960. std::string areport = compatibilityType;
  3961. areport += std::string(" of property \"") + p + "\" for target \"";
  3962. areport += std::string(this->GetName());
  3963. areport += "\" (result: \"";
  3964. areport += result;
  3965. areport += "\"):\n" + report;
  3966. this->LocalGenerator->GetCMakeInstance()->IssueMessage(cmake::LOG, areport);
  3967. }
  3968. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  3969. std::vector<cmLinkItem>& items) const
  3970. {
  3971. for (std::string const& n : names) {
  3972. std::string name = this->CheckCMP0004(n);
  3973. if (name == this->GetName() || name.empty()) {
  3974. continue;
  3975. }
  3976. items.emplace_back(name, this->FindTargetToLink(name));
  3977. }
  3978. }
  3979. void cmGeneratorTarget::ExpandLinkItems(
  3980. std::string const& prop, std::string const& value, std::string const& config,
  3981. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  3982. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition) const
  3983. {
  3984. cmGeneratorExpression ge;
  3985. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), prop, nullptr,
  3986. nullptr);
  3987. // The $<LINK_ONLY> expression may be in a link interface to specify private
  3988. // link dependencies that are otherwise excluded from usage requirements.
  3989. if (usage_requirements_only) {
  3990. dagChecker.SetTransitivePropertiesOnly();
  3991. }
  3992. std::vector<std::string> libs;
  3993. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  3994. cmSystemTools::ExpandListArgument(cge->Evaluate(this->LocalGenerator, config,
  3995. false, headTarget, this,
  3996. &dagChecker),
  3997. libs);
  3998. this->LookupLinkItems(libs, items);
  3999. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  4000. }
  4001. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  4002. const std::string& config, cmGeneratorTarget const* head) const
  4003. {
  4004. // Imported targets have their own link interface.
  4005. if (this->IsImported()) {
  4006. return this->GetImportLinkInterface(config, head, false);
  4007. }
  4008. // Link interfaces are not supported for executables that do not
  4009. // export symbols.
  4010. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  4011. !this->IsExecutableWithExports()) {
  4012. return nullptr;
  4013. }
  4014. // Lookup any existing link interface for this configuration.
  4015. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  4016. // If the link interface does not depend on the head target
  4017. // then return the one we computed first.
  4018. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4019. return &hm.begin()->second;
  4020. }
  4021. cmOptionalLinkInterface& iface = hm[head];
  4022. if (!iface.LibrariesDone) {
  4023. iface.LibrariesDone = true;
  4024. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  4025. }
  4026. if (!iface.AllDone) {
  4027. iface.AllDone = true;
  4028. if (iface.Exists) {
  4029. this->ComputeLinkInterface(config, iface, head);
  4030. }
  4031. }
  4032. return iface.Exists ? &iface : nullptr;
  4033. }
  4034. void cmGeneratorTarget::ComputeLinkInterface(
  4035. const std::string& config, cmOptionalLinkInterface& iface,
  4036. cmGeneratorTarget const* headTarget) const
  4037. {
  4038. if (iface.ExplicitLibraries) {
  4039. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4040. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4041. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4042. // Shared libraries may have runtime implementation dependencies
  4043. // on other shared libraries that are not in the interface.
  4044. std::unordered_set<std::string> emitted;
  4045. for (cmLinkItem const& lib : iface.Libraries) {
  4046. emitted.insert(lib);
  4047. }
  4048. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4049. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  4050. for (cmLinkImplItem const& lib : impl->Libraries) {
  4051. if (emitted.insert(lib).second) {
  4052. if (lib.Target) {
  4053. // This is a runtime dependency on another shared library.
  4054. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4055. iface.SharedDeps.push_back(lib);
  4056. }
  4057. } else {
  4058. // TODO: Recognize shared library file names. Perhaps this
  4059. // should be moved to cmComputeLinkInformation, but that creates
  4060. // a chicken-and-egg problem since this list is needed for its
  4061. // construction.
  4062. }
  4063. }
  4064. }
  4065. }
  4066. }
  4067. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4068. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  4069. // The link implementation is the default link interface.
  4070. cmLinkImplementationLibraries const* impl =
  4071. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4072. iface.ImplementationIsInterface = true;
  4073. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  4074. }
  4075. if (this->LinkLanguagePropagatesToDependents()) {
  4076. // Targets using this archive need its language runtime libraries.
  4077. if (cmLinkImplementation const* impl =
  4078. this->GetLinkImplementation(config)) {
  4079. iface.Languages = impl->Languages;
  4080. }
  4081. }
  4082. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4083. // Construct the property name suffix for this configuration.
  4084. std::string suffix = "_";
  4085. if (!config.empty()) {
  4086. suffix += cmSystemTools::UpperCase(config);
  4087. } else {
  4088. suffix += "NOCONFIG";
  4089. }
  4090. // How many repetitions are needed if this library has cyclic
  4091. // dependencies?
  4092. std::string propName = "LINK_INTERFACE_MULTIPLICITY";
  4093. propName += suffix;
  4094. if (const char* config_reps = this->GetProperty(propName)) {
  4095. sscanf(config_reps, "%u", &iface.Multiplicity);
  4096. } else if (const char* reps =
  4097. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  4098. sscanf(reps, "%u", &iface.Multiplicity);
  4099. }
  4100. }
  4101. }
  4102. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  4103. const std::string& config, cmGeneratorTarget const* head,
  4104. bool usage_requirements_only) const
  4105. {
  4106. // Imported targets have their own link interface.
  4107. if (this->IsImported()) {
  4108. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  4109. }
  4110. // Link interfaces are not supported for executables that do not
  4111. // export symbols.
  4112. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  4113. !this->IsExecutableWithExports()) {
  4114. return nullptr;
  4115. }
  4116. // Lookup any existing link interface for this configuration.
  4117. std::string CONFIG = cmSystemTools::UpperCase(config);
  4118. cmHeadToLinkInterfaceMap& hm =
  4119. (usage_requirements_only
  4120. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4121. : this->GetHeadToLinkInterfaceMap(config));
  4122. // If the link interface does not depend on the head target
  4123. // then return the one we computed first.
  4124. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4125. return &hm.begin()->second;
  4126. }
  4127. cmOptionalLinkInterface& iface = hm[head];
  4128. if (!iface.LibrariesDone) {
  4129. iface.LibrariesDone = true;
  4130. this->ComputeLinkInterfaceLibraries(config, iface, head,
  4131. usage_requirements_only);
  4132. }
  4133. return iface.Exists ? &iface : nullptr;
  4134. }
  4135. std::string cmGeneratorTarget::GetDirectory(
  4136. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4137. {
  4138. if (this->IsImported()) {
  4139. // Return the directory from which the target is imported.
  4140. return cmSystemTools::GetFilenamePath(
  4141. this->Target->ImportedGetFullPath(config, artifact));
  4142. }
  4143. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  4144. // Return the directory in which the target will be built.
  4145. switch (artifact) {
  4146. case cmStateEnums::RuntimeBinaryArtifact:
  4147. return info->OutDir;
  4148. case cmStateEnums::ImportLibraryArtifact:
  4149. return info->ImpDir;
  4150. }
  4151. }
  4152. return "";
  4153. }
  4154. bool cmGeneratorTarget::UsesDefaultOutputDir(
  4155. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4156. {
  4157. std::string dir;
  4158. return this->ComputeOutputDir(config, artifact, dir);
  4159. }
  4160. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  4161. const std::string& config) const
  4162. {
  4163. // There is no output information for imported targets.
  4164. if (this->IsImported()) {
  4165. return nullptr;
  4166. }
  4167. // Only libraries and executables have well-defined output files.
  4168. if (!this->HaveWellDefinedOutputFiles()) {
  4169. std::string msg = "cmGeneratorTarget::GetOutputInfo called for ";
  4170. msg += this->GetName();
  4171. msg += " which has type ";
  4172. msg += cmState::GetTargetTypeName(this->GetType());
  4173. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  4174. return nullptr;
  4175. }
  4176. // Lookup/compute/cache the output information for this configuration.
  4177. std::string config_upper;
  4178. if (!config.empty()) {
  4179. config_upper = cmSystemTools::UpperCase(config);
  4180. }
  4181. OutputInfoMapType::iterator i = this->OutputInfoMap.find(config_upper);
  4182. if (i == this->OutputInfoMap.end()) {
  4183. // Add empty info in map to detect potential recursion.
  4184. OutputInfo info;
  4185. OutputInfoMapType::value_type entry(config_upper, info);
  4186. i = this->OutputInfoMap.insert(entry).first;
  4187. // Compute output directories.
  4188. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  4189. info.OutDir);
  4190. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  4191. info.ImpDir);
  4192. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  4193. info.PdbDir = info.OutDir;
  4194. }
  4195. // Now update the previously-prepared map entry.
  4196. i->second = info;
  4197. } else if (i->second.empty()) {
  4198. // An empty map entry indicates we have been called recursively
  4199. // from the above block.
  4200. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4201. cmake::FATAL_ERROR,
  4202. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  4203. this->GetBacktrace());
  4204. return nullptr;
  4205. }
  4206. return &i->second;
  4207. }
  4208. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  4209. cmStateEnums::ArtifactType artifact,
  4210. std::string& out) const
  4211. {
  4212. bool usesDefaultOutputDir = false;
  4213. std::string conf = config;
  4214. // Look for a target property defining the target output directory
  4215. // based on the target type.
  4216. std::string targetTypeName = this->GetOutputTargetType(artifact);
  4217. const char* propertyName = nullptr;
  4218. std::string propertyNameStr = targetTypeName;
  4219. if (!propertyNameStr.empty()) {
  4220. propertyNameStr += "_OUTPUT_DIRECTORY";
  4221. propertyName = propertyNameStr.c_str();
  4222. }
  4223. // Check for a per-configuration output directory target property.
  4224. std::string configUpper = cmSystemTools::UpperCase(conf);
  4225. const char* configProp = nullptr;
  4226. std::string configPropStr = targetTypeName;
  4227. if (!configPropStr.empty()) {
  4228. configPropStr += "_OUTPUT_DIRECTORY_";
  4229. configPropStr += configUpper;
  4230. configProp = configPropStr.c_str();
  4231. }
  4232. // Select an output directory.
  4233. if (const char* config_outdir = this->GetProperty(configProp)) {
  4234. // Use the user-specified per-configuration output directory.
  4235. cmGeneratorExpression ge;
  4236. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4237. ge.Parse(config_outdir);
  4238. out = cge->Evaluate(this->LocalGenerator, config);
  4239. // Skip per-configuration subdirectory.
  4240. conf.clear();
  4241. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4242. // Use the user-specified output directory.
  4243. cmGeneratorExpression ge;
  4244. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4245. out = cge->Evaluate(this->LocalGenerator, config);
  4246. // Skip per-configuration subdirectory if the value contained a
  4247. // generator expression.
  4248. if (out != outdir) {
  4249. conf.clear();
  4250. }
  4251. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4252. // Lookup the output path for executables.
  4253. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  4254. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4255. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4256. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4257. // Lookup the output path for libraries.
  4258. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  4259. }
  4260. if (out.empty()) {
  4261. // Default to the current output directory.
  4262. usesDefaultOutputDir = true;
  4263. out = ".";
  4264. }
  4265. // Convert the output path to a full path in case it is
  4266. // specified as a relative path. Treat a relative path as
  4267. // relative to the current output directory for this makefile.
  4268. out = (cmSystemTools::CollapseFullPath(
  4269. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4270. // The generator may add the configuration's subdirectory.
  4271. if (!conf.empty()) {
  4272. bool useEPN =
  4273. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  4274. std::string suffix =
  4275. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  4276. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4277. "/", conf, suffix, out);
  4278. }
  4279. return usesDefaultOutputDir;
  4280. }
  4281. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  4282. const std::string& config,
  4283. std::string& out) const
  4284. {
  4285. // Look for a target property defining the target output directory
  4286. // based on the target type.
  4287. const char* propertyName = nullptr;
  4288. std::string propertyNameStr = kind;
  4289. if (!propertyNameStr.empty()) {
  4290. propertyNameStr += "_OUTPUT_DIRECTORY";
  4291. propertyName = propertyNameStr.c_str();
  4292. }
  4293. std::string conf = config;
  4294. // Check for a per-configuration output directory target property.
  4295. std::string configUpper = cmSystemTools::UpperCase(conf);
  4296. const char* configProp = nullptr;
  4297. std::string configPropStr = kind;
  4298. if (!configPropStr.empty()) {
  4299. configPropStr += "_OUTPUT_DIRECTORY_";
  4300. configPropStr += configUpper;
  4301. configProp = configPropStr.c_str();
  4302. }
  4303. // Select an output directory.
  4304. if (const char* config_outdir = this->GetProperty(configProp)) {
  4305. // Use the user-specified per-configuration output directory.
  4306. cmGeneratorExpression ge;
  4307. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4308. ge.Parse(config_outdir);
  4309. out = cge->Evaluate(this->LocalGenerator, config);
  4310. // Skip per-configuration subdirectory.
  4311. conf.clear();
  4312. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4313. // Use the user-specified output directory.
  4314. cmGeneratorExpression ge;
  4315. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4316. out = cge->Evaluate(this->LocalGenerator, config);
  4317. // Skip per-configuration subdirectory if the value contained a
  4318. // generator expression.
  4319. if (out != outdir) {
  4320. conf.clear();
  4321. }
  4322. }
  4323. if (out.empty()) {
  4324. return false;
  4325. }
  4326. // Convert the output path to a full path in case it is
  4327. // specified as a relative path. Treat a relative path as
  4328. // relative to the current output directory for this makefile.
  4329. out = (cmSystemTools::CollapseFullPath(
  4330. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4331. // The generator may add the configuration's subdirectory.
  4332. if (!conf.empty()) {
  4333. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4334. "/", conf, "", out);
  4335. }
  4336. return true;
  4337. }
  4338. bool cmGeneratorTarget::HaveInstallTreeRPATH() const
  4339. {
  4340. const char* install_rpath = this->GetProperty("INSTALL_RPATH");
  4341. return (install_rpath && *install_rpath) &&
  4342. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  4343. }
  4344. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  4345. const std::string& config, cmOptionalLinkInterface& iface,
  4346. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  4347. {
  4348. // Construct the property name suffix for this configuration.
  4349. std::string suffix = "_";
  4350. if (!config.empty()) {
  4351. suffix += cmSystemTools::UpperCase(config);
  4352. } else {
  4353. suffix += "NOCONFIG";
  4354. }
  4355. // An explicit list of interface libraries may be set for shared
  4356. // libraries and executables that export symbols.
  4357. const char* explicitLibraries = nullptr;
  4358. std::string linkIfaceProp;
  4359. if (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  4360. this->GetPolicyStatusCMP0022() != cmPolicies::WARN) {
  4361. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  4362. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  4363. explicitLibraries = this->GetProperty(linkIfaceProp);
  4364. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4365. this->IsExecutableWithExports()) {
  4366. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  4367. // shared lib or executable.
  4368. // Lookup the per-configuration property.
  4369. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4370. linkIfaceProp += suffix;
  4371. explicitLibraries = this->GetProperty(linkIfaceProp);
  4372. // If not set, try the generic property.
  4373. if (!explicitLibraries) {
  4374. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4375. explicitLibraries = this->GetProperty(linkIfaceProp);
  4376. }
  4377. }
  4378. if (explicitLibraries &&
  4379. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4380. !this->PolicyWarnedCMP0022) {
  4381. // Compare the explicitly set old link interface properties to the
  4382. // preferred new link interface property one and warn if different.
  4383. const char* newExplicitLibraries =
  4384. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  4385. if (newExplicitLibraries &&
  4386. strcmp(newExplicitLibraries, explicitLibraries) != 0) {
  4387. std::ostringstream w;
  4388. /* clang-format off */
  4389. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4390. "Target \"" << this->GetName() << "\" has an "
  4391. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  4392. linkIfaceProp << " properties."
  4393. "\n"
  4394. "INTERFACE_LINK_LIBRARIES:\n"
  4395. " " << newExplicitLibraries << "\n" <<
  4396. linkIfaceProp << ":\n"
  4397. " " << explicitLibraries << "\n";
  4398. /* clang-format on */
  4399. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4400. this->PolicyWarnedCMP0022 = true;
  4401. }
  4402. }
  4403. // There is no implicit link interface for executables or modules
  4404. // so if none was explicitly set then there is no link interface.
  4405. if (!explicitLibraries &&
  4406. (this->GetType() == cmStateEnums::EXECUTABLE ||
  4407. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  4408. return;
  4409. }
  4410. iface.Exists = true;
  4411. iface.ExplicitLibraries = explicitLibraries;
  4412. if (explicitLibraries) {
  4413. // The interface libraries have been explicitly set.
  4414. this->ExpandLinkItems(linkIfaceProp, explicitLibraries, config, headTarget,
  4415. usage_requirements_only, iface.Libraries,
  4416. iface.HadHeadSensitiveCondition);
  4417. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4418. this->GetPolicyStatusCMP0022() == cmPolicies::OLD)
  4419. // If CMP0022 is NEW then the plain tll signature sets the
  4420. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  4421. // cleared the property explicitly and we should not fall back
  4422. // to the link implementation.
  4423. {
  4424. // The link implementation is the default link interface.
  4425. cmLinkImplementationLibraries const* impl =
  4426. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4427. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  4428. impl->Libraries.end());
  4429. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4430. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  4431. // Compare the link implementation fallback link interface to the
  4432. // preferred new link interface property and warn if different.
  4433. std::vector<cmLinkItem> ifaceLibs;
  4434. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  4435. if (const char* newExplicitLibraries = this->GetProperty(newProp)) {
  4436. bool hadHeadSensitiveConditionDummy = false;
  4437. this->ExpandLinkItems(newProp, newExplicitLibraries, config,
  4438. headTarget, usage_requirements_only, ifaceLibs,
  4439. hadHeadSensitiveConditionDummy);
  4440. }
  4441. if (ifaceLibs != iface.Libraries) {
  4442. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  4443. std::string newLibraries = cmJoin(ifaceLibs, ";");
  4444. if (oldLibraries.empty()) {
  4445. oldLibraries = "(empty)";
  4446. }
  4447. if (newLibraries.empty()) {
  4448. newLibraries = "(empty)";
  4449. }
  4450. std::ostringstream w;
  4451. /* clang-format off */
  4452. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4453. "Target \"" << this->GetName() << "\" has an "
  4454. "INTERFACE_LINK_LIBRARIES property. "
  4455. "This should be preferred as the source of the link interface "
  4456. "for this library but because CMP0022 is not set CMake is "
  4457. "ignoring the property and using the link implementation "
  4458. "as the link interface instead."
  4459. "\n"
  4460. "INTERFACE_LINK_LIBRARIES:\n"
  4461. " " << newLibraries << "\n"
  4462. "Link implementation:\n"
  4463. " " << oldLibraries << "\n";
  4464. /* clang-format on */
  4465. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4466. this->PolicyWarnedCMP0022 = true;
  4467. }
  4468. }
  4469. }
  4470. }
  4471. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  4472. const std::string& config, cmGeneratorTarget const* headTarget,
  4473. bool usage_requirements_only) const
  4474. {
  4475. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  4476. if (!info) {
  4477. return nullptr;
  4478. }
  4479. std::string CONFIG = cmSystemTools::UpperCase(config);
  4480. cmHeadToLinkInterfaceMap& hm =
  4481. (usage_requirements_only
  4482. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4483. : this->GetHeadToLinkInterfaceMap(config));
  4484. // If the link interface does not depend on the head target
  4485. // then return the one we computed first.
  4486. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4487. return &hm.begin()->second;
  4488. }
  4489. cmOptionalLinkInterface& iface = hm[headTarget];
  4490. if (!iface.AllDone) {
  4491. iface.AllDone = true;
  4492. iface.Multiplicity = info->Multiplicity;
  4493. cmSystemTools::ExpandListArgument(info->Languages, iface.Languages);
  4494. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  4495. headTarget, usage_requirements_only, iface.Libraries,
  4496. iface.HadHeadSensitiveCondition);
  4497. std::vector<std::string> deps;
  4498. cmSystemTools::ExpandListArgument(info->SharedDeps, deps);
  4499. this->LookupLinkItems(deps, iface.SharedDeps);
  4500. }
  4501. return &iface;
  4502. }
  4503. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  4504. const std::string& config) const
  4505. {
  4506. // There is no imported information for non-imported targets.
  4507. if (!this->IsImported()) {
  4508. return nullptr;
  4509. }
  4510. // Lookup/compute/cache the import information for this
  4511. // configuration.
  4512. std::string config_upper;
  4513. if (!config.empty()) {
  4514. config_upper = cmSystemTools::UpperCase(config);
  4515. } else {
  4516. config_upper = "NOCONFIG";
  4517. }
  4518. ImportInfoMapType::const_iterator i = this->ImportInfoMap.find(config_upper);
  4519. if (i == this->ImportInfoMap.end()) {
  4520. ImportInfo info;
  4521. this->ComputeImportInfo(config_upper, info);
  4522. ImportInfoMapType::value_type entry(config_upper, info);
  4523. i = this->ImportInfoMap.insert(entry).first;
  4524. }
  4525. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4526. return &i->second;
  4527. }
  4528. // If the location is empty then the target is not available for
  4529. // this configuration.
  4530. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  4531. return nullptr;
  4532. }
  4533. // Return the import information.
  4534. return &i->second;
  4535. }
  4536. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  4537. ImportInfo& info) const
  4538. {
  4539. // This method finds information about an imported target from its
  4540. // properties. The "IMPORTED_" namespace is reserved for properties
  4541. // defined by the project exporting the target.
  4542. // Initialize members.
  4543. info.NoSOName = false;
  4544. const char* loc = nullptr;
  4545. const char* imp = nullptr;
  4546. std::string suffix;
  4547. if (!this->Target->GetMappedConfig(desired_config, &loc, &imp, suffix)) {
  4548. return;
  4549. }
  4550. // Get the link interface.
  4551. {
  4552. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  4553. const char* propertyLibs = this->GetProperty(linkProp);
  4554. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4555. if (!propertyLibs) {
  4556. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4557. linkProp += suffix;
  4558. propertyLibs = this->GetProperty(linkProp);
  4559. }
  4560. if (!propertyLibs) {
  4561. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4562. propertyLibs = this->GetProperty(linkProp);
  4563. }
  4564. }
  4565. if (propertyLibs) {
  4566. info.LibrariesProp = linkProp;
  4567. info.Libraries = propertyLibs;
  4568. }
  4569. }
  4570. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4571. if (loc) {
  4572. info.LibName = loc;
  4573. }
  4574. return;
  4575. }
  4576. // A provided configuration has been chosen. Load the
  4577. // configuration's properties.
  4578. // Get the location.
  4579. if (loc) {
  4580. info.Location = loc;
  4581. } else {
  4582. std::string impProp = "IMPORTED_LOCATION";
  4583. impProp += suffix;
  4584. if (const char* config_location = this->GetProperty(impProp)) {
  4585. info.Location = config_location;
  4586. } else if (const char* location = this->GetProperty("IMPORTED_LOCATION")) {
  4587. info.Location = location;
  4588. }
  4589. }
  4590. // Get the soname.
  4591. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4592. std::string soProp = "IMPORTED_SONAME";
  4593. soProp += suffix;
  4594. if (const char* config_soname = this->GetProperty(soProp)) {
  4595. info.SOName = config_soname;
  4596. } else if (const char* soname = this->GetProperty("IMPORTED_SONAME")) {
  4597. info.SOName = soname;
  4598. }
  4599. }
  4600. // Get the "no-soname" mark.
  4601. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4602. std::string soProp = "IMPORTED_NO_SONAME";
  4603. soProp += suffix;
  4604. if (const char* config_no_soname = this->GetProperty(soProp)) {
  4605. info.NoSOName = cmSystemTools::IsOn(config_no_soname);
  4606. } else if (const char* no_soname =
  4607. this->GetProperty("IMPORTED_NO_SONAME")) {
  4608. info.NoSOName = cmSystemTools::IsOn(no_soname);
  4609. }
  4610. }
  4611. // Get the import library.
  4612. if (imp) {
  4613. info.ImportLibrary = imp;
  4614. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4615. this->IsExecutableWithExports()) {
  4616. std::string impProp = "IMPORTED_IMPLIB";
  4617. impProp += suffix;
  4618. if (const char* config_implib = this->GetProperty(impProp)) {
  4619. info.ImportLibrary = config_implib;
  4620. } else if (const char* implib = this->GetProperty("IMPORTED_IMPLIB")) {
  4621. info.ImportLibrary = implib;
  4622. }
  4623. }
  4624. // Get the link dependencies.
  4625. {
  4626. std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
  4627. linkProp += suffix;
  4628. if (const char* config_libs = this->GetProperty(linkProp)) {
  4629. info.SharedDeps = config_libs;
  4630. } else if (const char* libs =
  4631. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  4632. info.SharedDeps = libs;
  4633. }
  4634. }
  4635. // Get the link languages.
  4636. if (this->LinkLanguagePropagatesToDependents()) {
  4637. std::string linkProp = "IMPORTED_LINK_INTERFACE_LANGUAGES";
  4638. linkProp += suffix;
  4639. if (const char* config_libs = this->GetProperty(linkProp)) {
  4640. info.Languages = config_libs;
  4641. } else if (const char* libs =
  4642. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  4643. info.Languages = libs;
  4644. }
  4645. }
  4646. // Get information if target is managed assembly.
  4647. {
  4648. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  4649. if (auto pc = this->GetProperty(linkProp + suffix)) {
  4650. info.Managed = this->CheckManagedType(pc);
  4651. } else if (auto p = this->GetProperty(linkProp)) {
  4652. info.Managed = this->CheckManagedType(p);
  4653. }
  4654. }
  4655. // Get the cyclic repetition count.
  4656. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4657. std::string linkProp = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  4658. linkProp += suffix;
  4659. if (const char* config_reps = this->GetProperty(linkProp)) {
  4660. sscanf(config_reps, "%u", &info.Multiplicity);
  4661. } else if (const char* reps =
  4662. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  4663. sscanf(reps, "%u", &info.Multiplicity);
  4664. }
  4665. }
  4666. }
  4667. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  4668. const std::string& config) const
  4669. {
  4670. std::string CONFIG = cmSystemTools::UpperCase(config);
  4671. return this->LinkInterfaceMap[CONFIG];
  4672. }
  4673. cmHeadToLinkInterfaceMap&
  4674. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  4675. const std::string& config) const
  4676. {
  4677. std::string CONFIG = cmSystemTools::UpperCase(config);
  4678. return this->LinkInterfaceUsageRequirementsOnlyMap[CONFIG];
  4679. }
  4680. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  4681. const std::string& config) const
  4682. {
  4683. // There is no link implementation for imported targets.
  4684. if (this->IsImported()) {
  4685. return nullptr;
  4686. }
  4687. std::string CONFIG = cmSystemTools::UpperCase(config);
  4688. cmOptionalLinkImplementation& impl = this->LinkImplMap[CONFIG][this];
  4689. if (!impl.LibrariesDone) {
  4690. impl.LibrariesDone = true;
  4691. this->ComputeLinkImplementationLibraries(config, impl, this);
  4692. }
  4693. if (!impl.LanguagesDone) {
  4694. impl.LanguagesDone = true;
  4695. this->ComputeLinkImplementationLanguages(config, impl);
  4696. }
  4697. return &impl;
  4698. }
  4699. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  4700. std::vector<cmSourceFile*>& files) const
  4701. {
  4702. std::vector<std::string> configs;
  4703. this->Makefile->GetConfigurations(configs);
  4704. if (configs.empty()) {
  4705. configs.emplace_back();
  4706. }
  4707. std::vector<std::string>::const_iterator it = configs.begin();
  4708. const std::string& firstConfig = *it;
  4709. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  4710. for (; it != configs.end(); ++it) {
  4711. std::vector<cmSourceFile*> configFiles;
  4712. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  4713. if (configFiles != files) {
  4714. std::string firstConfigFiles;
  4715. const char* sep = "";
  4716. for (cmSourceFile* f : files) {
  4717. firstConfigFiles += sep;
  4718. firstConfigFiles += f->GetFullPath();
  4719. sep = "\n ";
  4720. }
  4721. std::string thisConfigFiles;
  4722. sep = "";
  4723. for (cmSourceFile* f : configFiles) {
  4724. thisConfigFiles += sep;
  4725. thisConfigFiles += f->GetFullPath();
  4726. sep = "\n ";
  4727. }
  4728. std::ostringstream e;
  4729. /* clang-format off */
  4730. e << "Target \"" << this->GetName()
  4731. << "\" has source files which vary by "
  4732. "configuration. This is not supported by the \""
  4733. << this->GlobalGenerator->GetName()
  4734. << "\" generator.\n"
  4735. "Config \"" << firstConfig << "\":\n"
  4736. " " << firstConfigFiles << "\n"
  4737. "Config \"" << *it << "\":\n"
  4738. " " << thisConfigFiles << "\n";
  4739. /* clang-format on */
  4740. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  4741. return false;
  4742. }
  4743. }
  4744. return true;
  4745. }
  4746. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  4747. std::vector<cmGeneratorTarget*>& objlibs) const
  4748. {
  4749. // At configure-time, this method can be called as part of getting the
  4750. // LOCATION property or to export() a file to be include()d. However
  4751. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  4752. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  4753. // behavior of CMP0024 and CMP0026 only.
  4754. cmStringRange rng = this->Target->GetSourceEntries();
  4755. for (std::string const& entry : rng) {
  4756. std::vector<std::string> files;
  4757. cmSystemTools::ExpandListArgument(entry, files);
  4758. for (std::string const& li : files) {
  4759. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") &&
  4760. li[li.size() - 1] == '>') {
  4761. std::string objLibName = li.substr(17, li.size() - 18);
  4762. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  4763. continue;
  4764. }
  4765. cmGeneratorTarget* objLib =
  4766. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  4767. if (objLib) {
  4768. objlibs.push_back(objLib);
  4769. }
  4770. }
  4771. }
  4772. }
  4773. }
  4774. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  4775. {
  4776. // Strip whitespace off the library names because we used to do this
  4777. // in case variables were expanded at generate time. We no longer
  4778. // do the expansion but users link to libraries like " ${VAR} ".
  4779. std::string lib = item;
  4780. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  4781. if (pos != std::string::npos) {
  4782. lib = lib.substr(pos);
  4783. }
  4784. pos = lib.find_last_not_of(" \t\r\n");
  4785. if (pos != std::string::npos) {
  4786. lib = lib.substr(0, pos + 1);
  4787. }
  4788. if (lib != item) {
  4789. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  4790. switch (this->GetPolicyStatusCMP0004()) {
  4791. case cmPolicies::WARN: {
  4792. std::ostringstream w;
  4793. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  4794. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4795. << "\" which has leading or trailing whitespace.";
  4796. cm->IssueMessage(cmake::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  4797. }
  4798. case cmPolicies::OLD:
  4799. break;
  4800. case cmPolicies::NEW: {
  4801. std::ostringstream e;
  4802. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  4803. << "\" which has leading or trailing whitespace. "
  4804. << "This is now an error according to policy CMP0004.";
  4805. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4806. } break;
  4807. case cmPolicies::REQUIRED_IF_USED:
  4808. case cmPolicies::REQUIRED_ALWAYS: {
  4809. std::ostringstream e;
  4810. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  4811. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4812. << "\" which has leading or trailing whitespace.";
  4813. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4814. } break;
  4815. }
  4816. }
  4817. return lib;
  4818. }
  4819. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  4820. const std::string& config) const
  4821. {
  4822. std::vector<cmSourceFile*> sourceFiles;
  4823. this->GetSourceFiles(sourceFiles, config);
  4824. for (cmSourceFile* src : sourceFiles) {
  4825. const std::string& lang = src->GetLanguage();
  4826. if (!lang.empty()) {
  4827. languages.insert(lang);
  4828. }
  4829. }
  4830. std::vector<cmGeneratorTarget*> objectLibraries;
  4831. std::vector<cmSourceFile const*> externalObjects;
  4832. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4833. std::vector<cmGeneratorTarget*> objectTargets;
  4834. this->GetObjectLibrariesCMP0026(objectTargets);
  4835. objectLibraries.reserve(objectTargets.size());
  4836. for (cmGeneratorTarget* gt : objectTargets) {
  4837. objectLibraries.push_back(gt);
  4838. }
  4839. } else {
  4840. this->GetExternalObjects(externalObjects, config);
  4841. for (cmSourceFile const* extObj : externalObjects) {
  4842. std::string objLib = extObj->GetObjectLibrary();
  4843. if (cmGeneratorTarget* tgt =
  4844. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  4845. auto const objLibIt =
  4846. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  4847. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  4848. if (objectLibraries.cend() == objLibIt) {
  4849. objectLibraries.push_back(tgt);
  4850. }
  4851. }
  4852. }
  4853. }
  4854. for (cmGeneratorTarget* objLib : objectLibraries) {
  4855. objLib->GetLanguages(languages, config);
  4856. }
  4857. }
  4858. bool cmGeneratorTarget::HasLanguage(std::string const& language,
  4859. std::string const& config,
  4860. bool exclusive) const
  4861. {
  4862. std::set<std::string> languages;
  4863. this->GetLanguages(languages, config);
  4864. // add linker language (if it is different from compiler languages)
  4865. languages.insert(this->GetLinkerLanguage(config));
  4866. return (languages.size() == 1 || !exclusive) &&
  4867. languages.count(language) > 0;
  4868. }
  4869. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  4870. const std::string& config, cmOptionalLinkImplementation& impl) const
  4871. {
  4872. // This target needs runtime libraries for its source languages.
  4873. std::set<std::string> languages;
  4874. // Get languages used in our source files.
  4875. this->GetLanguages(languages, config);
  4876. // Copy the set of languages to the link implementation.
  4877. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  4878. languages.end());
  4879. }
  4880. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  4881. {
  4882. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  4883. return false;
  4884. }
  4885. if (this->GetProperty("BUILD_RPATH")) {
  4886. return true;
  4887. }
  4888. if (cmLinkImplementationLibraries const* impl =
  4889. this->GetLinkImplementationLibraries(config)) {
  4890. return !impl->Libraries.empty();
  4891. }
  4892. return false;
  4893. }
  4894. cmLinkImplementationLibraries const*
  4895. cmGeneratorTarget::GetLinkImplementationLibraries(
  4896. const std::string& config) const
  4897. {
  4898. return this->GetLinkImplementationLibrariesInternal(config, this);
  4899. }
  4900. cmLinkImplementationLibraries const*
  4901. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  4902. const std::string& config, cmGeneratorTarget const* head) const
  4903. {
  4904. // There is no link implementation for imported targets.
  4905. if (this->IsImported()) {
  4906. return nullptr;
  4907. }
  4908. // Populate the link implementation libraries for this configuration.
  4909. std::string CONFIG = cmSystemTools::UpperCase(config);
  4910. HeadToLinkImplementationMap& hm = this->LinkImplMap[CONFIG];
  4911. // If the link implementation does not depend on the head target
  4912. // then return the one we computed first.
  4913. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4914. return &hm.begin()->second;
  4915. }
  4916. cmOptionalLinkImplementation& impl = hm[head];
  4917. if (!impl.LibrariesDone) {
  4918. impl.LibrariesDone = true;
  4919. this->ComputeLinkImplementationLibraries(config, impl, head);
  4920. }
  4921. return &impl;
  4922. }
  4923. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  4924. const std::string& p) const
  4925. {
  4926. return this->LinkImplicitNullProperties.find(p) !=
  4927. this->LinkImplicitNullProperties.end();
  4928. }
  4929. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  4930. const std::string& config, cmOptionalLinkImplementation& impl,
  4931. cmGeneratorTarget const* head) const
  4932. {
  4933. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  4934. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  4935. cmBacktraceRange::const_iterator btIt = btRange.begin();
  4936. // Collect libraries directly linked in this configuration.
  4937. for (cmStringRange::const_iterator le = entryRange.begin(),
  4938. end = entryRange.end();
  4939. le != end; ++le, ++btIt) {
  4940. std::vector<std::string> llibs;
  4941. cmGeneratorExpressionDAGChecker dagChecker(
  4942. this->GetName(), "LINK_LIBRARIES", nullptr, nullptr);
  4943. cmGeneratorExpression ge(*btIt);
  4944. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  4945. std::string const evaluated =
  4946. cge->Evaluate(this->LocalGenerator, config, false, head, &dagChecker);
  4947. cmSystemTools::ExpandListArgument(evaluated, llibs);
  4948. if (cge->GetHadHeadSensitiveCondition()) {
  4949. impl.HadHeadSensitiveCondition = true;
  4950. }
  4951. for (std::string const& lib : llibs) {
  4952. // Skip entries that resolve to the target itself or are empty.
  4953. std::string name = this->CheckCMP0004(lib);
  4954. if (name == this->GetName() || name.empty()) {
  4955. if (name == this->GetName()) {
  4956. bool noMessage = false;
  4957. cmake::MessageType messageType = cmake::FATAL_ERROR;
  4958. std::ostringstream e;
  4959. switch (this->GetPolicyStatusCMP0038()) {
  4960. case cmPolicies::WARN: {
  4961. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  4962. messageType = cmake::AUTHOR_WARNING;
  4963. } break;
  4964. case cmPolicies::OLD:
  4965. noMessage = true;
  4966. case cmPolicies::REQUIRED_IF_USED:
  4967. case cmPolicies::REQUIRED_ALWAYS:
  4968. case cmPolicies::NEW:
  4969. // Issue the fatal message.
  4970. break;
  4971. }
  4972. if (!noMessage) {
  4973. e << "Target \"" << this->GetName() << "\" links to itself.";
  4974. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4975. messageType, e.str(), this->GetBacktrace());
  4976. if (messageType == cmake::FATAL_ERROR) {
  4977. return;
  4978. }
  4979. }
  4980. }
  4981. continue;
  4982. }
  4983. // The entry is meant for this configuration.
  4984. impl.Libraries.emplace_back(name, this->FindTargetToLink(name), *btIt,
  4985. evaluated != *le);
  4986. }
  4987. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  4988. for (std::string const& sp : seenProps) {
  4989. if (!this->GetProperty(sp)) {
  4990. this->LinkImplicitNullProperties.insert(sp);
  4991. }
  4992. }
  4993. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  4994. }
  4995. // Get the list of configurations considered to be DEBUG.
  4996. std::vector<std::string> debugConfigs =
  4997. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  4998. cmTargetLinkLibraryType linkType =
  4999. CMP0003_ComputeLinkType(config, debugConfigs);
  5000. cmTarget::LinkLibraryVectorType const& oldllibs =
  5001. this->Target->GetOriginalLinkLibraries();
  5002. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  5003. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  5004. std::string name = this->CheckCMP0004(oldllib.first);
  5005. if (name == this->GetName() || name.empty()) {
  5006. continue;
  5007. }
  5008. // Support OLD behavior for CMP0003.
  5009. impl.WrongConfigLibraries.emplace_back(name,
  5010. this->FindTargetToLink(name));
  5011. }
  5012. }
  5013. }
  5014. cmGeneratorTarget* cmGeneratorTarget::FindTargetToLink(
  5015. std::string const& name) const
  5016. {
  5017. cmGeneratorTarget* tgt =
  5018. this->LocalGenerator->FindGeneratorTargetToUse(name);
  5019. // Skip targets that will not really be linked. This is probably a
  5020. // name conflict between an external library and an executable
  5021. // within the project.
  5022. if (tgt && tgt->GetType() == cmStateEnums::EXECUTABLE &&
  5023. !tgt->IsExecutableWithExports()) {
  5024. tgt = nullptr;
  5025. }
  5026. return tgt;
  5027. }
  5028. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  5029. {
  5030. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  5031. // Return the directory in which the target will be built.
  5032. return info->PdbDir;
  5033. }
  5034. return "";
  5035. }
  5036. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  5037. {
  5038. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  5039. }
  5040. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  5041. std::string const& gnuName,
  5042. std::string& out,
  5043. const char* newExt) const
  5044. {
  5045. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  5046. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  5047. out = gnuName.substr(0, gnuName.size() - 6);
  5048. out += newExt ? newExt : ".lib";
  5049. return true;
  5050. }
  5051. return false;
  5052. }
  5053. bool cmGeneratorTarget::IsExecutableWithExports() const
  5054. {
  5055. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  5056. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  5057. }
  5058. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  5059. {
  5060. return (this->IsDLLPlatform() &&
  5061. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5062. this->IsExecutableWithExports()) &&
  5063. // Assemblies which have only managed code do not have
  5064. // import libraries.
  5065. this->GetManagedType(config) != ManagedType::Managed);
  5066. }
  5067. std::string cmGeneratorTarget::GetSupportDirectory() const
  5068. {
  5069. std::string dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  5070. dir += cmake::GetCMakeFilesDirectory();
  5071. dir += "/";
  5072. dir += this->GetName();
  5073. #if defined(__VMS)
  5074. dir += "_dir";
  5075. #else
  5076. dir += ".dir";
  5077. #endif
  5078. return dir;
  5079. }
  5080. bool cmGeneratorTarget::IsLinkable() const
  5081. {
  5082. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5083. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5084. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  5085. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  5086. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5087. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  5088. this->IsExecutableWithExports());
  5089. }
  5090. bool cmGeneratorTarget::IsFrameworkOnApple() const
  5091. {
  5092. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5093. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  5094. this->Makefile->IsOn("APPLE") &&
  5095. this->GetPropertyAsBool("FRAMEWORK"));
  5096. }
  5097. bool cmGeneratorTarget::IsAppBundleOnApple() const
  5098. {
  5099. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  5100. this->Makefile->IsOn("APPLE") &&
  5101. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  5102. }
  5103. bool cmGeneratorTarget::IsXCTestOnApple() const
  5104. {
  5105. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  5106. }
  5107. bool cmGeneratorTarget::IsCFBundleOnApple() const
  5108. {
  5109. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  5110. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  5111. }
  5112. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  5113. std::string const& propval) const
  5114. {
  5115. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  5116. // or only C++/CLI) does only depend on whether the property is an empty
  5117. // string or contains any value at all. In Visual Studio generators
  5118. // this propval is prepended with /clr[:] which results in:
  5119. //
  5120. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  5121. // lib
  5122. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  5123. // target, has import lib
  5124. // 3. any value (safe,pure): add /clr:[propval] as flag, target with
  5125. // managed code only, no import lib
  5126. return propval.empty() ? ManagedType::Mixed : ManagedType::Managed;
  5127. }
  5128. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  5129. const std::string& config) const
  5130. {
  5131. // Only libraries and executables can be managed targets.
  5132. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  5133. return ManagedType::Undefined;
  5134. }
  5135. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5136. return ManagedType::Native;
  5137. }
  5138. // Check imported target.
  5139. if (this->IsImported()) {
  5140. if (cmGeneratorTarget::ImportInfo const* info =
  5141. this->GetImportInfo(config)) {
  5142. return info->Managed;
  5143. }
  5144. return ManagedType::Undefined;
  5145. }
  5146. // Check for explicitly set clr target property.
  5147. if (auto* clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  5148. return this->CheckManagedType(clr);
  5149. }
  5150. // C# targets are always managed. This language specific check
  5151. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  5152. // has to be set manually for C# targets.
  5153. return this->HasLanguage("CSharp", config) ? ManagedType::Managed
  5154. : ManagedType::Native;
  5155. }