cmGeneratorTarget.cxx 179 KB

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