cmTarget.cxx 67 KB

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