cmTarget.cxx 67 KB

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