cmTarget.cxx 115 KB

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