cmTarget.cxx 65 KB

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