cmTarget.cxx 65 KB

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