cmTarget.cxx 100 KB

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