cmTarget.cxx 62 KB

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