cmTarget.cxx 59 KB

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