cmTarget.cxx 63 KB

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