cmTarget.cxx 61 KB

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