cmTarget.cxx 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383
  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_REMOVE_ENVIRONMENT_RPATH"_s, IC::CanCompileSources },
  389. { "INSTALL_RPATH"_s, ""_s, IC::CanCompileSources },
  390. { "INSTALL_RPATH_USE_LINK_PATH"_s, "OFF"_s, IC::CanCompileSources },
  391. // -- Platforms
  392. // ---- AIX
  393. { "AIX_EXPORT_ALL_SYMBOLS"_s, IC::TargetWithSymbolExports },
  394. // ---- Android
  395. { "ANDROID_GUI"_s, IC::ExecutableTarget },
  396. { "ANDROID_JAR_DIRECTORIES"_s, IC::CanCompileSources },
  397. { "ANDROID_JAR_DEPENDENCIES"_s, IC::CanCompileSources },
  398. { "ANDROID_NATIVE_LIB_DIRECTORIES"_s, IC::CanCompileSources },
  399. { "ANDROID_NATIVE_LIB_DEPENDENCIES"_s, IC::CanCompileSources },
  400. { "ANDROID_PROGUARD"_s, IC::CanCompileSources },
  401. { "ANDROID_PROGUARD_CONFIG_PATH"_s, IC::CanCompileSources },
  402. { "ANDROID_SECURE_PROPS_PATH"_s, IC::CanCompileSources },
  403. // ---- iOS
  404. { "IOS_INSTALL_COMBINED"_s, IC::CanCompileSources },
  405. // ---- macOS
  406. { "FRAMEWORK_MULTI_CONFIG_POSTFIX_"_s, IC::LinkableLibraryTarget, R::PerConfig },
  407. // ---- Windows
  408. { "DLL_NAME_WITH_SOVERSION"_s, IC::SharedLibraryTarget },
  409. { "GNUtoMS"_s, IC::CanCompileSources },
  410. { "WIN32_EXECUTABLE"_s, IC::CanCompileSources },
  411. { "WINDOWS_EXPORT_ALL_SYMBOLS"_s, IC::TargetWithSymbolExports },
  412. // -- Languages
  413. // ---- C
  414. { "C_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  415. // ---- C++
  416. { "CXX_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  417. // ---- CUDA
  418. { "CUDA_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  419. { "CUDA_RESOLVE_DEVICE_SYMBOLS"_s, IC::CanCompileSources },
  420. { "CUDA_RUNTIME_LIBRARY"_s, IC::CanCompileSources },
  421. // ---- HIP
  422. { "HIP_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  423. { "HIP_RUNTIME_LIBRARY"_s, IC::CanCompileSources },
  424. // ---- Objective C
  425. { "OBJC_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  426. // ---- Objective C++
  427. { "OBJCXX_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  428. // ---- Fortran
  429. { "Fortran_LINKER_LAUNCHER"_s, IC::CanCompileSources },
  430. // Static analysis
  431. // -- C
  432. { "C_CLANG_TIDY"_s, IC::CanCompileSources },
  433. { "C_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  434. { "C_CPPLINT"_s, IC::CanCompileSources },
  435. { "C_CPPCHECK"_s, IC::CanCompileSources },
  436. { "C_ICSTAT"_s, IC::CanCompileSources },
  437. { "C_INCLUDE_WHAT_YOU_USE"_s, IC::CanCompileSources },
  438. // -- C++
  439. { "CXX_CLANG_TIDY"_s, IC::CanCompileSources },
  440. { "CXX_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  441. { "CXX_CPPLINT"_s, IC::CanCompileSources },
  442. { "CXX_CPPCHECK"_s, IC::CanCompileSources },
  443. { "CXX_ICSTAT"_s, IC::CanCompileSources },
  444. { "CXX_INCLUDE_WHAT_YOU_USE"_s, IC::CanCompileSources },
  445. // -- Objective C
  446. { "OBJC_CLANG_TIDY"_s, IC::CanCompileSources },
  447. { "OBJC_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  448. // -- Objective C++
  449. { "OBJCXX_CLANG_TIDY"_s, IC::CanCompileSources },
  450. { "OBJCXX_CLANG_TIDY_EXPORT_FIXES_DIR"_s, IC::CanCompileSources },
  451. // -- Linking
  452. { "LINK_WHAT_YOU_USE"_s, IC::CanCompileSources },
  453. // Build graph properties
  454. { "LINK_DEPENDS_NO_SHARED"_s, IC::CanCompileSources },
  455. { "UNITY_BUILD"_s, IC::CanCompileSources },
  456. { "UNITY_BUILD_UNIQUE_ID"_s, IC::CanCompileSources },
  457. { "UNITY_BUILD_BATCH_SIZE"_s, "8"_s, IC::CanCompileSources },
  458. { "UNITY_BUILD_MODE"_s, "BATCH"_s, IC::CanCompileSources },
  459. { "UNITY_BUILD_RELOCATABLE"_s, IC::CanCompileSources },
  460. { "OPTIMIZE_DEPENDENCIES"_s, IC::CanCompileSources },
  461. { "VERIFY_INTERFACE_HEADER_SETS"_s },
  462. // -- Android
  463. { "ANDROID_ANT_ADDITIONAL_OPTIONS"_s, IC::CanCompileSources },
  464. { "ANDROID_PROCESS_MAX"_s, IC::CanCompileSources },
  465. { "ANDROID_SKIP_ANT_STEP"_s, IC::CanCompileSources },
  466. // -- Autogen
  467. { "AUTOGEN_COMMAND_LINE_LENGTH_MAX"_s, IC::CanCompileSources },
  468. { "AUTOGEN_ORIGIN_DEPENDS"_s, IC::CanCompileSources },
  469. { "AUTOGEN_PARALLEL"_s, IC::CanCompileSources },
  470. { "AUTOGEN_USE_SYSTEM_INCLUDE"_s, IC::CanCompileSources },
  471. { "AUTOGEN_BETTER_GRAPH_MULTI_CONFIG"_s, IC::CanCompileSources },
  472. // -- moc
  473. { "AUTOMOC_DEPEND_FILTERS"_s, IC::CanCompileSources },
  474. // -- C++
  475. { "CXX_SCAN_FOR_MODULES"_s, IC::CanCompileSources },
  476. // -- Ninja
  477. { "JOB_POOL_COMPILE"_s, IC::CanCompileSources },
  478. { "JOB_POOL_LINK"_s, IC::CanCompileSources },
  479. { "JOB_POOL_PRECOMPILE_HEADER"_s, IC::CanCompileSources },
  480. // -- Visual Studio
  481. { "VS_NO_COMPILE_BATCHING"_s, IC::CanCompileSources },
  482. { "VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION"_s, IC::CanCompileSources},
  483. // Output location properties
  484. { "ARCHIVE_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  485. { "ARCHIVE_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  486. { "COMPILE_PDB_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  487. { "COMPILE_PDB_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  488. { "LIBRARY_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  489. { "LIBRARY_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  490. { "PDB_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  491. { "PDB_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  492. { "RUNTIME_OUTPUT_DIRECTORY"_s, IC::CanCompileSources },
  493. { "RUNTIME_OUTPUT_DIRECTORY_"_s, IC::TargetWithArtifact, R::PerConfig },
  494. // macOS bundle properties
  495. { "FRAMEWORK"_s, IC::CanCompileSources },
  496. { "FRAMEWORK_MULTI_CONFIG_POSTFIX"_s, IC::CanCompileSources },
  497. { "MACOSX_BUNDLE"_s, IC::CanCompileSources },
  498. // Usage requirement properties
  499. { "LINK_INTERFACE_LIBRARIES"_s, IC::CanCompileSources },
  500. { "MAP_IMPORTED_CONFIG_"_s, IC::NormalTarget, R::PerConfig },
  501. { "EXPORT_FIND_PACKAGE_NAME"_s, IC::NormalTarget },
  502. // Metadata
  503. { "CROSSCOMPILING_EMULATOR"_s, IC::ExecutableTarget },
  504. { "EXPORT_BUILD_DATABASE"_s, IC::CanCompileSources },
  505. { "EXPORT_COMPILE_COMMANDS"_s, IC::CanCompileSources },
  506. { "FOLDER"_s },
  507. { "TEST_LAUNCHER"_s, IC::ExecutableTarget },
  508. // Xcode properties
  509. { "XCODE_GENERATE_SCHEME"_s, IC::NeedsXcode },
  510. #ifdef __APPLE__
  511. { "XCODE_SCHEME_ADDRESS_SANITIZER"_s, IC::NeedsXcodeAndCanCompileSources },
  512. { "XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN"_s, IC::NeedsXcodeAndCanCompileSources },
  513. { "XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING"_s, IC::NeedsXcodeAndCanCompileSources },
  514. { "XCODE_SCHEME_ENABLE_GPU_FRAME_CAPTURE_MODE"_s, IC::NeedsXcodeAndCanCompileSources },
  515. { "XCODE_SCHEME_THREAD_SANITIZER"_s, IC::NeedsXcodeAndCanCompileSources },
  516. { "XCODE_SCHEME_THREAD_SANITIZER_STOP"_s, IC::NeedsXcodeAndCanCompileSources },
  517. { "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER"_s, IC::NeedsXcodeAndCanCompileSources },
  518. { "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP"_s, IC::NeedsXcodeAndCanCompileSources },
  519. { "XCODE_SCHEME_LAUNCH_CONFIGURATION"_s, IC::NeedsXcodeAndCanCompileSources },
  520. { "XCODE_SCHEME_TEST_CONFIGURATION"_s, IC::NeedsXcodeAndCanCompileSources },
  521. { "XCODE_SCHEME_ENABLE_GPU_API_VALIDATION"_s, IC::NeedsXcodeAndCanCompileSources },
  522. { "XCODE_SCHEME_ENABLE_GPU_SHADER_VALIDATION"_s, IC::NeedsXcodeAndCanCompileSources },
  523. { "XCODE_SCHEME_WORKING_DIRECTORY"_s, IC::NeedsXcodeAndCanCompileSources },
  524. { "XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER"_s, IC::NeedsXcodeAndCanCompileSources },
  525. { "XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP"_s, IC::NeedsXcodeAndCanCompileSources },
  526. { "XCODE_SCHEME_MALLOC_SCRIBBLE"_s, IC::NeedsXcodeAndCanCompileSources },
  527. { "XCODE_SCHEME_MALLOC_GUARD_EDGES"_s, IC::NeedsXcodeAndCanCompileSources },
  528. { "XCODE_SCHEME_GUARD_MALLOC"_s, IC::NeedsXcodeAndCanCompileSources },
  529. { "XCODE_SCHEME_LAUNCH_MODE"_s, IC::NeedsXcodeAndCanCompileSources },
  530. { "XCODE_SCHEME_LLDB_INIT_FILE"_s, IC::NeedsXcodeAndCanCompileSources },
  531. { "XCODE_SCHEME_ZOMBIE_OBJECTS"_s, IC::NeedsXcodeAndCanCompileSources },
  532. { "XCODE_SCHEME_MALLOC_STACK"_s, IC::NeedsXcodeAndCanCompileSources },
  533. { "XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE"_s, IC::NeedsXcodeAndCanCompileSources },
  534. { "XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS"_s, IC::NeedsXcodeAndCanCompileSources },
  535. { "XCODE_SCHEME_ENVIRONMENT"_s, IC::NeedsXcodeAndCanCompileSources },
  536. { "XCODE_LINK_BUILD_PHASE_MODE"_s, "NONE"_s, IC::NeedsXcodeAndCanCompileSources },
  537. #endif
  538. /* clang-format on */
  539. };
  540. #undef COMMON_LANGUAGE_PROPERTIES
  541. #undef IC
  542. #undef R
  543. }
  544. class cmTargetInternals
  545. {
  546. public:
  547. cmStateEnums::TargetType TargetType;
  548. cmTarget::Origin Origin = cmTarget::Origin::Unknown;
  549. cmMakefile* Makefile;
  550. cmPolicies::PolicyMap PolicyMap;
  551. cmTarget const* TemplateTarget;
  552. std::string Name;
  553. std::string InstallPath;
  554. std::string RuntimeInstallPath;
  555. cmPropertyMap Properties;
  556. bool IsGeneratorProvided;
  557. bool HaveInstallRule;
  558. bool IsDLLPlatform;
  559. bool IsAIX;
  560. bool IsApple;
  561. bool IsAndroid;
  562. bool BuildInterfaceIncludesAppended;
  563. bool PerConfig;
  564. cmTarget::Visibility TargetVisibility;
  565. std::set<BT<std::pair<std::string, bool>>> Utilities;
  566. std::set<std::string> CodegenDependencies;
  567. std::vector<cmCustomCommand> PreBuildCommands;
  568. std::vector<cmCustomCommand> PreLinkCommands;
  569. std::vector<cmCustomCommand> PostBuildCommands;
  570. std::vector<cmInstallTargetGenerator*> InstallGenerators;
  571. std::set<std::string> SystemIncludeDirectories;
  572. cmTarget::LinkLibraryVectorType OriginalLinkLibraries;
  573. std::map<std::string, BTs<std::string>> LanguageStandardProperties;
  574. std::map<cmTargetExport const*, std::vector<std::string>>
  575. InstallIncludeDirectoriesEntries;
  576. std::vector<std::pair<cmTarget::TLLSignature, cmListFileContext>>
  577. TLLCommands;
  578. std::map<std::string, cmFileSet> FileSets;
  579. cmListFileBacktrace Backtrace;
  580. cmFindPackageStack FindPackageStack;
  581. UsageRequirementProperty IncludeDirectories;
  582. UsageRequirementProperty CompileOptions;
  583. UsageRequirementProperty CompileFeatures;
  584. UsageRequirementProperty CompileDefinitions;
  585. UsageRequirementProperty PrecompileHeaders;
  586. UsageRequirementProperty Sources;
  587. UsageRequirementProperty LinkOptions;
  588. UsageRequirementProperty LinkDirectories;
  589. UsageRequirementProperty LinkLibraries;
  590. UsageRequirementProperty InterfaceLinkLibraries;
  591. UsageRequirementProperty InterfaceLinkLibrariesDirect;
  592. UsageRequirementProperty InterfaceLinkLibrariesDirectExclude;
  593. UsageRequirementProperty ImportedCxxModulesIncludeDirectories;
  594. UsageRequirementProperty ImportedCxxModulesCompileDefinitions;
  595. UsageRequirementProperty ImportedCxxModulesCompileFeatures;
  596. UsageRequirementProperty ImportedCxxModulesCompileOptions;
  597. UsageRequirementProperty ImportedCxxModulesLinkLibraries;
  598. FileSetType HeadersFileSets;
  599. FileSetType CxxModulesFileSets;
  600. cmTargetInternals();
  601. bool IsImported() const;
  602. bool CheckImportedLibName(std::string const& prop,
  603. std::string const& value) const;
  604. template <typename ValueType>
  605. void AddDirectoryToFileSet(cmTarget* self, std::string const& fileSetName,
  606. ValueType value, cm::string_view fileSetType,
  607. cm::string_view description,
  608. FileSetType::Action action);
  609. template <typename ValueType>
  610. void AddPathToFileSet(cmTarget* self, std::string const& fileSetName,
  611. ValueType value, cm::string_view fileSetType,
  612. cm::string_view description,
  613. FileSetType::Action action);
  614. cmValue GetFileSetDirectories(cmTarget const* self,
  615. std::string const& fileSetName,
  616. cm::string_view fileSetType) const;
  617. cmValue GetFileSetPaths(cmTarget const* self, std::string const& fileSetName,
  618. cm::string_view fileSetType) const;
  619. cmListFileBacktrace GetBacktrace(
  620. cm::optional<cmListFileBacktrace> const& bt) const
  621. {
  622. return bt ? *bt : this->Makefile->GetBacktrace();
  623. }
  624. };
  625. cmTargetInternals::cmTargetInternals()
  626. : IncludeDirectories("INCLUDE_DIRECTORIES"_s)
  627. , CompileOptions("COMPILE_OPTIONS"_s)
  628. , CompileFeatures("COMPILE_FEATURES"_s)
  629. , CompileDefinitions("COMPILE_DEFINITIONS"_s)
  630. , PrecompileHeaders("PRECOMPILE_HEADERS"_s)
  631. , Sources("SOURCES"_s, UsageRequirementProperty::AppendEmpty::Yes)
  632. , LinkOptions("LINK_OPTIONS"_s)
  633. , LinkDirectories("LINK_DIRECTORIES"_s)
  634. , LinkLibraries("LINK_LIBRARIES"_s)
  635. , InterfaceLinkLibraries("INTERFACE_LINK_LIBRARIES"_s)
  636. , InterfaceLinkLibrariesDirect("INTERFACE_LINK_LIBRARIES_DIRECT"_s)
  637. , InterfaceLinkLibrariesDirectExclude(
  638. "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE"_s)
  639. , ImportedCxxModulesIncludeDirectories(
  640. "IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES"_s)
  641. , ImportedCxxModulesCompileDefinitions(
  642. "IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS"_s)
  643. , ImportedCxxModulesCompileFeatures(
  644. "IMPORTED_CXX_MODULES_COMPILE_FEATURES"_s)
  645. , ImportedCxxModulesCompileOptions("IMPORTED_CXX_MODULES_COMPILE_OPTIONS"_s)
  646. , ImportedCxxModulesLinkLibraries("IMPORTED_CXX_MODULES_LINK_LIBRARIES"_s)
  647. , HeadersFileSets("HEADERS"_s, "HEADER_DIRS"_s, "HEADER_SET"_s,
  648. "HEADER_DIRS_"_s, "HEADER_SET_"_s, "Header"_s,
  649. "The default header set"_s, "Header set"_s,
  650. FileSetEntries("HEADER_SETS"_s),
  651. FileSetEntries("INTERFACE_HEADER_SETS"_s))
  652. , CxxModulesFileSets("CXX_MODULES"_s, "CXX_MODULE_DIRS"_s,
  653. "CXX_MODULE_SET"_s, "CXX_MODULE_DIRS_"_s,
  654. "CXX_MODULE_SET_"_s, "C++ module"_s,
  655. "The default C++ module set"_s, "C++ module set"_s,
  656. FileSetEntries("CXX_MODULE_SETS"_s),
  657. FileSetEntries("INTERFACE_CXX_MODULE_SETS"_s))
  658. {
  659. }
  660. template <typename ValueType>
  661. bool FileSetType::WriteProperties(cmTarget* tgt, cmTargetInternals* impl,
  662. std::string const& prop, ValueType value,
  663. Action action)
  664. {
  665. if (prop == this->DefaultDirectoryProperty) {
  666. impl->AddDirectoryToFileSet(tgt, std::string(this->TypeName), value,
  667. this->TypeName, this->DefaultDescription,
  668. action);
  669. return true;
  670. }
  671. if (prop == this->DefaultPathProperty) {
  672. impl->AddPathToFileSet(tgt, std::string(this->TypeName), value,
  673. this->TypeName, this->DefaultDescription, action);
  674. return true;
  675. }
  676. if (cmHasPrefix(prop, this->DirectoryPrefix)) {
  677. auto fileSetName = prop.substr(this->DirectoryPrefix.size());
  678. if (fileSetName.empty()) {
  679. impl->Makefile->IssueMessage(
  680. MessageType::FATAL_ERROR,
  681. cmStrCat(this->ArbitraryDescription, " name cannot be empty."));
  682. } else {
  683. impl->AddDirectoryToFileSet(
  684. tgt, fileSetName, value, this->TypeName,
  685. cmStrCat(this->ArbitraryDescription, " \"", fileSetName, '"'), action);
  686. }
  687. return true;
  688. }
  689. if (cmHasPrefix(prop, this->PathPrefix)) {
  690. auto fileSetName = prop.substr(this->PathPrefix.size());
  691. if (fileSetName.empty()) {
  692. impl->Makefile->IssueMessage(
  693. MessageType::FATAL_ERROR,
  694. cmStrCat(this->ArbitraryDescription, " name cannot be empty."));
  695. } else {
  696. impl->AddPathToFileSet(
  697. tgt, fileSetName, value, this->TypeName,
  698. cmStrCat(this->ArbitraryDescription, " \"", fileSetName, '"'), action);
  699. }
  700. return true;
  701. }
  702. return false;
  703. }
  704. std::pair<bool, cmValue> FileSetType::ReadProperties(
  705. cmTarget const* tgt, cmTargetInternals const* impl,
  706. std::string const& prop) const
  707. {
  708. bool did_read = false;
  709. cmValue value = nullptr;
  710. if (prop == this->DefaultDirectoryProperty) {
  711. value = impl->GetFileSetDirectories(tgt, std::string(this->TypeName),
  712. this->TypeName);
  713. did_read = true;
  714. } else if (prop == this->DefaultPathProperty) {
  715. value =
  716. impl->GetFileSetPaths(tgt, std::string(this->TypeName), this->TypeName);
  717. did_read = true;
  718. } else if (prop == this->SelfEntries.PropertyName) {
  719. static std::string output;
  720. output = cmList::to_string(this->SelfEntries.Entries);
  721. value = cmValue(output);
  722. did_read = true;
  723. } else if (prop == this->InterfaceEntries.PropertyName) {
  724. static std::string output;
  725. output = cmList::to_string(this->InterfaceEntries.Entries);
  726. value = cmValue(output);
  727. did_read = true;
  728. } else if (cmHasPrefix(prop, this->DirectoryPrefix)) {
  729. std::string fileSetName = prop.substr(this->DirectoryPrefix.size());
  730. if (!fileSetName.empty()) {
  731. value = impl->GetFileSetDirectories(tgt, fileSetName, this->TypeName);
  732. }
  733. did_read = true;
  734. } else if (cmHasPrefix(prop, this->PathPrefix)) {
  735. std::string fileSetName = prop.substr(this->PathPrefix.size());
  736. if (!fileSetName.empty()) {
  737. value = impl->GetFileSetPaths(tgt, fileSetName, this->TypeName);
  738. }
  739. did_read = true;
  740. }
  741. return { did_read, value };
  742. }
  743. void FileSetType::AddFileSet(std::string const& name, cmFileSetVisibility vis,
  744. cmListFileBacktrace bt)
  745. {
  746. if (cmFileSetVisibilityIsForSelf(vis)) {
  747. this->SelfEntries.Entries.emplace_back(name, bt);
  748. }
  749. if (cmFileSetVisibilityIsForInterface(vis)) {
  750. this->InterfaceEntries.Entries.emplace_back(name, std::move(bt));
  751. }
  752. }
  753. template <typename ValueType>
  754. bool UsageRequirementProperty::Write(
  755. cmTargetInternals const* impl, cm::optional<cmListFileBacktrace> const& bt,
  756. std::string const& prop, ValueType value, Action action)
  757. {
  758. if (prop == this->Name) {
  759. this->WriteDirect(impl, bt, value, action);
  760. return true;
  761. }
  762. return false;
  763. }
  764. template <typename ValueType>
  765. void UsageRequirementProperty::WriteDirect(
  766. cmTargetInternals const* impl, cm::optional<cmListFileBacktrace> const& bt,
  767. ValueType value, Action action)
  768. {
  769. if (action == Action::Set) {
  770. this->Entries.clear();
  771. }
  772. if (value) {
  773. cmListFileBacktrace lfbt = impl->GetBacktrace(bt);
  774. if (action == Action::Prepend) {
  775. this->Entries.emplace(this->Entries.begin(), value, lfbt);
  776. } else if (action == Action::Set || cmNonempty(value) ||
  777. this->AppendBehavior == AppendEmpty::Yes) {
  778. this->Entries.emplace_back(value, lfbt);
  779. }
  780. }
  781. }
  782. void UsageRequirementProperty::WriteDirect(BT<std::string> value,
  783. Action action)
  784. {
  785. if (action == Action::Set) {
  786. this->Entries.clear();
  787. }
  788. if (action == Action::Prepend) {
  789. this->Entries.emplace(this->Entries.begin(), std::move(value));
  790. } else {
  791. this->Entries.emplace_back(std::move(value));
  792. }
  793. }
  794. std::pair<bool, cmValue> UsageRequirementProperty::Read(
  795. std::string const& prop) const
  796. {
  797. bool did_read = false;
  798. cmValue value = nullptr;
  799. if (prop == this->Name) {
  800. if (!this->Entries.empty()) {
  801. // Storage to back the returned `cmValue`.
  802. static std::string output;
  803. output = cmList::to_string(this->Entries);
  804. value = cmValue(output);
  805. }
  806. did_read = true;
  807. }
  808. return { did_read, value };
  809. }
  810. cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type,
  811. Visibility vis, cmMakefile* mf, PerConfig perConfig)
  812. : impl(cm::make_unique<cmTargetInternals>())
  813. {
  814. assert(mf);
  815. this->impl->TargetType = type;
  816. this->impl->Makefile = mf;
  817. this->impl->Name = name;
  818. this->impl->TemplateTarget = nullptr;
  819. this->impl->IsGeneratorProvided = false;
  820. this->impl->HaveInstallRule = false;
  821. this->impl->IsDLLPlatform = false;
  822. this->impl->IsAIX = false;
  823. this->impl->IsApple = false;
  824. this->impl->IsAndroid = false;
  825. this->impl->TargetVisibility = vis;
  826. this->impl->BuildInterfaceIncludesAppended = false;
  827. this->impl->PerConfig = (perConfig == PerConfig::Yes);
  828. // Check whether this is a DLL platform.
  829. this->impl->IsDLLPlatform =
  830. !this->impl->Makefile->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")
  831. .empty();
  832. // Check whether we are targeting AIX.
  833. {
  834. std::string const& systemName =
  835. this->impl->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  836. this->impl->IsAIX = (systemName == "AIX" || systemName == "OS400");
  837. }
  838. // Check whether we are targeting Apple.
  839. this->impl->IsApple = this->impl->Makefile->IsOn("APPLE");
  840. // Check whether we are targeting an Android platform.
  841. this->impl->IsAndroid = (this->impl->Makefile->GetSafeDefinition(
  842. "CMAKE_SYSTEM_NAME") == "Android");
  843. // Save the backtrace of target construction.
  844. this->impl->Backtrace = this->impl->Makefile->GetBacktrace();
  845. if (this->impl->IsImported()) {
  846. this->impl->FindPackageStack = this->impl->Makefile->GetFindPackageStack();
  847. }
  848. if (this->IsNormal()) {
  849. // Initialize the INCLUDE_DIRECTORIES property based on the current value
  850. // of the same directory property:
  851. this->impl->IncludeDirectories.CopyFromEntries(
  852. this->impl->Makefile->GetIncludeDirectoriesEntries());
  853. {
  854. auto const& sysInc = this->impl->Makefile->GetSystemIncludeDirectories();
  855. this->impl->SystemIncludeDirectories.insert(sysInc.begin(),
  856. sysInc.end());
  857. }
  858. this->impl->CompileOptions.CopyFromEntries(
  859. this->impl->Makefile->GetCompileOptionsEntries());
  860. this->impl->LinkOptions.CopyFromEntries(
  861. this->impl->Makefile->GetLinkOptionsEntries());
  862. this->impl->LinkDirectories.CopyFromEntries(
  863. this->impl->Makefile->GetLinkDirectoriesEntries());
  864. }
  865. // Record current policies for later use.
  866. this->impl->Makefile->RecordPolicies(this->impl->PolicyMap);
  867. std::set<TargetProperty::InitCondition> metConditions;
  868. metConditions.insert(TargetProperty::InitCondition::Always);
  869. if (this->CanCompileSources()) {
  870. metConditions.insert(TargetProperty::InitCondition::CanCompileSources);
  871. }
  872. if (this->GetGlobalGenerator()->IsXcode()) {
  873. metConditions.insert(TargetProperty::InitCondition::NeedsXcode);
  874. if (this->CanCompileSources()) {
  875. metConditions.insert(
  876. TargetProperty::InitCondition::NeedsXcodeAndCanCompileSources);
  877. }
  878. }
  879. if (!this->IsImported()) {
  880. metConditions.insert(TargetProperty::InitCondition::NonImportedTarget);
  881. }
  882. if (this->impl->TargetType != cmStateEnums::UTILITY &&
  883. this->impl->TargetType != cmStateEnums::GLOBAL_TARGET) {
  884. metConditions.insert(TargetProperty::InitCondition::NormalTarget);
  885. if (this->IsNormal()) {
  886. metConditions.insert(
  887. TargetProperty::InitCondition::NormalNonImportedTarget);
  888. }
  889. if (this->impl->TargetType != cmStateEnums::INTERFACE_LIBRARY) {
  890. metConditions.insert(TargetProperty::InitCondition::TargetWithArtifact);
  891. if (this->impl->TargetType != cmStateEnums::EXECUTABLE) {
  892. metConditions.insert(
  893. TargetProperty::InitCondition::NonExecutableWithArtifact);
  894. }
  895. }
  896. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  897. this->impl->TargetType == cmStateEnums::STATIC_LIBRARY) {
  898. metConditions.insert(
  899. TargetProperty::InitCondition::LinkableLibraryTarget);
  900. }
  901. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY) {
  902. metConditions.insert(TargetProperty::InitCondition::SharedLibraryTarget);
  903. }
  904. }
  905. if (this->impl->TargetType == cmStateEnums::EXECUTABLE) {
  906. metConditions.insert(TargetProperty::InitCondition::ExecutableTarget);
  907. }
  908. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  909. this->impl->TargetType == cmStateEnums::EXECUTABLE) {
  910. metConditions.insert(
  911. TargetProperty::InitCondition::TargetWithSymbolExports);
  912. }
  913. if (this->impl->TargetType <= cmStateEnums::GLOBAL_TARGET) {
  914. metConditions.insert(TargetProperty::InitCondition::TargetWithCommands);
  915. }
  916. std::vector<std::string> configNames =
  917. mf->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  918. for (auto& config : configNames) {
  919. config = cmSystemTools::UpperCase(config);
  920. }
  921. std::string defKey;
  922. defKey.reserve(128);
  923. defKey += "CMAKE_";
  924. auto initProperty = [this, mf, &defKey](std::string const& property,
  925. char const* default_value) {
  926. // special init for ENABLE_EXPORTS
  927. // For SHARED_LIBRARY, only CMAKE_SHARED_LIBRARY_ENABLE_EXPORTS variable
  928. // is used
  929. // For EXECUTABLE, CMAKE_EXECUTABLE_ENABLE_EXPORTS or else
  930. // CMAKE_ENABLE_EXPORTS variables are used
  931. if (property == "ENABLE_EXPORTS"_s) {
  932. // Replace everything after "CMAKE_"
  933. defKey.replace(
  934. defKey.begin() + 6, defKey.end(),
  935. cmStrCat(this->impl->TargetType == cmStateEnums::EXECUTABLE
  936. ? "EXECUTABLE"
  937. : "SHARED_LIBRARY",
  938. '_', property));
  939. if (cmValue value = mf->GetDefinition(defKey)) {
  940. this->SetProperty(property, value);
  941. return;
  942. }
  943. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY) {
  944. if (default_value) {
  945. this->SetProperty(property, default_value);
  946. }
  947. return;
  948. }
  949. }
  950. // Imported targets must set AIX_SHARED_LIBRARY_ARCHIVE explicitly.
  951. if (this->IsImported() && property == "AIX_SHARED_LIBRARY_ARCHIVE"_s) {
  952. return;
  953. }
  954. // Replace everything after "CMAKE_"
  955. defKey.replace(defKey.begin() + 6, defKey.end(), property);
  956. if (cmValue value = mf->GetDefinition(defKey)) {
  957. this->SetProperty(property, value);
  958. } else if (default_value) {
  959. this->SetProperty(property, default_value);
  960. }
  961. };
  962. std::string dflt_storage;
  963. for (auto const& tp : StaticTargetProperties) {
  964. // Ignore properties that we have not met the condition for.
  965. if (!metConditions.count(tp.InitConditional)) {
  966. continue;
  967. }
  968. char const* dflt = nullptr;
  969. if (tp.Default) {
  970. dflt_storage = std::string(*tp.Default);
  971. dflt = dflt_storage.c_str();
  972. }
  973. if (tp.Repeat == TargetProperty::Repetition::Once) {
  974. initProperty(std::string(tp.Name), dflt);
  975. } else {
  976. std::string propertyName;
  977. for (auto const& configName : configNames) {
  978. if (tp.Repeat == TargetProperty::Repetition::PerConfig) {
  979. propertyName = cmStrCat(tp.Name, configName);
  980. } else if (tp.Repeat == TargetProperty::Repetition::PerConfigPrefix) {
  981. propertyName = cmStrCat(configName, tp.Name);
  982. }
  983. initProperty(propertyName, dflt);
  984. }
  985. }
  986. }
  987. // Clean up some property defaults.
  988. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  989. this->impl->TargetType == cmStateEnums::MODULE_LIBRARY) {
  990. this->SetProperty("POSITION_INDEPENDENT_CODE", "True");
  991. }
  992. // check for "CMAKE_VS_GLOBALS" variable and set up target properties
  993. // if any
  994. cmValue globals = mf->GetDefinition("CMAKE_VS_GLOBALS");
  995. if (globals) {
  996. std::string const genName = mf->GetGlobalGenerator()->GetName();
  997. if (cmHasLiteralPrefix(genName, "Visual Studio")) {
  998. cmList props{ *globals };
  999. std::string const vsGlobal = "VS_GLOBAL_";
  1000. for (std::string const& i : props) {
  1001. // split NAME=VALUE
  1002. std::string::size_type const assignment = i.find('=');
  1003. if (assignment != std::string::npos) {
  1004. std::string const propName = vsGlobal + i.substr(0, assignment);
  1005. std::string const propValue = i.substr(assignment + 1);
  1006. initProperty(propName, propValue.c_str());
  1007. }
  1008. }
  1009. }
  1010. }
  1011. if (!this->IsNormal() || mf->GetPropertyAsBool("SYSTEM")) {
  1012. this->SetProperty("SYSTEM", "ON");
  1013. }
  1014. for (auto const& prop : mf->GetState()->GetPropertyDefinitions().GetMap()) {
  1015. if (prop.first.second == cmProperty::TARGET &&
  1016. !prop.second.GetInitializeFromVariable().empty()) {
  1017. if (auto value =
  1018. mf->GetDefinition(prop.second.GetInitializeFromVariable())) {
  1019. this->SetProperty(prop.first.first, value);
  1020. }
  1021. }
  1022. }
  1023. }
  1024. cmTarget::cmTarget(cmTarget&&) noexcept = default;
  1025. cmTarget::~cmTarget() = default;
  1026. cmTarget& cmTarget::operator=(cmTarget&&) noexcept = default;
  1027. cmStateEnums::TargetType cmTarget::GetType() const
  1028. {
  1029. return this->impl->TargetType;
  1030. }
  1031. void cmTarget::SetOrigin(Origin origin)
  1032. {
  1033. assert(origin != cmTarget::Origin::Unknown);
  1034. assert(this->impl->Origin == cmTarget::Origin::Unknown);
  1035. this->impl->Origin = origin;
  1036. }
  1037. cmTarget::Origin cmTarget::GetOrigin() const
  1038. {
  1039. return this->impl->Origin;
  1040. }
  1041. cmMakefile* cmTarget::GetMakefile() const
  1042. {
  1043. return this->impl->Makefile;
  1044. }
  1045. cmPolicies::PolicyMap const& cmTarget::GetPolicyMap() const
  1046. {
  1047. return this->impl->PolicyMap;
  1048. }
  1049. std::string const& cmTarget::GetName() const
  1050. {
  1051. return this->impl->Name;
  1052. }
  1053. std::string const& cmTarget::GetTemplateName() const
  1054. {
  1055. if (this->impl->TemplateTarget) {
  1056. return this->impl->TemplateTarget->GetTemplateName();
  1057. }
  1058. return this->impl->Name;
  1059. }
  1060. cmPolicies::PolicyStatus cmTarget::GetPolicyStatus(
  1061. cmPolicies::PolicyID policy) const
  1062. {
  1063. return this->impl->PolicyMap.Get(policy);
  1064. }
  1065. cmGlobalGenerator* cmTarget::GetGlobalGenerator() const
  1066. {
  1067. return this->impl->Makefile->GetGlobalGenerator();
  1068. }
  1069. BTs<std::string> const* cmTarget::GetLanguageStandardProperty(
  1070. std::string const& propertyName) const
  1071. {
  1072. auto entry = this->impl->LanguageStandardProperties.find(propertyName);
  1073. if (entry != this->impl->LanguageStandardProperties.end()) {
  1074. return &entry->second;
  1075. }
  1076. return nullptr;
  1077. }
  1078. void cmTarget::SetLanguageStandardProperty(std::string const& lang,
  1079. std::string const& value,
  1080. std::string const& feature)
  1081. {
  1082. cmListFileBacktrace featureBacktrace;
  1083. for (auto const& entry : this->impl->CompileFeatures.Entries) {
  1084. if (entry.Value == feature) {
  1085. featureBacktrace = entry.Backtrace;
  1086. break;
  1087. }
  1088. }
  1089. BTs<std::string>& languageStandardProperty =
  1090. this->impl->LanguageStandardProperties[cmStrCat(lang, "_STANDARD")];
  1091. if (languageStandardProperty.Value != value) {
  1092. languageStandardProperty.Value = value;
  1093. languageStandardProperty.Backtraces.clear();
  1094. }
  1095. languageStandardProperty.Backtraces.emplace_back(featureBacktrace);
  1096. }
  1097. void cmTarget::AddUtility(std::string const& name, bool cross,
  1098. cmMakefile const* mf)
  1099. {
  1100. this->impl->Utilities.insert(BT<std::pair<std::string, bool>>(
  1101. { name, cross }, mf ? mf->GetBacktrace() : cmListFileBacktrace()));
  1102. }
  1103. void cmTarget::AddUtility(BT<std::pair<std::string, bool>> util)
  1104. {
  1105. this->impl->Utilities.emplace(std::move(util));
  1106. }
  1107. void cmTarget::AddCodegenDependency(std::string const& name)
  1108. {
  1109. this->impl->CodegenDependencies.emplace(name);
  1110. }
  1111. std::set<std::string> const& cmTarget::GetCodegenDeps() const
  1112. {
  1113. return this->impl->CodegenDependencies;
  1114. }
  1115. std::set<BT<std::pair<std::string, bool>>> const& cmTarget::GetUtilities()
  1116. const
  1117. {
  1118. return this->impl->Utilities;
  1119. }
  1120. cmListFileBacktrace const& cmTarget::GetBacktrace() const
  1121. {
  1122. return this->impl->Backtrace;
  1123. }
  1124. cmFindPackageStack const& cmTarget::GetFindPackageStack() const
  1125. {
  1126. return this->impl->FindPackageStack;
  1127. }
  1128. bool cmTarget::IsExecutableWithExports() const
  1129. {
  1130. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  1131. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  1132. }
  1133. bool cmTarget::IsSharedLibraryWithExports() const
  1134. {
  1135. return (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  1136. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  1137. }
  1138. bool cmTarget::IsFrameworkOnApple() const
  1139. {
  1140. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1141. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  1142. this->IsApple() && this->GetPropertyAsBool("FRAMEWORK"));
  1143. }
  1144. bool cmTarget::IsArchivedAIXSharedLibrary() const
  1145. {
  1146. if (this->GetType() == cmStateEnums::SHARED_LIBRARY && this->IsAIX()) {
  1147. cmValue value = this->GetProperty("AIX_SHARED_LIBRARY_ARCHIVE");
  1148. if (!value.IsEmpty()) {
  1149. return value.IsOn();
  1150. }
  1151. if (this->IsImported()) {
  1152. return false;
  1153. }
  1154. switch (this->GetPolicyStatusCMP0182()) {
  1155. case cmPolicies::WARN:
  1156. case cmPolicies::OLD:
  1157. // The OLD behavior's default is to disable shared library archives.
  1158. break;
  1159. case cmPolicies::NEW:
  1160. // The NEW behavior's default is to enable shared library archives.
  1161. return true;
  1162. }
  1163. }
  1164. return false;
  1165. }
  1166. bool cmTarget::IsAppBundleOnApple() const
  1167. {
  1168. return (this->GetType() == cmStateEnums::EXECUTABLE && this->IsApple() &&
  1169. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  1170. }
  1171. bool cmTarget::IsAndroidGuiExecutable() const
  1172. {
  1173. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  1174. this->impl->IsAndroid && this->GetPropertyAsBool("ANDROID_GUI"));
  1175. }
  1176. bool cmTarget::HasKnownObjectFileLocation(std::string* reason) const
  1177. {
  1178. return this->GetGlobalGenerator()->HasKnownObjectFileLocation(*this, reason);
  1179. }
  1180. std::vector<cmCustomCommand> const& cmTarget::GetPreBuildCommands() const
  1181. {
  1182. return this->impl->PreBuildCommands;
  1183. }
  1184. void cmTarget::AddPreBuildCommand(cmCustomCommand const& cmd)
  1185. {
  1186. this->impl->PreBuildCommands.push_back(cmd);
  1187. }
  1188. void cmTarget::AddPreBuildCommand(cmCustomCommand&& cmd)
  1189. {
  1190. this->impl->PreBuildCommands.push_back(std::move(cmd));
  1191. }
  1192. std::vector<cmCustomCommand> const& cmTarget::GetPreLinkCommands() const
  1193. {
  1194. return this->impl->PreLinkCommands;
  1195. }
  1196. void cmTarget::AddPreLinkCommand(cmCustomCommand const& cmd)
  1197. {
  1198. this->impl->PreLinkCommands.push_back(cmd);
  1199. }
  1200. void cmTarget::AddPreLinkCommand(cmCustomCommand&& cmd)
  1201. {
  1202. this->impl->PreLinkCommands.push_back(std::move(cmd));
  1203. }
  1204. std::vector<cmCustomCommand> const& cmTarget::GetPostBuildCommands() const
  1205. {
  1206. return this->impl->PostBuildCommands;
  1207. }
  1208. void cmTarget::AddPostBuildCommand(cmCustomCommand const& cmd)
  1209. {
  1210. this->impl->PostBuildCommands.push_back(cmd);
  1211. }
  1212. void cmTarget::AddPostBuildCommand(cmCustomCommand&& cmd)
  1213. {
  1214. this->impl->PostBuildCommands.push_back(std::move(cmd));
  1215. }
  1216. void cmTarget::AddTracedSources(std::vector<std::string> const& srcs)
  1217. {
  1218. if (!srcs.empty()) {
  1219. this->impl->Sources.WriteDirect(this->impl.get(), {},
  1220. cmValue(cmJoin(srcs, ";")),
  1221. UsageRequirementProperty::Action::Append);
  1222. }
  1223. }
  1224. void cmTarget::AddSources(std::vector<std::string> const& srcs)
  1225. {
  1226. std::vector<std::string> srcFiles;
  1227. for (std::string const& filename : srcs) {
  1228. if (!cmGeneratorExpression::StartsWithGeneratorExpression(filename)) {
  1229. this->impl->Makefile->GetOrCreateSource(filename);
  1230. }
  1231. srcFiles.emplace_back(filename);
  1232. }
  1233. this->AddTracedSources(srcFiles);
  1234. }
  1235. struct CreateLocation
  1236. {
  1237. cmMakefile const* Makefile;
  1238. CreateLocation(cmMakefile const* mf)
  1239. : Makefile(mf)
  1240. {
  1241. }
  1242. cmSourceFileLocation operator()(std::string const& filename) const
  1243. {
  1244. return cmSourceFileLocation(this->Makefile, filename);
  1245. }
  1246. };
  1247. struct LocationMatcher
  1248. {
  1249. cmSourceFileLocation const& Needle;
  1250. LocationMatcher(cmSourceFileLocation const& needle)
  1251. : Needle(needle)
  1252. {
  1253. }
  1254. bool operator()(cmSourceFileLocation& loc)
  1255. {
  1256. return loc.Matches(this->Needle);
  1257. }
  1258. };
  1259. struct TargetPropertyEntryFinder
  1260. {
  1261. private:
  1262. cmSourceFileLocation const& Needle;
  1263. public:
  1264. TargetPropertyEntryFinder(cmSourceFileLocation const& needle)
  1265. : Needle(needle)
  1266. {
  1267. }
  1268. bool operator()(BT<std::string> const& entry)
  1269. {
  1270. cmList files{ entry.Value };
  1271. std::vector<cmSourceFileLocation> locations;
  1272. locations.reserve(files.size());
  1273. std::transform(files.begin(), files.end(), std::back_inserter(locations),
  1274. CreateLocation(this->Needle.GetMakefile()));
  1275. return std::find_if(locations.begin(), locations.end(),
  1276. LocationMatcher(this->Needle)) != locations.end();
  1277. }
  1278. };
  1279. cmSourceFile* cmTarget::AddSource(std::string const& src, bool before)
  1280. {
  1281. cmSourceFileLocation sfl(this->impl->Makefile, src,
  1282. cmSourceFileLocationKind::Known);
  1283. auto const& sources = this->impl->Sources.Entries;
  1284. if (std::find_if(sources.begin(), sources.end(),
  1285. TargetPropertyEntryFinder(sfl)) == sources.end()) {
  1286. this->impl->Sources.WriteDirect(
  1287. this->impl.get(), {}, cmValue(src),
  1288. before ? UsageRequirementProperty::Action::Prepend
  1289. : UsageRequirementProperty::Action::Append);
  1290. }
  1291. if (cmGeneratorExpression::Find(src) != std::string::npos) {
  1292. return nullptr;
  1293. }
  1294. return this->impl->Makefile->GetOrCreateSource(
  1295. src, false, cmSourceFileLocationKind::Known);
  1296. }
  1297. void cmTarget::ClearDependencyInformation(cmMakefile& mf) const
  1298. {
  1299. std::string depname = cmStrCat(this->GetName(), "_LIB_DEPENDS");
  1300. mf.RemoveCacheDefinition(depname);
  1301. }
  1302. std::string cmTarget::GetDebugGeneratorExpressions(
  1303. std::string const& value, cmTargetLinkLibraryType llt) const
  1304. {
  1305. if (llt == GENERAL_LibraryType) {
  1306. return value;
  1307. }
  1308. // Get the list of configurations considered to be DEBUG.
  1309. std::vector<std::string> debugConfigs =
  1310. this->impl->Makefile->GetCMakeInstance()->GetDebugConfigs();
  1311. std::string configString = "$<CONFIG:" + debugConfigs[0] + ">";
  1312. if (debugConfigs.size() > 1) {
  1313. for (std::string const& conf : cmMakeRange(debugConfigs).advance(1)) {
  1314. configString += ",$<CONFIG:" + conf + ">";
  1315. }
  1316. configString = "$<OR:" + configString + ">";
  1317. }
  1318. if (llt == OPTIMIZED_LibraryType) {
  1319. configString = "$<NOT:" + configString + ">";
  1320. }
  1321. return "$<" + configString + ":" + value + ">";
  1322. }
  1323. static std::string targetNameGenex(std::string const& lib)
  1324. {
  1325. return "$<TARGET_NAME:" + lib + ">";
  1326. }
  1327. bool cmTarget::PushTLLCommandTrace(TLLSignature signature,
  1328. cmListFileContext const& lfc)
  1329. {
  1330. bool ret = true;
  1331. if (!this->impl->TLLCommands.empty()) {
  1332. if (this->impl->TLLCommands.back().first != signature) {
  1333. ret = false;
  1334. }
  1335. }
  1336. if (this->impl->TLLCommands.empty() ||
  1337. this->impl->TLLCommands.back().second != lfc) {
  1338. this->impl->TLLCommands.emplace_back(signature, lfc);
  1339. }
  1340. return ret;
  1341. }
  1342. void cmTarget::GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const
  1343. {
  1344. char const* sigString =
  1345. (sig == cmTarget::KeywordTLLSignature ? "keyword" : "plain");
  1346. s << "The uses of the " << sigString << " signature are here:\n";
  1347. for (auto const& cmd : this->impl->TLLCommands) {
  1348. if (cmd.first == sig) {
  1349. cmListFileContext lfc = cmd.second;
  1350. lfc.FilePath = cmSystemTools::RelativeIfUnder(
  1351. this->impl->Makefile->GetState()->GetSourceDirectory(), lfc.FilePath);
  1352. s << " * " << lfc << '\n';
  1353. }
  1354. }
  1355. }
  1356. std::string const& cmTarget::GetInstallPath() const
  1357. {
  1358. return this->impl->InstallPath;
  1359. }
  1360. void cmTarget::SetInstallPath(std::string const& name)
  1361. {
  1362. this->impl->InstallPath = name;
  1363. }
  1364. std::string const& cmTarget::GetRuntimeInstallPath() const
  1365. {
  1366. return this->impl->RuntimeInstallPath;
  1367. }
  1368. void cmTarget::SetRuntimeInstallPath(std::string const& name)
  1369. {
  1370. this->impl->RuntimeInstallPath = name;
  1371. }
  1372. bool cmTarget::GetHaveInstallRule() const
  1373. {
  1374. return this->impl->HaveInstallRule;
  1375. }
  1376. void cmTarget::SetHaveInstallRule(bool hir)
  1377. {
  1378. this->impl->HaveInstallRule = hir;
  1379. }
  1380. void cmTarget::AddInstallGenerator(cmInstallTargetGenerator* g)
  1381. {
  1382. this->impl->InstallGenerators.emplace_back(g);
  1383. }
  1384. std::vector<cmInstallTargetGenerator*> const& cmTarget::GetInstallGenerators()
  1385. const
  1386. {
  1387. return this->impl->InstallGenerators;
  1388. }
  1389. bool cmTarget::GetIsGeneratorProvided() const
  1390. {
  1391. return this->impl->IsGeneratorProvided;
  1392. }
  1393. void cmTarget::SetIsGeneratorProvided(bool igp)
  1394. {
  1395. this->impl->IsGeneratorProvided = igp;
  1396. }
  1397. cmTarget::LinkLibraryVectorType const& cmTarget::GetOriginalLinkLibraries()
  1398. const
  1399. {
  1400. return this->impl->OriginalLinkLibraries;
  1401. }
  1402. void cmTarget::AddLinkLibrary(cmMakefile& mf, std::string const& lib,
  1403. cmTargetLinkLibraryType llt)
  1404. {
  1405. cmTarget* tgt = mf.FindTargetToUse(lib);
  1406. {
  1407. bool const isNonImportedTarget = tgt && !tgt->IsImported();
  1408. std::string const libName =
  1409. (isNonImportedTarget && llt != GENERAL_LibraryType)
  1410. ? targetNameGenex(lib)
  1411. : lib;
  1412. this->AppendProperty("LINK_LIBRARIES",
  1413. this->GetDebugGeneratorExpressions(libName, llt),
  1414. mf.GetBacktrace());
  1415. }
  1416. if (cmGeneratorExpression::Find(lib) != std::string::npos ||
  1417. (tgt &&
  1418. (tgt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  1419. tgt->GetType() == cmStateEnums::OBJECT_LIBRARY)) ||
  1420. (this->impl->Name == lib)) {
  1421. return;
  1422. }
  1423. this->impl->OriginalLinkLibraries.emplace_back(lib, llt);
  1424. // Add the explicit dependency information for libraries. This is
  1425. // simply a set of libraries separated by ";". There should always
  1426. // be a trailing ";". These library names are not canonical, in that
  1427. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  1428. // We shouldn't remove duplicates here because external libraries
  1429. // may be purposefully duplicated to handle recursive dependencies,
  1430. // and we removing one instance will break the link line. Duplicates
  1431. // will be appropriately eliminated at emit time.
  1432. if (this->impl->TargetType >= cmStateEnums::STATIC_LIBRARY &&
  1433. this->impl->TargetType <= cmStateEnums::MODULE_LIBRARY &&
  1434. (this->GetPolicyStatusCMP0073() == cmPolicies::OLD ||
  1435. this->GetPolicyStatusCMP0073() == cmPolicies::WARN)) {
  1436. std::string targetEntry = cmStrCat(this->impl->Name, "_LIB_DEPENDS");
  1437. std::string dependencies;
  1438. cmValue old_val = mf.GetDefinition(targetEntry);
  1439. if (old_val) {
  1440. dependencies += *old_val;
  1441. }
  1442. switch (llt) {
  1443. case GENERAL_LibraryType:
  1444. dependencies += "general";
  1445. break;
  1446. case DEBUG_LibraryType:
  1447. dependencies += "debug";
  1448. break;
  1449. case OPTIMIZED_LibraryType:
  1450. dependencies += "optimized";
  1451. break;
  1452. }
  1453. dependencies += ";";
  1454. dependencies += lib;
  1455. dependencies += ";";
  1456. mf.AddCacheDefinition(targetEntry, dependencies,
  1457. "Dependencies for the target", cmStateEnums::STATIC);
  1458. }
  1459. }
  1460. void cmTarget::AddSystemIncludeDirectories(std::set<std::string> const& incs)
  1461. {
  1462. this->impl->SystemIncludeDirectories.insert(incs.begin(), incs.end());
  1463. }
  1464. std::set<std::string> const& cmTarget::GetSystemIncludeDirectories() const
  1465. {
  1466. return this->impl->SystemIncludeDirectories;
  1467. }
  1468. void cmTarget::AddInstallIncludeDirectories(cmTargetExport const& te,
  1469. cmStringRange const& incs)
  1470. {
  1471. std::copy(
  1472. incs.begin(), incs.end(),
  1473. std::back_inserter(this->impl->InstallIncludeDirectoriesEntries[&te]));
  1474. }
  1475. cmStringRange cmTarget::GetInstallIncludeDirectoriesEntries(
  1476. cmTargetExport const& te) const
  1477. {
  1478. auto i = this->impl->InstallIncludeDirectoriesEntries.find(&te);
  1479. if (i == this->impl->InstallIncludeDirectoriesEntries.end()) {
  1480. decltype(i->second) empty;
  1481. return cmMakeRange(empty);
  1482. }
  1483. return cmMakeRange(i->second);
  1484. }
  1485. cmBTStringRange cmTarget::GetIncludeDirectoriesEntries() const
  1486. {
  1487. return cmMakeRange(this->impl->IncludeDirectories.Entries);
  1488. }
  1489. cmBTStringRange cmTarget::GetCompileOptionsEntries() const
  1490. {
  1491. return cmMakeRange(this->impl->CompileOptions.Entries);
  1492. }
  1493. cmBTStringRange cmTarget::GetCompileFeaturesEntries() const
  1494. {
  1495. return cmMakeRange(this->impl->CompileFeatures.Entries);
  1496. }
  1497. cmBTStringRange cmTarget::GetCompileDefinitionsEntries() const
  1498. {
  1499. return cmMakeRange(this->impl->CompileDefinitions.Entries);
  1500. }
  1501. cmBTStringRange cmTarget::GetPrecompileHeadersEntries() const
  1502. {
  1503. return cmMakeRange(this->impl->PrecompileHeaders.Entries);
  1504. }
  1505. cmBTStringRange cmTarget::GetSourceEntries() const
  1506. {
  1507. return cmMakeRange(this->impl->Sources.Entries);
  1508. }
  1509. cmBTStringRange cmTarget::GetLinkOptionsEntries() const
  1510. {
  1511. return cmMakeRange(this->impl->LinkOptions.Entries);
  1512. }
  1513. cmBTStringRange cmTarget::GetLinkDirectoriesEntries() const
  1514. {
  1515. return cmMakeRange(this->impl->LinkDirectories.Entries);
  1516. }
  1517. cmBTStringRange cmTarget::GetLinkImplementationEntries() const
  1518. {
  1519. return cmMakeRange(this->impl->LinkLibraries.Entries);
  1520. }
  1521. cmBTStringRange cmTarget::GetLinkInterfaceEntries() const
  1522. {
  1523. return cmMakeRange(this->impl->InterfaceLinkLibraries.Entries);
  1524. }
  1525. cmBTStringRange cmTarget::GetLinkInterfaceDirectEntries() const
  1526. {
  1527. return cmMakeRange(this->impl->InterfaceLinkLibrariesDirect.Entries);
  1528. }
  1529. cmBTStringRange cmTarget::GetLinkInterfaceDirectExcludeEntries() const
  1530. {
  1531. return cmMakeRange(this->impl->InterfaceLinkLibrariesDirectExclude.Entries);
  1532. }
  1533. void cmTarget::CopyPolicyStatuses(cmTarget const* tgt)
  1534. {
  1535. // Normal targets cannot be the target of a copy.
  1536. assert(!this->IsNormal());
  1537. // Imported targets cannot be the target of a copy.
  1538. assert(!this->IsImported());
  1539. // Only imported targets can be the source of a copy.
  1540. assert(tgt->IsImported());
  1541. this->impl->PolicyMap = tgt->impl->PolicyMap;
  1542. this->impl->TemplateTarget = tgt;
  1543. }
  1544. void cmTarget::CopyImportedCxxModulesEntries(cmTarget const* tgt)
  1545. {
  1546. // Normal targets cannot be the target of a copy.
  1547. assert(!this->IsNormal());
  1548. // Imported targets cannot be the target of a copy.
  1549. assert(!this->IsImported());
  1550. // Only imported targets can be the source of a copy.
  1551. assert(tgt->IsImported());
  1552. this->impl->IncludeDirectories.Entries.clear();
  1553. this->impl->IncludeDirectories.CopyFromEntries(
  1554. cmMakeRange(tgt->impl->ImportedCxxModulesIncludeDirectories.Entries));
  1555. this->impl->CompileDefinitions.Entries.clear();
  1556. this->impl->CompileDefinitions.CopyFromEntries(
  1557. cmMakeRange(tgt->impl->ImportedCxxModulesCompileDefinitions.Entries));
  1558. this->impl->CompileFeatures.Entries.clear();
  1559. this->impl->CompileFeatures.CopyFromEntries(
  1560. cmMakeRange(tgt->impl->ImportedCxxModulesCompileFeatures.Entries));
  1561. this->impl->CompileOptions.Entries.clear();
  1562. this->impl->CompileOptions.CopyFromEntries(
  1563. cmMakeRange(tgt->impl->ImportedCxxModulesCompileOptions.Entries));
  1564. this->impl->LinkLibraries.Entries.clear();
  1565. this->impl->LinkLibraries.CopyFromEntries(
  1566. cmMakeRange(tgt->impl->ImportedCxxModulesLinkLibraries.Entries));
  1567. // Copy the C++ module fileset entries from `tgt`'s `INTERFACE` to this
  1568. // target's `PRIVATE`.
  1569. this->impl->CxxModulesFileSets.SelfEntries.Entries.clear();
  1570. this->impl->CxxModulesFileSets.SelfEntries.Entries =
  1571. tgt->impl->CxxModulesFileSets.InterfaceEntries.Entries;
  1572. }
  1573. void cmTarget::CopyImportedCxxModulesProperties(cmTarget const* tgt)
  1574. {
  1575. // Normal targets cannot be the target of a copy.
  1576. assert(!this->IsNormal());
  1577. // Imported targets cannot be the target of a copy.
  1578. assert(!this->IsImported());
  1579. // Only imported targets can be the source of a copy.
  1580. assert(tgt->IsImported());
  1581. // The list of properties that are relevant here include:
  1582. // - compilation-specific properties for any language or platform
  1583. // - compilation-specific properties for C++
  1584. // - build graph-specific properties that affect compilation
  1585. // - IDE metadata properties
  1586. // - static analysis properties
  1587. static std::string const propertiesToCopy[] = {
  1588. // Compilation properties
  1589. "DEFINE_SYMBOL",
  1590. "DEPRECATION",
  1591. "NO_SYSTEM_FROM_IMPORTED",
  1592. "POSITION_INDEPENDENT_CODE",
  1593. "VISIBILITY_INLINES_HIDDEN",
  1594. // -- Platforms
  1595. // ---- Android
  1596. "ANDROID_API",
  1597. "ANDROID_API_MIN",
  1598. "ANDROID_ARCH",
  1599. "ANDROID_STL_TYPE",
  1600. // ---- macOS
  1601. "OSX_ARCHITECTURES",
  1602. // ---- Windows
  1603. "MSVC_DEBUG_INFORMATION_FORMAT",
  1604. "MSVC_RUNTIME_CHECKS",
  1605. "MSVC_RUNTIME_LIBRARY",
  1606. "VS_PLATFORM_TOOLSET",
  1607. // ---- OpenWatcom
  1608. "WATCOM_RUNTIME_LIBRARY",
  1609. // -- Language
  1610. // ---- C++
  1611. "CXX_COMPILER_LAUNCHER",
  1612. "CXX_STANDARD",
  1613. "CXX_STANDARD_REQUIRED",
  1614. "CXX_EXTENSIONS",
  1615. "CXX_VISIBILITY_PRESET",
  1616. "CXX_MODULE_STD",
  1617. // Static analysis
  1618. "CXX_CLANG_TIDY",
  1619. "CXX_CLANG_TIDY_EXPORT_FIXES_DIR",
  1620. "CXX_CPPLINT",
  1621. "CXX_CPPCHECK",
  1622. "CXX_ICSTAT",
  1623. "CXX_INCLUDE_WHAT_YOU_USE",
  1624. // Build graph properties
  1625. "EXCLUDE_FROM_ALL",
  1626. "EXCLUDE_FROM_DEFAULT_BUILD",
  1627. "OPTIMIZE_DEPENDENCIES",
  1628. // -- Ninja
  1629. "JOB_POOL_COMPILE",
  1630. // -- Visual Studio
  1631. "VS_NO_COMPILE_BATCHING",
  1632. "VS_PROJECT_IMPORT",
  1633. // Metadata
  1634. "EchoString",
  1635. "EXPORT_COMPILE_COMMANDS",
  1636. // Do *not* copy this property; it should be re-initialized at synthesis
  1637. // time from the `CMAKE_EXPORT_BUILD_DATABASE` variable as `IMPORTED`
  1638. // targets ignore the property initialization.
  1639. // "EXPORT_BUILD_DATABASE",
  1640. "FOLDER",
  1641. "LABELS",
  1642. "PROJECT_LABEL",
  1643. "SYSTEM",
  1644. };
  1645. auto copyProperty = [this, tgt](std::string const& prop) -> cmValue {
  1646. cmValue value = tgt->GetProperty(prop);
  1647. // Always set the property; it may have been explicitly unset.
  1648. this->SetProperty(prop, value);
  1649. return value;
  1650. };
  1651. for (auto const& prop : propertiesToCopy) {
  1652. copyProperty(prop);
  1653. }
  1654. static cm::static_string_view const perConfigPropertiesToCopy[] = {
  1655. "EXCLUDE_FROM_DEFAULT_BUILD_"_s,
  1656. "IMPORTED_CXX_MODULES_"_s,
  1657. "MAP_IMPORTED_CONFIG_"_s,
  1658. "OSX_ARCHITECTURES_"_s,
  1659. };
  1660. std::vector<std::string> configNames =
  1661. this->impl->Makefile->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  1662. for (std::string const& configName : configNames) {
  1663. std::string configUpper = cmSystemTools::UpperCase(configName);
  1664. for (auto const& perConfigProp : perConfigPropertiesToCopy) {
  1665. copyProperty(cmStrCat(perConfigProp, configUpper));
  1666. }
  1667. }
  1668. if (this->GetGlobalGenerator()->IsXcode()) {
  1669. cmValue xcodeGenerateScheme = copyProperty("XCODE_GENERATE_SCHEME");
  1670. // TODO: Make sure these show up on the imported target in the first place
  1671. // XCODE_ATTRIBUTE_???
  1672. if (xcodeGenerateScheme.IsOn()) {
  1673. #ifdef __APPLE__
  1674. static std::string const xcodeSchemePropertiesToCopy[] = {
  1675. // FIXME: Do all of these apply? Do they matter?
  1676. "XCODE_SCHEME_ADDRESS_SANITIZER",
  1677. "XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN",
  1678. "XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER",
  1679. "XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS",
  1680. "XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE",
  1681. "XCODE_SCHEME_ENABLE_GPU_API_VALIDATION",
  1682. "XCODE_SCHEME_ENABLE_GPU_SHADER_VALIDATION",
  1683. "XCODE_SCHEME_GUARD_MALLOC",
  1684. "XCODE_SCHEME_LAUNCH_CONFIGURATION",
  1685. "XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP",
  1686. "XCODE_SCHEME_MALLOC_GUARD_EDGES",
  1687. "XCODE_SCHEME_MALLOC_SCRIBBLE",
  1688. "XCODE_SCHEME_MALLOC_STACK",
  1689. "XCODE_SCHEME_THREAD_SANITIZER",
  1690. "XCODE_SCHEME_THREAD_SANITIZER_STOP",
  1691. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER",
  1692. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP",
  1693. "XCODE_SCHEME_ZOMBIE_OBJECTS",
  1694. };
  1695. for (auto const& xcodeProperty : xcodeSchemePropertiesToCopy) {
  1696. copyProperty(xcodeProperty);
  1697. }
  1698. #endif
  1699. }
  1700. }
  1701. }
  1702. cmBTStringRange cmTarget::GetHeaderSetsEntries() const
  1703. {
  1704. return cmMakeRange(this->impl->HeadersFileSets.SelfEntries.Entries);
  1705. }
  1706. cmBTStringRange cmTarget::GetCxxModuleSetsEntries() const
  1707. {
  1708. return cmMakeRange(this->impl->CxxModulesFileSets.SelfEntries.Entries);
  1709. }
  1710. cmBTStringRange cmTarget::GetInterfaceHeaderSetsEntries() const
  1711. {
  1712. return cmMakeRange(this->impl->HeadersFileSets.InterfaceEntries.Entries);
  1713. }
  1714. cmBTStringRange cmTarget::GetInterfaceCxxModuleSetsEntries() const
  1715. {
  1716. return cmMakeRange(this->impl->CxxModulesFileSets.InterfaceEntries.Entries);
  1717. }
  1718. namespace {
  1719. #define MAKE_PROP(PROP) const std::string prop##PROP = #PROP
  1720. MAKE_PROP(C_STANDARD);
  1721. MAKE_PROP(CXX_STANDARD);
  1722. MAKE_PROP(CUDA_STANDARD);
  1723. MAKE_PROP(HIP_STANDARD);
  1724. MAKE_PROP(OBJC_STANDARD);
  1725. MAKE_PROP(OBJCXX_STANDARD);
  1726. MAKE_PROP(COMPILE_DEFINITIONS);
  1727. MAKE_PROP(COMPILE_FEATURES);
  1728. MAKE_PROP(COMPILE_OPTIONS);
  1729. MAKE_PROP(PRECOMPILE_HEADERS);
  1730. MAKE_PROP(PRECOMPILE_HEADERS_REUSE_FROM);
  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 == propPRECOMPILE_HEADERS_REUSE_FROM) {
  1951. if (this->GetProperty("PRECOMPILE_HEADERS")) {
  1952. std::ostringstream e;
  1953. e << "PRECOMPILE_HEADERS property is already set on target (\""
  1954. << this->impl->Name << "\")\n";
  1955. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1956. return;
  1957. }
  1958. auto* reusedTarget = this->impl->Makefile->GetCMakeInstance()
  1959. ->GetGlobalGenerator()
  1960. ->FindTarget(value);
  1961. if (!reusedTarget) {
  1962. std::string const e(
  1963. "PRECOMPILE_HEADERS_REUSE_FROM set with non existing target");
  1964. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  1965. return;
  1966. }
  1967. std::string reusedFrom = reusedTarget->GetSafeProperty(prop);
  1968. if (reusedFrom.empty()) {
  1969. reusedFrom = *value;
  1970. }
  1971. this->impl->Properties.SetProperty(prop, reusedFrom);
  1972. reusedTarget->SetProperty("COMPILE_PDB_NAME", reusedFrom);
  1973. reusedTarget->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY",
  1974. cmStrCat(reusedFrom, ".dir/"));
  1975. cmValue tmp = reusedTarget->GetProperty("COMPILE_PDB_NAME");
  1976. this->SetProperty("COMPILE_PDB_NAME", tmp);
  1977. this->AddUtility(reusedFrom, false, this->impl->Makefile);
  1978. } else if (prop == propC_STANDARD || prop == propCXX_STANDARD ||
  1979. prop == propCUDA_STANDARD || prop == propHIP_STANDARD ||
  1980. prop == propOBJC_STANDARD || prop == propOBJCXX_STANDARD) {
  1981. if (value) {
  1982. this->impl->LanguageStandardProperties[prop] =
  1983. BTs<std::string>(value, this->impl->Makefile->GetBacktrace());
  1984. } else {
  1985. this->impl->LanguageStandardProperties.erase(prop);
  1986. }
  1987. } else {
  1988. this->impl->Properties.SetProperty(prop, value);
  1989. }
  1990. }
  1991. void cmTarget::AppendProperty(std::string const& prop,
  1992. std::string const& value,
  1993. cm::optional<cmListFileBacktrace> const& bt,
  1994. bool asString)
  1995. {
  1996. if (!IsSettableProperty(this->impl->Makefile, this, prop)) {
  1997. return;
  1998. }
  1999. if (prop == "IMPORTED_GLOBAL") {
  2000. this->impl->Makefile->IssueMessage(
  2001. MessageType::FATAL_ERROR,
  2002. cmStrCat("IMPORTED_GLOBAL property can't be appended, only set on "
  2003. "imported targets (\"",
  2004. this->impl->Name, "\")\n"));
  2005. }
  2006. if (prop == propPRECOMPILE_HEADERS &&
  2007. this->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM")) {
  2008. this->impl->Makefile->IssueMessage(
  2009. MessageType::FATAL_ERROR,
  2010. cmStrCat(
  2011. "PRECOMPILE_HEADERS_REUSE_FROM property is already set on target (\"",
  2012. this->impl->Name, "\")\n"));
  2013. return;
  2014. }
  2015. UsageRequirementProperty* usageRequirements[] = {
  2016. &this->impl->IncludeDirectories,
  2017. &this->impl->CompileOptions,
  2018. &this->impl->CompileFeatures,
  2019. &this->impl->CompileDefinitions,
  2020. &this->impl->PrecompileHeaders,
  2021. &this->impl->Sources,
  2022. &this->impl->LinkOptions,
  2023. &this->impl->LinkDirectories,
  2024. &this->impl->LinkLibraries,
  2025. &this->impl->InterfaceLinkLibraries,
  2026. &this->impl->InterfaceLinkLibrariesDirect,
  2027. &this->impl->InterfaceLinkLibrariesDirectExclude,
  2028. &this->impl->ImportedCxxModulesIncludeDirectories,
  2029. &this->impl->ImportedCxxModulesCompileDefinitions,
  2030. &this->impl->ImportedCxxModulesCompileFeatures,
  2031. &this->impl->ImportedCxxModulesCompileOptions,
  2032. &this->impl->ImportedCxxModulesLinkLibraries,
  2033. };
  2034. for (auto* usageRequirement : usageRequirements) {
  2035. if (usageRequirement->Write(this->impl.get(), bt, prop, cmValue(value),
  2036. UsageRequirementProperty::Action::Append)) {
  2037. return;
  2038. }
  2039. }
  2040. FileSetType* fileSetTypes[] = {
  2041. &this->impl->HeadersFileSets,
  2042. &this->impl->CxxModulesFileSets,
  2043. };
  2044. for (auto* fileSetType : fileSetTypes) {
  2045. if (fileSetType->WriteProperties(this, this->impl.get(), prop, value,
  2046. FileSetType::Action::Append)) {
  2047. return;
  2048. }
  2049. }
  2050. if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME")) {
  2051. this->impl->Makefile->IssueMessage(
  2052. MessageType::FATAL_ERROR, prop + " property may not be APPENDed.");
  2053. } else if (prop == "C_STANDARD" || prop == "CXX_STANDARD" ||
  2054. prop == "CUDA_STANDARD" || prop == "HIP_STANDARD" ||
  2055. prop == "OBJC_STANDARD" || prop == "OBJCXX_STANDARD") {
  2056. this->impl->Makefile->IssueMessage(
  2057. MessageType::FATAL_ERROR, prop + " property may not be appended.");
  2058. } else {
  2059. this->impl->Properties.AppendProperty(prop, value, asString);
  2060. }
  2061. }
  2062. template <typename ValueType>
  2063. void cmTargetInternals::AddDirectoryToFileSet(cmTarget* self,
  2064. std::string const& fileSetName,
  2065. ValueType value,
  2066. cm::string_view fileSetType,
  2067. cm::string_view description,
  2068. FileSetType::Action action)
  2069. {
  2070. auto* fileSet = self->GetFileSet(fileSetName);
  2071. if (!fileSet) {
  2072. this->Makefile->IssueMessage(
  2073. MessageType::FATAL_ERROR,
  2074. cmStrCat(description, "has not yet been created."));
  2075. return;
  2076. }
  2077. if (fileSet->GetType() != fileSetType) {
  2078. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2079. cmStrCat("File set \"", fileSetName,
  2080. "\" is not of type \"", fileSetType,
  2081. "\"."));
  2082. return;
  2083. }
  2084. if (action == FileSetType::Action::Set) {
  2085. fileSet->ClearDirectoryEntries();
  2086. }
  2087. if (cmNonempty(value)) {
  2088. fileSet->AddDirectoryEntry(
  2089. BT<std::string>(value, this->Makefile->GetBacktrace()));
  2090. }
  2091. }
  2092. template <typename ValueType>
  2093. void cmTargetInternals::AddPathToFileSet(cmTarget* self,
  2094. std::string const& fileSetName,
  2095. ValueType value,
  2096. cm::string_view fileSetType,
  2097. cm::string_view description,
  2098. FileSetType::Action action)
  2099. {
  2100. auto* fileSet = self->GetFileSet(fileSetName);
  2101. if (!fileSet) {
  2102. this->Makefile->IssueMessage(
  2103. MessageType::FATAL_ERROR,
  2104. cmStrCat(description, "has not yet been created."));
  2105. return;
  2106. }
  2107. if (fileSet->GetType() != fileSetType) {
  2108. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2109. cmStrCat("File set \"", fileSetName,
  2110. "\" is not of type \"", fileSetType,
  2111. "\"."));
  2112. return;
  2113. }
  2114. if (action == FileSetType::Action::Set) {
  2115. fileSet->ClearFileEntries();
  2116. }
  2117. if (cmNonempty(value)) {
  2118. fileSet->AddFileEntry(
  2119. BT<std::string>(value, this->Makefile->GetBacktrace()));
  2120. }
  2121. }
  2122. cmValue cmTargetInternals::GetFileSetDirectories(
  2123. cmTarget const* self, std::string const& fileSetName,
  2124. cm::string_view fileSetType) const
  2125. {
  2126. auto const* fileSet = self->GetFileSet(fileSetName);
  2127. if (!fileSet) {
  2128. return nullptr;
  2129. }
  2130. if (fileSet->GetType() != fileSetType) {
  2131. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2132. cmStrCat("File set \"", fileSetName,
  2133. "\" is not of type \"", fileSetType,
  2134. "\"."));
  2135. return nullptr;
  2136. }
  2137. static std::string output;
  2138. output = cmList::to_string(fileSet->GetDirectoryEntries());
  2139. return cmValue(output);
  2140. }
  2141. cmValue cmTargetInternals::GetFileSetPaths(cmTarget const* self,
  2142. std::string const& fileSetName,
  2143. cm::string_view fileSetType) const
  2144. {
  2145. auto const* fileSet = self->GetFileSet(fileSetName);
  2146. if (!fileSet) {
  2147. return nullptr;
  2148. }
  2149. if (fileSet->GetType() != fileSetType) {
  2150. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2151. cmStrCat("File set \"", fileSetName,
  2152. "\" is not of type \"", fileSetType,
  2153. "\"."));
  2154. return nullptr;
  2155. }
  2156. static std::string output;
  2157. output = cmList::to_string(fileSet->GetFileEntries());
  2158. return cmValue(output);
  2159. }
  2160. void cmTarget::AppendBuildInterfaceIncludes()
  2161. {
  2162. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2163. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  2164. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2165. this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  2166. !this->IsExecutableWithExports()) {
  2167. return;
  2168. }
  2169. if (this->impl->BuildInterfaceIncludesAppended) {
  2170. return;
  2171. }
  2172. this->impl->BuildInterfaceIncludesAppended = true;
  2173. if (this->impl->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE")) {
  2174. std::string dirs = this->impl->Makefile->GetCurrentBinaryDirectory();
  2175. if (!dirs.empty()) {
  2176. dirs += ';';
  2177. }
  2178. dirs += this->impl->Makefile->GetCurrentSourceDirectory();
  2179. if (!dirs.empty()) {
  2180. this->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
  2181. ("$<BUILD_INTERFACE:" + dirs + ">"));
  2182. }
  2183. }
  2184. }
  2185. namespace {
  2186. bool CheckLinkLibraryPattern(UsageRequirementProperty const& usage,
  2187. cmake* context)
  2188. {
  2189. // Look for <LINK_LIBRARY:> and </LINK_LIBRARY:> internal tags
  2190. static cmsys::RegularExpression linkPattern(
  2191. "(^|;)(</?LINK_(LIBRARY|GROUP):[^;>]*>)(;|$)");
  2192. bool isValid = true;
  2193. for (auto const& item : usage.Entries) {
  2194. if (!linkPattern.find(item.Value)) {
  2195. continue;
  2196. }
  2197. isValid = false;
  2198. // Report an error.
  2199. context->IssueMessage(
  2200. MessageType::FATAL_ERROR,
  2201. cmStrCat(
  2202. "Property ", usage.Name, " contains the invalid item \"",
  2203. linkPattern.match(2), "\". The ", usage.Name,
  2204. " property may contain the generator-expression \"$<LINK_",
  2205. linkPattern.match(3),
  2206. ":...>\" which may be used to specify how the libraries are linked."),
  2207. item.Backtrace);
  2208. }
  2209. return isValid;
  2210. }
  2211. }
  2212. void cmTarget::FinalizeTargetConfiguration(
  2213. cmBTStringRange const& compileDefinitions)
  2214. {
  2215. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2216. return;
  2217. }
  2218. if (!CheckLinkLibraryPattern(this->impl->LinkLibraries,
  2219. this->GetMakefile()->GetCMakeInstance()) ||
  2220. !CheckLinkLibraryPattern(this->impl->InterfaceLinkLibraries,
  2221. this->GetMakefile()->GetCMakeInstance()) ||
  2222. !CheckLinkLibraryPattern(this->impl->InterfaceLinkLibrariesDirect,
  2223. this->GetMakefile()->GetCMakeInstance())) {
  2224. return;
  2225. }
  2226. this->AppendBuildInterfaceIncludes();
  2227. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2228. return;
  2229. }
  2230. for (auto const& def : compileDefinitions) {
  2231. this->InsertCompileDefinition(def);
  2232. }
  2233. }
  2234. void cmTarget::InsertInclude(BT<std::string> const& entry, bool before)
  2235. {
  2236. this->impl->IncludeDirectories.WriteDirect(
  2237. entry,
  2238. before ? UsageRequirementProperty::Action::Prepend
  2239. : UsageRequirementProperty::Action::Append);
  2240. }
  2241. void cmTarget::InsertCompileOption(BT<std::string> const& entry, bool before)
  2242. {
  2243. this->impl->CompileOptions.WriteDirect(
  2244. entry,
  2245. before ? UsageRequirementProperty::Action::Prepend
  2246. : UsageRequirementProperty::Action::Append);
  2247. }
  2248. void cmTarget::InsertCompileDefinition(BT<std::string> const& entry)
  2249. {
  2250. this->impl->CompileDefinitions.WriteDirect(
  2251. entry, UsageRequirementProperty::Action::Append);
  2252. }
  2253. void cmTarget::InsertLinkOption(BT<std::string> const& entry, bool before)
  2254. {
  2255. this->impl->LinkOptions.WriteDirect(
  2256. entry,
  2257. before ? UsageRequirementProperty::Action::Prepend
  2258. : UsageRequirementProperty::Action::Append);
  2259. }
  2260. void cmTarget::InsertLinkDirectory(BT<std::string> const& entry, bool before)
  2261. {
  2262. this->impl->LinkDirectories.WriteDirect(
  2263. entry,
  2264. before ? UsageRequirementProperty::Action::Prepend
  2265. : UsageRequirementProperty::Action::Append);
  2266. }
  2267. void cmTarget::InsertPrecompileHeader(BT<std::string> const& entry)
  2268. {
  2269. this->impl->PrecompileHeaders.WriteDirect(
  2270. entry, UsageRequirementProperty::Action::Append);
  2271. }
  2272. namespace {
  2273. void CheckLINK_INTERFACE_LIBRARIES(std::string const& prop,
  2274. std::string const& value,
  2275. cmMakefile* context, bool imported)
  2276. {
  2277. // Support imported and non-imported versions of the property.
  2278. char const* base = (imported ? "IMPORTED_LINK_INTERFACE_LIBRARIES"
  2279. : "LINK_INTERFACE_LIBRARIES");
  2280. // Look for link-type keywords in the value.
  2281. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  2282. if (keys.find(value)) {
  2283. // Report an error.
  2284. std::ostringstream e;
  2285. e << "Property " << prop << " may not contain link-type keyword \""
  2286. << keys.match(2) << "\". "
  2287. << "The " << base << " property has a per-configuration "
  2288. << "version called " << base << "_<CONFIG> which may be "
  2289. << "used to specify per-configuration rules.";
  2290. if (!imported) {
  2291. e << " "
  2292. << "Alternatively, an IMPORTED library may be created, configured "
  2293. << "with a per-configuration location, and then named in the "
  2294. << "property value. "
  2295. << "See the add_library command's IMPORTED mode for details."
  2296. << "\n"
  2297. << "If you have a list of libraries that already contains the "
  2298. << "keyword, use the target_link_libraries command with its "
  2299. << "LINK_INTERFACE_LIBRARIES mode to set the property. "
  2300. << "The command automatically recognizes link-type keywords and sets "
  2301. << "the LINK_INTERFACE_LIBRARIES and LINK_INTERFACE_LIBRARIES_DEBUG "
  2302. << "properties accordingly.";
  2303. }
  2304. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  2305. }
  2306. }
  2307. void CheckINTERFACE_LINK_LIBRARIES(std::string const& value,
  2308. cmMakefile* context)
  2309. {
  2310. // Look for link-type keywords in the value.
  2311. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  2312. if (keys.find(value)) {
  2313. // Report an error.
  2314. std::ostringstream e;
  2315. e << "Property INTERFACE_LINK_LIBRARIES may not contain link-type "
  2316. "keyword \""
  2317. << keys.match(2)
  2318. << "\". The INTERFACE_LINK_LIBRARIES "
  2319. "property may contain configuration-sensitive generator-expressions "
  2320. "which may be used to specify per-configuration rules.";
  2321. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  2322. }
  2323. }
  2324. void CheckIMPORTED_GLOBAL(cmTarget const* target, cmMakefile* context)
  2325. {
  2326. auto const& targets = context->GetOwnedImportedTargets();
  2327. auto it =
  2328. std::find_if(targets.begin(), targets.end(),
  2329. [&](std::unique_ptr<cmTarget> const& importTarget) -> bool {
  2330. return target == importTarget.get();
  2331. });
  2332. if (it == targets.end()) {
  2333. std::ostringstream e;
  2334. e << "Attempt to promote imported target \"" << target->GetName()
  2335. << "\" to global scope (by setting IMPORTED_GLOBAL) "
  2336. "which is not built in this directory.";
  2337. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  2338. }
  2339. }
  2340. }
  2341. void cmTarget::CheckProperty(std::string const& prop,
  2342. cmMakefile* context) const
  2343. {
  2344. // Certain properties need checking.
  2345. if (cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES")) {
  2346. if (cmValue value = this->GetProperty(prop)) {
  2347. CheckLINK_INTERFACE_LIBRARIES(prop, *value, context, false);
  2348. }
  2349. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES")) {
  2350. if (cmValue value = this->GetProperty(prop)) {
  2351. CheckLINK_INTERFACE_LIBRARIES(prop, *value, context, true);
  2352. }
  2353. } else if (prop == "INTERFACE_LINK_LIBRARIES") {
  2354. if (cmValue value = this->GetProperty(prop)) {
  2355. CheckINTERFACE_LINK_LIBRARIES(*value, context);
  2356. }
  2357. } else if (prop == "IMPORTED_GLOBAL") {
  2358. if (this->IsImported()) {
  2359. CheckIMPORTED_GLOBAL(this, context);
  2360. }
  2361. }
  2362. }
  2363. cmValue cmTarget::GetComputedProperty(std::string const& prop,
  2364. cmMakefile& mf) const
  2365. {
  2366. return cmTargetPropertyComputer::GetProperty(this, prop, mf);
  2367. }
  2368. cmValue cmTarget::GetProperty(std::string const& prop) const
  2369. {
  2370. static std::unordered_set<std::string> const specialProps{
  2371. propC_STANDARD,
  2372. propCXX_STANDARD,
  2373. propCUDA_STANDARD,
  2374. propHIP_STANDARD,
  2375. propOBJC_STANDARD,
  2376. propOBJCXX_STANDARD,
  2377. propLINK_LIBRARIES,
  2378. propTYPE,
  2379. propINCLUDE_DIRECTORIES,
  2380. propCOMPILE_FEATURES,
  2381. propCOMPILE_OPTIONS,
  2382. propCOMPILE_DEFINITIONS,
  2383. propPRECOMPILE_HEADERS,
  2384. propLINK_OPTIONS,
  2385. propLINK_DIRECTORIES,
  2386. propIMPORTED,
  2387. propIMPORTED_GLOBAL,
  2388. propMANUALLY_ADDED_DEPENDENCIES,
  2389. propNAME,
  2390. propBINARY_DIR,
  2391. propSOURCE_DIR,
  2392. propSOURCES,
  2393. propINTERFACE_LINK_LIBRARIES,
  2394. propINTERFACE_LINK_LIBRARIES_DIRECT,
  2395. propINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  2396. propIMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES,
  2397. propIMPORTED_CXX_MODULES_COMPILE_DEFINITIONS,
  2398. propIMPORTED_CXX_MODULES_COMPILE_FEATURES,
  2399. propIMPORTED_CXX_MODULES_COMPILE_OPTIONS,
  2400. propIMPORTED_CXX_MODULES_LINK_LIBRARIES,
  2401. };
  2402. if (specialProps.count(prop)) {
  2403. if (prop == propC_STANDARD || prop == propCXX_STANDARD ||
  2404. prop == propCUDA_STANDARD || prop == propHIP_STANDARD ||
  2405. prop == propOBJC_STANDARD || prop == propOBJCXX_STANDARD) {
  2406. auto propertyIter = this->impl->LanguageStandardProperties.find(prop);
  2407. if (propertyIter == this->impl->LanguageStandardProperties.end()) {
  2408. return nullptr;
  2409. }
  2410. return cmValue(propertyIter->second.Value);
  2411. }
  2412. UsageRequirementProperty const* usageRequirements[] = {
  2413. &this->impl->IncludeDirectories,
  2414. &this->impl->CompileOptions,
  2415. &this->impl->CompileFeatures,
  2416. &this->impl->CompileDefinitions,
  2417. &this->impl->PrecompileHeaders,
  2418. &this->impl->Sources,
  2419. &this->impl->LinkOptions,
  2420. &this->impl->LinkDirectories,
  2421. &this->impl->LinkLibraries,
  2422. &this->impl->InterfaceLinkLibraries,
  2423. &this->impl->InterfaceLinkLibrariesDirect,
  2424. &this->impl->InterfaceLinkLibrariesDirectExclude,
  2425. &this->impl->ImportedCxxModulesIncludeDirectories,
  2426. &this->impl->ImportedCxxModulesCompileDefinitions,
  2427. &this->impl->ImportedCxxModulesCompileFeatures,
  2428. &this->impl->ImportedCxxModulesCompileOptions,
  2429. &this->impl->ImportedCxxModulesLinkLibraries,
  2430. };
  2431. for (auto const* usageRequirement : usageRequirements) {
  2432. auto value = usageRequirement->Read(prop);
  2433. if (value.first) {
  2434. return value.second;
  2435. }
  2436. }
  2437. // the type property returns what type the target is
  2438. if (prop == propTYPE) {
  2439. return cmValue(cmState::GetTargetTypeName(this->GetType()));
  2440. }
  2441. if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
  2442. if (this->impl->Utilities.empty()) {
  2443. return nullptr;
  2444. }
  2445. static std::string output;
  2446. static std::vector<std::string> utilities;
  2447. utilities.resize(this->impl->Utilities.size());
  2448. std::transform(
  2449. this->impl->Utilities.cbegin(), this->impl->Utilities.cend(),
  2450. utilities.begin(),
  2451. [](const BT<std::pair<std::string, bool>>& item) -> std::string {
  2452. return item.Value.first;
  2453. });
  2454. output = cmList::to_string(utilities);
  2455. return cmValue(output);
  2456. }
  2457. if (prop == propIMPORTED) {
  2458. return this->IsImported() ? cmValue(propTRUE) : cmValue(propFALSE);
  2459. }
  2460. if (prop == propIMPORTED_GLOBAL) {
  2461. return this->IsImportedGloballyVisible() ? cmValue(propTRUE)
  2462. : cmValue(propFALSE);
  2463. }
  2464. if (prop == propNAME) {
  2465. return cmValue(this->GetName());
  2466. }
  2467. if (prop == propBINARY_DIR) {
  2468. return cmValue(this->impl->Makefile->GetStateSnapshot()
  2469. .GetDirectory()
  2470. .GetCurrentBinary());
  2471. }
  2472. if (prop == propSOURCE_DIR) {
  2473. return cmValue(this->impl->Makefile->GetStateSnapshot()
  2474. .GetDirectory()
  2475. .GetCurrentSource());
  2476. }
  2477. }
  2478. // Check fileset properties.
  2479. {
  2480. FileSetType* fileSetTypes[] = {
  2481. &this->impl->HeadersFileSets,
  2482. &this->impl->CxxModulesFileSets,
  2483. };
  2484. for (auto* fileSetType : fileSetTypes) {
  2485. auto value = fileSetType->ReadProperties(this, this->impl.get(), prop);
  2486. if (value.first) {
  2487. return value.second;
  2488. }
  2489. }
  2490. }
  2491. cmValue retVal = this->impl->Properties.GetPropertyValue(prop);
  2492. if (!retVal) {
  2493. bool const chain = this->impl->Makefile->GetState()->IsPropertyChained(
  2494. prop, cmProperty::TARGET);
  2495. if (chain) {
  2496. return this->impl->Makefile->GetStateSnapshot()
  2497. .GetDirectory()
  2498. .GetProperty(prop, chain);
  2499. }
  2500. return nullptr;
  2501. }
  2502. return retVal;
  2503. }
  2504. std::string const& cmTarget::GetSafeProperty(std::string const& prop) const
  2505. {
  2506. cmValue ret = this->GetProperty(prop);
  2507. if (ret) {
  2508. return *ret;
  2509. }
  2510. static std::string const s_empty;
  2511. return s_empty;
  2512. }
  2513. bool cmTarget::GetPropertyAsBool(std::string const& prop) const
  2514. {
  2515. return this->GetProperty(prop).IsOn();
  2516. }
  2517. cmPropertyMap const& cmTarget::GetProperties() const
  2518. {
  2519. return this->impl->Properties;
  2520. }
  2521. bool cmTarget::IsDLLPlatform() const
  2522. {
  2523. return this->impl->IsDLLPlatform;
  2524. }
  2525. bool cmTarget::IsAIX() const
  2526. {
  2527. return this->impl->IsAIX;
  2528. }
  2529. bool cmTarget::IsApple() const
  2530. {
  2531. return this->impl->IsApple;
  2532. }
  2533. bool cmTarget::IsNormal() const
  2534. {
  2535. switch (this->impl->TargetVisibility) {
  2536. case Visibility::Normal:
  2537. return true;
  2538. case Visibility::Generated:
  2539. case Visibility::Imported:
  2540. case Visibility::ImportedGlobally:
  2541. case Visibility::Foreign:
  2542. return false;
  2543. }
  2544. assert(false && "unknown visibility (IsNormal)");
  2545. return false;
  2546. }
  2547. bool cmTarget::IsSynthetic() const
  2548. {
  2549. switch (this->impl->TargetVisibility) {
  2550. case Visibility::Generated:
  2551. return true;
  2552. case Visibility::Normal:
  2553. case Visibility::Imported:
  2554. case Visibility::ImportedGlobally:
  2555. case Visibility::Foreign:
  2556. return false;
  2557. }
  2558. assert(false && "unknown visibility (IsSynthetic)");
  2559. return false;
  2560. }
  2561. bool cmTargetInternals::IsImported() const
  2562. {
  2563. switch (this->TargetVisibility) {
  2564. case cmTarget::Visibility::Imported:
  2565. case cmTarget::Visibility::ImportedGlobally:
  2566. case cmTarget::Visibility::Foreign:
  2567. return true;
  2568. case cmTarget::Visibility::Normal:
  2569. case cmTarget::Visibility::Generated:
  2570. return false;
  2571. }
  2572. assert(false && "unknown visibility (IsImported)");
  2573. return false;
  2574. }
  2575. bool cmTarget::IsImported() const
  2576. {
  2577. return this->impl->IsImported();
  2578. }
  2579. bool cmTarget::IsImportedGloballyVisible() const
  2580. {
  2581. switch (this->impl->TargetVisibility) {
  2582. case Visibility::ImportedGlobally:
  2583. return true;
  2584. case Visibility::Normal:
  2585. case Visibility::Generated:
  2586. case Visibility::Imported:
  2587. case Visibility::Foreign:
  2588. return false;
  2589. }
  2590. assert(false && "unknown visibility (IsImportedGloballyVisible)");
  2591. return false;
  2592. }
  2593. bool cmTarget::IsForeign() const
  2594. {
  2595. switch (this->impl->TargetVisibility) {
  2596. case Visibility::Foreign:
  2597. return true;
  2598. case Visibility::Normal:
  2599. case Visibility::Generated:
  2600. case Visibility::Imported:
  2601. case Visibility::ImportedGlobally:
  2602. return false;
  2603. }
  2604. assert(false && "unknown visibility (isForeign)");
  2605. return false;
  2606. }
  2607. bool cmTarget::IsPerConfig() const
  2608. {
  2609. return this->impl->PerConfig;
  2610. }
  2611. bool cmTarget::IsRuntimeBinary() const
  2612. {
  2613. switch (this->GetType()) {
  2614. case cmStateEnums::EXECUTABLE:
  2615. case cmStateEnums::SHARED_LIBRARY:
  2616. case cmStateEnums::MODULE_LIBRARY:
  2617. return true;
  2618. case cmStateEnums::OBJECT_LIBRARY:
  2619. case cmStateEnums::STATIC_LIBRARY:
  2620. case cmStateEnums::UTILITY:
  2621. case cmStateEnums::INTERFACE_LIBRARY:
  2622. case cmStateEnums::GLOBAL_TARGET:
  2623. case cmStateEnums::UNKNOWN_LIBRARY:
  2624. break;
  2625. }
  2626. return false;
  2627. }
  2628. bool cmTarget::CanCompileSources() const
  2629. {
  2630. if (this->IsImported()) {
  2631. return false;
  2632. }
  2633. if (this->IsSynthetic()) {
  2634. return true;
  2635. }
  2636. switch (this->GetType()) {
  2637. case cmStateEnums::EXECUTABLE:
  2638. case cmStateEnums::STATIC_LIBRARY:
  2639. case cmStateEnums::SHARED_LIBRARY:
  2640. case cmStateEnums::MODULE_LIBRARY:
  2641. case cmStateEnums::OBJECT_LIBRARY:
  2642. return true;
  2643. case cmStateEnums::UTILITY:
  2644. case cmStateEnums::INTERFACE_LIBRARY:
  2645. case cmStateEnums::GLOBAL_TARGET:
  2646. case cmStateEnums::UNKNOWN_LIBRARY:
  2647. break;
  2648. }
  2649. return false;
  2650. }
  2651. char const* cmTarget::GetSuffixVariableInternal(
  2652. cmStateEnums::ArtifactType artifact) const
  2653. {
  2654. switch (this->GetType()) {
  2655. case cmStateEnums::STATIC_LIBRARY:
  2656. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  2657. case cmStateEnums::SHARED_LIBRARY:
  2658. switch (artifact) {
  2659. case cmStateEnums::RuntimeBinaryArtifact:
  2660. return this->IsArchivedAIXSharedLibrary()
  2661. ? "CMAKE_SHARED_LIBRARY_ARCHIVE_SUFFIX"
  2662. : "CMAKE_SHARED_LIBRARY_SUFFIX";
  2663. case cmStateEnums::ImportLibraryArtifact:
  2664. return this->IsApple() ? "CMAKE_APPLE_IMPORT_FILE_SUFFIX"
  2665. : "CMAKE_IMPORT_LIBRARY_SUFFIX";
  2666. }
  2667. break;
  2668. case cmStateEnums::MODULE_LIBRARY:
  2669. switch (artifact) {
  2670. case cmStateEnums::RuntimeBinaryArtifact:
  2671. return "CMAKE_SHARED_MODULE_SUFFIX";
  2672. case cmStateEnums::ImportLibraryArtifact:
  2673. return "CMAKE_IMPORT_LIBRARY_SUFFIX";
  2674. }
  2675. break;
  2676. case cmStateEnums::EXECUTABLE:
  2677. switch (artifact) {
  2678. case cmStateEnums::RuntimeBinaryArtifact:
  2679. // Android GUI application packages store the native
  2680. // binary as a shared library.
  2681. return (this->IsAndroidGuiExecutable()
  2682. ? "CMAKE_SHARED_LIBRARY_SUFFIX"
  2683. : "CMAKE_EXECUTABLE_SUFFIX");
  2684. case cmStateEnums::ImportLibraryArtifact:
  2685. return (this->impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_SUFFIX"
  2686. : "CMAKE_IMPORT_LIBRARY_SUFFIX");
  2687. }
  2688. break;
  2689. default:
  2690. break;
  2691. }
  2692. return "";
  2693. }
  2694. char const* cmTarget::GetPrefixVariableInternal(
  2695. cmStateEnums::ArtifactType artifact) const
  2696. {
  2697. switch (this->GetType()) {
  2698. case cmStateEnums::STATIC_LIBRARY:
  2699. return "CMAKE_STATIC_LIBRARY_PREFIX";
  2700. case cmStateEnums::SHARED_LIBRARY:
  2701. switch (artifact) {
  2702. case cmStateEnums::RuntimeBinaryArtifact:
  2703. return "CMAKE_SHARED_LIBRARY_PREFIX";
  2704. case cmStateEnums::ImportLibraryArtifact:
  2705. return this->IsApple() ? "CMAKE_APPLE_IMPORT_FILE_PREFIX"
  2706. : "CMAKE_IMPORT_LIBRARY_PREFIX";
  2707. }
  2708. break;
  2709. case cmStateEnums::MODULE_LIBRARY:
  2710. switch (artifact) {
  2711. case cmStateEnums::RuntimeBinaryArtifact:
  2712. return "CMAKE_SHARED_MODULE_PREFIX";
  2713. case cmStateEnums::ImportLibraryArtifact:
  2714. return "CMAKE_IMPORT_LIBRARY_PREFIX";
  2715. }
  2716. break;
  2717. case cmStateEnums::EXECUTABLE:
  2718. switch (artifact) {
  2719. case cmStateEnums::RuntimeBinaryArtifact:
  2720. // Android GUI application packages store the native
  2721. // binary as a shared library.
  2722. return (this->IsAndroidGuiExecutable()
  2723. ? "CMAKE_SHARED_LIBRARY_PREFIX"
  2724. : "");
  2725. case cmStateEnums::ImportLibraryArtifact:
  2726. return (this->impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_PREFIX"
  2727. : "CMAKE_IMPORT_LIBRARY_PREFIX");
  2728. }
  2729. break;
  2730. default:
  2731. break;
  2732. }
  2733. return "";
  2734. }
  2735. std::string cmTarget::ImportedGetFullPath(
  2736. std::string const& config, cmStateEnums::ArtifactType artifact) const
  2737. {
  2738. assert(this->IsImported());
  2739. // Lookup/compute/cache the import information for this
  2740. // configuration.
  2741. std::string desired_config = config;
  2742. if (config.empty()) {
  2743. desired_config = "NOCONFIG";
  2744. }
  2745. std::string result;
  2746. cmValue loc = nullptr;
  2747. cmValue imp = nullptr;
  2748. std::string suffix;
  2749. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  2750. this->GetMappedConfig(desired_config, loc, imp, suffix)) {
  2751. switch (artifact) {
  2752. case cmStateEnums::RuntimeBinaryArtifact:
  2753. if (loc) {
  2754. result = *loc;
  2755. } else if (imp) {
  2756. result = *imp;
  2757. } else {
  2758. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  2759. if (cmValue config_location = this->GetProperty(impProp)) {
  2760. result = *config_location;
  2761. } else if (cmValue location =
  2762. this->GetProperty("IMPORTED_LOCATION")) {
  2763. result = *location;
  2764. }
  2765. if (result.empty() &&
  2766. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2767. this->IsExecutableWithExports())) {
  2768. impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  2769. if (cmValue config_implib = this->GetProperty(impProp)) {
  2770. result = *config_implib;
  2771. } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
  2772. result = *implib;
  2773. }
  2774. }
  2775. }
  2776. if (this->IsApple() &&
  2777. (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  2778. this->impl->TargetType == cmStateEnums::STATIC_LIBRARY ||
  2779. this->impl->TargetType == cmStateEnums::UNKNOWN_LIBRARY) &&
  2780. cmSystemTools::IsPathToXcFramework(result)) {
  2781. auto plist = cmParseXcFrameworkPlist(result, *this->impl->Makefile,
  2782. this->impl->Backtrace);
  2783. if (!plist) {
  2784. return "";
  2785. }
  2786. auto const* library = plist->SelectSuitableLibrary(
  2787. *this->impl->Makefile, this->impl->Backtrace);
  2788. if (library) {
  2789. result = cmStrCat(result, '/', library->LibraryIdentifier, '/',
  2790. library->LibraryPath);
  2791. } else {
  2792. return "";
  2793. }
  2794. }
  2795. break;
  2796. case cmStateEnums::ImportLibraryArtifact:
  2797. if (imp) {
  2798. result = *imp;
  2799. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2800. this->IsExecutableWithExports()) {
  2801. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  2802. if (cmValue config_implib = this->GetProperty(impProp)) {
  2803. result = *config_implib;
  2804. } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
  2805. result = *implib;
  2806. }
  2807. }
  2808. break;
  2809. }
  2810. }
  2811. if (result.empty()) {
  2812. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2813. auto message = [&]() -> std::string {
  2814. std::string unset;
  2815. std::string configuration;
  2816. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  2817. artifact == cmStateEnums::RuntimeBinaryArtifact) {
  2818. unset = "IMPORTED_LOCATION or IMPORTED_IMPLIB";
  2819. } else if (artifact == cmStateEnums::RuntimeBinaryArtifact) {
  2820. unset = "IMPORTED_LOCATION";
  2821. } else if (artifact == cmStateEnums::ImportLibraryArtifact) {
  2822. unset = "IMPORTED_IMPLIB";
  2823. }
  2824. if (!config.empty()) {
  2825. configuration = cmStrCat(" configuration \"", config, '"');
  2826. }
  2827. return cmStrCat(unset, " not set for imported target \"",
  2828. this->GetName(), '"', configuration, '.');
  2829. };
  2830. switch (this->GetPolicyStatus(cmPolicies::CMP0111)) {
  2831. case cmPolicies::WARN:
  2832. this->impl->Makefile->IssueMessage(
  2833. MessageType::AUTHOR_WARNING,
  2834. cmPolicies::GetPolicyWarning(cmPolicies::CMP0111) + "\n" +
  2835. message());
  2836. CM_FALLTHROUGH;
  2837. case cmPolicies::OLD:
  2838. break;
  2839. default:
  2840. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2841. message());
  2842. }
  2843. }
  2844. result = cmStrCat(this->GetName(), "-NOTFOUND");
  2845. }
  2846. return result;
  2847. }
  2848. cmFileSet const* cmTarget::GetFileSet(std::string const& name) const
  2849. {
  2850. auto it = this->impl->FileSets.find(name);
  2851. return it == this->impl->FileSets.end() ? nullptr : &it->second;
  2852. }
  2853. cmFileSet* cmTarget::GetFileSet(std::string const& name)
  2854. {
  2855. auto it = this->impl->FileSets.find(name);
  2856. return it == this->impl->FileSets.end() ? nullptr : &it->second;
  2857. }
  2858. std::pair<cmFileSet*, bool> cmTarget::GetOrCreateFileSet(
  2859. std::string const& name, std::string const& type, cmFileSetVisibility vis)
  2860. {
  2861. auto result = this->impl->FileSets.emplace(
  2862. name,
  2863. cmFileSet(*this->GetMakefile()->GetCMakeInstance(), name, type, vis));
  2864. if (result.second) {
  2865. auto bt = this->impl->Makefile->GetBacktrace();
  2866. if (type == this->impl->HeadersFileSets.TypeName) {
  2867. this->impl->HeadersFileSets.AddFileSet(name, vis, std::move(bt));
  2868. } else if (type == this->impl->CxxModulesFileSets.TypeName) {
  2869. this->impl->CxxModulesFileSets.AddFileSet(name, vis, std::move(bt));
  2870. }
  2871. }
  2872. return std::make_pair(&result.first->second, result.second);
  2873. }
  2874. std::string cmTarget::GetFileSetsPropertyName(std::string const& type)
  2875. {
  2876. if (type == "HEADERS") {
  2877. return "HEADER_SETS";
  2878. }
  2879. if (type == "CXX_MODULES") {
  2880. return "CXX_MODULE_SETS";
  2881. }
  2882. return "";
  2883. }
  2884. std::string cmTarget::GetInterfaceFileSetsPropertyName(std::string const& type)
  2885. {
  2886. if (type == "HEADERS") {
  2887. return "INTERFACE_HEADER_SETS";
  2888. }
  2889. if (type == "CXX_MODULES") {
  2890. return "INTERFACE_CXX_MODULE_SETS";
  2891. }
  2892. return "";
  2893. }
  2894. std::vector<std::string> cmTarget::GetAllFileSetNames() const
  2895. {
  2896. std::vector<std::string> result;
  2897. for (auto const& it : this->impl->FileSets) {
  2898. result.push_back(it.first);
  2899. }
  2900. return result;
  2901. }
  2902. std::vector<std::string> cmTarget::GetAllInterfaceFileSets() const
  2903. {
  2904. std::vector<std::string> result;
  2905. auto inserter = std::back_inserter(result);
  2906. auto appendEntries = [=](std::vector<BT<std::string>> const& entries) {
  2907. for (auto const& entry : entries) {
  2908. cmList expanded{ entry.Value };
  2909. std::copy(expanded.begin(), expanded.end(), inserter);
  2910. }
  2911. };
  2912. appendEntries(this->impl->HeadersFileSets.InterfaceEntries.Entries);
  2913. appendEntries(this->impl->CxxModulesFileSets.InterfaceEntries.Entries);
  2914. return result;
  2915. }
  2916. bool cmTarget::HasFileSets() const
  2917. {
  2918. return !this->impl->FileSets.empty();
  2919. }
  2920. bool cmTargetInternals::CheckImportedLibName(std::string const& prop,
  2921. std::string const& value) const
  2922. {
  2923. if (this->TargetType != cmStateEnums::INTERFACE_LIBRARY ||
  2924. !this->IsImported()) {
  2925. this->Makefile->IssueMessage(
  2926. MessageType::FATAL_ERROR,
  2927. prop +
  2928. " property may be set only on imported INTERFACE library targets.");
  2929. return false;
  2930. }
  2931. if (!value.empty()) {
  2932. if (value[0] == '-') {
  2933. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2934. prop + " property value\n " + value +
  2935. "\nmay not start with '-'.");
  2936. return false;
  2937. }
  2938. std::string::size_type bad = value.find_first_of(":/\\;");
  2939. if (bad != std::string::npos) {
  2940. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2941. prop + " property value\n " + value +
  2942. "\nmay not contain '" +
  2943. value.substr(bad, 1) + "'.");
  2944. return false;
  2945. }
  2946. }
  2947. return true;
  2948. }
  2949. bool cmTarget::GetMappedConfig(std::string const& desired_config, cmValue& loc,
  2950. cmValue& imp, std::string& suffix) const
  2951. {
  2952. std::string config_upper;
  2953. if (!desired_config.empty()) {
  2954. config_upper = cmSystemTools::UpperCase(desired_config);
  2955. }
  2956. std::string locPropBase;
  2957. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2958. locPropBase = "IMPORTED_LIBNAME";
  2959. } else if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2960. locPropBase = "IMPORTED_OBJECTS";
  2961. } else {
  2962. locPropBase = "IMPORTED_LOCATION";
  2963. }
  2964. // Track the configuration-specific property suffix.
  2965. suffix = cmStrCat('_', config_upper);
  2966. cmList mappedConfigs;
  2967. {
  2968. std::string mapProp = cmStrCat("MAP_IMPORTED_CONFIG_", config_upper);
  2969. if (cmValue mapValue = this->GetProperty(mapProp)) {
  2970. mappedConfigs.assign(*mapValue, cmList::EmptyElements::Yes);
  2971. }
  2972. }
  2973. // If we needed to find one of the mapped configurations but did not
  2974. // There may be only IMPORTED_IMPLIB for a shared library or an executable
  2975. // with exports.
  2976. bool allowImp = (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2977. this->IsExecutableWithExports()) ||
  2978. (this->IsAIX() && this->IsExecutableWithExports()) ||
  2979. (this->GetMakefile()->PlatformSupportsAppleTextStubs() &&
  2980. this->IsSharedLibraryWithExports());
  2981. // If a mapping was found, check its configurations.
  2982. for (auto mci = mappedConfigs.begin();
  2983. !loc && !imp && mci != mappedConfigs.end(); ++mci) {
  2984. // Look for this configuration.
  2985. if (mci->empty()) {
  2986. // An empty string in the mapping has a special meaning:
  2987. // look up the config-less properties.
  2988. loc = this->GetProperty(locPropBase);
  2989. if (allowImp) {
  2990. imp = this->GetProperty("IMPORTED_IMPLIB");
  2991. }
  2992. // If it was found, set the suffix.
  2993. if (loc || imp) {
  2994. suffix.clear();
  2995. }
  2996. } else {
  2997. std::string mcUpper = cmSystemTools::UpperCase(*mci);
  2998. std::string locProp = cmStrCat(locPropBase, '_', mcUpper);
  2999. loc = this->GetProperty(locProp);
  3000. if (allowImp) {
  3001. std::string impProp = cmStrCat("IMPORTED_IMPLIB_", mcUpper);
  3002. imp = this->GetProperty(impProp);
  3003. }
  3004. // If it was found, use it for all properties below.
  3005. if (loc || imp) {
  3006. suffix = cmStrCat('_', mcUpper);
  3007. }
  3008. }
  3009. }
  3010. // If we needed to find one of the mapped configurations but did not
  3011. // then the target location is not found. The project does not want
  3012. // any other configuration.
  3013. if (!mappedConfigs.empty() && !loc && !imp) {
  3014. // Interface libraries are always available because their
  3015. // library name is optional so it is okay to leave loc empty.
  3016. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  3017. }
  3018. // If we have not yet found it then there are no mapped
  3019. // configurations. Look for an exact-match.
  3020. if (!loc && !imp) {
  3021. std::string locProp = cmStrCat(locPropBase, suffix);
  3022. loc = this->GetProperty(locProp);
  3023. if (allowImp) {
  3024. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  3025. imp = this->GetProperty(impProp);
  3026. }
  3027. }
  3028. // If we have not yet found it then there are no mapped
  3029. // configurations and no exact match.
  3030. if (!loc && !imp) {
  3031. // The suffix computed above is not useful.
  3032. suffix.clear();
  3033. // Look for a configuration-less location. This may be set by
  3034. // manually-written code.
  3035. loc = this->GetProperty(locPropBase);
  3036. if (allowImp) {
  3037. imp = this->GetProperty("IMPORTED_IMPLIB");
  3038. }
  3039. }
  3040. // If we have not yet found it then the project is willing to try
  3041. // any available configuration.
  3042. if (!loc && !imp) {
  3043. cmList availableConfigs;
  3044. if (cmValue iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS")) {
  3045. availableConfigs.assign(*iconfigs);
  3046. }
  3047. for (auto it = availableConfigs.begin();
  3048. !loc && !imp && it != availableConfigs.end(); ++it) {
  3049. suffix = cmStrCat('_', cmSystemTools::UpperCase(*it));
  3050. std::string locProp = cmStrCat(locPropBase, suffix);
  3051. loc = this->GetProperty(locProp);
  3052. if (allowImp) {
  3053. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  3054. imp = this->GetProperty(impProp);
  3055. }
  3056. }
  3057. }
  3058. // If we have not yet found it then the target location is not available.
  3059. if (!loc && !imp) {
  3060. // Interface libraries are always available because their
  3061. // library name is optional so it is okay to leave loc empty.
  3062. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  3063. }
  3064. return true;
  3065. }