cmTarget.cxx 102 KB

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