cmTarget.cxx 85 KB

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