cmTarget.cxx 110 KB

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