cmTarget.cxx 64 KB

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