cmTarget.cxx 69 KB

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