cmTarget.cxx 109 KB

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