cmTarget.cxx 65 KB

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