cmTarget.cxx 59 KB

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