cmTarget.cxx 69 KB

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