cmTarget.cxx 62 KB

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