cmTarget.cxx 95 KB

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