cmTarget.cxx 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmTarget.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstring>
  7. #include <initializer_list>
  8. #include <iterator>
  9. #include <map>
  10. #include <set>
  11. #include <sstream>
  12. #include <unordered_set>
  13. #include <cm/memory>
  14. #include <cmext/algorithm>
  15. #include <cmext/string_view>
  16. #include "cmsys/RegularExpression.hxx"
  17. #include "cmAlgorithms.h"
  18. #include "cmCustomCommand.h"
  19. #include "cmFileSet.h"
  20. #include "cmGeneratorExpression.h"
  21. #include "cmGeneratorTarget.h"
  22. #include "cmGlobalGenerator.h"
  23. #include "cmListFileCache.h"
  24. #include "cmMakefile.h"
  25. #include "cmMessageType.h"
  26. #include "cmProperty.h"
  27. #include "cmPropertyDefinition.h"
  28. #include "cmPropertyMap.h"
  29. #include "cmRange.h"
  30. #include "cmSourceFile.h"
  31. #include "cmSourceFileLocation.h"
  32. #include "cmSourceFileLocationKind.h"
  33. #include "cmState.h"
  34. #include "cmStateDirectory.h"
  35. #include "cmStateSnapshot.h"
  36. #include "cmSystemTools.h"
  37. #include "cmTargetPropertyComputer.h"
  38. #include "cmValue.h"
  39. #include "cmake.h"
  40. template <>
  41. const std::string& 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;
  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;
  56. }
  57. template <>
  58. const std::string& 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;
  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;
  74. }
  75. template <>
  76. cmValue cmTargetPropertyComputer::GetSources<cmTarget>(cmTarget const* tgt,
  77. cmMakefile const& mf)
  78. {
  79. cmBTStringRange entries = tgt->GetSourceEntries();
  80. if (entries.empty()) {
  81. return nullptr;
  82. }
  83. std::ostringstream ss;
  84. const char* sep = "";
  85. for (auto const& entry : entries) {
  86. std::vector<std::string> files = cmExpandedList(entry.Value);
  87. for (std::string const& file : files) {
  88. if (cmHasLiteralPrefix(file, "$<TARGET_OBJECTS:") &&
  89. file.back() == '>') {
  90. std::string objLibName = file.substr(17, file.size() - 18);
  91. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  92. ss << sep;
  93. sep = ";";
  94. ss << file;
  95. continue;
  96. }
  97. bool addContent = false;
  98. bool noMessage = true;
  99. std::ostringstream e;
  100. MessageType messageType = MessageType::AUTHOR_WARNING;
  101. switch (mf.GetPolicyStatus(cmPolicies::CMP0051)) {
  102. case cmPolicies::WARN:
  103. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0051) << "\n";
  104. noMessage = false;
  105. CM_FALLTHROUGH;
  106. case cmPolicies::OLD:
  107. break;
  108. case cmPolicies::REQUIRED_ALWAYS:
  109. case cmPolicies::REQUIRED_IF_USED:
  110. case cmPolicies::NEW:
  111. addContent = true;
  112. break;
  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. mf.IssueMessage(messageType, e.str());
  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 cmValue(srcs);
  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 IsDLLPlatform;
  166. bool IsAIX;
  167. bool IsAndroid;
  168. bool IsImportedTarget;
  169. bool ImportedGloballyVisible;
  170. bool BuildInterfaceIncludesAppended;
  171. bool PerConfig;
  172. std::set<BT<std::pair<std::string, bool>>> Utilities;
  173. std::vector<cmCustomCommand> PreBuildCommands;
  174. std::vector<cmCustomCommand> PreLinkCommands;
  175. std::vector<cmCustomCommand> PostBuildCommands;
  176. std::vector<cmInstallTargetGenerator*> InstallGenerators;
  177. std::set<std::string> SystemIncludeDirectories;
  178. cmTarget::LinkLibraryVectorType OriginalLinkLibraries;
  179. std::map<std::string, BTs<std::string>> LanguageStandardProperties;
  180. std::vector<BT<std::string>> IncludeDirectoriesEntries;
  181. std::map<cmTargetExport const*, std::vector<std::string>>
  182. InstallIncludeDirectoriesEntries;
  183. std::vector<BT<std::string>> CompileOptionsEntries;
  184. std::vector<BT<std::string>> CompileFeaturesEntries;
  185. std::vector<BT<std::string>> CompileDefinitionsEntries;
  186. std::vector<BT<std::string>> PrecompileHeadersEntries;
  187. std::vector<BT<std::string>> SourceEntries;
  188. std::vector<BT<std::string>> LinkOptionsEntries;
  189. std::vector<BT<std::string>> LinkDirectoriesEntries;
  190. std::vector<BT<std::string>> LinkImplementationPropertyEntries;
  191. std::vector<BT<std::string>> LinkInterfacePropertyEntries;
  192. std::vector<BT<std::string>> LinkInterfaceDirectPropertyEntries;
  193. std::vector<BT<std::string>> LinkInterfaceDirectExcludePropertyEntries;
  194. std::vector<BT<std::string>> HeaderSetsEntries;
  195. std::vector<BT<std::string>> InterfaceHeaderSetsEntries;
  196. std::vector<std::pair<cmTarget::TLLSignature, cmListFileContext>>
  197. TLLCommands;
  198. std::map<std::string, cmFileSet> FileSets;
  199. cmListFileBacktrace Backtrace;
  200. bool CheckImportedLibName(std::string const& prop,
  201. std::string const& value) const;
  202. std::string ProcessSourceItemCMP0049(const std::string& s) const;
  203. };
  204. namespace {
  205. #define SETUP_COMMON_LANGUAGE_PROPERTIES(lang) \
  206. initProp(#lang "_COMPILER_LAUNCHER"); \
  207. initProp(#lang "_STANDARD"); \
  208. initProp(#lang "_STANDARD_REQUIRED"); \
  209. initProp(#lang "_EXTENSIONS"); \
  210. initProp(#lang "_VISIBILITY_PRESET")
  211. }
  212. cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type,
  213. Visibility vis, cmMakefile* mf, PerConfig perConfig)
  214. : impl(cm::make_unique<cmTargetInternals>())
  215. {
  216. assert(mf);
  217. this->impl->TargetType = type;
  218. this->impl->Makefile = mf;
  219. this->impl->Name = name;
  220. this->impl->IsGeneratorProvided = false;
  221. this->impl->HaveInstallRule = false;
  222. this->impl->IsDLLPlatform = false;
  223. this->impl->IsAIX = false;
  224. this->impl->IsAndroid = false;
  225. this->impl->IsImportedTarget =
  226. (vis == VisibilityImported || vis == VisibilityImportedGlobally);
  227. this->impl->ImportedGloballyVisible = vis == VisibilityImportedGlobally;
  228. this->impl->BuildInterfaceIncludesAppended = false;
  229. this->impl->PerConfig = (perConfig == PerConfig::Yes);
  230. // Check whether this is a DLL platform.
  231. this->impl->IsDLLPlatform =
  232. !this->impl->Makefile->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")
  233. .empty();
  234. // Check whether we are targeting AIX.
  235. {
  236. std::string const& systemName =
  237. this->impl->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  238. this->impl->IsAIX = (systemName == "AIX" || systemName == "OS400");
  239. }
  240. // Check whether we are targeting an Android platform.
  241. this->impl->IsAndroid = (this->impl->Makefile->GetSafeDefinition(
  242. "CMAKE_SYSTEM_NAME") == "Android");
  243. std::string defKey;
  244. defKey.reserve(128);
  245. defKey += "CMAKE_";
  246. auto initProp = [this, mf, &defKey](const std::string& property) {
  247. // Replace everything after "CMAKE_"
  248. defKey.replace(defKey.begin() + 6, defKey.end(), property);
  249. if (cmValue value = mf->GetDefinition(defKey)) {
  250. this->SetProperty(property, value);
  251. }
  252. };
  253. auto initPropValue = [this, mf, &defKey](const std::string& property,
  254. const char* default_value) {
  255. // Replace everything after "CMAKE_"
  256. defKey.replace(defKey.begin() + 6, defKey.end(), property);
  257. if (cmValue value = mf->GetDefinition(defKey)) {
  258. this->SetProperty(property, value);
  259. } else if (default_value) {
  260. this->SetProperty(property, default_value);
  261. }
  262. };
  263. // Setup default property values.
  264. if (this->CanCompileSources()) {
  265. SETUP_COMMON_LANGUAGE_PROPERTIES(C);
  266. SETUP_COMMON_LANGUAGE_PROPERTIES(OBJC);
  267. SETUP_COMMON_LANGUAGE_PROPERTIES(CXX);
  268. SETUP_COMMON_LANGUAGE_PROPERTIES(OBJCXX);
  269. SETUP_COMMON_LANGUAGE_PROPERTIES(CUDA);
  270. SETUP_COMMON_LANGUAGE_PROPERTIES(HIP);
  271. initProp("ANDROID_API");
  272. initProp("ANDROID_API_MIN");
  273. initProp("ANDROID_ARCH");
  274. initProp("ANDROID_STL_TYPE");
  275. initProp("ANDROID_SKIP_ANT_STEP");
  276. initProp("ANDROID_PROCESS_MAX");
  277. initProp("ANDROID_PROGUARD");
  278. initProp("ANDROID_PROGUARD_CONFIG_PATH");
  279. initProp("ANDROID_SECURE_PROPS_PATH");
  280. initProp("ANDROID_NATIVE_LIB_DIRECTORIES");
  281. initProp("ANDROID_NATIVE_LIB_DEPENDENCIES");
  282. initProp("ANDROID_JAVA_SOURCE_DIR");
  283. initProp("ANDROID_JAR_DIRECTORIES");
  284. initProp("ANDROID_JAR_DEPENDENCIES");
  285. initProp("ANDROID_ASSETS_DIRECTORIES");
  286. initProp("ANDROID_ANT_ADDITIONAL_OPTIONS");
  287. initProp("BUILD_RPATH");
  288. initProp("BUILD_RPATH_USE_ORIGIN");
  289. initProp("INSTALL_NAME_DIR");
  290. initProp("INSTALL_REMOVE_ENVIRONMENT_RPATH");
  291. initPropValue("INSTALL_RPATH", "");
  292. initPropValue("INSTALL_RPATH_USE_LINK_PATH", "OFF");
  293. initProp("INTERPROCEDURAL_OPTIMIZATION");
  294. initPropValue("SKIP_BUILD_RPATH", "OFF");
  295. initPropValue("BUILD_WITH_INSTALL_RPATH", "OFF");
  296. initProp("ARCHIVE_OUTPUT_DIRECTORY");
  297. initProp("LIBRARY_OUTPUT_DIRECTORY");
  298. initProp("RUNTIME_OUTPUT_DIRECTORY");
  299. initProp("PDB_OUTPUT_DIRECTORY");
  300. initProp("COMPILE_PDB_OUTPUT_DIRECTORY");
  301. initProp("FRAMEWORK");
  302. initProp("FRAMEWORK_MULTI_CONFIG_POSTFIX");
  303. initProp("Fortran_FORMAT");
  304. initProp("Fortran_MODULE_DIRECTORY");
  305. initProp("Fortran_COMPILER_LAUNCHER");
  306. initProp("Fortran_PREPROCESS");
  307. initProp("Fortran_VISIBILITY_PRESET");
  308. initProp("GNUtoMS");
  309. initProp("OSX_ARCHITECTURES");
  310. initProp("IOS_INSTALL_COMBINED");
  311. initProp("AUTOMOC");
  312. initProp("AUTOUIC");
  313. initProp("AUTORCC");
  314. initProp("AUTOGEN_ORIGIN_DEPENDS");
  315. initProp("AUTOGEN_PARALLEL");
  316. initProp("AUTOMOC_COMPILER_PREDEFINES");
  317. initProp("AUTOMOC_DEPEND_FILTERS");
  318. initProp("AUTOMOC_MACRO_NAMES");
  319. initProp("AUTOMOC_MOC_OPTIONS");
  320. initProp("AUTOUIC_OPTIONS");
  321. initProp("AUTOMOC_PATH_PREFIX");
  322. initProp("AUTOUIC_SEARCH_PATHS");
  323. initProp("AUTORCC_OPTIONS");
  324. initProp("LINK_DEPENDS_NO_SHARED");
  325. initProp("LINK_INTERFACE_LIBRARIES");
  326. initProp("MSVC_RUNTIME_LIBRARY");
  327. initProp("WIN32_EXECUTABLE");
  328. initProp("MACOSX_BUNDLE");
  329. initProp("MACOSX_RPATH");
  330. initProp("NO_SYSTEM_FROM_IMPORTED");
  331. initProp("BUILD_WITH_INSTALL_NAME_DIR");
  332. initProp("C_CLANG_TIDY");
  333. initProp("C_CPPLINT");
  334. initProp("C_CPPCHECK");
  335. initProp("C_INCLUDE_WHAT_YOU_USE");
  336. initProp("C_LINKER_LAUNCHER");
  337. initProp("LINK_WHAT_YOU_USE");
  338. initProp("CXX_CLANG_TIDY");
  339. initProp("CXX_CPPLINT");
  340. initProp("CXX_CPPCHECK");
  341. initProp("CXX_INCLUDE_WHAT_YOU_USE");
  342. initProp("CXX_LINKER_LAUNCHER");
  343. initProp("CUDA_SEPARABLE_COMPILATION");
  344. initProp("CUDA_RESOLVE_DEVICE_SYMBOLS");
  345. initProp("CUDA_RUNTIME_LIBRARY");
  346. initProp("CUDA_ARCHITECTURES");
  347. initProp("HIP_RUNTIME_LIBRARY");
  348. initProp("HIP_ARCHITECTURES");
  349. initProp("VISIBILITY_INLINES_HIDDEN");
  350. initProp("JOB_POOL_COMPILE");
  351. initProp("JOB_POOL_LINK");
  352. initProp("JOB_POOL_PRECOMPILE_HEADER");
  353. initProp("ISPC_COMPILER_LAUNCHER");
  354. initProp("ISPC_HEADER_DIRECTORY");
  355. initPropValue("ISPC_HEADER_SUFFIX", "_ispc.h");
  356. initProp("ISPC_INSTRUCTION_SETS");
  357. initProp("LINK_SEARCH_START_STATIC");
  358. initProp("LINK_SEARCH_END_STATIC");
  359. initProp("OBJC_CLANG_TIDY");
  360. initProp("OBJC_LINKER_LAUNCHER");
  361. initProp("OBJCXX_CLANG_TIDY");
  362. initProp("OBJCXX_LINKER_LAUNCHER");
  363. initProp("Swift_LANGUAGE_VERSION");
  364. initProp("Swift_MODULE_DIRECTORY");
  365. initProp("VS_JUST_MY_CODE_DEBUGGING");
  366. initProp("DISABLE_PRECOMPILE_HEADERS");
  367. initProp("UNITY_BUILD");
  368. initProp("UNITY_BUILD_UNIQUE_ID");
  369. initProp("OPTIMIZE_DEPENDENCIES");
  370. initProp("EXPORT_COMPILE_COMMANDS");
  371. initPropValue("UNITY_BUILD_BATCH_SIZE", "8");
  372. initPropValue("UNITY_BUILD_MODE", "BATCH");
  373. initPropValue("PCH_WARN_INVALID", "ON");
  374. initPropValue("PCH_INSTANTIATE_TEMPLATES", "ON");
  375. #ifdef __APPLE__
  376. if (this->GetGlobalGenerator()->IsXcode()) {
  377. initProp("XCODE_SCHEME_ADDRESS_SANITIZER");
  378. initProp("XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN");
  379. initProp("XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING");
  380. initProp("XCODE_SCHEME_ENABLE_GPU_FRAME_CAPTURE_MODE");
  381. initProp("XCODE_SCHEME_THREAD_SANITIZER");
  382. initProp("XCODE_SCHEME_THREAD_SANITIZER_STOP");
  383. initProp("XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER");
  384. initProp("XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP");
  385. initProp("XCODE_SCHEME_WORKING_DIRECTORY");
  386. initProp("XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER");
  387. initProp("XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP");
  388. initProp("XCODE_SCHEME_MALLOC_SCRIBBLE");
  389. initProp("XCODE_SCHEME_MALLOC_GUARD_EDGES");
  390. initProp("XCODE_SCHEME_GUARD_MALLOC");
  391. initProp("XCODE_SCHEME_ZOMBIE_OBJECTS");
  392. initProp("XCODE_SCHEME_MALLOC_STACK");
  393. initProp("XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE");
  394. initProp("XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS");
  395. initProp("XCODE_SCHEME_ENVIRONMENT");
  396. initPropValue("XCODE_LINK_BUILD_PHASE_MODE", "NONE");
  397. }
  398. #endif
  399. }
  400. initProp("FOLDER");
  401. if (this->GetGlobalGenerator()->IsXcode()) {
  402. initProp("XCODE_GENERATE_SCHEME");
  403. }
  404. // Setup per-configuration property default values.
  405. if (this->GetType() != cmStateEnums::UTILITY &&
  406. this->GetType() != cmStateEnums::GLOBAL_TARGET) {
  407. static const auto configProps = {
  408. /* clang-format needs this comment to break after the opening brace */
  409. "ARCHIVE_OUTPUT_DIRECTORY_", "LIBRARY_OUTPUT_DIRECTORY_",
  410. "RUNTIME_OUTPUT_DIRECTORY_", "PDB_OUTPUT_DIRECTORY_",
  411. "COMPILE_PDB_OUTPUT_DIRECTORY_", "MAP_IMPORTED_CONFIG_",
  412. "INTERPROCEDURAL_OPTIMIZATION_"
  413. };
  414. // Collect the set of configuration types.
  415. std::vector<std::string> configNames =
  416. mf->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  417. for (std::string const& configName : configNames) {
  418. std::string configUpper = cmSystemTools::UpperCase(configName);
  419. for (auto const& prop : configProps) {
  420. // Interface libraries have no output locations, so honor only
  421. // the configuration map.
  422. if (this->impl->TargetType == cmStateEnums::INTERFACE_LIBRARY &&
  423. strcmp(prop, "MAP_IMPORTED_CONFIG_") != 0) {
  424. continue;
  425. }
  426. std::string property = cmStrCat(prop, configUpper);
  427. initProp(property);
  428. }
  429. // Initialize per-configuration name postfix property from the
  430. // variable only for non-executable targets. This preserves
  431. // compatibility with previous CMake versions in which executables
  432. // did not support this variable. Projects may still specify the
  433. // property directly.
  434. if (this->impl->TargetType != cmStateEnums::EXECUTABLE &&
  435. this->impl->TargetType != cmStateEnums::INTERFACE_LIBRARY) {
  436. std::string property =
  437. cmStrCat(cmSystemTools::UpperCase(configName), "_POSTFIX");
  438. initProp(property);
  439. }
  440. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  441. this->impl->TargetType == cmStateEnums::STATIC_LIBRARY) {
  442. std::string property = cmStrCat("FRAMEWORK_MULTI_CONFIG_POSTFIX_",
  443. cmSystemTools::UpperCase(configName));
  444. initProp(property);
  445. }
  446. }
  447. if (!this->IsImported()) {
  448. initProp("LINK_LIBRARIES_ONLY_TARGETS");
  449. }
  450. }
  451. // Save the backtrace of target construction.
  452. this->impl->Backtrace = this->impl->Makefile->GetBacktrace();
  453. if (!this->IsImported()) {
  454. // Initialize the INCLUDE_DIRECTORIES property based on the current value
  455. // of the same directory property:
  456. cm::append(this->impl->IncludeDirectoriesEntries,
  457. this->impl->Makefile->GetIncludeDirectoriesEntries());
  458. {
  459. auto const& sysInc = this->impl->Makefile->GetSystemIncludeDirectories();
  460. this->impl->SystemIncludeDirectories.insert(sysInc.begin(),
  461. sysInc.end());
  462. }
  463. cm::append(this->impl->CompileOptionsEntries,
  464. this->impl->Makefile->GetCompileOptionsEntries());
  465. cm::append(this->impl->LinkOptionsEntries,
  466. this->impl->Makefile->GetLinkOptionsEntries());
  467. cm::append(this->impl->LinkDirectoriesEntries,
  468. this->impl->Makefile->GetLinkDirectoriesEntries());
  469. }
  470. if (this->impl->TargetType == cmStateEnums::EXECUTABLE) {
  471. initProp("ANDROID_GUI");
  472. initProp("CROSSCOMPILING_EMULATOR");
  473. initProp("ENABLE_EXPORTS");
  474. }
  475. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  476. this->impl->TargetType == cmStateEnums::MODULE_LIBRARY) {
  477. this->SetProperty("POSITION_INDEPENDENT_CODE", "True");
  478. } else if (this->CanCompileSources()) {
  479. initProp("POSITION_INDEPENDENT_CODE");
  480. }
  481. if (this->impl->TargetType == cmStateEnums::SHARED_LIBRARY ||
  482. this->impl->TargetType == cmStateEnums::EXECUTABLE) {
  483. initProp("AIX_EXPORT_ALL_SYMBOLS");
  484. initProp("WINDOWS_EXPORT_ALL_SYMBOLS");
  485. }
  486. // Record current policies for later use.
  487. this->impl->Makefile->RecordPolicies(this->impl->PolicyMap);
  488. if (this->impl->TargetType == cmStateEnums::INTERFACE_LIBRARY) {
  489. // This policy is checked in a few conditions. The properties relevant
  490. // to the policy are always ignored for cmStateEnums::INTERFACE_LIBRARY
  491. // targets,
  492. // so ensure that the conditions don't lead to nonsense.
  493. this->impl->PolicyMap.Set(cmPolicies::CMP0022, cmPolicies::NEW);
  494. }
  495. if (!this->IsImported()) {
  496. initProp("DOTNET_SDK");
  497. }
  498. if (this->impl->TargetType <= cmStateEnums::GLOBAL_TARGET) {
  499. initProp("DOTNET_TARGET_FRAMEWORK");
  500. initProp("DOTNET_TARGET_FRAMEWORK_VERSION");
  501. }
  502. // check for "CMAKE_VS_GLOBALS" variable and set up target properties
  503. // if any
  504. cmValue globals = mf->GetDefinition("CMAKE_VS_GLOBALS");
  505. if (globals) {
  506. const std::string genName = mf->GetGlobalGenerator()->GetName();
  507. if (cmHasLiteralPrefix(genName, "Visual Studio")) {
  508. std::vector<std::string> props = cmExpandedList(*globals);
  509. const std::string vsGlobal = "VS_GLOBAL_";
  510. for (const std::string& i : props) {
  511. // split NAME=VALUE
  512. const std::string::size_type assignment = i.find('=');
  513. if (assignment != std::string::npos) {
  514. const std::string propName = vsGlobal + i.substr(0, assignment);
  515. const std::string propValue = i.substr(assignment + 1);
  516. initPropValue(propName, propValue.c_str());
  517. }
  518. }
  519. }
  520. }
  521. for (auto const& prop : mf->GetState()->GetPropertyDefinitions().GetMap()) {
  522. if (prop.first.second == cmProperty::TARGET &&
  523. !prop.second.GetInitializeFromVariable().empty()) {
  524. if (auto value =
  525. mf->GetDefinition(prop.second.GetInitializeFromVariable())) {
  526. this->SetProperty(prop.first.first, value);
  527. }
  528. }
  529. }
  530. }
  531. cmTarget::cmTarget(cmTarget&&) noexcept = default;
  532. cmTarget::~cmTarget() = default;
  533. cmTarget& cmTarget::operator=(cmTarget&&) noexcept = default;
  534. cmStateEnums::TargetType cmTarget::GetType() const
  535. {
  536. return this->impl->TargetType;
  537. }
  538. cmMakefile* cmTarget::GetMakefile() const
  539. {
  540. return this->impl->Makefile;
  541. }
  542. cmPolicies::PolicyMap const& cmTarget::GetPolicyMap() const
  543. {
  544. return this->impl->PolicyMap;
  545. }
  546. const std::string& cmTarget::GetName() const
  547. {
  548. return this->impl->Name;
  549. }
  550. cmPolicies::PolicyStatus cmTarget::GetPolicyStatus(
  551. cmPolicies::PolicyID policy) const
  552. {
  553. return this->impl->PolicyMap.Get(policy);
  554. }
  555. cmGlobalGenerator* cmTarget::GetGlobalGenerator() const
  556. {
  557. return this->impl->Makefile->GetGlobalGenerator();
  558. }
  559. BTs<std::string> const* cmTarget::GetLanguageStandardProperty(
  560. const std::string& propertyName) const
  561. {
  562. auto entry = this->impl->LanguageStandardProperties.find(propertyName);
  563. if (entry != this->impl->LanguageStandardProperties.end()) {
  564. return &entry->second;
  565. }
  566. return nullptr;
  567. }
  568. void cmTarget::SetLanguageStandardProperty(std::string const& lang,
  569. std::string const& value,
  570. const std::string& feature)
  571. {
  572. cmListFileBacktrace featureBacktrace;
  573. for (auto const& entry : this->impl->CompileFeaturesEntries) {
  574. if (entry.Value == feature) {
  575. featureBacktrace = entry.Backtrace;
  576. break;
  577. }
  578. }
  579. BTs<std::string>& languageStandardProperty =
  580. this->impl->LanguageStandardProperties[cmStrCat(lang, "_STANDARD")];
  581. if (languageStandardProperty.Value != value) {
  582. languageStandardProperty.Value = value;
  583. languageStandardProperty.Backtraces.clear();
  584. }
  585. languageStandardProperty.Backtraces.emplace_back(featureBacktrace);
  586. }
  587. void cmTarget::AddUtility(std::string const& name, bool cross, cmMakefile* mf)
  588. {
  589. this->impl->Utilities.insert(BT<std::pair<std::string, bool>>(
  590. { name, cross }, mf ? mf->GetBacktrace() : cmListFileBacktrace()));
  591. }
  592. void cmTarget::AddUtility(BT<std::pair<std::string, bool>> util)
  593. {
  594. this->impl->Utilities.emplace(std::move(util));
  595. }
  596. std::set<BT<std::pair<std::string, bool>>> const& cmTarget::GetUtilities()
  597. const
  598. {
  599. return this->impl->Utilities;
  600. }
  601. cmListFileBacktrace const& cmTarget::GetBacktrace() const
  602. {
  603. return this->impl->Backtrace;
  604. }
  605. bool cmTarget::IsExecutableWithExports() const
  606. {
  607. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  608. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  609. }
  610. bool cmTarget::IsFrameworkOnApple() const
  611. {
  612. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  613. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  614. this->impl->Makefile->IsOn("APPLE") &&
  615. this->GetPropertyAsBool("FRAMEWORK"));
  616. }
  617. bool cmTarget::IsAppBundleOnApple() const
  618. {
  619. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  620. this->impl->Makefile->IsOn("APPLE") &&
  621. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  622. }
  623. bool cmTarget::IsAndroidGuiExecutable() const
  624. {
  625. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  626. this->impl->IsAndroid && this->GetPropertyAsBool("ANDROID_GUI"));
  627. }
  628. std::vector<cmCustomCommand> const& cmTarget::GetPreBuildCommands() const
  629. {
  630. return this->impl->PreBuildCommands;
  631. }
  632. void cmTarget::AddPreBuildCommand(cmCustomCommand const& cmd)
  633. {
  634. this->impl->PreBuildCommands.push_back(cmd);
  635. }
  636. void cmTarget::AddPreBuildCommand(cmCustomCommand&& cmd)
  637. {
  638. this->impl->PreBuildCommands.push_back(std::move(cmd));
  639. }
  640. std::vector<cmCustomCommand> const& cmTarget::GetPreLinkCommands() const
  641. {
  642. return this->impl->PreLinkCommands;
  643. }
  644. void cmTarget::AddPreLinkCommand(cmCustomCommand const& cmd)
  645. {
  646. this->impl->PreLinkCommands.push_back(cmd);
  647. }
  648. void cmTarget::AddPreLinkCommand(cmCustomCommand&& cmd)
  649. {
  650. this->impl->PreLinkCommands.push_back(std::move(cmd));
  651. }
  652. std::vector<cmCustomCommand> const& cmTarget::GetPostBuildCommands() const
  653. {
  654. return this->impl->PostBuildCommands;
  655. }
  656. void cmTarget::AddPostBuildCommand(cmCustomCommand const& cmd)
  657. {
  658. this->impl->PostBuildCommands.push_back(cmd);
  659. }
  660. void cmTarget::AddPostBuildCommand(cmCustomCommand&& cmd)
  661. {
  662. this->impl->PostBuildCommands.push_back(std::move(cmd));
  663. }
  664. void cmTarget::AddTracedSources(std::vector<std::string> const& srcs)
  665. {
  666. if (!srcs.empty()) {
  667. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  668. this->impl->SourceEntries.emplace_back(cmJoin(srcs, ";"), lfbt);
  669. }
  670. }
  671. void cmTarget::AddSources(std::vector<std::string> const& srcs)
  672. {
  673. std::string srcFiles;
  674. const char* sep = "";
  675. for (auto filename : srcs) {
  676. if (!cmGeneratorExpression::StartsWithGeneratorExpression(filename)) {
  677. if (!filename.empty()) {
  678. filename = this->impl->ProcessSourceItemCMP0049(filename);
  679. if (filename.empty()) {
  680. return;
  681. }
  682. }
  683. this->impl->Makefile->GetOrCreateSource(filename);
  684. }
  685. srcFiles += sep;
  686. srcFiles += filename;
  687. sep = ";";
  688. }
  689. if (!srcFiles.empty()) {
  690. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  691. this->impl->SourceEntries.emplace_back(std::move(srcFiles), lfbt);
  692. }
  693. }
  694. std::string cmTargetInternals::ProcessSourceItemCMP0049(
  695. const std::string& s) const
  696. {
  697. std::string src = s;
  698. // For backwards compatibility replace variables in source names.
  699. // This should eventually be removed.
  700. this->Makefile->ExpandVariablesInString(src);
  701. if (src != s) {
  702. std::ostringstream e;
  703. bool noMessage = false;
  704. MessageType messageType = MessageType::AUTHOR_WARNING;
  705. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0049)) {
  706. case cmPolicies::WARN:
  707. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0049) << "\n";
  708. break;
  709. case cmPolicies::OLD:
  710. noMessage = true;
  711. break;
  712. case cmPolicies::REQUIRED_ALWAYS:
  713. case cmPolicies::REQUIRED_IF_USED:
  714. case cmPolicies::NEW:
  715. messageType = MessageType::FATAL_ERROR;
  716. }
  717. if (!noMessage) {
  718. e << "Legacy variable expansion in source file \"" << s
  719. << "\" expanded to \"" << src << "\" in target \"" << this->Name
  720. << "\". This behavior will be removed in a "
  721. "future version of CMake.";
  722. this->Makefile->IssueMessage(messageType, e.str());
  723. if (messageType == MessageType::FATAL_ERROR) {
  724. return "";
  725. }
  726. }
  727. }
  728. return src;
  729. }
  730. std::string cmTarget::GetSourceCMP0049(const std::string& s)
  731. {
  732. return this->impl->ProcessSourceItemCMP0049(s);
  733. }
  734. struct CreateLocation
  735. {
  736. cmMakefile const* Makefile;
  737. CreateLocation(cmMakefile const* mf)
  738. : Makefile(mf)
  739. {
  740. }
  741. cmSourceFileLocation operator()(const std::string& filename) const
  742. {
  743. return cmSourceFileLocation(this->Makefile, filename);
  744. }
  745. };
  746. struct LocationMatcher
  747. {
  748. const cmSourceFileLocation& Needle;
  749. LocationMatcher(const cmSourceFileLocation& needle)
  750. : Needle(needle)
  751. {
  752. }
  753. bool operator()(cmSourceFileLocation& loc)
  754. {
  755. return loc.Matches(this->Needle);
  756. }
  757. };
  758. struct TargetPropertyEntryFinder
  759. {
  760. private:
  761. const cmSourceFileLocation& Needle;
  762. public:
  763. TargetPropertyEntryFinder(const cmSourceFileLocation& needle)
  764. : Needle(needle)
  765. {
  766. }
  767. bool operator()(BT<std::string> const& entry)
  768. {
  769. std::vector<std::string> files = cmExpandedList(entry.Value);
  770. std::vector<cmSourceFileLocation> locations;
  771. locations.reserve(files.size());
  772. std::transform(files.begin(), files.end(), std::back_inserter(locations),
  773. CreateLocation(this->Needle.GetMakefile()));
  774. return std::find_if(locations.begin(), locations.end(),
  775. LocationMatcher(this->Needle)) != locations.end();
  776. }
  777. };
  778. cmSourceFile* cmTarget::AddSource(const std::string& src, bool before)
  779. {
  780. cmSourceFileLocation sfl(this->impl->Makefile, src,
  781. cmSourceFileLocationKind::Known);
  782. if (std::find_if(
  783. this->impl->SourceEntries.begin(), this->impl->SourceEntries.end(),
  784. TargetPropertyEntryFinder(sfl)) == this->impl->SourceEntries.end()) {
  785. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  786. this->impl->SourceEntries.insert(before ? this->impl->SourceEntries.begin()
  787. : this->impl->SourceEntries.end(),
  788. BT<std::string>(src, lfbt));
  789. }
  790. if (cmGeneratorExpression::Find(src) != std::string::npos) {
  791. return nullptr;
  792. }
  793. return this->impl->Makefile->GetOrCreateSource(
  794. src, false, cmSourceFileLocationKind::Known);
  795. }
  796. void cmTarget::ClearDependencyInformation(cmMakefile& mf) const
  797. {
  798. std::string depname = cmStrCat(this->GetName(), "_LIB_DEPENDS");
  799. mf.RemoveCacheDefinition(depname);
  800. }
  801. std::string cmTarget::GetDebugGeneratorExpressions(
  802. const std::string& value, cmTargetLinkLibraryType llt) const
  803. {
  804. if (llt == GENERAL_LibraryType) {
  805. return value;
  806. }
  807. // Get the list of configurations considered to be DEBUG.
  808. std::vector<std::string> debugConfigs =
  809. this->impl->Makefile->GetCMakeInstance()->GetDebugConfigs();
  810. std::string configString = "$<CONFIG:" + debugConfigs[0] + ">";
  811. if (debugConfigs.size() > 1) {
  812. for (std::string const& conf : cmMakeRange(debugConfigs).advance(1)) {
  813. configString += ",$<CONFIG:" + conf + ">";
  814. }
  815. configString = "$<OR:" + configString + ">";
  816. }
  817. if (llt == OPTIMIZED_LibraryType) {
  818. configString = "$<NOT:" + configString + ">";
  819. }
  820. return "$<" + configString + ":" + value + ">";
  821. }
  822. static std::string targetNameGenex(const std::string& lib)
  823. {
  824. return "$<TARGET_NAME:" + lib + ">";
  825. }
  826. bool cmTarget::PushTLLCommandTrace(TLLSignature signature,
  827. cmListFileContext const& lfc)
  828. {
  829. bool ret = true;
  830. if (!this->impl->TLLCommands.empty()) {
  831. if (this->impl->TLLCommands.back().first != signature) {
  832. ret = false;
  833. }
  834. }
  835. if (this->impl->TLLCommands.empty() ||
  836. this->impl->TLLCommands.back().second != lfc) {
  837. this->impl->TLLCommands.emplace_back(signature, lfc);
  838. }
  839. return ret;
  840. }
  841. void cmTarget::GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const
  842. {
  843. const char* sigString =
  844. (sig == cmTarget::KeywordTLLSignature ? "keyword" : "plain");
  845. s << "The uses of the " << sigString << " signature are here:\n";
  846. for (auto const& cmd : this->impl->TLLCommands) {
  847. if (cmd.first == sig) {
  848. cmListFileContext lfc = cmd.second;
  849. lfc.FilePath = cmSystemTools::RelativeIfUnder(
  850. this->impl->Makefile->GetState()->GetSourceDirectory(), lfc.FilePath);
  851. s << " * " << lfc << '\n';
  852. }
  853. }
  854. }
  855. std::string const& cmTarget::GetInstallPath() const
  856. {
  857. return this->impl->InstallPath;
  858. }
  859. void cmTarget::SetInstallPath(std::string const& name)
  860. {
  861. this->impl->InstallPath = name;
  862. }
  863. std::string const& cmTarget::GetRuntimeInstallPath() const
  864. {
  865. return this->impl->RuntimeInstallPath;
  866. }
  867. void cmTarget::SetRuntimeInstallPath(std::string const& name)
  868. {
  869. this->impl->RuntimeInstallPath = name;
  870. }
  871. bool cmTarget::GetHaveInstallRule() const
  872. {
  873. return this->impl->HaveInstallRule;
  874. }
  875. void cmTarget::SetHaveInstallRule(bool hir)
  876. {
  877. this->impl->HaveInstallRule = hir;
  878. }
  879. void cmTarget::AddInstallGenerator(cmInstallTargetGenerator* g)
  880. {
  881. this->impl->InstallGenerators.emplace_back(g);
  882. }
  883. std::vector<cmInstallTargetGenerator*> const& cmTarget::GetInstallGenerators()
  884. const
  885. {
  886. return this->impl->InstallGenerators;
  887. }
  888. bool cmTarget::GetIsGeneratorProvided() const
  889. {
  890. return this->impl->IsGeneratorProvided;
  891. }
  892. void cmTarget::SetIsGeneratorProvided(bool igp)
  893. {
  894. this->impl->IsGeneratorProvided = igp;
  895. }
  896. cmTarget::LinkLibraryVectorType const& cmTarget::GetOriginalLinkLibraries()
  897. const
  898. {
  899. return this->impl->OriginalLinkLibraries;
  900. }
  901. void cmTarget::AddLinkLibrary(cmMakefile& mf, std::string const& lib,
  902. cmTargetLinkLibraryType llt)
  903. {
  904. cmTarget* tgt = mf.FindTargetToUse(lib);
  905. {
  906. const bool isNonImportedTarget = tgt && !tgt->IsImported();
  907. const std::string libName =
  908. (isNonImportedTarget && llt != GENERAL_LibraryType)
  909. ? targetNameGenex(lib)
  910. : lib;
  911. this->AppendProperty("LINK_LIBRARIES",
  912. this->GetDebugGeneratorExpressions(libName, llt));
  913. }
  914. if (cmGeneratorExpression::Find(lib) != std::string::npos ||
  915. (tgt &&
  916. (tgt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  917. tgt->GetType() == cmStateEnums::OBJECT_LIBRARY)) ||
  918. (this->impl->Name == lib)) {
  919. return;
  920. }
  921. this->impl->OriginalLinkLibraries.emplace_back(lib, llt);
  922. // Add the explicit dependency information for libraries. This is
  923. // simply a set of libraries separated by ";". There should always
  924. // be a trailing ";". These library names are not canonical, in that
  925. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  926. // We shouldn't remove duplicates here because external libraries
  927. // may be purposefully duplicated to handle recursive dependencies,
  928. // and we removing one instance will break the link line. Duplicates
  929. // will be appropriately eliminated at emit time.
  930. if (this->impl->TargetType >= cmStateEnums::STATIC_LIBRARY &&
  931. this->impl->TargetType <= cmStateEnums::MODULE_LIBRARY &&
  932. (this->GetPolicyStatusCMP0073() == cmPolicies::OLD ||
  933. this->GetPolicyStatusCMP0073() == cmPolicies::WARN)) {
  934. std::string targetEntry = cmStrCat(this->impl->Name, "_LIB_DEPENDS");
  935. std::string dependencies;
  936. cmValue old_val = mf.GetDefinition(targetEntry);
  937. if (old_val) {
  938. dependencies += *old_val;
  939. }
  940. switch (llt) {
  941. case GENERAL_LibraryType:
  942. dependencies += "general";
  943. break;
  944. case DEBUG_LibraryType:
  945. dependencies += "debug";
  946. break;
  947. case OPTIMIZED_LibraryType:
  948. dependencies += "optimized";
  949. break;
  950. }
  951. dependencies += ";";
  952. dependencies += lib;
  953. dependencies += ";";
  954. mf.AddCacheDefinition(targetEntry, dependencies,
  955. "Dependencies for the target", cmStateEnums::STATIC);
  956. }
  957. }
  958. void cmTarget::AddSystemIncludeDirectories(const std::set<std::string>& incs)
  959. {
  960. this->impl->SystemIncludeDirectories.insert(incs.begin(), incs.end());
  961. }
  962. std::set<std::string> const& cmTarget::GetSystemIncludeDirectories() const
  963. {
  964. return this->impl->SystemIncludeDirectories;
  965. }
  966. void cmTarget::AddInstallIncludeDirectories(cmTargetExport const& te,
  967. cmStringRange const& incs)
  968. {
  969. std::copy(
  970. incs.begin(), incs.end(),
  971. std::back_inserter(this->impl->InstallIncludeDirectoriesEntries[&te]));
  972. }
  973. cmStringRange cmTarget::GetInstallIncludeDirectoriesEntries(
  974. cmTargetExport const& te) const
  975. {
  976. return cmMakeRange(this->impl->InstallIncludeDirectoriesEntries[&te]);
  977. }
  978. cmBTStringRange cmTarget::GetIncludeDirectoriesEntries() const
  979. {
  980. return cmMakeRange(this->impl->IncludeDirectoriesEntries);
  981. }
  982. cmBTStringRange cmTarget::GetCompileOptionsEntries() const
  983. {
  984. return cmMakeRange(this->impl->CompileOptionsEntries);
  985. }
  986. cmBTStringRange cmTarget::GetCompileFeaturesEntries() const
  987. {
  988. return cmMakeRange(this->impl->CompileFeaturesEntries);
  989. }
  990. cmBTStringRange cmTarget::GetCompileDefinitionsEntries() const
  991. {
  992. return cmMakeRange(this->impl->CompileDefinitionsEntries);
  993. }
  994. cmBTStringRange cmTarget::GetPrecompileHeadersEntries() const
  995. {
  996. return cmMakeRange(this->impl->PrecompileHeadersEntries);
  997. }
  998. cmBTStringRange cmTarget::GetSourceEntries() const
  999. {
  1000. return cmMakeRange(this->impl->SourceEntries);
  1001. }
  1002. cmBTStringRange cmTarget::GetLinkOptionsEntries() const
  1003. {
  1004. return cmMakeRange(this->impl->LinkOptionsEntries);
  1005. }
  1006. cmBTStringRange cmTarget::GetLinkDirectoriesEntries() const
  1007. {
  1008. return cmMakeRange(this->impl->LinkDirectoriesEntries);
  1009. }
  1010. cmBTStringRange cmTarget::GetLinkImplementationEntries() const
  1011. {
  1012. return cmMakeRange(this->impl->LinkImplementationPropertyEntries);
  1013. }
  1014. cmBTStringRange cmTarget::GetLinkInterfaceEntries() const
  1015. {
  1016. return cmMakeRange(this->impl->LinkInterfacePropertyEntries);
  1017. }
  1018. cmBTStringRange cmTarget::GetLinkInterfaceDirectEntries() const
  1019. {
  1020. return cmMakeRange(this->impl->LinkInterfaceDirectPropertyEntries);
  1021. }
  1022. cmBTStringRange cmTarget::GetLinkInterfaceDirectExcludeEntries() const
  1023. {
  1024. return cmMakeRange(this->impl->LinkInterfaceDirectExcludePropertyEntries);
  1025. }
  1026. cmBTStringRange cmTarget::GetHeaderSetsEntries() const
  1027. {
  1028. return cmMakeRange(this->impl->HeaderSetsEntries);
  1029. }
  1030. cmBTStringRange cmTarget::GetInterfaceHeaderSetsEntries() const
  1031. {
  1032. return cmMakeRange(this->impl->InterfaceHeaderSetsEntries);
  1033. }
  1034. namespace {
  1035. #define MAKE_PROP(PROP) const std::string prop##PROP = #PROP
  1036. MAKE_PROP(C_STANDARD);
  1037. MAKE_PROP(CXX_STANDARD);
  1038. MAKE_PROP(CUDA_STANDARD);
  1039. MAKE_PROP(HIP_STANDARD);
  1040. MAKE_PROP(OBJC_STANDARD);
  1041. MAKE_PROP(OBJCXX_STANDARD);
  1042. MAKE_PROP(COMPILE_DEFINITIONS);
  1043. MAKE_PROP(COMPILE_FEATURES);
  1044. MAKE_PROP(COMPILE_OPTIONS);
  1045. MAKE_PROP(PRECOMPILE_HEADERS);
  1046. MAKE_PROP(PRECOMPILE_HEADERS_REUSE_FROM);
  1047. MAKE_PROP(CUDA_PTX_COMPILATION);
  1048. MAKE_PROP(EXPORT_NAME);
  1049. MAKE_PROP(IMPORTED);
  1050. MAKE_PROP(IMPORTED_GLOBAL);
  1051. MAKE_PROP(INCLUDE_DIRECTORIES);
  1052. MAKE_PROP(LINK_OPTIONS);
  1053. MAKE_PROP(LINK_DIRECTORIES);
  1054. MAKE_PROP(LINK_LIBRARIES);
  1055. MAKE_PROP(MANUALLY_ADDED_DEPENDENCIES);
  1056. MAKE_PROP(NAME);
  1057. MAKE_PROP(SOURCES);
  1058. MAKE_PROP(TYPE);
  1059. MAKE_PROP(BINARY_DIR);
  1060. MAKE_PROP(SOURCE_DIR);
  1061. MAKE_PROP(FALSE);
  1062. MAKE_PROP(TRUE);
  1063. MAKE_PROP(HEADER_DIRS);
  1064. MAKE_PROP(HEADER_SET);
  1065. MAKE_PROP(HEADER_SETS);
  1066. MAKE_PROP(INTERFACE_HEADER_SETS);
  1067. MAKE_PROP(INTERFACE_LINK_LIBRARIES);
  1068. MAKE_PROP(INTERFACE_LINK_LIBRARIES_DIRECT);
  1069. MAKE_PROP(INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE);
  1070. #undef MAKE_PROP
  1071. }
  1072. namespace {
  1073. // to workaround bug on GCC/AIX
  1074. // Define a template to force conversion to std::string
  1075. template <typename ValueType>
  1076. std::string ConvertToString(ValueType value);
  1077. template <>
  1078. std::string ConvertToString<const char*>(const char* value)
  1079. {
  1080. return std::string(value);
  1081. }
  1082. template <>
  1083. std::string ConvertToString<cmValue>(cmValue value)
  1084. {
  1085. return std::string(*value);
  1086. }
  1087. template <typename ValueType>
  1088. bool StringIsEmpty(ValueType value);
  1089. template <>
  1090. bool StringIsEmpty<const char*>(const char* value)
  1091. {
  1092. return cmValue::IsEmpty(value);
  1093. }
  1094. template <>
  1095. bool StringIsEmpty<cmValue>(cmValue value)
  1096. {
  1097. return value.IsEmpty();
  1098. }
  1099. }
  1100. template <typename ValueType>
  1101. void cmTarget::StoreProperty(const std::string& prop, ValueType value)
  1102. {
  1103. if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
  1104. this->impl->Makefile->IssueMessage(
  1105. MessageType::FATAL_ERROR,
  1106. "MANUALLY_ADDED_DEPENDENCIES property is read-only\n");
  1107. return;
  1108. }
  1109. if (prop == propNAME) {
  1110. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1111. "NAME property is read-only\n");
  1112. return;
  1113. }
  1114. if (prop == propTYPE) {
  1115. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1116. "TYPE property is read-only\n");
  1117. return;
  1118. }
  1119. if (prop == propEXPORT_NAME && this->IsImported()) {
  1120. std::ostringstream e;
  1121. e << "EXPORT_NAME property can't be set on imported targets (\""
  1122. << this->impl->Name << "\")\n";
  1123. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1124. return;
  1125. }
  1126. if (prop == propSOURCES && this->IsImported()) {
  1127. std::ostringstream e;
  1128. e << "SOURCES property can't be set on imported targets (\""
  1129. << this->impl->Name << "\")\n";
  1130. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1131. return;
  1132. }
  1133. if (prop == propIMPORTED_GLOBAL && !this->IsImported()) {
  1134. std::ostringstream e;
  1135. e << "IMPORTED_GLOBAL property can't be set on non-imported targets (\""
  1136. << this->impl->Name << "\")\n";
  1137. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1138. return;
  1139. }
  1140. if (prop == propINCLUDE_DIRECTORIES) {
  1141. this->impl->IncludeDirectoriesEntries.clear();
  1142. if (value) {
  1143. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1144. this->impl->IncludeDirectoriesEntries.emplace_back(value, lfbt);
  1145. }
  1146. } else if (prop == propCOMPILE_OPTIONS) {
  1147. this->impl->CompileOptionsEntries.clear();
  1148. if (value) {
  1149. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1150. this->impl->CompileOptionsEntries.emplace_back(value, lfbt);
  1151. }
  1152. } else if (prop == propCOMPILE_FEATURES) {
  1153. this->impl->CompileFeaturesEntries.clear();
  1154. if (value) {
  1155. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1156. this->impl->CompileFeaturesEntries.emplace_back(value, lfbt);
  1157. }
  1158. } else if (prop == propCOMPILE_DEFINITIONS) {
  1159. this->impl->CompileDefinitionsEntries.clear();
  1160. if (value) {
  1161. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1162. this->impl->CompileDefinitionsEntries.emplace_back(value, lfbt);
  1163. }
  1164. } else if (prop == propLINK_OPTIONS) {
  1165. this->impl->LinkOptionsEntries.clear();
  1166. if (value) {
  1167. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1168. this->impl->LinkOptionsEntries.emplace_back(value, lfbt);
  1169. }
  1170. } else if (prop == propLINK_DIRECTORIES) {
  1171. this->impl->LinkDirectoriesEntries.clear();
  1172. if (value) {
  1173. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1174. this->impl->LinkDirectoriesEntries.emplace_back(value, lfbt);
  1175. }
  1176. } else if (prop == propPRECOMPILE_HEADERS) {
  1177. this->impl->PrecompileHeadersEntries.clear();
  1178. if (value) {
  1179. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1180. this->impl->PrecompileHeadersEntries.emplace_back(value, lfbt);
  1181. }
  1182. } else if (prop == propLINK_LIBRARIES) {
  1183. this->impl->LinkImplementationPropertyEntries.clear();
  1184. if (value) {
  1185. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1186. this->impl->LinkImplementationPropertyEntries.emplace_back(value, lfbt);
  1187. }
  1188. } else if (prop == propINTERFACE_LINK_LIBRARIES) {
  1189. this->impl->LinkInterfacePropertyEntries.clear();
  1190. if (value) {
  1191. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1192. this->impl->LinkInterfacePropertyEntries.emplace_back(value, lfbt);
  1193. }
  1194. } else if (prop == propINTERFACE_LINK_LIBRARIES_DIRECT) {
  1195. this->impl->LinkInterfaceDirectPropertyEntries.clear();
  1196. if (value) {
  1197. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1198. this->impl->LinkInterfaceDirectPropertyEntries.emplace_back(value, lfbt);
  1199. }
  1200. } else if (prop == propINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE) {
  1201. this->impl->LinkInterfaceDirectExcludePropertyEntries.clear();
  1202. if (value) {
  1203. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1204. this->impl->LinkInterfaceDirectExcludePropertyEntries.emplace_back(value,
  1205. lfbt);
  1206. }
  1207. } else if (prop == propSOURCES) {
  1208. this->impl->SourceEntries.clear();
  1209. if (value) {
  1210. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1211. this->impl->SourceEntries.emplace_back(value, lfbt);
  1212. }
  1213. } else if (prop == propIMPORTED_GLOBAL) {
  1214. if (!cmIsOn(value)) {
  1215. std::ostringstream e;
  1216. e << "IMPORTED_GLOBAL property can't be set to FALSE on targets (\""
  1217. << this->impl->Name << "\")\n";
  1218. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1219. return;
  1220. }
  1221. /* no need to change anything if value does not change */
  1222. if (!this->impl->ImportedGloballyVisible) {
  1223. this->impl->ImportedGloballyVisible = true;
  1224. this->GetGlobalGenerator()->IndexTarget(this);
  1225. }
  1226. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME") &&
  1227. !this->impl->CheckImportedLibName(
  1228. prop, value ? value : std::string{})) {
  1229. /* error was reported by check method */
  1230. } else if (prop == propCUDA_PTX_COMPILATION &&
  1231. this->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1232. std::ostringstream e;
  1233. e << "CUDA_PTX_COMPILATION property can only be applied to OBJECT "
  1234. "targets (\""
  1235. << this->impl->Name << "\")\n";
  1236. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1237. return;
  1238. } else if (prop == propPRECOMPILE_HEADERS_REUSE_FROM) {
  1239. if (this->GetProperty("PRECOMPILE_HEADERS")) {
  1240. std::ostringstream e;
  1241. e << "PRECOMPILE_HEADERS property is already set on target (\""
  1242. << this->impl->Name << "\")\n";
  1243. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1244. return;
  1245. }
  1246. auto* reusedTarget = this->impl->Makefile->GetCMakeInstance()
  1247. ->GetGlobalGenerator()
  1248. ->FindTarget(value);
  1249. if (!reusedTarget) {
  1250. const std::string e(
  1251. "PRECOMPILE_HEADERS_REUSE_FROM set with non existing target");
  1252. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  1253. return;
  1254. }
  1255. std::string reusedFrom = reusedTarget->GetSafeProperty(prop);
  1256. if (reusedFrom.empty()) {
  1257. reusedFrom = ConvertToString(value);
  1258. }
  1259. this->impl->Properties.SetProperty(prop, reusedFrom);
  1260. reusedTarget->SetProperty("COMPILE_PDB_NAME", reusedFrom);
  1261. reusedTarget->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY",
  1262. cmStrCat(reusedFrom, ".dir/"));
  1263. cmValue tmp = reusedTarget->GetProperty("COMPILE_PDB_NAME");
  1264. this->SetProperty("COMPILE_PDB_NAME", tmp);
  1265. this->AddUtility(reusedFrom, false, this->impl->Makefile);
  1266. } else if (prop == propC_STANDARD || prop == propCXX_STANDARD ||
  1267. prop == propCUDA_STANDARD || prop == propHIP_STANDARD ||
  1268. prop == propOBJC_STANDARD || prop == propOBJCXX_STANDARD) {
  1269. if (value) {
  1270. this->impl->LanguageStandardProperties[prop] =
  1271. BTs<std::string>(value, this->impl->Makefile->GetBacktrace());
  1272. } else {
  1273. this->impl->LanguageStandardProperties.erase(prop);
  1274. }
  1275. } else if (prop == propHEADER_DIRS) {
  1276. auto* fileSet = this->GetFileSet("HEADERS");
  1277. if (!fileSet) {
  1278. this->impl->Makefile->IssueMessage(
  1279. MessageType::FATAL_ERROR,
  1280. "The default header set has not yet been created.");
  1281. return;
  1282. }
  1283. fileSet->ClearDirectoryEntries();
  1284. if (!StringIsEmpty(value)) {
  1285. fileSet->AddDirectoryEntry(
  1286. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1287. }
  1288. } else if (prop == propHEADER_SET) {
  1289. auto* fileSet = this->GetFileSet("HEADERS");
  1290. if (!fileSet) {
  1291. this->impl->Makefile->IssueMessage(
  1292. MessageType::FATAL_ERROR,
  1293. "The default header set has not yet been created.");
  1294. return;
  1295. }
  1296. fileSet->ClearFileEntries();
  1297. if (!StringIsEmpty(value)) {
  1298. fileSet->AddFileEntry(
  1299. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1300. }
  1301. } else if (cmHasLiteralPrefix(prop, "HEADER_DIRS_")) {
  1302. auto fileSetName = prop.substr(cmStrLen("HEADER_DIRS_"));
  1303. if (fileSetName.empty()) {
  1304. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1305. "Header set name cannot be empty.");
  1306. return;
  1307. }
  1308. auto* fileSet = this->GetFileSet(fileSetName);
  1309. if (!fileSet) {
  1310. this->impl->Makefile->IssueMessage(
  1311. MessageType::FATAL_ERROR,
  1312. cmStrCat("Header set \"", fileSetName,
  1313. "\" has not yet been created."));
  1314. return;
  1315. }
  1316. fileSet->ClearDirectoryEntries();
  1317. if (!StringIsEmpty(value)) {
  1318. fileSet->AddDirectoryEntry(
  1319. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1320. }
  1321. } else if (cmHasLiteralPrefix(prop, "HEADER_SET_")) {
  1322. auto fileSetName = prop.substr(cmStrLen("HEADER_SET_"));
  1323. if (fileSetName.empty()) {
  1324. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1325. "Header set name cannot be empty.");
  1326. return;
  1327. }
  1328. auto* fileSet = this->GetFileSet(fileSetName);
  1329. if (!fileSet) {
  1330. this->impl->Makefile->IssueMessage(
  1331. MessageType::FATAL_ERROR,
  1332. cmStrCat("Header set \"", fileSetName,
  1333. "\" has not yet been created."));
  1334. return;
  1335. }
  1336. fileSet->ClearFileEntries();
  1337. if (!StringIsEmpty(value)) {
  1338. fileSet->AddFileEntry(
  1339. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1340. }
  1341. } else if (prop == propHEADER_SETS) {
  1342. if (value) {
  1343. for (auto const& name : cmExpandedList(value)) {
  1344. if (!this->GetFileSet(name)) {
  1345. this->impl->Makefile->IssueMessage(
  1346. MessageType::FATAL_ERROR,
  1347. cmStrCat("Header set \"", name, "\" has not yet been created."));
  1348. return;
  1349. }
  1350. }
  1351. }
  1352. this->impl->HeaderSetsEntries.clear();
  1353. if (!StringIsEmpty(value)) {
  1354. this->impl->HeaderSetsEntries.emplace_back(
  1355. value, this->impl->Makefile->GetBacktrace());
  1356. }
  1357. } else if (prop == propINTERFACE_HEADER_SETS) {
  1358. if (value) {
  1359. for (auto const& name : cmExpandedList(value)) {
  1360. if (!this->GetFileSet(name)) {
  1361. this->impl->Makefile->IssueMessage(
  1362. MessageType::FATAL_ERROR,
  1363. cmStrCat("Header set \"", name, "\" has not yet been created."));
  1364. return;
  1365. }
  1366. }
  1367. }
  1368. this->impl->InterfaceHeaderSetsEntries.clear();
  1369. if (!StringIsEmpty(value)) {
  1370. this->impl->InterfaceHeaderSetsEntries.emplace_back(
  1371. value, this->impl->Makefile->GetBacktrace());
  1372. }
  1373. } else {
  1374. this->impl->Properties.SetProperty(prop, value);
  1375. }
  1376. }
  1377. void cmTarget::AppendProperty(const std::string& prop,
  1378. const std::string& value, bool asString)
  1379. {
  1380. if (prop == "NAME") {
  1381. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1382. "NAME property is read-only\n");
  1383. return;
  1384. }
  1385. if (prop == "EXPORT_NAME" && this->IsImported()) {
  1386. std::ostringstream e;
  1387. e << "EXPORT_NAME property can't be set on imported targets (\""
  1388. << this->impl->Name << "\")\n";
  1389. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1390. return;
  1391. }
  1392. if (prop == "SOURCES" && this->IsImported()) {
  1393. std::ostringstream e;
  1394. e << "SOURCES property can't be set on imported targets (\""
  1395. << this->impl->Name << "\")\n";
  1396. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1397. return;
  1398. }
  1399. if (prop == "IMPORTED_GLOBAL") {
  1400. std::ostringstream e;
  1401. e << "IMPORTED_GLOBAL property can't be appended, only set on imported "
  1402. "targets (\""
  1403. << this->impl->Name << "\")\n";
  1404. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1405. return;
  1406. }
  1407. if (prop == "INCLUDE_DIRECTORIES") {
  1408. if (!value.empty()) {
  1409. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1410. this->impl->IncludeDirectoriesEntries.emplace_back(value, lfbt);
  1411. }
  1412. } else if (prop == "COMPILE_OPTIONS") {
  1413. if (!value.empty()) {
  1414. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1415. this->impl->CompileOptionsEntries.emplace_back(value, lfbt);
  1416. }
  1417. } else if (prop == "COMPILE_FEATURES") {
  1418. if (!value.empty()) {
  1419. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1420. this->impl->CompileFeaturesEntries.emplace_back(value, lfbt);
  1421. }
  1422. } else if (prop == "COMPILE_DEFINITIONS") {
  1423. if (!value.empty()) {
  1424. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1425. this->impl->CompileDefinitionsEntries.emplace_back(value, lfbt);
  1426. }
  1427. } else if (prop == "LINK_OPTIONS") {
  1428. if (!value.empty()) {
  1429. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1430. this->impl->LinkOptionsEntries.emplace_back(value, lfbt);
  1431. }
  1432. } else if (prop == "LINK_DIRECTORIES") {
  1433. if (!value.empty()) {
  1434. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1435. this->impl->LinkDirectoriesEntries.emplace_back(value, lfbt);
  1436. }
  1437. } else if (prop == "PRECOMPILE_HEADERS") {
  1438. if (this->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM")) {
  1439. std::ostringstream e;
  1440. e << "PRECOMPILE_HEADERS_REUSE_FROM property is already set on target "
  1441. "(\""
  1442. << this->impl->Name << "\")\n";
  1443. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1444. return;
  1445. }
  1446. if (!value.empty()) {
  1447. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1448. this->impl->PrecompileHeadersEntries.emplace_back(value, lfbt);
  1449. }
  1450. } else if (prop == "LINK_LIBRARIES") {
  1451. if (!value.empty()) {
  1452. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1453. this->impl->LinkImplementationPropertyEntries.emplace_back(value, lfbt);
  1454. }
  1455. } else if (prop == propINTERFACE_LINK_LIBRARIES) {
  1456. if (!value.empty()) {
  1457. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1458. this->impl->LinkInterfacePropertyEntries.emplace_back(value, lfbt);
  1459. }
  1460. } else if (prop == propINTERFACE_LINK_LIBRARIES_DIRECT) {
  1461. if (!value.empty()) {
  1462. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1463. this->impl->LinkInterfaceDirectPropertyEntries.emplace_back(value, lfbt);
  1464. }
  1465. } else if (prop == propINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE) {
  1466. if (!value.empty()) {
  1467. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1468. this->impl->LinkInterfaceDirectExcludePropertyEntries.emplace_back(value,
  1469. lfbt);
  1470. }
  1471. } else if (prop == "SOURCES") {
  1472. cmListFileBacktrace lfbt = this->impl->Makefile->GetBacktrace();
  1473. this->impl->SourceEntries.emplace_back(value, lfbt);
  1474. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME")) {
  1475. this->impl->Makefile->IssueMessage(
  1476. MessageType::FATAL_ERROR, prop + " property may not be APPENDed.");
  1477. } else if (prop == "C_STANDARD" || prop == "CXX_STANDARD" ||
  1478. prop == "CUDA_STANDARD" || prop == "HIP_STANDARD" ||
  1479. prop == "OBJC_STANDARD" || prop == "OBJCXX_STANDARD") {
  1480. this->impl->Makefile->IssueMessage(
  1481. MessageType::FATAL_ERROR, prop + " property may not be appended.");
  1482. } else if (prop == "HEADER_DIRS") {
  1483. auto* fileSet = this->GetFileSet("HEADERS");
  1484. if (!fileSet) {
  1485. this->impl->Makefile->IssueMessage(
  1486. MessageType::FATAL_ERROR,
  1487. "The default header set has not yet been created.");
  1488. return;
  1489. }
  1490. fileSet->AddDirectoryEntry(
  1491. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1492. } else if (cmHasLiteralPrefix(prop, "HEADER_DIRS_")) {
  1493. auto fileSetName = prop.substr(cmStrLen("HEADER_DIRS_"));
  1494. if (fileSetName.empty()) {
  1495. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1496. "Header set name cannot be empty.");
  1497. return;
  1498. }
  1499. auto* fileSet = this->GetFileSet(fileSetName);
  1500. if (!fileSet) {
  1501. this->impl->Makefile->IssueMessage(
  1502. MessageType::FATAL_ERROR,
  1503. cmStrCat("Header set \"", fileSetName,
  1504. "\" has not yet been created."));
  1505. return;
  1506. }
  1507. fileSet->AddDirectoryEntry(
  1508. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1509. } else if (prop == "HEADER_SET") {
  1510. auto* fileSet = this->GetFileSet("HEADERS");
  1511. if (!fileSet) {
  1512. this->impl->Makefile->IssueMessage(
  1513. MessageType::FATAL_ERROR,
  1514. "The default header set has not yet been created.");
  1515. return;
  1516. }
  1517. fileSet->AddFileEntry(
  1518. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1519. } else if (cmHasLiteralPrefix(prop, "HEADER_SET_")) {
  1520. auto fileSetName = prop.substr(cmStrLen("HEADER_SET_"));
  1521. if (fileSetName.empty()) {
  1522. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1523. "Header set name cannot be empty.");
  1524. return;
  1525. }
  1526. auto* fileSet = this->GetFileSet(fileSetName);
  1527. if (!fileSet) {
  1528. this->impl->Makefile->IssueMessage(
  1529. MessageType::FATAL_ERROR,
  1530. cmStrCat("Header set \"", fileSetName,
  1531. "\" has not yet been created."));
  1532. return;
  1533. }
  1534. fileSet->AddFileEntry(
  1535. BT<std::string>(value, this->impl->Makefile->GetBacktrace()));
  1536. } else if (prop == "HEADER_SETS") {
  1537. for (auto const& name : cmExpandedList(value)) {
  1538. if (!this->GetFileSet(name)) {
  1539. this->impl->Makefile->IssueMessage(
  1540. MessageType::FATAL_ERROR,
  1541. cmStrCat("Header set \"", name, "\" has not yet been created."));
  1542. return;
  1543. }
  1544. }
  1545. this->impl->HeaderSetsEntries.emplace_back(
  1546. value, this->impl->Makefile->GetBacktrace());
  1547. } else if (prop == "INTERFACE_HEADER_SETS") {
  1548. for (auto const& name : cmExpandedList(value)) {
  1549. if (!this->GetFileSet(name)) {
  1550. this->impl->Makefile->IssueMessage(
  1551. MessageType::FATAL_ERROR,
  1552. cmStrCat("Header set \"", name, "\" has not yet been created."));
  1553. return;
  1554. }
  1555. }
  1556. this->impl->InterfaceHeaderSetsEntries.emplace_back(
  1557. value, this->impl->Makefile->GetBacktrace());
  1558. } else {
  1559. this->impl->Properties.AppendProperty(prop, value, asString);
  1560. }
  1561. }
  1562. void cmTarget::SetProperty(const std::string& prop, const char* value)
  1563. {
  1564. this->StoreProperty(prop, value);
  1565. }
  1566. void cmTarget::SetProperty(const std::string& prop, cmValue value)
  1567. {
  1568. this->StoreProperty(prop, value);
  1569. }
  1570. void cmTarget::AppendBuildInterfaceIncludes()
  1571. {
  1572. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1573. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  1574. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1575. this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  1576. !this->IsExecutableWithExports()) {
  1577. return;
  1578. }
  1579. if (this->impl->BuildInterfaceIncludesAppended) {
  1580. return;
  1581. }
  1582. this->impl->BuildInterfaceIncludesAppended = true;
  1583. if (this->impl->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE")) {
  1584. std::string dirs = this->impl->Makefile->GetCurrentBinaryDirectory();
  1585. if (!dirs.empty()) {
  1586. dirs += ';';
  1587. }
  1588. dirs += this->impl->Makefile->GetCurrentSourceDirectory();
  1589. if (!dirs.empty()) {
  1590. this->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
  1591. ("$<BUILD_INTERFACE:" + dirs + ">"));
  1592. }
  1593. }
  1594. }
  1595. void cmTarget::InsertInclude(BT<std::string> const& entry, bool before)
  1596. {
  1597. auto position = before ? this->impl->IncludeDirectoriesEntries.begin()
  1598. : this->impl->IncludeDirectoriesEntries.end();
  1599. this->impl->IncludeDirectoriesEntries.insert(position, entry);
  1600. }
  1601. void cmTarget::InsertCompileOption(BT<std::string> const& entry, bool before)
  1602. {
  1603. auto position = before ? this->impl->CompileOptionsEntries.begin()
  1604. : this->impl->CompileOptionsEntries.end();
  1605. this->impl->CompileOptionsEntries.insert(position, entry);
  1606. }
  1607. void cmTarget::InsertCompileDefinition(BT<std::string> const& entry)
  1608. {
  1609. this->impl->CompileDefinitionsEntries.push_back(entry);
  1610. }
  1611. void cmTarget::InsertLinkOption(BT<std::string> const& entry, bool before)
  1612. {
  1613. auto position = before ? this->impl->LinkOptionsEntries.begin()
  1614. : this->impl->LinkOptionsEntries.end();
  1615. this->impl->LinkOptionsEntries.insert(position, entry);
  1616. }
  1617. void cmTarget::InsertLinkDirectory(BT<std::string> const& entry, bool before)
  1618. {
  1619. auto position = before ? this->impl->LinkDirectoriesEntries.begin()
  1620. : this->impl->LinkDirectoriesEntries.end();
  1621. this->impl->LinkDirectoriesEntries.insert(position, entry);
  1622. }
  1623. void cmTarget::InsertPrecompileHeader(BT<std::string> const& entry)
  1624. {
  1625. this->impl->PrecompileHeadersEntries.push_back(entry);
  1626. }
  1627. namespace {
  1628. void CheckLinkLibraryPattern(const std::string& property,
  1629. const std::string& value, cmMakefile* context)
  1630. {
  1631. // Look for <LINK_LIBRARY:> and </LINK_LIBRARY:> internal tags
  1632. static cmsys::RegularExpression linkLibrary(
  1633. "(^|;)(</?LINK_LIBRARY:[^;>]*>)(;|$)");
  1634. if (!linkLibrary.find(value)) {
  1635. return;
  1636. }
  1637. // Report an error.
  1638. context->IssueMessage(
  1639. MessageType::FATAL_ERROR,
  1640. cmStrCat("Property ", property, " contains the invalid item \"",
  1641. linkLibrary.match(2), "\". The ", property,
  1642. " property may contain the generator-expression "
  1643. "\"$<LINK_LIBRARY:...>\" "
  1644. "which may be used to specify how the libraries are linked."));
  1645. }
  1646. void CheckLINK_INTERFACE_LIBRARIES(const std::string& prop,
  1647. const std::string& value,
  1648. cmMakefile* context, bool imported)
  1649. {
  1650. // Support imported and non-imported versions of the property.
  1651. const char* base = (imported ? "IMPORTED_LINK_INTERFACE_LIBRARIES"
  1652. : "LINK_INTERFACE_LIBRARIES");
  1653. // Look for link-type keywords in the value.
  1654. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  1655. if (keys.find(value)) {
  1656. // Report an error.
  1657. std::ostringstream e;
  1658. e << "Property " << prop << " may not contain link-type keyword \""
  1659. << keys.match(2) << "\". "
  1660. << "The " << base << " property has a per-configuration "
  1661. << "version called " << base << "_<CONFIG> which may be "
  1662. << "used to specify per-configuration rules.";
  1663. if (!imported) {
  1664. e << " "
  1665. << "Alternatively, an IMPORTED library may be created, configured "
  1666. << "with a per-configuration location, and then named in the "
  1667. << "property value. "
  1668. << "See the add_library command's IMPORTED mode for details."
  1669. << "\n"
  1670. << "If you have a list of libraries that already contains the "
  1671. << "keyword, use the target_link_libraries command with its "
  1672. << "LINK_INTERFACE_LIBRARIES mode to set the property. "
  1673. << "The command automatically recognizes link-type keywords and sets "
  1674. << "the LINK_INTERFACE_LIBRARIES and LINK_INTERFACE_LIBRARIES_DEBUG "
  1675. << "properties accordingly.";
  1676. }
  1677. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1678. }
  1679. CheckLinkLibraryPattern(base, value, context);
  1680. }
  1681. void CheckLINK_LIBRARIES(const std::string& value, cmMakefile* context)
  1682. {
  1683. CheckLinkLibraryPattern("LINK_LIBRARIES", value, context);
  1684. }
  1685. void CheckINTERFACE_LINK_LIBRARIES(const std::string& value,
  1686. cmMakefile* context)
  1687. {
  1688. // Look for link-type keywords in the value.
  1689. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  1690. if (keys.find(value)) {
  1691. // Report an error.
  1692. std::ostringstream e;
  1693. e << "Property INTERFACE_LINK_LIBRARIES may not contain link-type "
  1694. "keyword \""
  1695. << keys.match(2)
  1696. << "\". The INTERFACE_LINK_LIBRARIES "
  1697. "property may contain configuration-sensitive generator-expressions "
  1698. "which may be used to specify per-configuration rules.";
  1699. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1700. }
  1701. CheckLinkLibraryPattern("INTERFACE_LINK_LIBRARIES", value, context);
  1702. }
  1703. void CheckIMPORTED_GLOBAL(const cmTarget* target, cmMakefile* context)
  1704. {
  1705. const auto& targets = context->GetOwnedImportedTargets();
  1706. auto it =
  1707. std::find_if(targets.begin(), targets.end(),
  1708. [&](const std::unique_ptr<cmTarget>& importTarget) -> bool {
  1709. return target == importTarget.get();
  1710. });
  1711. if (it == targets.end()) {
  1712. std::ostringstream e;
  1713. e << "Attempt to promote imported target \"" << target->GetName()
  1714. << "\" to global scope (by setting IMPORTED_GLOBAL) "
  1715. "which is not built in this directory.";
  1716. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1717. }
  1718. }
  1719. }
  1720. void cmTarget::CheckProperty(const std::string& prop,
  1721. cmMakefile* context) const
  1722. {
  1723. // Certain properties need checking.
  1724. if (cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES")) {
  1725. if (cmValue value = this->GetProperty(prop)) {
  1726. CheckLINK_INTERFACE_LIBRARIES(prop, *value, context, false);
  1727. }
  1728. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES")) {
  1729. if (cmValue value = this->GetProperty(prop)) {
  1730. CheckLINK_INTERFACE_LIBRARIES(prop, *value, context, true);
  1731. }
  1732. } else if (prop == "LINK_LIBRARIES") {
  1733. if (cmValue value = this->GetProperty(prop)) {
  1734. CheckLINK_LIBRARIES(*value, context);
  1735. }
  1736. } else if (prop == "INTERFACE_LINK_LIBRARIES") {
  1737. if (cmValue value = this->GetProperty(prop)) {
  1738. CheckINTERFACE_LINK_LIBRARIES(*value, context);
  1739. }
  1740. } else if (prop == "IMPORTED_GLOBAL") {
  1741. if (this->IsImported()) {
  1742. CheckIMPORTED_GLOBAL(this, context);
  1743. }
  1744. }
  1745. }
  1746. cmValue cmTarget::GetComputedProperty(const std::string& prop,
  1747. cmMakefile& mf) const
  1748. {
  1749. return cmTargetPropertyComputer::GetProperty(this, prop, mf);
  1750. }
  1751. cmValue cmTarget::GetProperty(const std::string& prop) const
  1752. {
  1753. static std::unordered_set<std::string> const specialProps{
  1754. propC_STANDARD,
  1755. propCXX_STANDARD,
  1756. propCUDA_STANDARD,
  1757. propOBJC_STANDARD,
  1758. propOBJCXX_STANDARD,
  1759. propLINK_LIBRARIES,
  1760. propTYPE,
  1761. propINCLUDE_DIRECTORIES,
  1762. propCOMPILE_FEATURES,
  1763. propCOMPILE_OPTIONS,
  1764. propCOMPILE_DEFINITIONS,
  1765. propPRECOMPILE_HEADERS,
  1766. propLINK_OPTIONS,
  1767. propLINK_DIRECTORIES,
  1768. propIMPORTED,
  1769. propIMPORTED_GLOBAL,
  1770. propMANUALLY_ADDED_DEPENDENCIES,
  1771. propNAME,
  1772. propBINARY_DIR,
  1773. propSOURCE_DIR,
  1774. propSOURCES,
  1775. propHEADER_DIRS,
  1776. propHEADER_SET,
  1777. propHEADER_SETS,
  1778. propINTERFACE_HEADER_SETS,
  1779. propINTERFACE_LINK_LIBRARIES,
  1780. propINTERFACE_LINK_LIBRARIES_DIRECT,
  1781. propINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  1782. };
  1783. if (specialProps.count(prop)) {
  1784. if (prop == propC_STANDARD || prop == propCXX_STANDARD ||
  1785. prop == propCUDA_STANDARD || prop == propOBJC_STANDARD ||
  1786. prop == propOBJCXX_STANDARD) {
  1787. auto propertyIter = this->impl->LanguageStandardProperties.find(prop);
  1788. if (propertyIter == this->impl->LanguageStandardProperties.end()) {
  1789. return nullptr;
  1790. }
  1791. return cmValue(propertyIter->second.Value);
  1792. }
  1793. if (prop == propLINK_LIBRARIES) {
  1794. if (this->impl->LinkImplementationPropertyEntries.empty()) {
  1795. return nullptr;
  1796. }
  1797. static std::string output;
  1798. output = cmJoin(this->impl->LinkImplementationPropertyEntries, ";");
  1799. return cmValue(output);
  1800. }
  1801. if (prop == propINTERFACE_LINK_LIBRARIES) {
  1802. if (this->impl->LinkInterfacePropertyEntries.empty()) {
  1803. return nullptr;
  1804. }
  1805. static std::string output;
  1806. output = cmJoin(this->impl->LinkInterfacePropertyEntries, ";");
  1807. return cmValue(output);
  1808. }
  1809. if (prop == propINTERFACE_LINK_LIBRARIES_DIRECT) {
  1810. if (this->impl->LinkInterfaceDirectPropertyEntries.empty()) {
  1811. return nullptr;
  1812. }
  1813. static std::string output;
  1814. output = cmJoin(this->impl->LinkInterfaceDirectPropertyEntries, ";");
  1815. return cmValue(output);
  1816. }
  1817. if (prop == propINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE) {
  1818. if (this->impl->LinkInterfaceDirectExcludePropertyEntries.empty()) {
  1819. return nullptr;
  1820. }
  1821. static std::string output;
  1822. output =
  1823. cmJoin(this->impl->LinkInterfaceDirectExcludePropertyEntries, ";");
  1824. return cmValue(output);
  1825. }
  1826. // the type property returns what type the target is
  1827. if (prop == propTYPE) {
  1828. return cmValue(cmState::GetTargetTypeName(this->GetType()));
  1829. }
  1830. if (prop == propINCLUDE_DIRECTORIES) {
  1831. if (this->impl->IncludeDirectoriesEntries.empty()) {
  1832. return nullptr;
  1833. }
  1834. static std::string output;
  1835. output = cmJoin(this->impl->IncludeDirectoriesEntries, ";");
  1836. return cmValue(output);
  1837. }
  1838. if (prop == propCOMPILE_FEATURES) {
  1839. if (this->impl->CompileFeaturesEntries.empty()) {
  1840. return nullptr;
  1841. }
  1842. static std::string output;
  1843. output = cmJoin(this->impl->CompileFeaturesEntries, ";");
  1844. return cmValue(output);
  1845. }
  1846. if (prop == propCOMPILE_OPTIONS) {
  1847. if (this->impl->CompileOptionsEntries.empty()) {
  1848. return nullptr;
  1849. }
  1850. static std::string output;
  1851. output = cmJoin(this->impl->CompileOptionsEntries, ";");
  1852. return cmValue(output);
  1853. }
  1854. if (prop == propCOMPILE_DEFINITIONS) {
  1855. if (this->impl->CompileDefinitionsEntries.empty()) {
  1856. return nullptr;
  1857. }
  1858. static std::string output;
  1859. output = cmJoin(this->impl->CompileDefinitionsEntries, ";");
  1860. return cmValue(output);
  1861. }
  1862. if (prop == propLINK_OPTIONS) {
  1863. if (this->impl->LinkOptionsEntries.empty()) {
  1864. return nullptr;
  1865. }
  1866. static std::string output;
  1867. output = cmJoin(this->impl->LinkOptionsEntries, ";");
  1868. return cmValue(output);
  1869. }
  1870. if (prop == propLINK_DIRECTORIES) {
  1871. if (this->impl->LinkDirectoriesEntries.empty()) {
  1872. return nullptr;
  1873. }
  1874. static std::string output;
  1875. output = cmJoin(this->impl->LinkDirectoriesEntries, ";");
  1876. return cmValue(output);
  1877. }
  1878. if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
  1879. if (this->impl->Utilities.empty()) {
  1880. return nullptr;
  1881. }
  1882. static std::string output;
  1883. static std::vector<std::string> utilities;
  1884. utilities.resize(this->impl->Utilities.size());
  1885. std::transform(
  1886. this->impl->Utilities.cbegin(), this->impl->Utilities.cend(),
  1887. utilities.begin(),
  1888. [](const BT<std::pair<std::string, bool>>& item) -> std::string {
  1889. return item.Value.first;
  1890. });
  1891. output = cmJoin(utilities, ";");
  1892. return cmValue(output);
  1893. }
  1894. if (prop == propPRECOMPILE_HEADERS) {
  1895. if (this->impl->PrecompileHeadersEntries.empty()) {
  1896. return nullptr;
  1897. }
  1898. static std::string output;
  1899. output = cmJoin(this->impl->PrecompileHeadersEntries, ";");
  1900. return cmValue(output);
  1901. }
  1902. if (prop == propIMPORTED) {
  1903. return this->IsImported() ? cmValue(propTRUE) : cmValue(propFALSE);
  1904. }
  1905. if (prop == propIMPORTED_GLOBAL) {
  1906. return this->IsImportedGloballyVisible() ? cmValue(propTRUE)
  1907. : cmValue(propFALSE);
  1908. }
  1909. if (prop == propNAME) {
  1910. return cmValue(this->GetName());
  1911. }
  1912. if (prop == propBINARY_DIR) {
  1913. return cmValue(this->impl->Makefile->GetStateSnapshot()
  1914. .GetDirectory()
  1915. .GetCurrentBinary());
  1916. }
  1917. if (prop == propSOURCE_DIR) {
  1918. return cmValue(this->impl->Makefile->GetStateSnapshot()
  1919. .GetDirectory()
  1920. .GetCurrentSource());
  1921. }
  1922. if (prop == propHEADER_DIRS) {
  1923. auto const* fileSet = this->GetFileSet("HEADERS");
  1924. if (!fileSet) {
  1925. return nullptr;
  1926. }
  1927. static std::string output;
  1928. output = cmJoin(fileSet->GetDirectoryEntries(), ";"_s);
  1929. return cmValue(output);
  1930. }
  1931. if (prop == propHEADER_SET) {
  1932. auto const* fileSet = this->GetFileSet("HEADERS");
  1933. if (!fileSet) {
  1934. return nullptr;
  1935. }
  1936. static std::string output;
  1937. output = cmJoin(fileSet->GetFileEntries(), ";"_s);
  1938. return cmValue(output);
  1939. }
  1940. if (prop == propHEADER_SETS) {
  1941. static std::string output;
  1942. output = cmJoin(this->impl->HeaderSetsEntries, ";"_s);
  1943. return cmValue(output);
  1944. }
  1945. if (prop == propINTERFACE_HEADER_SETS) {
  1946. static std::string output;
  1947. output = cmJoin(this->impl->InterfaceHeaderSetsEntries, ";"_s);
  1948. return cmValue(output);
  1949. }
  1950. }
  1951. if (cmHasLiteralPrefix(prop, "HEADER_DIRS_")) {
  1952. std::string fileSetName = prop.substr(cmStrLen("HEADER_DIRS_"));
  1953. if (fileSetName.empty()) {
  1954. return nullptr;
  1955. }
  1956. auto const* fileSet = this->GetFileSet(fileSetName);
  1957. if (!fileSet) {
  1958. return nullptr;
  1959. }
  1960. static std::string output;
  1961. output = cmJoin(fileSet->GetDirectoryEntries(), ";"_s);
  1962. return cmValue(output);
  1963. }
  1964. if (cmHasLiteralPrefix(prop, "HEADER_SET_")) {
  1965. std::string fileSetName = prop.substr(cmStrLen("HEADER_SET_"));
  1966. if (fileSetName.empty()) {
  1967. return nullptr;
  1968. }
  1969. auto const* fileSet = this->GetFileSet(fileSetName);
  1970. if (!fileSet) {
  1971. return nullptr;
  1972. }
  1973. static std::string output;
  1974. output = cmJoin(fileSet->GetFileEntries(), ";"_s);
  1975. return cmValue(output);
  1976. }
  1977. cmValue retVal = this->impl->Properties.GetPropertyValue(prop);
  1978. if (!retVal) {
  1979. const bool chain = this->impl->Makefile->GetState()->IsPropertyChained(
  1980. prop, cmProperty::TARGET);
  1981. if (chain) {
  1982. return this->impl->Makefile->GetStateSnapshot()
  1983. .GetDirectory()
  1984. .GetProperty(prop, chain);
  1985. }
  1986. return nullptr;
  1987. }
  1988. return retVal;
  1989. }
  1990. std::string const& cmTarget::GetSafeProperty(std::string const& prop) const
  1991. {
  1992. cmValue ret = this->GetProperty(prop);
  1993. if (ret) {
  1994. return *ret;
  1995. }
  1996. static std::string const s_empty;
  1997. return s_empty;
  1998. }
  1999. bool cmTarget::GetPropertyAsBool(const std::string& prop) const
  2000. {
  2001. return cmIsOn(this->GetProperty(prop));
  2002. }
  2003. cmPropertyMap const& cmTarget::GetProperties() const
  2004. {
  2005. return this->impl->Properties;
  2006. }
  2007. bool cmTarget::IsDLLPlatform() const
  2008. {
  2009. return this->impl->IsDLLPlatform;
  2010. }
  2011. bool cmTarget::IsAIX() const
  2012. {
  2013. return this->impl->IsAIX;
  2014. }
  2015. bool cmTarget::IsImported() const
  2016. {
  2017. return this->impl->IsImportedTarget;
  2018. }
  2019. bool cmTarget::IsImportedGloballyVisible() const
  2020. {
  2021. return this->impl->ImportedGloballyVisible;
  2022. }
  2023. bool cmTarget::IsPerConfig() const
  2024. {
  2025. return this->impl->PerConfig;
  2026. }
  2027. bool cmTarget::CanCompileSources() const
  2028. {
  2029. if (this->IsImported()) {
  2030. return false;
  2031. }
  2032. switch (this->GetType()) {
  2033. case cmStateEnums::EXECUTABLE:
  2034. case cmStateEnums::STATIC_LIBRARY:
  2035. case cmStateEnums::SHARED_LIBRARY:
  2036. case cmStateEnums::MODULE_LIBRARY:
  2037. case cmStateEnums::OBJECT_LIBRARY:
  2038. return true;
  2039. case cmStateEnums::UTILITY:
  2040. case cmStateEnums::INTERFACE_LIBRARY:
  2041. case cmStateEnums::GLOBAL_TARGET:
  2042. case cmStateEnums::UNKNOWN_LIBRARY:
  2043. break;
  2044. }
  2045. return false;
  2046. }
  2047. const char* cmTarget::GetSuffixVariableInternal(
  2048. cmStateEnums::ArtifactType artifact) const
  2049. {
  2050. switch (this->GetType()) {
  2051. case cmStateEnums::STATIC_LIBRARY:
  2052. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  2053. case cmStateEnums::SHARED_LIBRARY:
  2054. switch (artifact) {
  2055. case cmStateEnums::RuntimeBinaryArtifact:
  2056. return "CMAKE_SHARED_LIBRARY_SUFFIX";
  2057. case cmStateEnums::ImportLibraryArtifact:
  2058. return "CMAKE_IMPORT_LIBRARY_SUFFIX";
  2059. }
  2060. break;
  2061. case cmStateEnums::MODULE_LIBRARY:
  2062. switch (artifact) {
  2063. case cmStateEnums::RuntimeBinaryArtifact:
  2064. return "CMAKE_SHARED_MODULE_SUFFIX";
  2065. case cmStateEnums::ImportLibraryArtifact:
  2066. return "CMAKE_IMPORT_LIBRARY_SUFFIX";
  2067. }
  2068. break;
  2069. case cmStateEnums::EXECUTABLE:
  2070. switch (artifact) {
  2071. case cmStateEnums::RuntimeBinaryArtifact:
  2072. // Android GUI application packages store the native
  2073. // binary as a shared library.
  2074. return (this->IsAndroidGuiExecutable()
  2075. ? "CMAKE_SHARED_LIBRARY_SUFFIX"
  2076. : "CMAKE_EXECUTABLE_SUFFIX");
  2077. case cmStateEnums::ImportLibraryArtifact:
  2078. return (this->impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_SUFFIX"
  2079. : "CMAKE_IMPORT_LIBRARY_SUFFIX");
  2080. }
  2081. break;
  2082. default:
  2083. break;
  2084. }
  2085. return "";
  2086. }
  2087. const char* cmTarget::GetPrefixVariableInternal(
  2088. cmStateEnums::ArtifactType artifact) const
  2089. {
  2090. switch (this->GetType()) {
  2091. case cmStateEnums::STATIC_LIBRARY:
  2092. return "CMAKE_STATIC_LIBRARY_PREFIX";
  2093. case cmStateEnums::SHARED_LIBRARY:
  2094. switch (artifact) {
  2095. case cmStateEnums::RuntimeBinaryArtifact:
  2096. return "CMAKE_SHARED_LIBRARY_PREFIX";
  2097. case cmStateEnums::ImportLibraryArtifact:
  2098. return "CMAKE_IMPORT_LIBRARY_PREFIX";
  2099. }
  2100. break;
  2101. case cmStateEnums::MODULE_LIBRARY:
  2102. switch (artifact) {
  2103. case cmStateEnums::RuntimeBinaryArtifact:
  2104. return "CMAKE_SHARED_MODULE_PREFIX";
  2105. case cmStateEnums::ImportLibraryArtifact:
  2106. return "CMAKE_IMPORT_LIBRARY_PREFIX";
  2107. }
  2108. break;
  2109. case cmStateEnums::EXECUTABLE:
  2110. switch (artifact) {
  2111. case cmStateEnums::RuntimeBinaryArtifact:
  2112. // Android GUI application packages store the native
  2113. // binary as a shared library.
  2114. return (this->IsAndroidGuiExecutable()
  2115. ? "CMAKE_SHARED_LIBRARY_PREFIX"
  2116. : "");
  2117. case cmStateEnums::ImportLibraryArtifact:
  2118. return (this->impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_PREFIX"
  2119. : "CMAKE_IMPORT_LIBRARY_PREFIX");
  2120. }
  2121. break;
  2122. default:
  2123. break;
  2124. }
  2125. return "";
  2126. }
  2127. std::string cmTarget::ImportedGetFullPath(
  2128. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2129. {
  2130. assert(this->IsImported());
  2131. // Lookup/compute/cache the import information for this
  2132. // configuration.
  2133. std::string desired_config = config;
  2134. if (config.empty()) {
  2135. desired_config = "NOCONFIG";
  2136. }
  2137. std::string result;
  2138. cmValue loc = nullptr;
  2139. cmValue imp = nullptr;
  2140. std::string suffix;
  2141. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  2142. this->GetMappedConfig(desired_config, loc, imp, suffix)) {
  2143. switch (artifact) {
  2144. case cmStateEnums::RuntimeBinaryArtifact:
  2145. if (loc) {
  2146. result = *loc;
  2147. } else {
  2148. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  2149. if (cmValue config_location = this->GetProperty(impProp)) {
  2150. result = *config_location;
  2151. } else if (cmValue location =
  2152. this->GetProperty("IMPORTED_LOCATION")) {
  2153. result = *location;
  2154. }
  2155. }
  2156. break;
  2157. case cmStateEnums::ImportLibraryArtifact:
  2158. if (imp) {
  2159. result = *imp;
  2160. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2161. this->IsExecutableWithExports()) {
  2162. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  2163. if (cmValue config_implib = this->GetProperty(impProp)) {
  2164. result = *config_implib;
  2165. } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
  2166. result = *implib;
  2167. }
  2168. }
  2169. break;
  2170. }
  2171. }
  2172. if (result.empty()) {
  2173. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2174. auto message = [&]() -> std::string {
  2175. std::string unset;
  2176. std::string configuration;
  2177. if (artifact == cmStateEnums::RuntimeBinaryArtifact) {
  2178. unset = "IMPORTED_LOCATION";
  2179. } else if (artifact == cmStateEnums::ImportLibraryArtifact) {
  2180. unset = "IMPORTED_IMPLIB";
  2181. }
  2182. if (!config.empty()) {
  2183. configuration = cmStrCat(" configuration \"", config, "\"");
  2184. }
  2185. return cmStrCat(unset, " not set for imported target \"",
  2186. this->GetName(), "\"", configuration, ".");
  2187. };
  2188. switch (this->GetPolicyStatus(cmPolicies::CMP0111)) {
  2189. case cmPolicies::WARN:
  2190. this->impl->Makefile->IssueMessage(
  2191. MessageType::AUTHOR_WARNING,
  2192. cmPolicies::GetPolicyWarning(cmPolicies::CMP0111) + "\n" +
  2193. message());
  2194. CM_FALLTHROUGH;
  2195. case cmPolicies::OLD:
  2196. break;
  2197. default:
  2198. this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2199. message());
  2200. }
  2201. }
  2202. result = cmStrCat(this->GetName(), "-NOTFOUND");
  2203. }
  2204. return result;
  2205. }
  2206. const cmFileSet* cmTarget::GetFileSet(const std::string& name) const
  2207. {
  2208. auto it = this->impl->FileSets.find(name);
  2209. return it == this->impl->FileSets.end() ? nullptr : &it->second;
  2210. }
  2211. cmFileSet* cmTarget::GetFileSet(const std::string& name)
  2212. {
  2213. auto it = this->impl->FileSets.find(name);
  2214. return it == this->impl->FileSets.end() ? nullptr : &it->second;
  2215. }
  2216. std::pair<cmFileSet*, bool> cmTarget::GetOrCreateFileSet(
  2217. const std::string& name, const std::string& type)
  2218. {
  2219. auto result =
  2220. this->impl->FileSets.emplace(std::make_pair(name, cmFileSet(name, type)));
  2221. return std::make_pair(&result.first->second, result.second);
  2222. }
  2223. std::string cmTarget::GetFileSetsPropertyName(const std::string& type)
  2224. {
  2225. if (type == "HEADERS") {
  2226. return "HEADER_SETS";
  2227. }
  2228. return "";
  2229. }
  2230. std::string cmTarget::GetInterfaceFileSetsPropertyName(const std::string& type)
  2231. {
  2232. if (type == "HEADERS") {
  2233. return "INTERFACE_HEADER_SETS";
  2234. }
  2235. return "";
  2236. }
  2237. std::vector<std::string> cmTarget::GetAllInterfaceFileSets() const
  2238. {
  2239. std::vector<std::string> result;
  2240. auto inserter = std::back_inserter(result);
  2241. auto appendEntries = [=](const std::vector<BT<std::string>>& entries) {
  2242. for (auto const& entry : entries) {
  2243. auto expanded = cmExpandedList(entry.Value);
  2244. std::copy(expanded.begin(), expanded.end(), inserter);
  2245. }
  2246. };
  2247. appendEntries(this->impl->InterfaceHeaderSetsEntries);
  2248. return result;
  2249. }
  2250. bool cmTargetInternals::CheckImportedLibName(std::string const& prop,
  2251. std::string const& value) const
  2252. {
  2253. if (this->TargetType != cmStateEnums::INTERFACE_LIBRARY ||
  2254. !this->IsImportedTarget) {
  2255. this->Makefile->IssueMessage(
  2256. MessageType::FATAL_ERROR,
  2257. prop +
  2258. " property may be set only on imported INTERFACE library targets.");
  2259. return false;
  2260. }
  2261. if (!value.empty()) {
  2262. if (value[0] == '-') {
  2263. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2264. prop + " property value\n " + value +
  2265. "\nmay not start with '-'.");
  2266. return false;
  2267. }
  2268. std::string::size_type bad = value.find_first_of(":/\\;");
  2269. if (bad != std::string::npos) {
  2270. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  2271. prop + " property value\n " + value +
  2272. "\nmay not contain '" +
  2273. value.substr(bad, 1) + "'.");
  2274. return false;
  2275. }
  2276. }
  2277. return true;
  2278. }
  2279. bool cmTarget::GetMappedConfig(std::string const& desired_config, cmValue& loc,
  2280. cmValue& imp, std::string& suffix) const
  2281. {
  2282. std::string config_upper;
  2283. if (!desired_config.empty()) {
  2284. config_upper = cmSystemTools::UpperCase(desired_config);
  2285. }
  2286. std::string locPropBase;
  2287. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2288. locPropBase = "IMPORTED_LIBNAME";
  2289. } else if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2290. locPropBase = "IMPORTED_OBJECTS";
  2291. } else {
  2292. locPropBase = "IMPORTED_LOCATION";
  2293. }
  2294. // Track the configuration-specific property suffix.
  2295. suffix = cmStrCat('_', config_upper);
  2296. std::vector<std::string> mappedConfigs;
  2297. {
  2298. std::string mapProp = cmStrCat("MAP_IMPORTED_CONFIG_", config_upper);
  2299. if (cmValue mapValue = this->GetProperty(mapProp)) {
  2300. cmExpandList(*mapValue, mappedConfigs, true);
  2301. }
  2302. }
  2303. // If we needed to find one of the mapped configurations but did not
  2304. // On a DLL platform there may be only IMPORTED_IMPLIB for a shared
  2305. // library or an executable with exports.
  2306. bool allowImp = (this->IsDLLPlatform() &&
  2307. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2308. this->IsExecutableWithExports())) ||
  2309. (this->IsAIX() && this->IsExecutableWithExports());
  2310. // If a mapping was found, check its configurations.
  2311. for (auto mci = mappedConfigs.begin();
  2312. !loc && !imp && mci != mappedConfigs.end(); ++mci) {
  2313. // Look for this configuration.
  2314. if (mci->empty()) {
  2315. // An empty string in the mapping has a special meaning:
  2316. // look up the config-less properties.
  2317. loc = this->GetProperty(locPropBase);
  2318. if (allowImp) {
  2319. imp = this->GetProperty("IMPORTED_IMPLIB");
  2320. }
  2321. // If it was found, set the suffix.
  2322. if (loc || imp) {
  2323. suffix.clear();
  2324. }
  2325. } else {
  2326. std::string mcUpper = cmSystemTools::UpperCase(*mci);
  2327. std::string locProp = cmStrCat(locPropBase, '_', mcUpper);
  2328. loc = this->GetProperty(locProp);
  2329. if (allowImp) {
  2330. std::string impProp = cmStrCat("IMPORTED_IMPLIB_", mcUpper);
  2331. imp = this->GetProperty(impProp);
  2332. }
  2333. // If it was found, use it for all properties below.
  2334. if (loc || imp) {
  2335. suffix = cmStrCat('_', mcUpper);
  2336. }
  2337. }
  2338. }
  2339. // If we needed to find one of the mapped configurations but did not
  2340. // then the target location is not found. The project does not want
  2341. // any other configuration.
  2342. if (!mappedConfigs.empty() && !loc && !imp) {
  2343. // Interface libraries are always available because their
  2344. // library name is optional so it is okay to leave loc empty.
  2345. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  2346. }
  2347. // If we have not yet found it then there are no mapped
  2348. // configurations. Look for an exact-match.
  2349. if (!loc && !imp) {
  2350. std::string locProp = cmStrCat(locPropBase, suffix);
  2351. loc = this->GetProperty(locProp);
  2352. if (allowImp) {
  2353. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  2354. imp = this->GetProperty(impProp);
  2355. }
  2356. }
  2357. // If we have not yet found it then there are no mapped
  2358. // configurations and no exact match.
  2359. if (!loc && !imp) {
  2360. // The suffix computed above is not useful.
  2361. suffix.clear();
  2362. // Look for a configuration-less location. This may be set by
  2363. // manually-written code.
  2364. loc = this->GetProperty(locPropBase);
  2365. if (allowImp) {
  2366. imp = this->GetProperty("IMPORTED_IMPLIB");
  2367. }
  2368. }
  2369. // If we have not yet found it then the project is willing to try
  2370. // any available configuration.
  2371. if (!loc && !imp) {
  2372. std::vector<std::string> availableConfigs;
  2373. if (cmValue iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS")) {
  2374. cmExpandList(*iconfigs, availableConfigs);
  2375. }
  2376. for (auto aci = availableConfigs.begin();
  2377. !loc && !imp && aci != availableConfigs.end(); ++aci) {
  2378. suffix = cmStrCat('_', cmSystemTools::UpperCase(*aci));
  2379. std::string locProp = cmStrCat(locPropBase, suffix);
  2380. loc = this->GetProperty(locProp);
  2381. if (allowImp) {
  2382. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  2383. imp = this->GetProperty(impProp);
  2384. }
  2385. }
  2386. }
  2387. // If we have not yet found it then the target location is not available.
  2388. if (!loc && !imp) {
  2389. // Interface libraries are always available because their
  2390. // library name is optional so it is okay to leave loc empty.
  2391. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  2392. }
  2393. return true;
  2394. }