cmTarget.cxx 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmTarget.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <iterator>
  7. #include <map>
  8. #include <set>
  9. #include <sstream>
  10. #include <unordered_map>
  11. #include <unordered_set>
  12. #include <cm/memory>
  13. #include <cm/string_view>
  14. #include <cmext/algorithm>
  15. #include <cmext/string_view>
  16. #include "cmsys/RegularExpression.hxx"
  17. #include "cmAlgorithms.h"
  18. #include "cmCustomCommand.h"
  19. #include "cmFileSet.h"
  20. #include "cmFindPackageStack.h"
  21. #include "cmGeneratorExpression.h"
  22. #include "cmGlobalGenerator.h"
  23. #include "cmList.h"
  24. #include "cmListFileCache.h"
  25. #include "cmMakefile.h"
  26. #include "cmMessageType.h"
  27. #include "cmProperty.h"
  28. #include "cmPropertyDefinition.h"
  29. #include "cmPropertyMap.h"
  30. #include "cmRange.h"
  31. #include "cmSourceFile.h"
  32. #include "cmSourceFileLocation.h"
  33. #include "cmSourceFileLocationKind.h"
  34. #include "cmState.h"
  35. #include "cmStateDirectory.h"
  36. #include "cmStateSnapshot.h"
  37. #include "cmSystemTools.h"
  38. #include "cmTargetPropertyComputer.h"
  39. #include "cmValue.h"
  40. #include "cmXcFramework.h"
  41. #include "cmake.h"
  42. template <>
  43. std::string const& cmTargetPropertyComputer::ImportedLocation<cmTarget>(
  44. cmTarget const* tgt, std::string const& config)
  45. {
  46. static std::string loc;
  47. assert(tgt->IsImported());
  48. loc = tgt->ImportedGetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  49. return loc;
  50. }
  51. template <>
  52. cmValue cmTargetPropertyComputer::GetSources<cmTarget>(cmTarget const* tgt)
  53. {
  54. cmBTStringRange entries = tgt->GetSourceEntries();
  55. if (entries.empty()) {
  56. return nullptr;
  57. }
  58. std::ostringstream ss;
  59. char const* sep = "";
  60. for (auto const& entry : entries) {
  61. cmList files{ entry.Value };
  62. for (std::string const& file : files) {
  63. if ((cmHasLiteralPrefix(file, "$<TARGET_OBJECTS:") &&
  64. file.back() == '>') ||
  65. cmGeneratorExpression::Find(file) == std::string::npos) {
  66. ss << sep;
  67. sep = ";";
  68. ss << file;
  69. } else {
  70. cmSourceFile* sf = tgt->GetMakefile()->GetOrCreateSource(file);
  71. // Construct what is known about this source file location.
  72. cmSourceFileLocation const& location = sf->GetLocation();
  73. std::string sname = location.GetDirectory();
  74. if (!sname.empty()) {
  75. sname += "/";
  76. }
  77. sname += location.GetName();
  78. ss << sep;
  79. sep = ";";
  80. // Append this list entry.
  81. ss << sname;
  82. }
  83. }
  84. }
  85. static std::string srcs;
  86. srcs = ss.str();
  87. return cmValue(srcs);
  88. }
  89. namespace {
  90. struct FileSetEntries
  91. {
  92. FileSetEntries(cm::static_string_view propertyName)
  93. : PropertyName(propertyName)
  94. {
  95. }
  96. cm::static_string_view const PropertyName;
  97. std::vector<BT<std::string>> Entries;
  98. };
  99. struct FileSetType
  100. {
  101. FileSetType(cm::static_string_view typeName,
  102. cm::static_string_view defaultDirectoryProperty,
  103. cm::static_string_view defaultPathProperty,
  104. cm::static_string_view directoryPrefix,
  105. cm::static_string_view pathPrefix,
  106. cm::static_string_view typeDescription,
  107. cm::static_string_view defaultDescription,
  108. cm::static_string_view arbitraryDescription,
  109. FileSetEntries selfEntries, FileSetEntries interfaceEntries)
  110. : TypeName(typeName)
  111. , DefaultDirectoryProperty(defaultDirectoryProperty)
  112. , DefaultPathProperty(defaultPathProperty)
  113. , DirectoryPrefix(directoryPrefix)
  114. , PathPrefix(pathPrefix)
  115. , TypeDescription(typeDescription)
  116. , DefaultDescription(defaultDescription)
  117. , ArbitraryDescription(arbitraryDescription)
  118. , SelfEntries(std::move(selfEntries))
  119. , InterfaceEntries(std::move(interfaceEntries))
  120. {
  121. }
  122. cm::static_string_view const TypeName;
  123. cm::static_string_view const DefaultDirectoryProperty;
  124. cm::static_string_view const DefaultPathProperty;
  125. cm::static_string_view const DirectoryPrefix;
  126. cm::static_string_view const PathPrefix;
  127. cm::static_string_view const TypeDescription;
  128. cm::static_string_view const DefaultDescription;
  129. cm::static_string_view const ArbitraryDescription;
  130. FileSetEntries SelfEntries;
  131. FileSetEntries InterfaceEntries;
  132. enum class Action
  133. {
  134. Set,
  135. Append,
  136. };
  137. template <typename ValueType>
  138. bool WriteProperties(cmTarget* tgt, cmTargetInternals* impl,
  139. std::string const& prop, ValueType value,
  140. Action action);
  141. std::pair<bool, cmValue> ReadProperties(cmTarget const* tgt,
  142. cmTargetInternals const* impl,
  143. std::string const& prop) const;
  144. void AddFileSet(std::string const& name, cmFileSetVisibility vis,
  145. cmListFileBacktrace bt);
  146. };
  147. struct UsageRequirementProperty
  148. {
  149. enum class AppendEmpty
  150. {
  151. Yes,
  152. No,
  153. };
  154. UsageRequirementProperty(cm::static_string_view name,
  155. AppendEmpty appendEmpty = AppendEmpty::No)
  156. : Name(name)
  157. , AppendBehavior(appendEmpty)
  158. {
  159. }
  160. void CopyFromEntries(cmBTStringRange entries)
  161. {
  162. cm::append(this->Entries, entries);
  163. }
  164. enum class Action
  165. {
  166. Set,
  167. Prepend,
  168. Append,
  169. };
  170. template <typename ValueType>
  171. bool Write(cmTargetInternals const* impl,
  172. cm::optional<cmListFileBacktrace> const& bt,
  173. std::string const& prop, ValueType value, Action action);
  174. template <typename ValueType>
  175. void WriteDirect(cmTargetInternals const* impl,
  176. cm::optional<cmListFileBacktrace> const& bt,
  177. ValueType value, Action action);
  178. void WriteDirect(BT<std::string> value, Action action);
  179. std::pair<bool, cmValue> Read(std::string const& prop) const;
  180. cm::static_string_view const Name;
  181. AppendEmpty const AppendBehavior;
  182. std::vector<BT<std::string>> Entries;
  183. };
  184. struct TargetProperty
  185. {
  186. enum class InitCondition
  187. {
  188. // Always initialize the property.
  189. Always,
  190. // Never initialize the property.
  191. Never,
  192. // Only initialize if the target can compile sources.
  193. CanCompileSources,
  194. // Only apply to Xcode generators.
  195. NeedsXcode,
  196. // Only apply to Xcode generators on targets that can compile sources.
  197. NeedsXcodeAndCanCompileSources,
  198. // Needs to be a "normal" target (any non-global, non-utility target).
  199. NormalTarget,
  200. // Any non-imported target.
  201. NonImportedTarget,
  202. // Needs to be a "normal" target (any non-global, non-utility target) that
  203. // is not `IMPORTED`.
  204. NormalNonImportedTarget,
  205. // Needs to be a "normal" target with an artifact (no `INTERFACE`
  206. // libraries).
  207. TargetWithArtifact,
  208. // Needs to be a "normal" target with an artifact that is not an
  209. // executable.
  210. NonExecutableWithArtifact,
  211. // Needs to be a linkable library target (no `OBJECT` or `MODULE`
  212. // libraries).
  213. LinkableLibraryTarget,
  214. // Needs to be an executable.
  215. ExecutableTarget,
  216. // Needs to be a shared library (`SHARED`).
  217. SharedLibraryTarget,
  218. // Needs to be a target with meaningful symbol exports (`SHARED` or
  219. // `EXECUTABLE`).
  220. TargetWithSymbolExports,
  221. // Targets with "commands" associated with them. Basically everything
  222. // except global and `INTERFACE` targets.
  223. TargetWithCommands,
  224. };
  225. enum class Repetition
  226. {
  227. Once,
  228. PerConfig,
  229. PerConfigPrefix,
  230. };
  231. TargetProperty(cm::static_string_view name)
  232. : Name(name)
  233. {
  234. }
  235. TargetProperty(cm::static_string_view name, cm::static_string_view dflt,
  236. InitCondition init)
  237. : Name(name)
  238. , Default(dflt)
  239. , InitConditional(init)
  240. {
  241. }
  242. TargetProperty(cm::static_string_view name, InitCondition init)
  243. : Name(name)
  244. , InitConditional(init)
  245. {
  246. }
  247. TargetProperty(cm::static_string_view name, InitCondition init,
  248. Repetition repeat)
  249. : Name(name)
  250. , InitConditional(init)
  251. , Repeat(repeat)
  252. {
  253. }
  254. cm::static_string_view const Name;
  255. // Explicit initialization is needed for AppleClang in Xcode 8 and below
  256. // NOLINTNEXTLINE(readability-redundant-member-init)
  257. cm::optional<cm::static_string_view> const Default = {};
  258. InitCondition const InitConditional = InitCondition::Always;
  259. Repetition const Repeat = Repetition::Once;
  260. };
  261. #define IC TargetProperty::InitCondition
  262. #define R TargetProperty::Repetition
  263. /* clang-format off */
  264. #define COMMON_LANGUAGE_PROPERTIES(lang) \
  265. { #lang "_COMPILER_LAUNCHER"_s, IC::CanCompileSources }, \
  266. { #lang "_STANDARD"_s, IC::CanCompileSources }, \
  267. { #lang "_STANDARD_REQUIRED"_s, IC::CanCompileSources }, \
  268. { #lang "_EXTENSIONS"_s, IC::CanCompileSources }, \
  269. { #lang "_VISIBILITY_PRESET"_s, IC::CanCompileSources }
  270. /* clang-format on */
  271. TargetProperty const StaticTargetProperties[] = {
  272. /* clang-format off */
  273. // -- Debugger Properties
  274. { "DEBUGGER_WORKING_DIRECTORY"_s, IC::ExecutableTarget },
  275. // Compilation properties
  276. { "COMPILE_WARNING_AS_ERROR"_s, IC::CanCompileSources },
  277. { "INTERPROCEDURAL_OPTIMIZATION"_s, IC::CanCompileSources },
  278. { "INTERPROCEDURAL_OPTIMIZATION_"_s, IC::TargetWithArtifact, R::PerConfig },
  279. { "NO_SYSTEM_FROM_IMPORTED"_s, IC::CanCompileSources },
  280. // Set to `True` for `SHARED` and `MODULE` targets.
  281. { "POSITION_INDEPENDENT_CODE"_s, IC::CanCompileSources },
  282. { "VISIBILITY_INLINES_HIDDEN"_s, IC::CanCompileSources },
  283. // -- Features
  284. // ---- PCH
  285. { "DISABLE_PRECOMPILE_HEADERS"_s, IC::CanCompileSources },
  286. { "PCH_WARN_INVALID"_s, "ON"_s, IC::CanCompileSources },
  287. { "PCH_INSTANTIATE_TEMPLATES"_s, "ON"_s, IC::CanCompileSources },
  288. // -- Platforms
  289. // ---- Android
  290. { "ANDROID_API"_s, IC::CanCompileSources },
  291. { "ANDROID_API_MIN"_s, IC::CanCompileSources },
  292. { "ANDROID_ARCH"_s, IC::CanCompileSources },
  293. { "ANDROID_ASSETS_DIRECTORIES"_s, IC::CanCompileSources },
  294. { "ANDROID_JAVA_SOURCE_DIR"_s, IC::CanCompileSources },
  295. { "ANDROID_STL_TYPE"_s, IC::CanCompileSources },
  296. // ---- macOS
  297. { "OSX_ARCHITECTURES"_s, IC::CanCompileSources },
  298. // ---- Windows
  299. { "MSVC_DEBUG_INFORMATION_FORMAT"_s, IC::CanCompileSources },
  300. { "MSVC_RUNTIME_CHECKS"_s, IC::CanCompileSources },
  301. { "MSVC_RUNTIME_LIBRARY"_s, IC::CanCompileSources },
  302. { "VS_JUST_MY_CODE_DEBUGGING"_s, IC::CanCompileSources },
  303. { "VS_DEBUGGER_COMMAND"_s, IC::ExecutableTarget },
  304. { "VS_DEBUGGER_COMMAND_ARGUMENTS"_s, IC::ExecutableTarget },
  305. { "VS_DEBUGGER_ENVIRONMENT"_s, IC::ExecutableTarget },
  306. { "VS_DEBUGGER_WORKING_DIRECTORY"_s, IC::ExecutableTarget },
  307. { "VS_USE_DEBUG_LIBRARIES"_s, IC::NonImportedTarget },
  308. // ---- OpenWatcom
  309. { "WATCOM_RUNTIME_LIBRARY"_s, IC::CanCompileSources },
  310. // ---- AIX
  311. { "AIX_SHARED_LIBRARY_ARCHIVE"_s, IC::SharedLibraryTarget },
  312. // -- Language
  313. // ---- C
  314. COMMON_LANGUAGE_PROPERTIES(C),
  315. // ---- C++
  316. COMMON_LANGUAGE_PROPERTIES(CXX),
  317. { "CXX_MODULE_STD"_s, IC::CanCompileSources },
  318. // ---- CSharp
  319. { "DOTNET_SDK"_s, IC::NonImportedTarget },
  320. { "DOTNET_TARGET_FRAMEWORK"_s, IC::TargetWithCommands },
  321. { "DOTNET_TARGET_FRAMEWORK_VERSION"_s, IC::TargetWithCommands },
  322. // ---- CUDA
  323. COMMON_LANGUAGE_PROPERTIES(CUDA),
  324. { "CUDA_SEPARABLE_COMPILATION"_s, IC::CanCompileSources },
  325. { "CUDA_ARCHITECTURES"_s, IC::CanCompileSources },
  326. // ---- Fortran
  327. { "Fortran_FORMAT"_s, IC::CanCompileSources },
  328. { "Fortran_MODULE_DIRECTORY"_s, IC::CanCompileSources },
  329. { "Fortran_COMPILER_LAUNCHER"_s, IC::CanCompileSources },
  330. { "Fortran_PREPROCESS"_s, IC::CanCompileSources },
  331. { "Fortran_VISIBILITY_PRESET"_s, IC::CanCompileSources },
  332. // ---- HIP
  333. COMMON_LANGUAGE_PROPERTIES(HIP),
  334. { "HIP_ARCHITECTURES"_s, IC::CanCompileSources },
  335. // ---- ISPC
  336. { "ISPC_COMPILER_LAUNCHER"_s, IC::CanCompileSources },
  337. { "ISPC_HEADER_DIRECTORY"_s, IC::CanCompileSources },
  338. { "ISPC_HEADER_SUFFIX"_s, "_ispc.h"_s, IC::CanCompileSources },
  339. { "ISPC_INSTRUCTION_SETS"_s, IC::CanCompileSources },
  340. // ---- Objective C
  341. COMMON_LANGUAGE_PROPERTIES(OBJC),
  342. // ---- Objective C++
  343. COMMON_LANGUAGE_PROPERTIES(OBJCXX),
  344. // ---- Swift
  345. { "Swift_LANGUAGE_VERSION"_s, IC::CanCompileSources },
  346. { "Swift_MODULE_DIRECTORY"_s, IC::CanCompileSources },
  347. { "Swift_COMPILATION_MODE"_s, IC::CanCompileSources },
  348. // ---- moc
  349. { "AUTOMOC"_s, IC::CanCompileSources },
  350. { "AUTOMOC_COMPILER_PREDEFINES"_s, IC::CanCompileSources },
  351. { "AUTOMOC_INCLUDE_DIRECTORIES"_s, IC::CanCompileSources },
  352. { "AUTOMOC_MACRO_NAMES"_s, IC::CanCompileSources },
  353. { "AUTOMOC_MOC_OPTIONS"_s, IC::CanCompileSources },
  354. { "AUTOMOC_PATH_PREFIX"_s, IC::CanCompileSources },
  355. { "AUTOMOC_EXECUTABLE"_s, IC::CanCompileSources },
  356. // ---- uic
  357. { "AUTOUIC"_s, IC::CanCompileSources },
  358. { "AUTOUIC_OPTIONS"_s, IC::CanCompileSources },
  359. { "AUTOUIC_SEARCH_PATHS"_s, IC::CanCompileSources },
  360. { "AUTOUIC_EXECUTABLE"_s, IC::CanCompileSources },
  361. // ---- rcc
  362. { "AUTORCC"_s, IC::CanCompileSources },
  363. { "AUTORCC_OPTIONS"_s, IC::CanCompileSources },
  364. { "AUTORCC_EXECUTABLE"_s, IC::CanCompileSources },
  365. // Linking properties
  366. { "LINKER_TYPE"_s, IC::CanCompileSources },
  367. { "LINK_WARNING_AS_ERROR"_s, IC::CanCompileSources },
  368. { "ENABLE_EXPORTS"_s, IC::TargetWithSymbolExports },
  369. { "LINK_LIBRARIES_ONLY_TARGETS"_s, IC::NormalNonImportedTarget },
  370. { "LINK_LIBRARIES_STRATEGY"_s, IC::NormalNonImportedTarget },
  371. { "LINK_SEARCH_START_STATIC"_s, IC::CanCompileSources },
  372. { "LINK_SEARCH_END_STATIC"_s, IC::CanCompileSources },
  373. // Initialize per-configuration name postfix property from the variable only
  374. // for non-executable targets. This preserves compatibility with previous
  375. // CMake versions in which executables did not support this variable.
  376. // Projects may still specify the property directly.
  377. { "_POSTFIX"_s, IC::NonExecutableWithArtifact, R::PerConfigPrefix },
  378. // -- Dependent library lookup
  379. { "MACOSX_RPATH"_s, IC::CanCompileSources },
  380. // ---- Build
  381. { "BUILD_RPATH"_s, IC::CanCompileSources },
  382. { "BUILD_RPATH_USE_ORIGIN"_s, IC::CanCompileSources },
  383. { "SKIP_BUILD_RPATH"_s, "OFF"_s, IC::CanCompileSources },
  384. { "BUILD_WITH_INSTALL_RPATH"_s, "OFF"_s, IC::CanCompileSources },
  385. { "BUILD_WITH_INSTALL_NAME_DIR"_s, IC::CanCompileSources },
  386. // ---- Install
  387. { "INSTALL_NAME_DIR"_s, IC::CanCompileSources },
  388. { "INSTALL_OBJECT_NAME_STRATEGY"_s, IC::CanCompileSources },
  389. { "INSTALL_REMOVE_ENVIRONMENT_RPATH"_s, IC::CanCompileSources },
  390. { "INSTALL_RPATH"_s, ""_s, IC::CanCompileSources },
  391. { "INSTALL_RPATH_USE_LINK_PATH"_s, "OFF"_s, IC::CanCompileSources },
  392. // -- Platforms
  393. // ---- AIX
  394. { "AIX_EXPORT_ALL_SYMBOLS"_s, IC::TargetWithSymbolExports },
  395. // ---- Android
  396. { "ANDROID_GUI"_s, IC::ExecutableTarget },
  397. { "ANDROID_JAR_DIRECTORIES"_s, IC::CanCompileSources },
  398. { "ANDROID_JAR_DEPENDENCIES"_s, IC::CanCompileSources },
  399. { "ANDROID_NATIVE_LIB_DIRECTORIES"_s, IC::CanCompileSources },
  400. { "ANDROID_NATIVE_LIB_DEPENDENCIES"_s, IC::CanCompileSources },
  401. { "ANDROID_PROGUARD"_s, IC::CanCompileSources },
  402. { "ANDROID_PROGUARD_CONFIG_PATH"_s, IC::CanCompileSources },
  403. { "ANDROID_SECURE_PROPS_PATH"_s, IC::CanCompileSources },
  404. // ---- iOS
  405. { "IOS_INSTALL_COMBINED"_s, IC::CanCompileSources },
  406. // ---- macOS
  407. { "FRAMEWORK_MULTI_CONFIG_POSTFIX_"_s, IC::LinkableLibraryTarget, R::PerConfig },
  408. // ---- Windows
  409. { "DLL_NAME_WITH_SOVERSION"_s, IC::SharedLibraryTarget },
  410. { "GNUtoMS"_s, IC::CanCompileSources },
  411. { "WIN32_EXECUTABLE"_s, IC::CanCompileSources },
  412. { "WINDOWS_EXPORT_ALL_SYMBOLS"_s, IC::TargetWithSymbolExports },
  413. // -- Languages
  414. // ---- C
  415. { "C_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  416. // ---- C++
  417. { "CXX_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  418. // ---- CUDA
  419. { "CUDA_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  420. { "CUDA_RESOLVE_DEVICE_SYMBOLS"_s, IC::CanCompileSources },
  421. { "CUDA_RUNTIME_LIBRARY"_s, IC::CanCompileSources },
  422. // ---- HIP
  423. { "HIP_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  424. { "HIP_RUNTIME_LIBRARY"_s, IC::CanCompileSources },
  425. // ---- Objective C
  426. { "OBJC_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  427. // ---- Objective C++
  428. { "OBJCXX_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  429. // ---- Fortran
  430. { "Fortran_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  431. // Static analysis
  432. { "SKIP_LINTING"_s, IC::CanCompileSources },
  433. // -- C
  434. { "C_CLANG_TIDY"_s, IC::CanCompileSources },
  435. { "C_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  436. { "C_CPPLINT"_s, IC::CanCompileSources },
  437. { "C_CPPCHECK"_s, IC::CanCompileSources },
  438. { "C_ICSTAT"_s, IC::CanCompileSources },
  439. { "C_INCLUDE_WHAT_YOU_USE"_s, IC::CanCompileSources },
  440. // -- C++
  441. { "CXX_CLANG_TIDY"_s, IC::CanCompileSources },
  442. { "CXX_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  443. { "CXX_CPPLINT"_s, IC::CanCompileSources },
  444. { "CXX_CPPCHECK"_s, IC::CanCompileSources },
  445. { "CXX_ICSTAT"_s, IC::CanCompileSources },
  446. { "CXX_INCLUDE_WHAT_YOU_USE"_s, IC::CanCompileSources },
  447. // -- Objective C
  448. { "OBJC_CLANG_TIDY"_s, IC::CanCompileSources },
  449. { "OBJC_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  450. // -- Objective C++
  451. { "OBJCXX_CLANG_TIDY"_s, IC::CanCompileSources },
  452. { "OBJCXX_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  453. // -- Linking
  454. { "LINK_WHAT_YOU_USE"_s, IC::CanCompileSources },
  455. // Build graph properties
  456. { "LINK_DEPENDS_NO_SHARED"_s, IC::CanCompileSources },
  457. { "UNITY_BUILD"_s, IC::CanCompileSources },
  458. { "UNITY_BUILD_UNIQUE_ID"_s, IC::CanCompileSources },
  459. { "UNITY_BUILD_BATCH_SIZE"_s, "8"_s, IC::CanCompileSources },
  460. { "UNITY_BUILD_MODE"_s, "BATCH"_s, IC::CanCompileSources },
  461. { "UNITY_BUILD_RELOCATABLE"_s, IC::CanCompileSources },
  462. { "OPTIMIZE_DEPENDENCIES"_s, IC::CanCompileSources },
  463. { "VERIFY_INTERFACE_HEADER_SETS"_s },
  464. // -- Android
  465. { "ANDROID_ANT_ADDITIONAL_OPTIONS"_s, IC::CanCompileSources },
  466. { "ANDROID_PROCESS_MAX"_s, IC::CanCompileSources },
  467. { "ANDROID_SKIP_ANT_STEP"_s, IC::CanCompileSources },
  468. // -- Autogen
  469. { "AUTOGEN_COMMAND_LINE_LENGTH_MAX"_s, IC::CanCompileSources },
  470. { "AUTOGEN_ORIGIN_DEPENDS"_s, IC::CanCompileSources },
  471. { "AUTOGEN_PARALLEL"_s, IC::CanCompileSources },
  472. { "AUTOGEN_USE_SYSTEM_INCLUDE"_s, IC::CanCompileSources },
  473. { "AUTOGEN_BETTER_GRAPH_MULTI_CONFIG"_s, IC::CanCompileSources },
  474. // -- moc
  475. { "AUTOMOC_DEPEND_FILTERS"_s, IC::CanCompileSources },
  476. // -- C++
  477. { "CXX_SCAN_FOR_MODULES"_s, IC::CanCompileSources },
  478. // -- Ninja
  479. { "JOB_POOL_COMPILE"_s, IC::CanCompileSources },
  480. { "JOB_POOL_LINK"_s, IC::CanCompileSources },
  481. { "JOB_POOL_PRECOMPILE_HEADER"_s, IC::CanCompileSources },
  482. // -- Visual Studio
  483. { "VS_NO_COMPILE_BATCHING"_s, IC::CanCompileSources },
  484. { "VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION"_s, IC::CanCompileSources},
  485. // Output location properties
  486. { "ARCHIVE_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  487. { "ARCHIVE_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  488. { "COMPILE_PDB_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  489. { "COMPILE_PDB_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  490. { "LIBRARY_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  491. { "LIBRARY_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  492. { "PDB_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  493. { "PDB_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  494. { "RUNTIME_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  495. { "RUNTIME_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  496. // macOS bundle properties
  497. { "FRAMEWORK"_s, IC::CanCompileSources },
  498. { "FRAMEWORK_MULTI_CONFIG_POSTFIX"_s, IC::CanCompileSources },
  499. { "MACOSX_BUNDLE"_s, IC::CanCompileSources },
  500. // Usage requirement properties
  501. { "LINK_INTERFACE_LIBRARIES"_s, IC::CanCompileSources },
  502. { "MAP_IMPORTED_CONFIG_"_s, IC::NormalTarget, R::PerConfig },
  503. { "EXPORT_FIND_PACKAGE_NAME"_s, IC::NormalTarget },
  504. // Metadata
  505. { "CROSSCOMPILING_EMULATOR"_s, IC::ExecutableTarget },
  506. { "EXPORT_BUILD_DATABASE"_s, IC::CanCompileSources },
  507. { "EXPORT_COMPILE_COMMANDS"_s, IC::CanCompileSources },
  508. { "FOLDER"_s },
  509. { "TEST_LAUNCHER"_s, IC::ExecutableTarget },
  510. // Xcode properties
  511. { "XCODE_GENERATE_SCHEME"_s, IC::NeedsXcode },
  512. #ifdef __APPLE__
  513. { "XCODE_SCHEME_ADDRESS_SANITIZER"_s, IC::NeedsXcodeAndCanCompileSources },
  514. { "XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN"_s, IC::NeedsXcodeAndCanCompileSources },
  515. { "XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING"_s, IC::NeedsXcodeAndCanCompileSources },
  516. { "XCODE_SCHEME_ENABLE_GPU_FRAME_CAPTURE_MODE"_s, IC::NeedsXcodeAndCanCompileSources },
  517. { "XCODE_SCHEME_THREAD_SANITIZER"_s, IC::NeedsXcodeAndCanCompileSources },
  518. { "XCODE_SCHEME_THREAD_SANITIZER_STOP"_s, IC::NeedsXcodeAndCanCompileSources },
  519. { "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER"_s, IC::NeedsXcodeAndCanCompileSources },
  520. { "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP"_s, IC::NeedsXcodeAndCanCompileSources },
  521. { "XCODE_SCHEME_LAUNCH_CONFIGURATION"_s, IC::NeedsXcodeAndCanCompileSources },
  522. { "XCODE_SCHEME_TEST_CONFIGURATION"_s, IC::NeedsXcodeAndCanCompileSources },
  523. { "XCODE_SCHEME_ENABLE_GPU_API_VALIDATION"_s, IC::NeedsXcodeAndCanCompileSources },
  524. { "XCODE_SCHEME_ENABLE_GPU_SHADER_VALIDATION"_s, IC::NeedsXcodeAndCanCompileSources },
  525. { "XCODE_SCHEME_WORKING_DIRECTORY"_s, IC::NeedsXcodeAndCanCompileSources },
  526. { "XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER"_s, IC::NeedsXcodeAndCanCompileSources },
  527. { "XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP"_s, IC::NeedsXcodeAndCanCompileSources },
  528. { "XCODE_SCHEME_MALLOC_SCRIBBLE"_s, IC::NeedsXcodeAndCanCompileSources },
  529. { "XCODE_SCHEME_MALLOC_GUARD_EDGES"_s, IC::NeedsXcodeAndCanCompileSources },
  530. { "XCODE_SCHEME_GUARD_MALLOC"_s, IC::NeedsXcodeAndCanCompileSources },
  531. { "XCODE_SCHEME_LAUNCH_MODE"_s, IC::NeedsXcodeAndCanCompileSources },
  532. { "XCODE_SCHEME_LLDB_INIT_FILE"_s, IC::NeedsXcodeAndCanCompileSources },
  533. { "XCODE_SCHEME_ZOMBIE_OBJECTS"_s, IC::NeedsXcodeAndCanCompileSources },
  534. { "XCODE_SCHEME_MALLOC_STACK"_s, IC::NeedsXcodeAndCanCompileSources },
  535. { "XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE"_s, IC::NeedsXcodeAndCanCompileSources },
  536. { "XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS"_s, IC::NeedsXcodeAndCanCompileSources },
  537. { "XCODE_SCHEME_ENVIRONMENT"_s, IC::NeedsXcodeAndCanCompileSources },
  538. { "XCODE_LINK_BUILD_PHASE_MODE"_s, "NONE"_s, IC::NeedsXcodeAndCanCompileSources },
  539. #endif
  540. /* clang-format on */
  541. };
  542. #undef COMMON_LANGUAGE_PROPERTIES
  543. #undef IC
  544. #undef R
  545. }
  546. class cmTargetInternals
  547. {
  548. public:
  549. cmStateEnums::TargetType TargetType;
  550. cmTarget::Origin Origin = cmTarget::Origin::Unknown;
  551. cmMakefile* Makefile;
  552. cmPolicies::PolicyMap PolicyMap;
  553. cmTarget const* TemplateTarget;
  554. std::string Name;
  555. std::string InstallPath;
  556. std::string RuntimeInstallPath;
  557. cmPropertyMap Properties;
  558. bool IsGeneratorProvided;
  559. bool HaveInstallRule;
  560. bool IsDLLPlatform;
  561. bool IsAIX;
  562. bool IsApple;
  563. bool IsAndroid;
  564. bool BuildInterfaceIncludesAppended;
  565. bool PerConfig;
  566. cmTarget::Visibility TargetVisibility;
  567. std::set<BT<std::pair<std::string, bool>>> Utilities;
  568. std::set<std::string> CodegenDependencies;
  569. std::vector<cmCustomCommand> PreBuildCommands;
  570. std::vector<cmCustomCommand> PreLinkCommands;
  571. std::vector<cmCustomCommand> PostBuildCommands;
  572. std::vector<cmInstallTargetGenerator*> InstallGenerators;
  573. std::set<std::string> SystemIncludeDirectories;
  574. cmTarget::LinkLibraryVectorType OriginalLinkLibraries;
  575. std::map<std::string, BTs<std::string>> LanguageStandardProperties;
  576. std::map<cmTargetExport const*, std::vector<std::string>>
  577. InstallIncludeDirectoriesEntries;
  578. std::vector<std::pair<cmTarget::TLLSignature, cmListFileContext>>
  579. TLLCommands;
  580. std::map<std::string, cmFileSet> FileSets;
  581. cmListFileBacktrace Backtrace;
  582. cmFindPackageStack FindPackageStack;
  583. UsageRequirementProperty IncludeDirectories;
  584. UsageRequirementProperty CompileOptions;
  585. UsageRequirementProperty CompileFeatures;
  586. UsageRequirementProperty CompileDefinitions;
  587. UsageRequirementProperty PrecompileHeaders;
  588. UsageRequirementProperty Sources;
  589. UsageRequirementProperty LinkOptions;
  590. UsageRequirementProperty LinkDirectories;
  591. UsageRequirementProperty LinkLibraries;
  592. UsageRequirementProperty InterfaceLinkLibraries;
  593. UsageRequirementProperty InterfaceLinkLibrariesDirect;
  594. UsageRequirementProperty InterfaceLinkLibrariesDirectExclude;
  595. UsageRequirementProperty ImportedCxxModulesIncludeDirectories;
  596. UsageRequirementProperty ImportedCxxModulesCompileDefinitions;
  597. UsageRequirementProperty ImportedCxxModulesCompileFeatures;
  598. UsageRequirementProperty ImportedCxxModulesCompileOptions;
  599. UsageRequirementProperty ImportedCxxModulesLinkLibraries;
  600. FileSetType HeadersFileSets;
  601. FileSetType CxxModulesFileSets;
  602. cmTargetInternals();
  603. bool IsImported() const;
  604. bool CheckImportedLibName(std::string const& prop,
  605. std::string const& value) const;
  606. template <typename ValueType>
  607. void AddDirectoryToFileSet(cmTarget* self, std::string const& fileSetName,
  608. ValueType value, cm::string_view fileSetType,
  609. cm::string_view description,
  610. FileSetType::Action action);
  611. template <typename ValueType>
  612. void AddPathToFileSet(cmTarget* self, std::string const& fileSetName,
  613. ValueType value, cm::string_view fileSetType,
  614. cm::string_view description,
  615. FileSetType::Action action);
  616. cmValue GetFileSetDirectories(cmTarget const* self,
  617. std::string const& fileSetName,
  618. cm::string_view fileSetType) const;
  619. cmValue GetFileSetPaths(cmTarget const* self, std::string const& fileSetName,
  620. cm::string_view fileSetType) const;
  621. cmListFileBacktrace GetBacktrace(
  622. cm::optional<cmListFileBacktrace> const& bt) const
  623. {
  624. return bt ? *bt : this->Makefile->GetBacktrace();
  625. }
  626. };
  627. cmTargetInternals::cmTargetInternals()
  628. : IncludeDirectories("INCLUDE_DIRECTORIES"_s)
  629. , CompileOptions("COMPILE_OPTIONS"_s)
  630. , CompileFeatures("COMPILE_FEATURES"_s)
  631. , CompileDefinitions("COMPILE_DEFINITIONS"_s)
  632. , PrecompileHeaders("PRECOMPILE_HEADERS"_s)
  633. , Sources("SOURCES"_s, UsageRequirementProperty::AppendEmpty::Yes)
  634. , LinkOptions("LINK_OPTIONS"_s)
  635. , LinkDirectories("LINK_DIRECTORIES"_s)
  636. , LinkLibraries("LINK_LIBRARIES"_s)
  637. , InterfaceLinkLibraries("INTERFACE_LINK_LIBRARIES"_s)
  638. , InterfaceLinkLibrariesDirect("INTERFACE_LINK_LIBRARIES_DIRECT"_s)
  639. , InterfaceLinkLibrariesDirectExclude(
  640. "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE"_s)
  641. , ImportedCxxModulesIncludeDirectories(
  642. "IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES"_s)
  643. , ImportedCxxModulesCompileDefinitions(
  644. "IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS"_s)
  645. , ImportedCxxModulesCompileFeatures(
  646. "IMPORTED_CXX_MODULES_COMPILE_FEATURES"_s)
  647. , ImportedCxxModulesCompileOptions("IMPORTED_CXX_MODULES_COMPILE_OPTIONS"_s)
  648. , ImportedCxxModulesLinkLibraries("IMPORTED_CXX_MODULES_LINK_LIBRARIES"_s)
  649. , HeadersFileSets("HEADERS"_s, "HEADER_DIRS"_s, "HEADER_SET"_s,
  650. "HEADER_DIRS_"_s, "HEADER_SET_"_s, "Header"_s,
  651. "The default header set"_s, "Header set"_s,
  652. FileSetEntries("HEADER_SETS"_s),
  653. FileSetEntries("INTERFACE_HEADER_SETS"_s))
  654. , CxxModulesFileSets("CXX_MODULES"_s, "CXX_MODULE_DIRS"_s,
  655. "CXX_MODULE_SET"_s, "CXX_MODULE_DIRS_"_s,
  656. "CXX_MODULE_SET_"_s, "C++ module"_s,
  657. "The default C++ module set"_s, "C++ module set"_s,
  658. FileSetEntries("CXX_MODULE_SETS"_s),
  659. FileSetEntries("INTERFACE_CXX_MODULE_SETS"_s))
  660. {
  661. }
  662. template <typename ValueType>
  663. bool FileSetType::WriteProperties(cmTarget* tgt, cmTargetInternals* impl,
  664. std::string const& prop, ValueType value,
  665. Action action)
  666. {
  667. if (prop == this->DefaultDirectoryProperty) {
  668. impl->AddDirectoryToFileSet(tgt, std::string(this->TypeName), value,
  669. this->TypeName, this->DefaultDescription,
  670. action);
  671. return true;
  672. }
  673. if (prop == this->DefaultPathProperty) {
  674. impl->AddPathToFileSet(tgt, std::string(this->TypeName), value,
  675. this->TypeName, this->DefaultDescription, action);
  676. return true;
  677. }
  678. if (cmHasPrefix(prop, this->DirectoryPrefix)) {
  679. auto fileSetName = prop.substr(this->DirectoryPrefix.size());
  680. if (fileSetName.empty()) {
  681. impl->Makefile->IssueMessage(
  682. MessageType::FATAL_ERROR,
  683. cmStrCat(this->ArbitraryDescription, " name cannot be empty."));
  684. } else {
  685. impl->AddDirectoryToFileSet(
  686. tgt, fileSetName, value, this->TypeName,
  687. cmStrCat(this->ArbitraryDescription, " \"", fileSetName, '"'), action);
  688. }
  689. return true;
  690. }
  691. if (cmHasPrefix(prop, this->PathPrefix)) {
  692. auto fileSetName = prop.substr(this->PathPrefix.size());
  693. if (fileSetName.empty()) {
  694. impl->Makefile->IssueMessage(
  695. MessageType::FATAL_ERROR,
  696. cmStrCat(this->ArbitraryDescription, " name cannot be empty."));
  697. } else {
  698. impl->AddPathToFileSet(
  699. tgt, fileSetName, value, this->TypeName,
  700. cmStrCat(this->ArbitraryDescription, " \"", fileSetName, '"'), action);
  701. }
  702. return true;
  703. }
  704. return false;
  705. }
  706. std::pair<bool, cmValue> FileSetType::ReadProperties(
  707. cmTarget const* tgt, cmTargetInternals const* impl,
  708. std::string const& prop) const
  709. {
  710. bool did_read = false;
  711. cmValue value = nullptr;
  712. if (prop == this->DefaultDirectoryProperty) {
  713. value = impl->GetFileSetDirectories(tgt, std::string(this->TypeName),
  714. this->TypeName);
  715. did_read = true;
  716. } else if (prop == this->DefaultPathProperty) {
  717. value =
  718. impl->GetFileSetPaths(tgt, std::string(this->TypeName), this->TypeName);
  719. did_read = true;
  720. } else if (prop == this->SelfEntries.PropertyName) {
  721. static std::string output;
  722. output = cmList::to_string(this->SelfEntries.Entries);
  723. value = cmValue(output);
  724. did_read = true;
  725. } else if (prop == this->InterfaceEntries.PropertyName) {
  726. static std::string output;
  727. output = cmList::to_string(this->InterfaceEntries.Entries);
  728. value = cmValue(output);
  729. did_read = true;
  730. } else if (cmHasPrefix(prop, this->DirectoryPrefix)) {
  731. std::string fileSetName = prop.substr(this->DirectoryPrefix.size());
  732. if (!fileSetName.empty()) {
  733. value = impl->GetFileSetDirectories(tgt, fileSetName, this->TypeName);
  734. }
  735. did_read = true;
  736. } else if (cmHasPrefix(prop, this->PathPrefix)) {
  737. std::string fileSetName = prop.substr(this->PathPrefix.size());
  738. if (!fileSetName.empty()) {
  739. value = impl->GetFileSetPaths(tgt, fileSetName, this->TypeName);
  740. }
  741. did_read = true;
  742. }
  743. return { did_read, value };
  744. }
  745. void FileSetType::AddFileSet(std::string const& name, cmFileSetVisibility vis,
  746. cmListFileBacktrace bt)
  747. {
  748. if (cmFileSetVisibilityIsForSelf(vis)) {
  749. this->SelfEntries.Entries.emplace_back(name, bt);
  750. }
  751. if (cmFileSetVisibilityIsForInterface(vis)) {
  752. this->InterfaceEntries.Entries.emplace_back(name, std::move(bt));
  753. }
  754. }
  755. template <typename ValueType>
  756. bool UsageRequirementProperty::Write(
  757. cmTargetInternals const* impl, cm::optional<cmListFileBacktrace> const& bt,
  758. std::string const& prop, ValueType value, Action action)
  759. {
  760. if (prop == this->Name) {
  761. this->WriteDirect(impl, bt, value, action);
  762. return true;
  763. }
  764. return false;
  765. }
  766. template <typename ValueType>
  767. void UsageRequirementProperty::WriteDirect(
  768. cmTargetInternals const* impl, cm::optional<cmListFileBacktrace> const& bt,
  769. ValueType value, Action action)
  770. {
  771. if (action == Action::Set) {
  772. this->Entries.clear();
  773. }
  774. if (value) {
  775. cmListFileBacktrace lfbt = impl->GetBacktrace(bt);
  776. if (action == Action::Prepend) {
  777. this->Entries.emplace(this->Entries.begin(), value, lfbt);
  778. } else if (action == Action::Set || cmNonempty(value) ||
  779. this->AppendBehavior == AppendEmpty::Yes) {
  780. this->Entries.emplace_back(value, lfbt);
  781. }
  782. }
  783. }
  784. void UsageRequirementProperty::WriteDirect(BT<std::string> value,
  785. Action action)
  786. {
  787. if (action == Action::Set) {
  788. this->Entries.clear();
  789. }
  790. if (action == Action::Prepend) {
  791. this->Entries.emplace(this->Entries.begin(), std::move(value));
  792. } else {
  793. this->Entries.emplace_back(std::move(value));
  794. }
  795. }
  796. std::pair<bool, cmValue> UsageRequirementProperty::Read(
  797. std::string const& prop) const
  798. {
  799. bool did_read = false;
  800. cmValue value = nullptr;
  801. if (prop == this->Name) {
  802. if (!this->Entries.empty()) {
  803. // Storage to back the returned `cmValue`.
  804. static std::string output;
  805. output = cmList::to_string(this->Entries);
  806. value = cmValue(output);
  807. }
  808. did_read = true;
  809. }
  810. return { did_read, value };
  811. }
  812. cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type,
  813. Visibility vis, cmMakefile* mf, PerConfig perConfig)
  814. : impl(cm::make_unique<cmTargetInternals>())
  815. {
  816. assert(mf);
  817. this->impl->TargetType = type;
  818. this->impl->Makefile = mf;
  819. this->impl->Name = name;
  820. this->impl->TemplateTarget = nullptr;
  821. this->impl->IsGeneratorProvided = false;
  822. this->impl->HaveInstallRule = false;
  823. this->impl->IsDLLPlatform = false;
  824. this->impl->IsAIX = false;
  825. this->impl->IsApple = false;
  826. this->impl->IsAndroid = false;
  827. this->impl->TargetVisibility = vis;
  828. this->impl->BuildInterfaceIncludesAppended = false;
  829. this->impl->PerConfig = (perConfig == PerConfig::Yes);
  830. // Check whether this is a DLL platform.
  831. this->impl->IsDLLPlatform =
  832. !this->impl->Makefile->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")
  833. .empty();
  834. // Check whether we are targeting AIX.
  835. {
  836. std::string const& systemName =
  837. this->impl->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  838. this->impl->IsAIX = (systemName == "AIX" || systemName == "OS400");
  839. }
  840. // Check whether we are targeting Apple.
  841. this->impl->IsApple = this->impl->Makefile->IsOn("APPLE");
  842. // Check whether we are targeting an Android platform.
  843. this->impl->IsAndroid = (this->impl->Makefile->GetSafeDefinition(
  844. "CMAKE_SYSTEM_NAME") == "Android");
  845. // Save the backtrace of target construction.
  846. this->impl->Backtrace = this->impl->Makefile->GetBacktrace();
  847. if (this->impl->IsImported()) {
  848. this->impl->FindPackageStack = this->impl->Makefile->GetFindPackageStack();
  849. }
  850. if (this->IsNormal()) {
  851. // Initialize the INCLUDE_DIRECTORIES property based on the current value
  852. // of the same directory property:
  853. this->impl->IncludeDirectories.CopyFromEntries(
  854. this->impl->Makefile->GetIncludeDirectoriesEntries());
  855. {
  856. auto const& sysInc = this->impl->Makefile->GetSystemIncludeDirectories();
  857. this->impl->SystemIncludeDirectories.insert(sysInc.begin(),
  858. sysInc.end());
  859. }
  860. this->impl->CompileOptions.CopyFromEntries(
  861. this->impl->Makefile->GetCompileOptionsEntries());
  862. this->impl->LinkOptions.CopyFromEntries(
  863. this->impl->Makefile->GetLinkOptionsEntries());
  864. this->impl->LinkDirectories.CopyFromEntries(
  865. this->impl->Makefile->GetLinkDirectoriesEntries());
  866. }
  867. // Record current policies for later use.
  868. this->impl->Makefile->RecordPolicies(this->impl->PolicyMap);
  869. std::set<TargetProperty::InitCondition> metConditions;
  870. metConditions.insert(TargetProperty::InitCondition::Always);
  871. if (this->CanCompileSources()) {
  872. metConditions.insert(TargetProperty::InitCondition::CanCompileSources);
  873. }
  874. if (this->GetGlobalGenerator()->IsXcode()) {
  875. metConditions.insert(TargetProperty::InitCondition::NeedsXcode);
  876. if (this->CanCompileSources()) {
  877. metConditions.insert(
  878. TargetProperty::InitCondition::NeedsXcodeAndCanCompileSources);
  879. }
  880. }
  881. if (!this->IsImported()) {
  882. metConditions.insert(TargetProperty::InitCondition::NonImportedTarget);
  883. }
  884. if (this->impl->TargetType != cmStateEnums::UTILITY &&
  885. this->impl->TargetType != cmStateEnums::GLOBAL_TARGET) {
  886. metConditions.insert(TargetProperty::InitCondition::NormalTarget);
  887. if (this->IsNormal()) {
  888. metConditions.insert(
  889. TargetProperty::InitCondition::NormalNonImportedTarget);
  890. }
  891. if (this->impl->TargetType != cmStateEnums::INTERFACE_LIBRARY) {
  892. metConditions.insert(TargetProperty::InitCondition::TargetWithArtifact);
  893. if (this->impl->TargetType != cmStateEnums::EXECUTABLE) {
  894. metConditions.insert(
  895. TargetProperty::InitCondition::NonExecutableWithArtifact);
  896. }
  897. }
  898. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  899. this->impl->TargetType == cmStateEnums::STATIC_LIBRARY) {
  900. metConditions.insert(
  901. TargetProperty::InitCondition::LinkableLibraryTarget);
  902. }
  903. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY) {
  904. metConditions.insert(TargetProperty::InitCondition::SharedLibraryTarget);
  905. }
  906. }
  907. if (this->impl->TargetType == cmStateEnums::EXECUTABLE) {
  908. metConditions.insert(TargetProperty::InitCondition::ExecutableTarget);
  909. }
  910. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  911. this->impl->TargetType == cmStateEnums::EXECUTABLE) {
  912. metConditions.insert(
  913. TargetProperty::InitCondition::TargetWithSymbolExports);
  914. }
  915. if (this->impl->TargetType <= cmStateEnums::GLOBAL_TARGET) {
  916. metConditions.insert(TargetProperty::InitCondition::TargetWithCommands);
  917. }
  918. std::vector<std::string> configNames =
  919. mf->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  920. for (auto& config : configNames) {
  921. config = cmSystemTools::UpperCase(config);
  922. }
  923. std::string defKey;
  924. defKey.reserve(128);
  925. defKey += "CMAKE_";
  926. auto initProperty = [this, mf, &defKey](std::string const& property,
  927. char const* default_value) {
  928. // special init for ENABLE_EXPORTS
  929. // For SHARED_LIBRARY, only CMAKE_SHARED_LIBRARY_ENABLE_EXPORTS variable
  930. // is used
  931. // For EXECUTABLE, CMAKE_EXECUTABLE_ENABLE_EXPORTS or else
  932. // CMAKE_ENABLE_EXPORTS variables are used
  933. if (property == "ENABLE_EXPORTS"_s) {
  934. // Replace everything after "CMAKE_"
  935. defKey.replace(
  936. defKey.begin() + 6, defKey.end(),
  937. cmStrCat(this->impl->TargetType == cmStateEnums::EXECUTABLE
  938. ? "EXECUTABLE"
  939. : "SHARED_LIBRARY",
  940. '_', property));
  941. if (cmValue value = mf->GetDefinition(defKey)) {
  942. this->SetProperty(property, value);
  943. return;
  944. }
  945. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY) {
  946. if (default_value) {
  947. this->SetProperty(property, default_value);
  948. }
  949. return;
  950. }
  951. }
  952. // Imported targets must set AIX_SHARED_LIBRARY_ARCHIVE explicitly.
  953. if (this->IsImported() && property == "AIX_SHARED_LIBRARY_ARCHIVE"_s) {
  954. return;
  955. }
  956. // Replace everything after "CMAKE_"
  957. defKey.replace(defKey.begin() + 6, defKey.end(), property);
  958. if (cmValue value = mf->GetDefinition(defKey)) {
  959. this->SetProperty(property, value);
  960. } else if (default_value) {
  961. this->SetProperty(property, default_value);
  962. }
  963. };
  964. std::string dflt_storage;
  965. for (auto const& tp : StaticTargetProperties) {
  966. // Ignore properties that we have not met the condition for.
  967. if (!metConditions.count(tp.InitConditional)) {
  968. continue;
  969. }
  970. char const* dflt = nullptr;
  971. if (tp.Default) {
  972. dflt_storage = std::string(*tp.Default);
  973. dflt = dflt_storage.c_str();
  974. }
  975. if (tp.Repeat == TargetProperty::Repetition::Once) {
  976. initProperty(std::string(tp.Name), dflt);
  977. } else {
  978. std::string propertyName;
  979. for (auto const& configName : configNames) {
  980. if (tp.Repeat == TargetProperty::Repetition::PerConfig) {
  981. propertyName = cmStrCat(tp.Name, configName);
  982. } else if (tp.Repeat == TargetProperty::Repetition::PerConfigPrefix) {
  983. propertyName = cmStrCat(configName, tp.Name);
  984. }
  985. initProperty(propertyName, dflt);
  986. }
  987. }
  988. }
  989. // Clean up some property defaults.
  990. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  991. this->impl->TargetType == cmStateEnums::MODULE_LIBRARY) {
  992. this->SetProperty("POSITION_INDEPENDENT_CODE", "True");
  993. }
  994. // check for "CMAKE_VS_GLOBALS" variable and set up target properties
  995. // if any
  996. cmValue globals = mf->GetDefinition("CMAKE_VS_GLOBALS");
  997. if (globals) {
  998. std::string const genName = mf->GetGlobalGenerator()->GetName();
  999. if (cmHasLiteralPrefix(genName, "Visual Studio")) {
  1000. cmList props{ *globals };
  1001. std::string const vsGlobal = "VS_GLOBAL_";
  1002. for (std::string const& i : props) {
  1003. // split NAME=VALUE
  1004. std::string::size_type const assignment = i.find('=');
  1005. if (assignment != std::string::npos) {
  1006. std::string const propName = vsGlobal + i.substr(0, assignment);
  1007. std::string const propValue = i.substr(assignment + 1);
  1008. initProperty(propName, propValue.c_str());
  1009. }
  1010. }
  1011. }
  1012. }
  1013. if (!this->IsNormal() || mf->GetPropertyAsBool("SYSTEM")) {
  1014. this->SetProperty("SYSTEM", "ON");
  1015. }
  1016. for (auto const& prop : mf->GetState()->GetPropertyDefinitions().GetMap()) {
  1017. if (prop.first.second == cmProperty::TARGET &&
  1018. !prop.second.GetInitializeFromVariable().empty()) {
  1019. if (auto value =
  1020. mf->GetDefinition(prop.second.GetInitializeFromVariable())) {
  1021. this->SetProperty(prop.first.first, value);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. cmTarget::cmTarget(cmTarget&&) noexcept = default;
  1027. cmTarget::~cmTarget() = default;
  1028. cmTarget& cmTarget::operator=(cmTarget&&) noexcept = default;
  1029. cmStateEnums::TargetType cmTarget::GetType() const
  1030. {
  1031. return this->impl->TargetType;
  1032. }
  1033. void cmTarget::SetOrigin(Origin origin)
  1034. {
  1035. assert(origin != cmTarget::Origin::Unknown);
  1036. assert(this->impl->Origin == cmTarget::Origin::Unknown);
  1037. this->impl->Origin = origin;
  1038. }
  1039. cmTarget::Origin cmTarget::GetOrigin() const
  1040. {
  1041. return this->impl->Origin;
  1042. }
  1043. cmMakefile* cmTarget::GetMakefile() const
  1044. {
  1045. return this->impl->Makefile;
  1046. }
  1047. cmPolicies::PolicyMap const& cmTarget::GetPolicyMap() const
  1048. {
  1049. return this->impl->PolicyMap;
  1050. }
  1051. std::string const& cmTarget::GetName() const
  1052. {
  1053. return this->impl->Name;
  1054. }
  1055. std::string const& cmTarget::GetTemplateName() const
  1056. {
  1057. if (this->impl->TemplateTarget) {
  1058. return this->impl->TemplateTarget->GetTemplateName();
  1059. }
  1060. return this->impl->Name;
  1061. }
  1062. cmPolicies::PolicyStatus cmTarget::GetPolicyStatus(
  1063. cmPolicies::PolicyID policy) const
  1064. {
  1065. return this->impl->PolicyMap.Get(policy);
  1066. }
  1067. cmGlobalGenerator* cmTarget::GetGlobalGenerator() const
  1068. {
  1069. return this->impl->Makefile->GetGlobalGenerator();
  1070. }
  1071. BTs<std::string> const* cmTarget::GetLanguageStandardProperty(
  1072. std::string const& propertyName) const
  1073. {
  1074. auto entry = this->impl->LanguageStandardProperties.find(propertyName);
  1075. if (entry != this->impl->LanguageStandardProperties.end()) {
  1076. return &entry->second;
  1077. }
  1078. return nullptr;
  1079. }
  1080. void cmTarget::SetLanguageStandardProperty(std::string const& lang,
  1081. std::string const& value,
  1082. std::string const& feature)
  1083. {
  1084. cmListFileBacktrace featureBacktrace;
  1085. for (auto const& entry : this->impl->CompileFeatures.Entries) {
  1086. if (entry.Value == feature) {
  1087. featureBacktrace = entry.Backtrace;
  1088. break;
  1089. }
  1090. }
  1091. BTs<std::string>& languageStandardProperty =
  1092. this->impl->LanguageStandardProperties[cmStrCat(lang, "_STANDARD")];
  1093. if (languageStandardProperty.Value != value) {
  1094. languageStandardProperty.Value = value;
  1095. languageStandardProperty.Backtraces.clear();
  1096. }
  1097. languageStandardProperty.Backtraces.emplace_back(featureBacktrace);
  1098. }
  1099. void cmTarget::AddUtility(std::string const& name, bool cross,
  1100. cmMakefile const* mf)
  1101. {
  1102. this->impl->Utilities.insert(BT<std::pair<std::string, bool>>(
  1103. { name, cross }, mf ? mf->GetBacktrace() : cmListFileBacktrace()));
  1104. }
  1105. void cmTarget::AddUtility(BT<std::pair<std::string, bool>> util)
  1106. {
  1107. this->impl->Utilities.emplace(std::move(util));
  1108. }
  1109. void cmTarget::AddCodegenDependency(std::string const& name)
  1110. {
  1111. this->impl->CodegenDependencies.emplace(name);
  1112. }
  1113. std::set<std::string> const& cmTarget::GetCodegenDeps() const
  1114. {
  1115. return this->impl->CodegenDependencies;
  1116. }
  1117. std::set<BT<std::pair<std::string, bool>>> const& cmTarget::GetUtilities()
  1118. const
  1119. {
  1120. return this->impl->Utilities;
  1121. }
  1122. cmListFileBacktrace const& cmTarget::GetBacktrace() const
  1123. {
  1124. return this->impl->Backtrace;
  1125. }
  1126. cmFindPackageStack const& cmTarget::GetFindPackageStack() const
  1127. {
  1128. return this->impl->FindPackageStack;
  1129. }
  1130. bool cmTarget::IsExecutableWithExports() const
  1131. {
  1132. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  1133. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  1134. }
  1135. bool cmTarget::IsSharedLibraryWithExports() const
  1136. {
  1137. return (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  1138. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  1139. }
  1140. bool cmTarget::IsFrameworkOnApple() const
  1141. {
  1142. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1143. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  1144. this->IsApple() && this->GetPropertyAsBool("FRAMEWORK"));
  1145. }
  1146. bool cmTarget::IsArchivedAIXSharedLibrary() const
  1147. {
  1148. if (this->GetType() == cmStateEnums::SHARED_LIBRARY && this->IsAIX()) {
  1149. cmValue value = this->GetProperty("AIX_SHARED_LIBRARY_ARCHIVE");
  1150. if (!value.IsEmpty()) {
  1151. return value.IsOn();
  1152. }
  1153. if (this->IsImported()) {
  1154. return false;
  1155. }
  1156. switch (this->GetPolicyStatusCMP0182()) {
  1157. case cmPolicies::WARN:
  1158. case cmPolicies::OLD:
  1159. // The OLD behavior's default is to disable shared library archives.
  1160. break;
  1161. case cmPolicies::NEW:
  1162. // The NEW behavior's default is to enable shared library archives.
  1163. return true;
  1164. }
  1165. }
  1166. return false;
  1167. }
  1168. bool cmTarget::IsAppBundleOnApple() const
  1169. {
  1170. return (this->GetType() == cmStateEnums::EXECUTABLE && this->IsApple() &&
  1171. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  1172. }
  1173. bool cmTarget::IsAndroidGuiExecutable() const
  1174. {
  1175. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  1176. this->impl->IsAndroid && this->GetPropertyAsBool("ANDROID_GUI"));
  1177. }
  1178. bool cmTarget::HasKnownObjectFileLocation(std::string* reason) const
  1179. {
  1180. return this->GetGlobalGenerator()->HasKnownObjectFileLocation(*this, reason);
  1181. }
  1182. std::vector<cmCustomCommand> const& cmTarget::GetPreBuildCommands() const
  1183. {
  1184. return this->impl->PreBuildCommands;
  1185. }
  1186. void cmTarget::AddPreBuildCommand(cmCustomCommand const& cmd)
  1187. {
  1188. this->impl->PreBuildCommands.push_back(cmd);
  1189. }
  1190. void cmTarget::AddPreBuildCommand(cmCustomCommand&& cmd)
  1191. {
  1192. this->impl->PreBuildCommands.push_back(std::move(cmd));
  1193. }
  1194. std::vector<cmCustomCommand> const& cmTarget::GetPreLinkCommands() const
  1195. {
  1196. return this->impl->PreLinkCommands;
  1197. }
  1198. void cmTarget::AddPreLinkCommand(cmCustomCommand const& cmd)
  1199. {
  1200. this->impl->PreLinkCommands.push_back(cmd);
  1201. }
  1202. void cmTarget::AddPreLinkCommand(cmCustomCommand&& cmd)
  1203. {
  1204. this->impl->PreLinkCommands.push_back(std::move(cmd));
  1205. }
  1206. std::vector<cmCustomCommand> const& cmTarget::GetPostBuildCommands() const
  1207. {
  1208. return this->impl->PostBuildCommands;
  1209. }
  1210. void cmTarget::AddPostBuildCommand(cmCustomCommand const& cmd)
  1211. {
  1212. this->impl->PostBuildCommands.push_back(cmd);
  1213. }
  1214. void cmTarget::AddPostBuildCommand(cmCustomCommand&& cmd)
  1215. {
  1216. this->impl->PostBuildCommands.push_back(std::move(cmd));
  1217. }
  1218. void cmTarget::AddTracedSources(std::vector<std::string> const& srcs)
  1219. {
  1220. if (!srcs.empty()) {
  1221. this->impl->Sources.WriteDirect(this->impl.get(), {},
  1222. cmValue(cmJoin(srcs, ";")),
  1223. UsageRequirementProperty::Action::Append);
  1224. }
  1225. }
  1226. void cmTarget::AddSources(std::vector<std::string> const& srcs)
  1227. {
  1228. std::vector<std::string> srcFiles;
  1229. for (std::string const& filename : srcs) {
  1230. if (!cmGeneratorExpression::StartsWithGeneratorExpression(filename)) {
  1231. this->impl->Makefile->GetOrCreateSource(filename);
  1232. }
  1233. srcFiles.emplace_back(filename);
  1234. }
  1235. this->AddTracedSources(srcFiles);
  1236. }
  1237. struct CreateLocation
  1238. {
  1239. cmMakefile const* Makefile;
  1240. CreateLocation(cmMakefile const* mf)
  1241. : Makefile(mf)
  1242. {
  1243. }
  1244. cmSourceFileLocation operator()(std::string const& filename) const
  1245. {
  1246. return cmSourceFileLocation(this->Makefile, filename);
  1247. }
  1248. };
  1249. struct LocationMatcher
  1250. {
  1251. cmSourceFileLocation const& Needle;
  1252. LocationMatcher(cmSourceFileLocation const& needle)
  1253. : Needle(needle)
  1254. {
  1255. }
  1256. bool operator()(cmSourceFileLocation& loc)
  1257. {
  1258. return loc.Matches(this->Needle);
  1259. }
  1260. };
  1261. struct TargetPropertyEntryFinder
  1262. {
  1263. private:
  1264. cmSourceFileLocation const& Needle;
  1265. public:
  1266. TargetPropertyEntryFinder(cmSourceFileLocation const& needle)
  1267. : Needle(needle)
  1268. {
  1269. }
  1270. bool operator()(BT<std::string> const& entry)
  1271. {
  1272. cmList files{ entry.Value };
  1273. std::vector<cmSourceFileLocation> locations;
  1274. locations.reserve(files.size());
  1275. std::transform(files.begin(), files.end(), std::back_inserter(locations),
  1276. CreateLocation(this->Needle.GetMakefile()));
  1277. return std::find_if(locations.begin(), locations.end(),
  1278. LocationMatcher(this->Needle)) != locations.end();
  1279. }
  1280. };
  1281. cmSourceFile* cmTarget::AddSource(std::string const& src, bool before)
  1282. {
  1283. cmSourceFileLocation sfl(this->impl->Makefile, src,
  1284. cmSourceFileLocationKind::Known);
  1285. auto const& sources = this->impl->Sources.Entries;
  1286. if (std::find_if(sources.begin(), sources.end(),
  1287. TargetPropertyEntryFinder(sfl)) == sources.end()) {
  1288. this->impl->Sources.WriteDirect(
  1289. this->impl.get(), {}, cmValue(src),
  1290. before ? UsageRequirementProperty::Action::Prepend
  1291. : UsageRequirementProperty::Action::Append);
  1292. }
  1293. if (cmGeneratorExpression::Find(src) != std::string::npos) {
  1294. return nullptr;
  1295. }
  1296. return this->impl->Makefile->GetOrCreateSource(
  1297. src, false, cmSourceFileLocationKind::Known);
  1298. }
  1299. void cmTarget::ClearDependencyInformation(cmMakefile& mf) const
  1300. {
  1301. std::string depname = cmStrCat(this->GetName(), "_LIB_DEPENDS");
  1302. mf.RemoveCacheDefinition(depname);
  1303. }
  1304. std::string cmTarget::GetDebugGeneratorExpressions(
  1305. std::string const& value, cmTargetLinkLibraryType llt) const
  1306. {
  1307. if (llt == GENERAL_LibraryType) {
  1308. return value;
  1309. }
  1310. // Get the list of configurations considered to be DEBUG.
  1311. std::vector<std::string> debugConfigs =
  1312. this->impl->Makefile->GetCMakeInstance()->GetDebugConfigs();
  1313. std::string configString = "$<CONFIG:" + debugConfigs[0] + ">";
  1314. if (debugConfigs.size() > 1) {
  1315. for (std::string const& conf : cmMakeRange(debugConfigs).advance(1)) {
  1316. configString += ",$<CONFIG:" + conf + ">";
  1317. }
  1318. configString = "$<OR:" + configString + ">";
  1319. }
  1320. if (llt == OPTIMIZED_LibraryType) {
  1321. configString = "$<NOT:" + configString + ">";
  1322. }
  1323. return "$<" + configString + ":" + value + ">";
  1324. }
  1325. static std::string targetNameGenex(std::string const& lib)
  1326. {
  1327. return "$<TARGET_NAME:" + lib + ">";
  1328. }
  1329. bool cmTarget::PushTLLCommandTrace(TLLSignature signature,
  1330. cmListFileContext const& lfc)
  1331. {
  1332. bool ret = true;
  1333. if (!this->impl->TLLCommands.empty()) {
  1334. if (this->impl->TLLCommands.back().first != signature) {
  1335. ret = false;
  1336. }
  1337. }
  1338. if (this->impl->TLLCommands.empty() ||
  1339. this->impl->TLLCommands.back().second != lfc) {
  1340. this->impl->TLLCommands.emplace_back(signature, lfc);
  1341. }
  1342. return ret;
  1343. }
  1344. void cmTarget::GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const
  1345. {
  1346. char const* sigString =
  1347. (sig == cmTarget::KeywordTLLSignature ? "keyword" : "plain");
  1348. s << "The uses of the " << sigString << " signature are here:\n";
  1349. for (auto const& cmd : this->impl->TLLCommands) {
  1350. if (cmd.first == sig) {
  1351. cmListFileContext lfc = cmd.second;
  1352. lfc.FilePath = cmSystemTools::RelativeIfUnder(
  1353. this->impl->Makefile->GetState()->GetSourceDirectory(), lfc.FilePath);
  1354. s << " * " << lfc << '\n';
  1355. }
  1356. }
  1357. }
  1358. std::string const& cmTarget::GetInstallPath() const
  1359. {
  1360. return this->impl->InstallPath;
  1361. }
  1362. void cmTarget::SetInstallPath(std::string const& name)
  1363. {
  1364. this->impl->InstallPath = name;
  1365. }
  1366. std::string const& cmTarget::GetRuntimeInstallPath() const
  1367. {
  1368. return this->impl->RuntimeInstallPath;
  1369. }
  1370. void cmTarget::SetRuntimeInstallPath(std::string const& name)
  1371. {
  1372. this->impl->RuntimeInstallPath = name;
  1373. }
  1374. bool cmTarget::GetHaveInstallRule() const
  1375. {
  1376. return this->impl->HaveInstallRule;
  1377. }
  1378. void cmTarget::SetHaveInstallRule(bool hir)
  1379. {
  1380. this->impl->HaveInstallRule = hir;
  1381. }
  1382. void cmTarget::AddInstallGenerator(cmInstallTargetGenerator* g)
  1383. {
  1384. this->impl->InstallGenerators.emplace_back(g);
  1385. }
  1386. std::vector<cmInstallTargetGenerator*> const& cmTarget::GetInstallGenerators()
  1387. const
  1388. {
  1389. return this->impl->InstallGenerators;
  1390. }
  1391. bool cmTarget::GetIsGeneratorProvided() const
  1392. {
  1393. return this->impl->IsGeneratorProvided;
  1394. }
  1395. void cmTarget::SetIsGeneratorProvided(bool igp)
  1396. {
  1397. this->impl->IsGeneratorProvided = igp;
  1398. }
  1399. cmTarget::LinkLibraryVectorType const& cmTarget::GetOriginalLinkLibraries()
  1400. const
  1401. {
  1402. return this->impl->OriginalLinkLibraries;
  1403. }
  1404. void cmTarget::AddLinkLibrary(cmMakefile& mf, std::string const& lib,
  1405. cmTargetLinkLibraryType llt)
  1406. {
  1407. cmTarget* tgt = mf.FindTargetToUse(lib);
  1408. {
  1409. bool const isNonImportedTarget = tgt && !tgt->IsImported();
  1410. std::string const libName =
  1411. (isNonImportedTarget && llt != GENERAL_LibraryType)
  1412. ? targetNameGenex(lib)
  1413. : lib;
  1414. this->AppendProperty("LINK_LIBRARIES",
  1415. this->GetDebugGeneratorExpressions(libName, llt),
  1416. mf.GetBacktrace());
  1417. }
  1418. if (cmGeneratorExpression::Find(lib) != std::string::npos ||
  1419. (tgt &&
  1420. (tgt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  1421. tgt->GetType() == cmStateEnums::OBJECT_LIBRARY)) ||
  1422. (this->impl->Name == lib)) {
  1423. return;
  1424. }
  1425. this->impl->OriginalLinkLibraries.emplace_back(lib, llt);
  1426. // Add the explicit dependency information for libraries. This is
  1427. // simply a set of libraries separated by ";". There should always
  1428. // be a trailing ";". These library names are not canonical, in that
  1429. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  1430. // We shouldn't remove duplicates here because external libraries
  1431. // may be purposefully duplicated to handle recursive dependencies,
  1432. // and we removing one instance will break the link line. Duplicates
  1433. // will be appropriately eliminated at emit time.
  1434. if (this->impl->TargetType >= cmStateEnums::STATIC_LIBRARY &&
  1435. this->impl->TargetType <= cmStateEnums::MODULE_LIBRARY &&
  1436. (this->GetPolicyStatusCMP0073() == cmPolicies::OLD ||
  1437. this->GetPolicyStatusCMP0073() == cmPolicies::WARN)) {
  1438. std::string targetEntry = cmStrCat(this->impl->Name, "_LIB_DEPENDS");
  1439. std::string dependencies;
  1440. cmValue old_val = mf.GetDefinition(targetEntry);
  1441. if (old_val) {
  1442. dependencies += *old_val;
  1443. }
  1444. switch (llt) {
  1445. case GENERAL_LibraryType:
  1446. dependencies += "general";
  1447. break;
  1448. case DEBUG_LibraryType:
  1449. dependencies += "debug";
  1450. break;
  1451. case OPTIMIZED_LibraryType:
  1452. dependencies += "optimized";
  1453. break;
  1454. }
  1455. dependencies += ";";
  1456. dependencies += lib;
  1457. dependencies += ";";
  1458. mf.AddCacheDefinition(targetEntry, dependencies,
  1459. "Dependencies for the target", cmStateEnums::STATIC);
  1460. }
  1461. }
  1462. void cmTarget::AddSystemIncludeDirectories(std::set<std::string> const& incs)
  1463. {
  1464. this->impl->SystemIncludeDirectories.insert(incs.begin(), incs.end());
  1465. }
  1466. std::set<std::string> const& cmTarget::GetSystemIncludeDirectories() const
  1467. {
  1468. return this->impl->SystemIncludeDirectories;
  1469. }
  1470. void cmTarget::AddInstallIncludeDirectories(cmTargetExport const& te,
  1471. cmStringRange incs)
  1472. {
  1473. std::copy(
  1474. incs.begin(), incs.end(),
  1475. std::back_inserter(this->impl->InstallIncludeDirectoriesEntries[&te]));
  1476. }
  1477. cmStringRange cmTarget::GetInstallIncludeDirectoriesEntries(
  1478. cmTargetExport const& te) const
  1479. {
  1480. auto i = this->impl->InstallIncludeDirectoriesEntries.find(&te);
  1481. if (i == this->impl->InstallIncludeDirectoriesEntries.end()) {
  1482. decltype(i->second) empty;
  1483. return cmMakeRange(empty);
  1484. }
  1485. return cmMakeRange(i->second);
  1486. }
  1487. cmBTStringRange cmTarget::GetIncludeDirectoriesEntries() const
  1488. {
  1489. return cmMakeRange(this->impl->IncludeDirectories.Entries);
  1490. }
  1491. cmBTStringRange cmTarget::GetCompileOptionsEntries() const
  1492. {
  1493. return cmMakeRange(this->impl->CompileOptions.Entries);
  1494. }
  1495. cmBTStringRange cmTarget::GetCompileFeaturesEntries() const
  1496. {
  1497. return cmMakeRange(this->impl->CompileFeatures.Entries);
  1498. }
  1499. cmBTStringRange cmTarget::GetCompileDefinitionsEntries() const
  1500. {
  1501. return cmMakeRange(this->impl->CompileDefinitions.Entries);
  1502. }
  1503. cmBTStringRange cmTarget::GetPrecompileHeadersEntries() const
  1504. {
  1505. return cmMakeRange(this->impl->PrecompileHeaders.Entries);
  1506. }
  1507. cmBTStringRange cmTarget::GetSourceEntries() const
  1508. {
  1509. return cmMakeRange(this->impl->Sources.Entries);
  1510. }
  1511. cmBTStringRange cmTarget::GetLinkOptionsEntries() const
  1512. {
  1513. return cmMakeRange(this->impl->LinkOptions.Entries);
  1514. }
  1515. cmBTStringRange cmTarget::GetLinkDirectoriesEntries() const
  1516. {
  1517. return cmMakeRange(this->impl->LinkDirectories.Entries);
  1518. }
  1519. cmBTStringRange cmTarget::GetLinkImplementationEntries() const
  1520. {
  1521. return cmMakeRange(this->impl->LinkLibraries.Entries);
  1522. }
  1523. cmBTStringRange cmTarget::GetLinkInterfaceEntries() const
  1524. {
  1525. return cmMakeRange(this->impl->InterfaceLinkLibraries.Entries);
  1526. }
  1527. cmBTStringRange cmTarget::GetLinkInterfaceDirectEntries() const
  1528. {
  1529. return cmMakeRange(this->impl->InterfaceLinkLibrariesDirect.Entries);
  1530. }
  1531. cmBTStringRange cmTarget::GetLinkInterfaceDirectExcludeEntries() const
  1532. {
  1533. return cmMakeRange(this->impl->InterfaceLinkLibrariesDirectExclude.Entries);
  1534. }
  1535. void cmTarget::CopyPolicyStatuses(cmTarget const* tgt)
  1536. {
  1537. // Normal targets cannot be the target of a copy.
  1538. assert(!this->IsNormal());
  1539. // Imported targets cannot be the target of a copy.
  1540. assert(!this->IsImported());
  1541. // Only imported targets can be the source of a copy.
  1542. assert(tgt->IsImported());
  1543. this->impl->PolicyMap = tgt->impl->PolicyMap;
  1544. this->impl->TemplateTarget = tgt;
  1545. }
  1546. void cmTarget::CopyImportedCxxModulesEntries(cmTarget const* tgt)
  1547. {
  1548. // Normal targets cannot be the target of a copy.
  1549. assert(!this->IsNormal());
  1550. // Imported targets cannot be the target of a copy.
  1551. assert(!this->IsImported());
  1552. // Only imported targets can be the source of a copy.
  1553. assert(tgt->IsImported());
  1554. this->impl->IncludeDirectories.Entries.clear();
  1555. this->impl->IncludeDirectories.CopyFromEntries(
  1556. cmMakeRange(tgt->impl->ImportedCxxModulesIncludeDirectories.Entries));
  1557. this->impl->CompileDefinitions.Entries.clear();
  1558. this->impl->CompileDefinitions.CopyFromEntries(
  1559. cmMakeRange(tgt->impl->ImportedCxxModulesCompileDefinitions.Entries));
  1560. this->impl->CompileFeatures.Entries.clear();
  1561. this->impl->CompileFeatures.CopyFromEntries(
  1562. cmMakeRange(tgt->impl->ImportedCxxModulesCompileFeatures.Entries));
  1563. this->impl->CompileOptions.Entries.clear();
  1564. this->impl->CompileOptions.CopyFromEntries(
  1565. cmMakeRange(tgt->impl->ImportedCxxModulesCompileOptions.Entries));
  1566. this->impl->LinkLibraries.Entries.clear();
  1567. this->impl->LinkLibraries.CopyFromEntries(
  1568. cmMakeRange(tgt->impl->ImportedCxxModulesLinkLibraries.Entries));
  1569. // Copy the C++ module fileset entries from `tgt`'s `INTERFACE` to this
  1570. // target's `PRIVATE`.
  1571. this->impl->CxxModulesFileSets.SelfEntries.Entries.clear();
  1572. this->impl->CxxModulesFileSets.SelfEntries.Entries =
  1573. tgt->impl->CxxModulesFileSets.InterfaceEntries.Entries;
  1574. }
  1575. void cmTarget::CopyImportedCxxModulesProperties(cmTarget const* tgt)
  1576. {
  1577. // Normal targets cannot be the target of a copy.
  1578. assert(!this->IsNormal());
  1579. // Imported targets cannot be the target of a copy.
  1580. assert(!this->IsImported());
  1581. // Only imported targets can be the source of a copy.
  1582. assert(tgt->IsImported());
  1583. // The list of properties that are relevant here include:
  1584. // - compilation-specific properties for any language or platform
  1585. // - compilation-specific properties for C++
  1586. // - build graph-specific properties that affect compilation
  1587. // - IDE metadata properties
  1588. // - static analysis properties
  1589. static std::string const propertiesToCopy[] = {
  1590. // Compilation properties
  1591. "DEFINE_SYMBOL",
  1592. "DEPRECATION",
  1593. "NO_SYSTEM_FROM_IMPORTED",
  1594. "POSITION_INDEPENDENT_CODE",
  1595. "VISIBILITY_INLINES_HIDDEN",
  1596. // -- Platforms
  1597. // ---- Android
  1598. "ANDROID_API",
  1599. "ANDROID_API_MIN",
  1600. "ANDROID_ARCH",
  1601. "ANDROID_STL_TYPE",
  1602. // ---- macOS
  1603. "OSX_ARCHITECTURES",
  1604. // ---- Windows
  1605. "MSVC_DEBUG_INFORMATION_FORMAT",
  1606. "MSVC_RUNTIME_CHECKS",
  1607. "MSVC_RUNTIME_LIBRARY",
  1608. "VS_PLATFORM_TOOLSET",
  1609. // ---- OpenWatcom
  1610. "WATCOM_RUNTIME_LIBRARY",
  1611. // -- Language
  1612. // ---- C++
  1613. "CXX_COMPILER_LAUNCHER",
  1614. "CXX_STANDARD",
  1615. "CXX_STANDARD_REQUIRED",
  1616. "CXX_EXTENSIONS",
  1617. "CXX_VISIBILITY_PRESET",
  1618. "CXX_MODULE_STD",
  1619. // Static analysis
  1620. "CXX_CLANG_TIDY",
  1621. "CXX_CLANG_TIDY_EXPORT_FIXES_DIR",
  1622. "CXX_CPPLINT",
  1623. "CXX_CPPCHECK",
  1624. "CXX_ICSTAT",
  1625. "CXX_INCLUDE_WHAT_YOU_USE",
  1626. "SKIP_LINTING",
  1627. // Build graph properties
  1628. "EXCLUDE_FROM_ALL",
  1629. "EXCLUDE_FROM_DEFAULT_BUILD",
  1630. "OPTIMIZE_DEPENDENCIES",
  1631. // -- Ninja
  1632. "JOB_POOL_COMPILE",
  1633. // -- Visual Studio
  1634. "VS_NO_COMPILE_BATCHING",
  1635. "VS_PROJECT_IMPORT",
  1636. // Metadata
  1637. "EchoString",
  1638. "EXPORT_COMPILE_COMMANDS",
  1639. // Do *not* copy this property; it should be re-initialized at synthesis
  1640. // time from the `CMAKE_EXPORT_BUILD_DATABASE` variable as `IMPORTED`
  1641. // targets ignore the property initialization.
  1642. // "EXPORT_BUILD_DATABASE",
  1643. "FOLDER",
  1644. "LABELS",
  1645. "PROJECT_LABEL",
  1646. "SYSTEM",
  1647. };
  1648. auto copyProperty = [this, tgt](std::string const& prop) -> cmValue {
  1649. cmValue value = tgt->GetProperty(prop);
  1650. // Always set the property; it may have been explicitly unset.
  1651. this->SetProperty(prop, value);
  1652. return value;
  1653. };
  1654. for (auto const& prop : propertiesToCopy) {
  1655. copyProperty(prop);
  1656. }
  1657. static cm::static_string_view const perConfigPropertiesToCopy[] = {
  1658. "EXCLUDE_FROM_DEFAULT_BUILD_"_s,
  1659. "IMPORTED_CXX_MODULES_"_s,
  1660. "MAP_IMPORTED_CONFIG_"_s,
  1661. "OSX_ARCHITECTURES_"_s,
  1662. };
  1663. std::vector<std::string> configNames =
  1664. this->impl->Makefile->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  1665. for (std::string const& configName : configNames) {
  1666. std::string configUpper = cmSystemTools::UpperCase(configName);
  1667. for (auto const& perConfigProp : perConfigPropertiesToCopy) {
  1668. copyProperty(cmStrCat(perConfigProp, configUpper));
  1669. }
  1670. }
  1671. if (this->GetGlobalGenerator()->IsXcode()) {
  1672. cmValue xcodeGenerateScheme = copyProperty("XCODE_GENERATE_SCHEME");
  1673. // TODO: Make sure these show up on the imported target in the first place
  1674. // XCODE_ATTRIBUTE_???
  1675. if (xcodeGenerateScheme.IsOn()) {
  1676. #ifdef __APPLE__
  1677. static std::string const xcodeSchemePropertiesToCopy[] = {
  1678. // FIXME: Do all of these apply? Do they matter?
  1679. "XCODE_SCHEME_ADDRESS_SANITIZER",
  1680. "XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN",
  1681. "XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER",
  1682. "XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS",
  1683. "XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE",
  1684. "XCODE_SCHEME_ENABLE_GPU_API_VALIDATION",
  1685. "XCODE_SCHEME_ENABLE_GPU_SHADER_VALIDATION",
  1686. "XCODE_SCHEME_GUARD_MALLOC",
  1687. "XCODE_SCHEME_LAUNCH_CONFIGURATION",
  1688. "XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP",
  1689. "XCODE_SCHEME_MALLOC_GUARD_EDGES",
  1690. "XCODE_SCHEME_MALLOC_SCRIBBLE",
  1691. "XCODE_SCHEME_MALLOC_STACK",
  1692. "XCODE_SCHEME_THREAD_SANITIZER",
  1693. "XCODE_SCHEME_THREAD_SANITIZER_STOP",
  1694. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER",
  1695. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP",
  1696. "XCODE_SCHEME_ZOMBIE_OBJECTS",
  1697. };
  1698. for (auto const& xcodeProperty : xcodeSchemePropertiesToCopy) {
  1699. copyProperty(xcodeProperty);
  1700. }
  1701. #endif
  1702. }
  1703. }
  1704. }
  1705. cmBTStringRange cmTarget::GetHeaderSetsEntries() const
  1706. {
  1707. return cmMakeRange(this->impl->HeadersFileSets.SelfEntries.Entries);
  1708. }
  1709. cmBTStringRange cmTarget::GetCxxModuleSetsEntries() const
  1710. {
  1711. return cmMakeRange(this->impl->CxxModulesFileSets.SelfEntries.Entries);
  1712. }
  1713. cmBTStringRange cmTarget::GetInterfaceHeaderSetsEntries() const
  1714. {
  1715. return cmMakeRange(this->impl->HeadersFileSets.InterfaceEntries.Entries);
  1716. }
  1717. cmBTStringRange cmTarget::GetInterfaceCxxModuleSetsEntries() const
  1718. {
  1719. return cmMakeRange(this->impl->CxxModulesFileSets.InterfaceEntries.Entries);
  1720. }
  1721. namespace {
  1722. #define MAKE_PROP(PROP) const std::string prop##PROP = #PROP
  1723. MAKE_PROP(C_STANDARD);
  1724. MAKE_PROP(CXX_STANDARD);
  1725. MAKE_PROP(CUDA_STANDARD);
  1726. MAKE_PROP(HIP_STANDARD);
  1727. MAKE_PROP(OBJC_STANDARD);
  1728. MAKE_PROP(OBJCXX_STANDARD);
  1729. MAKE_PROP(COMPILE_DEFINITIONS);
  1730. MAKE_PROP(COMPILE_FEATURES);
  1731. MAKE_PROP(COMPILE_OPTIONS);
  1732. MAKE_PROP(PRECOMPILE_HEADERS);
  1733. MAKE_PROP(CUDA_CUBIN_COMPILATION);
  1734. MAKE_PROP(CUDA_FATBIN_COMPILATION);
  1735. MAKE_PROP(CUDA_OPTIX_COMPILATION);
  1736. MAKE_PROP(CUDA_PTX_COMPILATION);
  1737. MAKE_PROP(IMPORTED);
  1738. MAKE_PROP(IMPORTED_GLOBAL);
  1739. MAKE_PROP(INCLUDE_DIRECTORIES);
  1740. MAKE_PROP(LINK_OPTIONS);
  1741. MAKE_PROP(IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES);
  1742. MAKE_PROP(IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS);
  1743. MAKE_PROP(IMPORTED_CXX_MODULES_COMPILE_FEATURES);
  1744. MAKE_PROP(IMPORTED_CXX_MODULES_COMPILE_OPTIONS);
  1745. MAKE_PROP(IMPORTED_CXX_MODULES_LINK_LIBRARIES);
  1746. MAKE_PROP(LINK_DIRECTORIES);
  1747. MAKE_PROP(LINK_LIBRARIES);
  1748. MAKE_PROP(MANUALLY_ADDED_DEPENDENCIES);
  1749. MAKE_PROP(NAME);
  1750. MAKE_PROP(SOURCES);
  1751. MAKE_PROP(TYPE);
  1752. MAKE_PROP(BINARY_DIR);
  1753. MAKE_PROP(SOURCE_DIR);
  1754. MAKE_PROP(FALSE);
  1755. MAKE_PROP(TRUE);
  1756. MAKE_PROP(INTERFACE_LINK_LIBRARIES);
  1757. MAKE_PROP(INTERFACE_LINK_LIBRARIES_DIRECT);
  1758. MAKE_PROP(INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE);
  1759. #undef MAKE_PROP
  1760. }
  1761. namespace {
  1762. enum class ReadOnlyCondition
  1763. {
  1764. All,
  1765. Imported,
  1766. NonImported,
  1767. };
  1768. struct ReadOnlyProperty
  1769. {
  1770. ReadOnlyProperty(ReadOnlyCondition cond)
  1771. : Condition{ cond }
  1772. {
  1773. }
  1774. ReadOnlyProperty(ReadOnlyCondition cond, cmPolicies::PolicyID id)
  1775. : Condition{ cond }
  1776. , Policy{ id }
  1777. {
  1778. }
  1779. ReadOnlyCondition Condition;
  1780. cm::optional<cmPolicies::PolicyID> Policy;
  1781. std::string message(std::string const& prop, cmTarget* target) const
  1782. {
  1783. std::string msg;
  1784. if (this->Condition == ReadOnlyCondition::All) {
  1785. msg = " property is read-only for target(\"";
  1786. } else if (this->Condition == ReadOnlyCondition::Imported) {
  1787. msg = " property can't be set on imported targets(\"";
  1788. } else if (this->Condition == ReadOnlyCondition::NonImported) {
  1789. msg = " property can't be set on non-imported targets(\"";
  1790. }
  1791. return cmStrCat(prop, msg, target->GetName(), "\")\n");
  1792. }
  1793. bool isReadOnly(std::string const& prop, cmMakefile* context,
  1794. cmTarget* target) const
  1795. {
  1796. auto importedTarget = target->IsImported();
  1797. bool matchingCondition = true;
  1798. if ((!importedTarget && this->Condition == ReadOnlyCondition::Imported) ||
  1799. (importedTarget &&
  1800. this->Condition == ReadOnlyCondition::NonImported)) {
  1801. matchingCondition = false;
  1802. }
  1803. if (!matchingCondition) {
  1804. // Not read-only in this scenario
  1805. return false;
  1806. }
  1807. bool readOnly = true;
  1808. if (!this->Policy) {
  1809. // No policy associated, so is always read-only
  1810. context->IssueMessage(MessageType::FATAL_ERROR,
  1811. this->message(prop, target));
  1812. } else {
  1813. switch (target->GetPolicyStatus(*this->Policy)) {
  1814. case cmPolicies::WARN:
  1815. context->IssueMessage(
  1816. MessageType::AUTHOR_WARNING,
  1817. cmPolicies::GetPolicyWarning(cmPolicies::CMP0160) + "\n" +
  1818. this->message(prop, target));
  1819. CM_FALLTHROUGH;
  1820. case cmPolicies::OLD:
  1821. readOnly = false;
  1822. break;
  1823. case cmPolicies::NEW:
  1824. context->IssueMessage(MessageType::FATAL_ERROR,
  1825. this->message(prop, target));
  1826. break;
  1827. }
  1828. }
  1829. return readOnly;
  1830. }
  1831. };
  1832. bool IsSettableProperty(cmMakefile* context, cmTarget* target,
  1833. std::string const& prop)
  1834. {
  1835. using ROC = ReadOnlyCondition;
  1836. static std::unordered_map<std::string, ReadOnlyProperty> const readOnlyProps{
  1837. { "EXPORT_NAME", { ROC::Imported } },
  1838. { "HEADER_SETS", { ROC::All } },
  1839. { "IMPORTED_GLOBAL", { ROC::NonImported } },
  1840. { "INTERFACE_HEADER_SETS", { ROC::All } },
  1841. { "MANUALLY_ADDED_DEPENDENCIES", { ROC::All } },
  1842. { "NAME", { ROC::All } },
  1843. { "SOURCES", { ROC::Imported } },
  1844. { "TYPE", { ROC::All } },
  1845. { "ALIAS_GLOBAL", { ROC::All, cmPolicies::CMP0160 } },
  1846. { "BINARY_DIR", { ROC::All, cmPolicies::CMP0160 } },
  1847. { "CXX_MODULE_SETS", { ROC::All, cmPolicies::CMP0160 } },
  1848. { "IMPORTED", { ROC::All, cmPolicies::CMP0160 } },
  1849. { "INTERFACE_CXX_MODULE_SETS", { ROC::All, cmPolicies::CMP0160 } },
  1850. { "LOCATION", { ROC::All, cmPolicies::CMP0160 } },
  1851. { "LOCATION_CONFIG", { ROC::All, cmPolicies::CMP0160 } },
  1852. { "SOURCE_DIR", { ROC::All, cmPolicies::CMP0160 } }
  1853. };
  1854. auto it = readOnlyProps.find(prop);
  1855. if (it != readOnlyProps.end()) {
  1856. return !(it->second.isReadOnly(prop, context, target));
  1857. }
  1858. return true;
  1859. }
  1860. }
  1861. void cmTarget::SetProperty(std::string const& prop, cmValue value)
  1862. {
  1863. if (!IsSettableProperty(this->impl->Makefile, this, prop)) {
  1864. return;
  1865. }
  1866. UsageRequirementProperty* usageRequirements[] = {
  1867. &this->impl->IncludeDirectories,
  1868. &this->impl->CompileOptions,
  1869. &this->impl->CompileFeatures,
  1870. &this->impl->CompileDefinitions,
  1871. &this->impl->PrecompileHeaders,
  1872. &this->impl->Sources,
  1873. &this->impl->LinkOptions,
  1874. &this->impl->LinkDirectories,
  1875. &this->impl->LinkLibraries,
  1876. &this->impl->InterfaceLinkLibraries,
  1877. &this->impl->InterfaceLinkLibrariesDirect,
  1878. &this->impl->InterfaceLinkLibrariesDirectExclude,
  1879. &this->impl->ImportedCxxModulesIncludeDirectories,
  1880. &this->impl->ImportedCxxModulesCompileDefinitions,
  1881. &this->impl->ImportedCxxModulesCompileFeatures,
  1882. &this->impl->ImportedCxxModulesCompileOptions,
  1883. &this->impl->ImportedCxxModulesLinkLibraries,
  1884. };
  1885. for (auto* usageRequirement : usageRequirements) {
  1886. if (usageRequirement->Write(this->impl.get(), {}, prop, value,
  1887. UsageRequirementProperty::Action::Set)) {
  1888. return;
  1889. }
  1890. }
  1891. FileSetType* fileSetTypes[] = {
  1892. &this->impl->HeadersFileSets,
  1893. &this->impl->CxxModulesFileSets,
  1894. };
  1895. for (auto* fileSetType : fileSetTypes) {
  1896. if (fileSetType->WriteProperties(this, this->impl.get(), prop, value,
  1897. FileSetType::Action::Set)) {
  1898. return;
  1899. }
  1900. }
  1901. if (prop == propIMPORTED_GLOBAL) {
  1902. if (!value.IsOn()) {
  1903. std::ostringstream e;
  1904. e << "IMPORTED_GLOBAL property can't be set to FALSE on targets (\""
  1905. << this->impl->Name << "\")\n";
  1906. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1907. return;
  1908. }
  1909. /* no need to change anything if value does not change */
  1910. if (!this->IsImportedGloballyVisible()) {
  1911. this->impl->TargetVisibility = Visibility::ImportedGlobally;
  1912. this->GetGlobalGenerator()->IndexTarget(this);
  1913. }
  1914. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME") &&
  1915. !this->impl->CheckImportedLibName(
  1916. prop,
  1917. value ? value
  1918. : std::string{})) { // NOLINT(bugprone-branch-clone)
  1919. /* error was reported by check method */
  1920. } else if (prop == propCUDA_CUBIN_COMPILATION ||
  1921. prop == propCUDA_FATBIN_COMPILATION ||
  1922. prop == propCUDA_OPTIX_COMPILATION ||
  1923. prop == propCUDA_PTX_COMPILATION) {
  1924. auto const& compiler =
  1925. this->impl->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  1926. auto const& compilerVersion =
  1927. this->impl->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_VERSION");
  1928. if (this->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1929. auto e =
  1930. cmStrCat(prop, " property can only be applied to OBJECT targets(",
  1931. this->impl->Name, ")\n");
  1932. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  1933. return;
  1934. }
  1935. bool const flag_found =
  1936. (prop == propCUDA_PTX_COMPILATION &&
  1937. this->impl->Makefile->GetDefinition("_CMAKE_CUDA_PTX_FLAG")) ||
  1938. (prop == propCUDA_CUBIN_COMPILATION &&
  1939. this->impl->Makefile->GetDefinition("_CMAKE_CUDA_CUBIN_FLAG")) ||
  1940. (prop == propCUDA_FATBIN_COMPILATION &&
  1941. this->impl->Makefile->GetDefinition("_CMAKE_CUDA_FATBIN_FLAG")) ||
  1942. (prop == propCUDA_OPTIX_COMPILATION &&
  1943. this->impl->Makefile->GetDefinition("_CMAKE_CUDA_OPTIX_FLAG"));
  1944. if (flag_found) {
  1945. this->impl->Properties.SetProperty(prop, value);
  1946. } else {
  1947. auto e = cmStrCat(prop, " property is not supported by ", compiler,
  1948. " compiler version ", compilerVersion, '.');
  1949. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  1950. return;
  1951. }
  1952. } else if (prop == propC_STANDARD || prop == propCXX_STANDARD ||
  1953. prop == propCUDA_STANDARD || prop == propHIP_STANDARD ||
  1954. prop == propOBJC_STANDARD || prop == propOBJCXX_STANDARD) {
  1955. if (value) {
  1956. this->impl->LanguageStandardProperties[prop] =
  1957. BTs<std::string>(value, this->impl->Makefile->GetBacktrace());
  1958. } else {
  1959. this->impl->LanguageStandardProperties.erase(prop);
  1960. }
  1961. } else {
  1962. this->impl->Properties.SetProperty(prop, value);
  1963. }
  1964. }
  1965. void cmTarget::AppendProperty(std::string const& prop,
  1966. std::string const& value,
  1967. cm::optional<cmListFileBacktrace> const& bt,
  1968. bool asString)
  1969. {
  1970. if (!IsSettableProperty(this->impl->Makefile, this, prop)) {
  1971. return;
  1972. }
  1973. if (prop == "IMPORTED_GLOBAL") {
  1974. this->impl->Makefile->IssueMessage(
  1975. MessageType::FATAL_ERROR,
  1976. cmStrCat("IMPORTED_GLOBAL property can't be appended, only set on "
  1977. "imported targets (\"",
  1978. this->impl->Name, "\")\n"));
  1979. }
  1980. UsageRequirementProperty* usageRequirements[] = {
  1981. &this->impl->IncludeDirectories,
  1982. &this->impl->CompileOptions,
  1983. &this->impl->CompileFeatures,
  1984. &this->impl->CompileDefinitions,
  1985. &this->impl->PrecompileHeaders,
  1986. &this->impl->Sources,
  1987. &this->impl->LinkOptions,
  1988. &this->impl->LinkDirectories,
  1989. &this->impl->LinkLibraries,
  1990. &this->impl->InterfaceLinkLibraries,
  1991. &this->impl->InterfaceLinkLibrariesDirect,
  1992. &this->impl->InterfaceLinkLibrariesDirectExclude,
  1993. &this->impl->ImportedCxxModulesIncludeDirectories,
  1994. &this->impl->ImportedCxxModulesCompileDefinitions,
  1995. &this->impl->ImportedCxxModulesCompileFeatures,
  1996. &this->impl->ImportedCxxModulesCompileOptions,
  1997. &this->impl->ImportedCxxModulesLinkLibraries,
  1998. };
  1999. for (auto* usageRequirement : usageRequirements) {
  2000. if (usageRequirement->Write(this->impl.get(), bt, prop, cmValue(value),
  2001. UsageRequirementProperty::Action::Append)) {
  2002. return;
  2003. }
  2004. }
  2005. FileSetType* fileSetTypes[] = {
  2006. &this->impl->HeadersFileSets,
  2007. &this->impl->CxxModulesFileSets,
  2008. };
  2009. for (auto* fileSetType : fileSetTypes) {
  2010. if (fileSetType->WriteProperties(this, this->impl.get(), prop, value,
  2011. FileSetType::Action::Append)) {
  2012. return;
  2013. }
  2014. }
  2015. if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME")) {
  2016. this->impl->Makefile->IssueMessage(
  2017. MessageType::FATAL_ERROR, prop + " property may not be APPENDed.");
  2018. } else if (prop == "C_STANDARD" || prop == "CXX_STANDARD" ||
  2019. prop == "CUDA_STANDARD" || prop == "HIP_STANDARD" ||
  2020. prop == "OBJC_STANDARD" || prop == "OBJCXX_STANDARD") {
  2021. this->impl->Makefile->IssueMessage(
  2022. MessageType::FATAL_ERROR, prop + " property may not be appended.");
  2023. } else {
  2024. this->impl->Properties.AppendProperty(prop, value, asString);
  2025. }
  2026. }
  2027. template <typename ValueType>
  2028. void cmTargetInternals::AddDirectoryToFileSet(cmTarget* self,
  2029. std::string const& fileSetName,
  2030. ValueType value,
  2031. cm::string_view fileSetType,
  2032. cm::string_view description,
  2033. FileSetType::Action action)
  2034. {
  2035. auto* fileSet = self->GetFileSet(fileSetName);
  2036. if (!fileSet) {
  2037. this->Makefile->IssueMessage(
  2038. MessageType::FATAL_ERROR,
  2039. cmStrCat(description, "has not yet been created."));
  2040. return;
  2041. }
  2042. if (fileSet->GetType() != fileSetType) {
  2043. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2044. cmStrCat("File set \"", fileSetName,
  2045. "\" is not of type \"", fileSetType,
  2046. "\"."));
  2047. return;
  2048. }
  2049. if (action == FileSetType::Action::Set) {
  2050. fileSet->ClearDirectoryEntries();
  2051. }
  2052. if (cmNonempty(value)) {
  2053. fileSet->AddDirectoryEntry(
  2054. BT<std::string>(value, this->Makefile->GetBacktrace()));
  2055. }
  2056. }
  2057. template <typename ValueType>
  2058. void cmTargetInternals::AddPathToFileSet(cmTarget* self,
  2059. std::string const& fileSetName,
  2060. ValueType value,
  2061. cm::string_view fileSetType,
  2062. cm::string_view description,
  2063. FileSetType::Action action)
  2064. {
  2065. auto* fileSet = self->GetFileSet(fileSetName);
  2066. if (!fileSet) {
  2067. this->Makefile->IssueMessage(
  2068. MessageType::FATAL_ERROR,
  2069. cmStrCat(description, "has not yet been created."));
  2070. return;
  2071. }
  2072. if (fileSet->GetType() != fileSetType) {
  2073. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2074. cmStrCat("File set \"", fileSetName,
  2075. "\" is not of type \"", fileSetType,
  2076. "\"."));
  2077. return;
  2078. }
  2079. if (action == FileSetType::Action::Set) {
  2080. fileSet->ClearFileEntries();
  2081. }
  2082. if (cmNonempty(value)) {
  2083. fileSet->AddFileEntry(
  2084. BT<std::string>(value, this->Makefile->GetBacktrace()));
  2085. }
  2086. }
  2087. cmValue cmTargetInternals::GetFileSetDirectories(
  2088. cmTarget const* self, std::string const& fileSetName,
  2089. cm::string_view fileSetType) const
  2090. {
  2091. auto const* fileSet = self->GetFileSet(fileSetName);
  2092. if (!fileSet) {
  2093. return nullptr;
  2094. }
  2095. if (fileSet->GetType() != fileSetType) {
  2096. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2097. cmStrCat("File set \"", fileSetName,
  2098. "\" is not of type \"", fileSetType,
  2099. "\"."));
  2100. return nullptr;
  2101. }
  2102. static std::string output;
  2103. output = cmList::to_string(fileSet->GetDirectoryEntries());
  2104. return cmValue(output);
  2105. }
  2106. cmValue cmTargetInternals::GetFileSetPaths(cmTarget const* self,
  2107. std::string const& fileSetName,
  2108. cm::string_view fileSetType) const
  2109. {
  2110. auto const* fileSet = self->GetFileSet(fileSetName);
  2111. if (!fileSet) {
  2112. return nullptr;
  2113. }
  2114. if (fileSet->GetType() != fileSetType) {
  2115. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2116. cmStrCat("File set \"", fileSetName,
  2117. "\" is not of type \"", fileSetType,
  2118. "\"."));
  2119. return nullptr;
  2120. }
  2121. static std::string output;
  2122. output = cmList::to_string(fileSet->GetFileEntries());
  2123. return cmValue(output);
  2124. }
  2125. void cmTarget::AppendBuildInterfaceIncludes()
  2126. {
  2127. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2128. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  2129. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2130. this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  2131. !this->IsExecutableWithExports()) {
  2132. return;
  2133. }
  2134. if (this->impl->BuildInterfaceIncludesAppended) {
  2135. return;
  2136. }
  2137. this->impl->BuildInterfaceIncludesAppended = true;
  2138. if (this->impl->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE")) {
  2139. std::string dirs = this->impl->Makefile->GetCurrentBinaryDirectory();
  2140. if (!dirs.empty()) {
  2141. dirs += ';';
  2142. }
  2143. dirs += this->impl->Makefile->GetCurrentSourceDirectory();
  2144. if (!dirs.empty()) {
  2145. this->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
  2146. ("$<BUILD_INTERFACE:" + dirs + ">"));
  2147. }
  2148. }
  2149. }
  2150. namespace {
  2151. bool CheckLinkLibraryPattern(UsageRequirementProperty const& usage,
  2152. cmake* context)
  2153. {
  2154. // Look for <LINK_LIBRARY:> and </LINK_LIBRARY:> internal tags
  2155. static cmsys::RegularExpression linkPattern(
  2156. "(^|;)(</?LINK_(LIBRARY|GROUP):[^;>]*>)(;|$)");
  2157. bool isValid = true;
  2158. for (auto const& item : usage.Entries) {
  2159. if (!linkPattern.find(item.Value)) {
  2160. continue;
  2161. }
  2162. isValid = false;
  2163. // Report an error.
  2164. context->IssueMessage(
  2165. MessageType::FATAL_ERROR,
  2166. cmStrCat(
  2167. "Property ", usage.Name, " contains the invalid item \"",
  2168. linkPattern.match(2), "\". The ", usage.Name,
  2169. " property may contain the generator-expression \"$<LINK_",
  2170. linkPattern.match(3),
  2171. ":...>\" which may be used to specify how the libraries are linked."),
  2172. item.Backtrace);
  2173. }
  2174. return isValid;
  2175. }
  2176. }
  2177. void cmTarget::FinalizeTargetConfiguration(cmBTStringRange compileDefinitions)
  2178. {
  2179. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2180. return;
  2181. }
  2182. if (!CheckLinkLibraryPattern(this->impl->LinkLibraries,
  2183. this->GetMakefile()->GetCMakeInstance()) ||
  2184. !CheckLinkLibraryPattern(this->impl->InterfaceLinkLibraries,
  2185. this->GetMakefile()->GetCMakeInstance()) ||
  2186. !CheckLinkLibraryPattern(this->impl->InterfaceLinkLibrariesDirect,
  2187. this->GetMakefile()->GetCMakeInstance())) {
  2188. return;
  2189. }
  2190. this->AppendBuildInterfaceIncludes();
  2191. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2192. return;
  2193. }
  2194. for (auto const& def : compileDefinitions) {
  2195. this->InsertCompileDefinition(def);
  2196. }
  2197. }
  2198. void cmTarget::InsertInclude(BT<std::string> const& entry, bool before)
  2199. {
  2200. this->impl->IncludeDirectories.WriteDirect(
  2201. entry,
  2202. before ? UsageRequirementProperty::Action::Prepend
  2203. : UsageRequirementProperty::Action::Append);
  2204. }
  2205. void cmTarget::InsertCompileOption(BT<std::string> const& entry, bool before)
  2206. {
  2207. this->impl->CompileOptions.WriteDirect(
  2208. entry,
  2209. before ? UsageRequirementProperty::Action::Prepend
  2210. : UsageRequirementProperty::Action::Append);
  2211. }
  2212. void cmTarget::InsertCompileDefinition(BT<std::string> const& entry)
  2213. {
  2214. this->impl->CompileDefinitions.WriteDirect(
  2215. entry, UsageRequirementProperty::Action::Append);
  2216. }
  2217. void cmTarget::InsertLinkOption(BT<std::string> const& entry, bool before)
  2218. {
  2219. this->impl->LinkOptions.WriteDirect(
  2220. entry,
  2221. before ? UsageRequirementProperty::Action::Prepend
  2222. : UsageRequirementProperty::Action::Append);
  2223. }
  2224. void cmTarget::InsertLinkDirectory(BT<std::string> const& entry, bool before)
  2225. {
  2226. this->impl->LinkDirectories.WriteDirect(
  2227. entry,
  2228. before ? UsageRequirementProperty::Action::Prepend
  2229. : UsageRequirementProperty::Action::Append);
  2230. }
  2231. void cmTarget::InsertPrecompileHeader(BT<std::string> const& entry)
  2232. {
  2233. this->impl->PrecompileHeaders.WriteDirect(
  2234. entry, UsageRequirementProperty::Action::Append);
  2235. }
  2236. namespace {
  2237. void CheckLINK_INTERFACE_LIBRARIES(std::string const& prop,
  2238. std::string const& value,
  2239. cmMakefile* context, bool imported)
  2240. {
  2241. // Support imported and non-imported versions of the property.
  2242. char const* base = (imported ? "IMPORTED_LINK_INTERFACE_LIBRARIES"
  2243. : "LINK_INTERFACE_LIBRARIES");
  2244. // Look for link-type keywords in the value.
  2245. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  2246. if (keys.find(value)) {
  2247. // Report an error.
  2248. std::ostringstream e;
  2249. e << "Property " << prop << " may not contain link-type keyword \""
  2250. << keys.match(2) << "\". "
  2251. << "The " << base << " property has a per-configuration "
  2252. << "version called " << base << "_<CONFIG> which may be "
  2253. << "used to specify per-configuration rules.";
  2254. if (!imported) {
  2255. e << " "
  2256. << "Alternatively, an IMPORTED library may be created, configured "
  2257. << "with a per-configuration location, and then named in the "
  2258. << "property value. "
  2259. << "See the add_library command's IMPORTED mode for details."
  2260. << "\n"
  2261. << "If you have a list of libraries that already contains the "
  2262. << "keyword, use the target_link_libraries command with its "
  2263. << "LINK_INTERFACE_LIBRARIES mode to set the property. "
  2264. << "The command automatically recognizes link-type keywords and sets "
  2265. << "the LINK_INTERFACE_LIBRARIES and LINK_INTERFACE_LIBRARIES_DEBUG "
  2266. << "properties accordingly.";
  2267. }
  2268. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  2269. }
  2270. }
  2271. void CheckINTERFACE_LINK_LIBRARIES(std::string const& value,
  2272. cmMakefile* context)
  2273. {
  2274. // Look for link-type keywords in the value.
  2275. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  2276. if (keys.find(value)) {
  2277. // Report an error.
  2278. std::ostringstream e;
  2279. e << "Property INTERFACE_LINK_LIBRARIES may not contain link-type "
  2280. "keyword \""
  2281. << keys.match(2)
  2282. << "\". The INTERFACE_LINK_LIBRARIES "
  2283. "property may contain configuration-sensitive generator-expressions "
  2284. "which may be used to specify per-configuration rules.";
  2285. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  2286. }
  2287. }
  2288. void CheckIMPORTED_GLOBAL(cmTarget const* target, cmMakefile* context)
  2289. {
  2290. auto const& targets = context->GetOwnedImportedTargets();
  2291. auto it =
  2292. std::find_if(targets.begin(), targets.end(),
  2293. [&](std::unique_ptr<cmTarget> const& importTarget) -> bool {
  2294. return target == importTarget.get();
  2295. });
  2296. if (it == targets.end()) {
  2297. std::ostringstream e;
  2298. e << "Attempt to promote imported target \"" << target->GetName()
  2299. << "\" to global scope (by setting IMPORTED_GLOBAL) "
  2300. "which is not built in this directory.";
  2301. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  2302. }
  2303. }
  2304. }
  2305. void cmTarget::CheckProperty(std::string const& prop,
  2306. cmMakefile* context) const
  2307. {
  2308. // Certain properties need checking.
  2309. if (cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES")) {
  2310. if (cmValue value = this->GetProperty(prop)) {
  2311. CheckLINK_INTERFACE_LIBRARIES(prop, *value, context, false);
  2312. }
  2313. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES")) {
  2314. if (cmValue value = this->GetProperty(prop)) {
  2315. CheckLINK_INTERFACE_LIBRARIES(prop, *value, context, true);
  2316. }
  2317. } else if (prop == "INTERFACE_LINK_LIBRARIES") {
  2318. if (cmValue value = this->GetProperty(prop)) {
  2319. CheckINTERFACE_LINK_LIBRARIES(*value, context);
  2320. }
  2321. } else if (prop == "IMPORTED_GLOBAL") {
  2322. if (this->IsImported()) {
  2323. CheckIMPORTED_GLOBAL(this, context);
  2324. }
  2325. }
  2326. }
  2327. cmValue cmTarget::GetComputedProperty(std::string const& prop,
  2328. cmMakefile& mf) const
  2329. {
  2330. return cmTargetPropertyComputer::GetProperty(this, prop, mf);
  2331. }
  2332. cmValue cmTarget::GetProperty(std::string const& prop) const
  2333. {
  2334. static std::unordered_set<std::string> const specialProps{
  2335. propC_STANDARD,
  2336. propCXX_STANDARD,
  2337. propCUDA_STANDARD,
  2338. propHIP_STANDARD,
  2339. propOBJC_STANDARD,
  2340. propOBJCXX_STANDARD,
  2341. propLINK_LIBRARIES,
  2342. propTYPE,
  2343. propINCLUDE_DIRECTORIES,
  2344. propCOMPILE_FEATURES,
  2345. propCOMPILE_OPTIONS,
  2346. propCOMPILE_DEFINITIONS,
  2347. propPRECOMPILE_HEADERS,
  2348. propLINK_OPTIONS,
  2349. propLINK_DIRECTORIES,
  2350. propIMPORTED,
  2351. propIMPORTED_GLOBAL,
  2352. propMANUALLY_ADDED_DEPENDENCIES,
  2353. propNAME,
  2354. propBINARY_DIR,
  2355. propSOURCE_DIR,
  2356. propSOURCES,
  2357. propINTERFACE_LINK_LIBRARIES,
  2358. propINTERFACE_LINK_LIBRARIES_DIRECT,
  2359. propINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  2360. propIMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES,
  2361. propIMPORTED_CXX_MODULES_COMPILE_DEFINITIONS,
  2362. propIMPORTED_CXX_MODULES_COMPILE_FEATURES,
  2363. propIMPORTED_CXX_MODULES_COMPILE_OPTIONS,
  2364. propIMPORTED_CXX_MODULES_LINK_LIBRARIES,
  2365. };
  2366. if (specialProps.count(prop)) {
  2367. if (prop == propC_STANDARD || prop == propCXX_STANDARD ||
  2368. prop == propCUDA_STANDARD || prop == propHIP_STANDARD ||
  2369. prop == propOBJC_STANDARD || prop == propOBJCXX_STANDARD) {
  2370. auto propertyIter = this->impl->LanguageStandardProperties.find(prop);
  2371. if (propertyIter == this->impl->LanguageStandardProperties.end()) {
  2372. return nullptr;
  2373. }
  2374. return cmValue(propertyIter->second.Value);
  2375. }
  2376. UsageRequirementProperty const* usageRequirements[] = {
  2377. &this->impl->IncludeDirectories,
  2378. &this->impl->CompileOptions,
  2379. &this->impl->CompileFeatures,
  2380. &this->impl->CompileDefinitions,
  2381. &this->impl->PrecompileHeaders,
  2382. &this->impl->Sources,
  2383. &this->impl->LinkOptions,
  2384. &this->impl->LinkDirectories,
  2385. &this->impl->LinkLibraries,
  2386. &this->impl->InterfaceLinkLibraries,
  2387. &this->impl->InterfaceLinkLibrariesDirect,
  2388. &this->impl->InterfaceLinkLibrariesDirectExclude,
  2389. &this->impl->ImportedCxxModulesIncludeDirectories,
  2390. &this->impl->ImportedCxxModulesCompileDefinitions,
  2391. &this->impl->ImportedCxxModulesCompileFeatures,
  2392. &this->impl->ImportedCxxModulesCompileOptions,
  2393. &this->impl->ImportedCxxModulesLinkLibraries,
  2394. };
  2395. for (auto const* usageRequirement : usageRequirements) {
  2396. auto value = usageRequirement->Read(prop);
  2397. if (value.first) {
  2398. return value.second;
  2399. }
  2400. }
  2401. // the type property returns what type the target is
  2402. if (prop == propTYPE) {
  2403. return cmValue(cmState::GetTargetTypeName(this->GetType()));
  2404. }
  2405. if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
  2406. if (this->impl->Utilities.empty()) {
  2407. return nullptr;
  2408. }
  2409. static std::string output;
  2410. static std::vector<std::string> utilities;
  2411. utilities.resize(this->impl->Utilities.size());
  2412. std::transform(
  2413. this->impl->Utilities.cbegin(), this->impl->Utilities.cend(),
  2414. utilities.begin(),
  2415. [](const BT<std::pair<std::string, bool>>& item) -> std::string {
  2416. return item.Value.first;
  2417. });
  2418. output = cmList::to_string(utilities);
  2419. return cmValue(output);
  2420. }
  2421. if (prop == propIMPORTED) {
  2422. return this->IsImported() ? cmValue(propTRUE) : cmValue(propFALSE);
  2423. }
  2424. if (prop == propIMPORTED_GLOBAL) {
  2425. return this->IsImportedGloballyVisible() ? cmValue(propTRUE)
  2426. : cmValue(propFALSE);
  2427. }
  2428. if (prop == propNAME) {
  2429. return cmValue(this->GetName());
  2430. }
  2431. if (prop == propBINARY_DIR) {
  2432. return cmValue(this->impl->Makefile->GetStateSnapshot()
  2433. .GetDirectory()
  2434. .GetCurrentBinary());
  2435. }
  2436. if (prop == propSOURCE_DIR) {
  2437. return cmValue(this->impl->Makefile->GetStateSnapshot()
  2438. .GetDirectory()
  2439. .GetCurrentSource());
  2440. }
  2441. }
  2442. // Check fileset properties.
  2443. {
  2444. FileSetType* fileSetTypes[] = {
  2445. &this->impl->HeadersFileSets,
  2446. &this->impl->CxxModulesFileSets,
  2447. };
  2448. for (auto* fileSetType : fileSetTypes) {
  2449. auto value = fileSetType->ReadProperties(this, this->impl.get(), prop);
  2450. if (value.first) {
  2451. return value.second;
  2452. }
  2453. }
  2454. }
  2455. cmValue retVal = this->impl->Properties.GetPropertyValue(prop);
  2456. if (!retVal) {
  2457. bool const chain = this->impl->Makefile->GetState()->IsPropertyChained(
  2458. prop, cmProperty::TARGET);
  2459. if (chain) {
  2460. return this->impl->Makefile->GetStateSnapshot()
  2461. .GetDirectory()
  2462. .GetProperty(prop, chain);
  2463. }
  2464. return nullptr;
  2465. }
  2466. return retVal;
  2467. }
  2468. std::string const& cmTarget::GetSafeProperty(std::string const& prop) const
  2469. {
  2470. cmValue ret = this->GetProperty(prop);
  2471. if (ret) {
  2472. return *ret;
  2473. }
  2474. static std::string const s_empty;
  2475. return s_empty;
  2476. }
  2477. bool cmTarget::GetPropertyAsBool(std::string const& prop) const
  2478. {
  2479. return this->GetProperty(prop).IsOn();
  2480. }
  2481. cmPropertyMap const& cmTarget::GetProperties() const
  2482. {
  2483. return this->impl->Properties;
  2484. }
  2485. bool cmTarget::IsDLLPlatform() const
  2486. {
  2487. return this->impl->IsDLLPlatform;
  2488. }
  2489. bool cmTarget::IsAIX() const
  2490. {
  2491. return this->impl->IsAIX;
  2492. }
  2493. bool cmTarget::IsApple() const
  2494. {
  2495. return this->impl->IsApple;
  2496. }
  2497. bool cmTarget::IsNormal() const
  2498. {
  2499. switch (this->impl->TargetVisibility) {
  2500. case Visibility::Normal:
  2501. return true;
  2502. case Visibility::Generated:
  2503. case Visibility::Imported:
  2504. case Visibility::ImportedGlobally:
  2505. case Visibility::Foreign:
  2506. return false;
  2507. }
  2508. assert(false && "unknown visibility (IsNormal)");
  2509. return false;
  2510. }
  2511. bool cmTarget::IsSynthetic() const
  2512. {
  2513. switch (this->impl->TargetVisibility) {
  2514. case Visibility::Generated:
  2515. return true;
  2516. case Visibility::Normal:
  2517. case Visibility::Imported:
  2518. case Visibility::ImportedGlobally:
  2519. case Visibility::Foreign:
  2520. return false;
  2521. }
  2522. assert(false && "unknown visibility (IsSynthetic)");
  2523. return false;
  2524. }
  2525. bool cmTargetInternals::IsImported() const
  2526. {
  2527. switch (this->TargetVisibility) {
  2528. case cmTarget::Visibility::Imported:
  2529. case cmTarget::Visibility::ImportedGlobally:
  2530. case cmTarget::Visibility::Foreign:
  2531. return true;
  2532. case cmTarget::Visibility::Normal:
  2533. case cmTarget::Visibility::Generated:
  2534. return false;
  2535. }
  2536. assert(false && "unknown visibility (IsImported)");
  2537. return false;
  2538. }
  2539. bool cmTarget::IsImported() const
  2540. {
  2541. return this->impl->IsImported();
  2542. }
  2543. bool cmTarget::IsImportedGloballyVisible() const
  2544. {
  2545. switch (this->impl->TargetVisibility) {
  2546. case Visibility::ImportedGlobally:
  2547. return true;
  2548. case Visibility::Normal:
  2549. case Visibility::Generated:
  2550. case Visibility::Imported:
  2551. case Visibility::Foreign:
  2552. return false;
  2553. }
  2554. assert(false && "unknown visibility (IsImportedGloballyVisible)");
  2555. return false;
  2556. }
  2557. bool cmTarget::IsForeign() const
  2558. {
  2559. switch (this->impl->TargetVisibility) {
  2560. case Visibility::Foreign:
  2561. return true;
  2562. case Visibility::Normal:
  2563. case Visibility::Generated:
  2564. case Visibility::Imported:
  2565. case Visibility::ImportedGlobally:
  2566. return false;
  2567. }
  2568. assert(false && "unknown visibility (isForeign)");
  2569. return false;
  2570. }
  2571. bool cmTarget::IsPerConfig() const
  2572. {
  2573. return this->impl->PerConfig;
  2574. }
  2575. bool cmTarget::IsRuntimeBinary() const
  2576. {
  2577. switch (this->GetType()) {
  2578. case cmStateEnums::EXECUTABLE:
  2579. case cmStateEnums::SHARED_LIBRARY:
  2580. case cmStateEnums::MODULE_LIBRARY:
  2581. return true;
  2582. case cmStateEnums::OBJECT_LIBRARY:
  2583. case cmStateEnums::STATIC_LIBRARY:
  2584. case cmStateEnums::UTILITY:
  2585. case cmStateEnums::INTERFACE_LIBRARY:
  2586. case cmStateEnums::GLOBAL_TARGET:
  2587. case cmStateEnums::UNKNOWN_LIBRARY:
  2588. break;
  2589. }
  2590. return false;
  2591. }
  2592. bool cmTarget::CanCompileSources() const
  2593. {
  2594. if (this->IsImported()) {
  2595. return false;
  2596. }
  2597. if (this->IsSynthetic()) {
  2598. return true;
  2599. }
  2600. switch (this->GetType()) {
  2601. case cmStateEnums::EXECUTABLE:
  2602. case cmStateEnums::STATIC_LIBRARY:
  2603. case cmStateEnums::SHARED_LIBRARY:
  2604. case cmStateEnums::MODULE_LIBRARY:
  2605. case cmStateEnums::OBJECT_LIBRARY:
  2606. return true;
  2607. case cmStateEnums::UTILITY:
  2608. case cmStateEnums::INTERFACE_LIBRARY:
  2609. case cmStateEnums::GLOBAL_TARGET:
  2610. case cmStateEnums::UNKNOWN_LIBRARY:
  2611. break;
  2612. }
  2613. return false;
  2614. }
  2615. char const* cmTarget::GetSuffixVariableInternal(
  2616. cmStateEnums::ArtifactType artifact) const
  2617. {
  2618. switch (this->GetType()) {
  2619. case cmStateEnums::STATIC_LIBRARY:
  2620. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  2621. case cmStateEnums::SHARED_LIBRARY:
  2622. switch (artifact) {
  2623. case cmStateEnums::RuntimeBinaryArtifact:
  2624. return this->IsArchivedAIXSharedLibrary()
  2625. ? "CMAKE_SHARED_LIBRARY_ARCHIVE_SUFFIX"
  2626. : "CMAKE_SHARED_LIBRARY_SUFFIX";
  2627. case cmStateEnums::ImportLibraryArtifact:
  2628. return this->IsApple() ? "CMAKE_APPLE_IMPORT_FILE_SUFFIX"
  2629. : "CMAKE_IMPORT_LIBRARY_SUFFIX";
  2630. }
  2631. break;
  2632. case cmStateEnums::MODULE_LIBRARY:
  2633. switch (artifact) {
  2634. case cmStateEnums::RuntimeBinaryArtifact:
  2635. return "CMAKE_SHARED_MODULE_SUFFIX";
  2636. case cmStateEnums::ImportLibraryArtifact:
  2637. return "CMAKE_IMPORT_LIBRARY_SUFFIX";
  2638. }
  2639. break;
  2640. case cmStateEnums::EXECUTABLE:
  2641. switch (artifact) {
  2642. case cmStateEnums::RuntimeBinaryArtifact:
  2643. // Android GUI application packages store the native
  2644. // binary as a shared library.
  2645. return (this->IsAndroidGuiExecutable()
  2646. ? "CMAKE_SHARED_LIBRARY_SUFFIX"
  2647. : "CMAKE_EXECUTABLE_SUFFIX");
  2648. case cmStateEnums::ImportLibraryArtifact:
  2649. return (this->impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_SUFFIX"
  2650. : "CMAKE_IMPORT_LIBRARY_SUFFIX");
  2651. }
  2652. break;
  2653. default:
  2654. break;
  2655. }
  2656. return "";
  2657. }
  2658. char const* cmTarget::GetPrefixVariableInternal(
  2659. cmStateEnums::ArtifactType artifact) const
  2660. {
  2661. switch (this->GetType()) {
  2662. case cmStateEnums::STATIC_LIBRARY:
  2663. return "CMAKE_STATIC_LIBRARY_PREFIX";
  2664. case cmStateEnums::SHARED_LIBRARY:
  2665. switch (artifact) {
  2666. case cmStateEnums::RuntimeBinaryArtifact:
  2667. return "CMAKE_SHARED_LIBRARY_PREFIX";
  2668. case cmStateEnums::ImportLibraryArtifact:
  2669. return this->IsApple() ? "CMAKE_APPLE_IMPORT_FILE_PREFIX"
  2670. : "CMAKE_IMPORT_LIBRARY_PREFIX";
  2671. }
  2672. break;
  2673. case cmStateEnums::MODULE_LIBRARY:
  2674. switch (artifact) {
  2675. case cmStateEnums::RuntimeBinaryArtifact:
  2676. return "CMAKE_SHARED_MODULE_PREFIX";
  2677. case cmStateEnums::ImportLibraryArtifact:
  2678. return "CMAKE_IMPORT_LIBRARY_PREFIX";
  2679. }
  2680. break;
  2681. case cmStateEnums::EXECUTABLE:
  2682. switch (artifact) {
  2683. case cmStateEnums::RuntimeBinaryArtifact:
  2684. // Android GUI application packages store the native
  2685. // binary as a shared library.
  2686. return (this->IsAndroidGuiExecutable()
  2687. ? "CMAKE_SHARED_LIBRARY_PREFIX"
  2688. : "");
  2689. case cmStateEnums::ImportLibraryArtifact:
  2690. return (this->impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_PREFIX"
  2691. : "CMAKE_IMPORT_LIBRARY_PREFIX");
  2692. }
  2693. break;
  2694. default:
  2695. break;
  2696. }
  2697. return "";
  2698. }
  2699. std::string cmTarget::ImportedGetFullPath(
  2700. std::string const& config, cmStateEnums::ArtifactType artifact) const
  2701. {
  2702. assert(this->IsImported());
  2703. // Lookup/compute/cache the import information for this
  2704. // configuration.
  2705. std::string desired_config = config;
  2706. if (config.empty()) {
  2707. desired_config = "NOCONFIG";
  2708. }
  2709. std::string result;
  2710. cmValue loc = nullptr;
  2711. cmValue imp = nullptr;
  2712. std::string suffix;
  2713. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  2714. this->GetMappedConfig(desired_config, loc, imp, suffix)) {
  2715. switch (artifact) {
  2716. case cmStateEnums::RuntimeBinaryArtifact:
  2717. if (loc) {
  2718. result = *loc;
  2719. } else if (imp) {
  2720. result = *imp;
  2721. } else {
  2722. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  2723. if (cmValue config_location = this->GetProperty(impProp)) {
  2724. result = *config_location;
  2725. } else if (cmValue location =
  2726. this->GetProperty("IMPORTED_LOCATION")) {
  2727. result = *location;
  2728. }
  2729. if (result.empty() &&
  2730. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2731. this->IsExecutableWithExports())) {
  2732. impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  2733. if (cmValue config_implib = this->GetProperty(impProp)) {
  2734. result = *config_implib;
  2735. } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
  2736. result = *implib;
  2737. }
  2738. }
  2739. }
  2740. if (this->IsApple() &&
  2741. (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  2742. this->impl->TargetType == cmStateEnums::STATIC_LIBRARY ||
  2743. this->impl->TargetType == cmStateEnums::UNKNOWN_LIBRARY) &&
  2744. cmSystemTools::IsPathToXcFramework(result)) {
  2745. auto plist = cmParseXcFrameworkPlist(result, *this->impl->Makefile,
  2746. this->impl->Backtrace);
  2747. if (!plist) {
  2748. return "";
  2749. }
  2750. auto const* library = plist->SelectSuitableLibrary(
  2751. *this->impl->Makefile, this->impl->Backtrace);
  2752. if (library) {
  2753. result = cmStrCat(result, '/', library->LibraryIdentifier, '/',
  2754. library->LibraryPath);
  2755. } else {
  2756. return "";
  2757. }
  2758. }
  2759. break;
  2760. case cmStateEnums::ImportLibraryArtifact:
  2761. if (imp) {
  2762. result = *imp;
  2763. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2764. this->IsExecutableWithExports()) {
  2765. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  2766. if (cmValue config_implib = this->GetProperty(impProp)) {
  2767. result = *config_implib;
  2768. } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
  2769. result = *implib;
  2770. }
  2771. }
  2772. break;
  2773. }
  2774. }
  2775. if (result.empty()) {
  2776. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2777. auto message = [&]() -> std::string {
  2778. std::string unset;
  2779. std::string configuration;
  2780. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  2781. artifact == cmStateEnums::RuntimeBinaryArtifact) {
  2782. unset = "IMPORTED_LOCATION or IMPORTED_IMPLIB";
  2783. } else if (artifact == cmStateEnums::RuntimeBinaryArtifact) {
  2784. unset = "IMPORTED_LOCATION";
  2785. } else if (artifact == cmStateEnums::ImportLibraryArtifact) {
  2786. unset = "IMPORTED_IMPLIB";
  2787. }
  2788. if (!config.empty()) {
  2789. configuration = cmStrCat(" configuration \"", config, '"');
  2790. }
  2791. return cmStrCat(unset, " not set for imported target \"",
  2792. this->GetName(), '"', configuration, '.');
  2793. };
  2794. switch (this->GetPolicyStatus(cmPolicies::CMP0111)) {
  2795. case cmPolicies::WARN:
  2796. this->impl->Makefile->IssueMessage(
  2797. MessageType::AUTHOR_WARNING,
  2798. cmPolicies::GetPolicyWarning(cmPolicies::CMP0111) + "\n" +
  2799. message());
  2800. CM_FALLTHROUGH;
  2801. case cmPolicies::OLD:
  2802. break;
  2803. default:
  2804. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2805. message());
  2806. }
  2807. }
  2808. result = cmStrCat(this->GetName(), "-NOTFOUND");
  2809. }
  2810. return result;
  2811. }
  2812. cmFileSet const* cmTarget::GetFileSet(std::string const& name) const
  2813. {
  2814. auto it = this->impl->FileSets.find(name);
  2815. return it == this->impl->FileSets.end() ? nullptr : &it->second;
  2816. }
  2817. cmFileSet* cmTarget::GetFileSet(std::string const& name)
  2818. {
  2819. auto it = this->impl->FileSets.find(name);
  2820. return it == this->impl->FileSets.end() ? nullptr : &it->second;
  2821. }
  2822. std::pair<cmFileSet*, bool> cmTarget::GetOrCreateFileSet(
  2823. std::string const& name, std::string const& type, cmFileSetVisibility vis)
  2824. {
  2825. auto result = this->impl->FileSets.emplace(
  2826. name,
  2827. cmFileSet(*this->GetMakefile()->GetCMakeInstance(), name, type, vis));
  2828. if (result.second) {
  2829. auto bt = this->impl->Makefile->GetBacktrace();
  2830. if (type == this->impl->HeadersFileSets.TypeName) {
  2831. this->impl->HeadersFileSets.AddFileSet(name, vis, std::move(bt));
  2832. } else if (type == this->impl->CxxModulesFileSets.TypeName) {
  2833. this->impl->CxxModulesFileSets.AddFileSet(name, vis, std::move(bt));
  2834. }
  2835. }
  2836. return std::make_pair(&result.first->second, result.second);
  2837. }
  2838. std::string cmTarget::GetFileSetsPropertyName(std::string const& type)
  2839. {
  2840. if (type == "HEADERS") {
  2841. return "HEADER_SETS";
  2842. }
  2843. if (type == "CXX_MODULES") {
  2844. return "CXX_MODULE_SETS";
  2845. }
  2846. return "";
  2847. }
  2848. std::string cmTarget::GetInterfaceFileSetsPropertyName(std::string const& type)
  2849. {
  2850. if (type == "HEADERS") {
  2851. return "INTERFACE_HEADER_SETS";
  2852. }
  2853. if (type == "CXX_MODULES") {
  2854. return "INTERFACE_CXX_MODULE_SETS";
  2855. }
  2856. return "";
  2857. }
  2858. std::vector<std::string> cmTarget::GetAllFileSetNames() const
  2859. {
  2860. std::vector<std::string> result;
  2861. for (auto const& it : this->impl->FileSets) {
  2862. result.push_back(it.first);
  2863. }
  2864. return result;
  2865. }
  2866. std::vector<std::string> cmTarget::GetAllInterfaceFileSets() const
  2867. {
  2868. std::vector<std::string> result;
  2869. auto inserter = std::back_inserter(result);
  2870. auto appendEntries = [=](std::vector<BT<std::string>> const& entries) {
  2871. for (auto const& entry : entries) {
  2872. cmList expanded{ entry.Value };
  2873. std::copy(expanded.begin(), expanded.end(), inserter);
  2874. }
  2875. };
  2876. appendEntries(this->impl->HeadersFileSets.InterfaceEntries.Entries);
  2877. appendEntries(this->impl->CxxModulesFileSets.InterfaceEntries.Entries);
  2878. return result;
  2879. }
  2880. bool cmTarget::HasFileSets() const
  2881. {
  2882. return !this->impl->FileSets.empty();
  2883. }
  2884. bool cmTargetInternals::CheckImportedLibName(std::string const& prop,
  2885. std::string const& value) const
  2886. {
  2887. if (this->TargetType != cmStateEnums::INTERFACE_LIBRARY ||
  2888. !this->IsImported()) {
  2889. this->Makefile->IssueMessage(
  2890. MessageType::FATAL_ERROR,
  2891. prop +
  2892. " property may be set only on imported INTERFACE library targets.");
  2893. return false;
  2894. }
  2895. if (!value.empty()) {
  2896. if (value[0] == '-') {
  2897. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2898. prop + " property value\n " + value +
  2899. "\nmay not start with '-'.");
  2900. return false;
  2901. }
  2902. std::string::size_type bad = value.find_first_of(":/\\;");
  2903. if (bad != std::string::npos) {
  2904. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2905. prop + " property value\n " + value +
  2906. "\nmay not contain '" +
  2907. value.substr(bad, 1) + "'.");
  2908. return false;
  2909. }
  2910. }
  2911. return true;
  2912. }
  2913. bool cmTarget::GetMappedConfig(std::string const& desiredConfig, cmValue& loc,
  2914. cmValue& imp, std::string& suffix) const
  2915. {
  2916. switch (this->GetPolicyStatusCMP0200()) {
  2917. case cmPolicies::WARN:
  2918. if (this->GetMakefile()->PolicyOptionalWarningEnabled(
  2919. "CMAKE_POLICY_WARNING_CMP0200")) {
  2920. break;
  2921. }
  2922. CM_FALLTHROUGH;
  2923. case cmPolicies::OLD:
  2924. return this->GetMappedConfigOld(desiredConfig, loc, imp, suffix);
  2925. case cmPolicies::NEW:
  2926. return this->GetMappedConfigNew(desiredConfig, loc, imp, suffix);
  2927. }
  2928. cmValue newLoc;
  2929. cmValue newImp;
  2930. std::string newSuffix;
  2931. bool const newResult =
  2932. this->GetMappedConfigNew(desiredConfig, newLoc, newImp, newSuffix);
  2933. if (!this->GetMappedConfigOld(desiredConfig, loc, imp, suffix)) {
  2934. if (newResult) {
  2935. // NEW policy found a configuration, OLD did not.
  2936. auto newConfig = cm::string_view{ newSuffix }.substr(1);
  2937. std::string const err = cmStrCat(
  2938. cmPolicies::GetPolicyWarning(cmPolicies::CMP0200),
  2939. "\nConfiguration selection for imported target \"", this->GetName(),
  2940. "\" failed, but would select configuration \"", newConfig,
  2941. "\" under the NEW policy.\n");
  2942. this->GetMakefile()->IssueMessage(MessageType::AUTHOR_WARNING, err);
  2943. }
  2944. return false;
  2945. }
  2946. auto oldConfig = cm::string_view{ suffix }.substr(1);
  2947. if (!newResult) {
  2948. // NEW policy did not find a configuration, OLD did.
  2949. std::string const err =
  2950. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0200),
  2951. "\nConfiguration selection for imported target \"",
  2952. this->GetName(), "\" selected configuration \"", oldConfig,
  2953. "\", but would fail under the NEW policy.\n");
  2954. this->GetMakefile()->IssueMessage(MessageType::AUTHOR_WARNING, err);
  2955. } else if (suffix != newSuffix) {
  2956. // OLD and NEW policies found different configurations.
  2957. auto newConfig = cm::string_view{ newSuffix }.substr(1);
  2958. std::string const err =
  2959. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0200),
  2960. "\nConfiguration selection for imported target \"",
  2961. this->GetName(), "\" selected configuration \"", oldConfig,
  2962. "\", but would select configuration \"", newConfig,
  2963. "\" under the NEW policy.\n");
  2964. this->GetMakefile()->IssueMessage(MessageType::AUTHOR_WARNING, err);
  2965. }
  2966. return true;
  2967. }
  2968. bool cmTarget::GetMappedConfigOld(std::string const& desired_config,
  2969. cmValue& loc, cmValue& imp,
  2970. std::string& suffix) const
  2971. {
  2972. std::string config_upper;
  2973. if (!desired_config.empty()) {
  2974. config_upper = cmSystemTools::UpperCase(desired_config);
  2975. }
  2976. std::string locPropBase;
  2977. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2978. locPropBase = "IMPORTED_LIBNAME";
  2979. } else if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2980. locPropBase = "IMPORTED_OBJECTS";
  2981. } else {
  2982. locPropBase = "IMPORTED_LOCATION";
  2983. }
  2984. // Track the configuration-specific property suffix.
  2985. suffix = cmStrCat('_', config_upper);
  2986. cmList mappedConfigs;
  2987. {
  2988. std::string mapProp = cmStrCat("MAP_IMPORTED_CONFIG_", config_upper);
  2989. if (cmValue mapValue = this->GetProperty(mapProp)) {
  2990. mappedConfigs.assign(*mapValue, cmList::EmptyElements::Yes);
  2991. }
  2992. }
  2993. // If we needed to find one of the mapped configurations but did not
  2994. // There may be only IMPORTED_IMPLIB for a shared library or an executable
  2995. // with exports.
  2996. bool allowImp = (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2997. this->IsExecutableWithExports()) ||
  2998. (this->IsAIX() && this->IsExecutableWithExports()) ||
  2999. (this->GetMakefile()->PlatformSupportsAppleTextStubs() &&
  3000. this->IsSharedLibraryWithExports());
  3001. // If a mapping was found, check its configurations.
  3002. for (auto mci = mappedConfigs.begin();
  3003. !loc && !imp && mci != mappedConfigs.end(); ++mci) {
  3004. // Look for this configuration.
  3005. if (mci->empty()) {
  3006. // An empty string in the mapping has a special meaning:
  3007. // look up the config-less properties.
  3008. loc = this->GetProperty(locPropBase);
  3009. if (allowImp) {
  3010. imp = this->GetProperty("IMPORTED_IMPLIB");
  3011. }
  3012. // If it was found, set the suffix.
  3013. if (loc || imp) {
  3014. suffix.clear();
  3015. }
  3016. } else {
  3017. std::string mcUpper = cmSystemTools::UpperCase(*mci);
  3018. std::string locProp = cmStrCat(locPropBase, '_', mcUpper);
  3019. loc = this->GetProperty(locProp);
  3020. if (allowImp) {
  3021. std::string impProp = cmStrCat("IMPORTED_IMPLIB_", mcUpper);
  3022. imp = this->GetProperty(impProp);
  3023. }
  3024. // If it was found, use it for all properties below.
  3025. if (loc || imp) {
  3026. suffix = cmStrCat('_', mcUpper);
  3027. }
  3028. }
  3029. }
  3030. // If we needed to find one of the mapped configurations but did not
  3031. // then the target location is not found. The project does not want
  3032. // any other configuration.
  3033. if (!mappedConfigs.empty() && !loc && !imp) {
  3034. // Interface libraries are always available because their
  3035. // library name is optional so it is okay to leave loc empty.
  3036. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  3037. }
  3038. // If we have not yet found it then there are no mapped
  3039. // configurations. Look for an exact-match.
  3040. if (!loc && !imp) {
  3041. std::string locProp = cmStrCat(locPropBase, suffix);
  3042. loc = this->GetProperty(locProp);
  3043. if (allowImp) {
  3044. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  3045. imp = this->GetProperty(impProp);
  3046. }
  3047. }
  3048. // If we have not yet found it then there are no mapped
  3049. // configurations and no exact match.
  3050. if (!loc && !imp) {
  3051. // The suffix computed above is not useful.
  3052. suffix.clear();
  3053. // Look for a configuration-less location. This may be set by
  3054. // manually-written code.
  3055. loc = this->GetProperty(locPropBase);
  3056. if (allowImp) {
  3057. imp = this->GetProperty("IMPORTED_IMPLIB");
  3058. }
  3059. }
  3060. // If we have not yet found it then the project is willing to try
  3061. // any available configuration.
  3062. if (!loc && !imp) {
  3063. cmList availableConfigs;
  3064. if (cmValue iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS")) {
  3065. availableConfigs.assign(*iconfigs);
  3066. }
  3067. for (auto it = availableConfigs.begin();
  3068. !loc && !imp && it != availableConfigs.end(); ++it) {
  3069. suffix = cmStrCat('_', cmSystemTools::UpperCase(*it));
  3070. std::string locProp = cmStrCat(locPropBase, suffix);
  3071. loc = this->GetProperty(locProp);
  3072. if (allowImp) {
  3073. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  3074. imp = this->GetProperty(impProp);
  3075. }
  3076. }
  3077. }
  3078. // If we have not yet found it then the target location is not available.
  3079. if (!loc && !imp) {
  3080. // Interface libraries are always available because their
  3081. // library name is optional so it is okay to leave loc empty.
  3082. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  3083. }
  3084. return true;
  3085. }
  3086. cmValue cmTarget::GetLocation(std::string const& base,
  3087. std::string const& suffix) const
  3088. {
  3089. cmValue value = this->GetProperty(cmStrCat(base, suffix));
  3090. if (value || suffix.empty()) {
  3091. return value;
  3092. }
  3093. return this->GetProperty(base);
  3094. }
  3095. bool cmTarget::GetLocation(std::string const& config, cmValue& loc,
  3096. cmValue& imp, std::string& suffix) const
  3097. {
  3098. suffix = (config.empty() ? std::string{} : cmStrCat('_', config));
  3099. // There may be only IMPORTED_IMPLIB for a shared library or an executable
  3100. // with exports.
  3101. bool const allowImp = (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3102. this->IsExecutableWithExports()) ||
  3103. (this->IsAIX() && this->IsExecutableWithExports()) ||
  3104. (this->GetMakefile()->PlatformSupportsAppleTextStubs() &&
  3105. this->IsSharedLibraryWithExports());
  3106. if (allowImp) {
  3107. imp = this->GetLocation("IMPORTED_IMPLIB", suffix);
  3108. }
  3109. switch (this->GetType()) {
  3110. case cmStateEnums::INTERFACE_LIBRARY:
  3111. loc = this->GetLocation("IMPORTED_LIBNAME", suffix);
  3112. break;
  3113. case cmStateEnums::OBJECT_LIBRARY:
  3114. loc = this->GetLocation("IMPORTED_OBJECTS", suffix);
  3115. break;
  3116. default:
  3117. loc = this->GetLocation("IMPORTED_LOCATION", suffix);
  3118. break;
  3119. }
  3120. return loc || imp || (this->GetType() == cmStateEnums::INTERFACE_LIBRARY);
  3121. }
  3122. bool cmTarget::GetMappedConfigNew(std::string desiredConfig, cmValue& loc,
  3123. cmValue& imp, std::string& suffix) const
  3124. {
  3125. desiredConfig = cmSystemTools::UpperCase(desiredConfig);
  3126. // Get configuration mapping, if present.
  3127. cmList mappedConfigs;
  3128. if (!desiredConfig.empty()) {
  3129. std::string mapProp = cmStrCat("MAP_IMPORTED_CONFIG_", desiredConfig);
  3130. if (cmValue mapValue = this->GetProperty(mapProp)) {
  3131. mappedConfigs.assign(cmSystemTools::UpperCase(*mapValue),
  3132. cmList::EmptyElements::Yes);
  3133. }
  3134. }
  3135. // Get imported configurations, if specified.
  3136. if (cmValue iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS")) {
  3137. cmList const availableConfigs{ cmSystemTools::UpperCase(*iconfigs) };
  3138. if (!mappedConfigs.empty()) {
  3139. for (auto const& c : mappedConfigs) {
  3140. if (cm::contains(availableConfigs, c)) {
  3141. this->GetLocation(c, loc, imp, suffix);
  3142. return true;
  3143. }
  3144. }
  3145. // If a configuration mapping was specified, but no matching
  3146. // configuration was found, we don't want to try anything else.
  3147. return false;
  3148. }
  3149. // There is no mapping; try the requested configuration first.
  3150. if (cm::contains(availableConfigs, desiredConfig)) {
  3151. this->GetLocation(desiredConfig, loc, imp, suffix);
  3152. return true;
  3153. }
  3154. // If there is no mapping and the requested configuration is not one of
  3155. // the available configurations, just take the first available
  3156. // configuration.
  3157. this->GetLocation(availableConfigs[0], loc, imp, suffix);
  3158. return true;
  3159. }
  3160. if (!mappedConfigs.empty()) {
  3161. for (auto const& c : mappedConfigs) {
  3162. if (this->GetLocation(c, loc, imp, suffix)) {
  3163. return true;
  3164. }
  3165. }
  3166. // If a configuration mapping was specified, but no matching
  3167. // configuration was found, we don't want to try anything else.
  3168. return false;
  3169. }
  3170. // There is no mapping and no explicit list of configurations; the only
  3171. // configuration left to try is the requested configuration.
  3172. if (this->GetLocation(desiredConfig, loc, imp, suffix)) {
  3173. return true;
  3174. }
  3175. return false;
  3176. }