cmTarget.cxx 82 KB

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