cmTarget.cxx 81 KB

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