cmTarget.cxx 68 KB

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