cmTarget.cxx 68 KB

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