cmTarget.cxx 65 KB

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