cmTarget.cxx 114 KB

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