cmGeneratorTarget.cxx 180 KB

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