cmTarget.cxx 67 KB

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