cmTarget.cxx 68 KB

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