cmTarget.cxx 115 KB

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