cmTarget.cxx 66 KB

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