cmTarget.cxx 115 KB

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