cmTarget.cxx 62 KB

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