cmTarget.cxx 110 KB

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