cmTarget.cxx 69 KB

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