cmTarget.cxx 68 KB

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