cmTarget.cxx 65 KB

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