cmTarget.cxx 103 KB

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