cmTarget.cxx 103 KB

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