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("Swift_LANGUAGE_VERSION");
  340. initProp("Swift_MODULE_DIRECTORY");
  341. initProp("VS_JUST_MY_CODE_DEBUGGING");
  342. initProp("DISABLE_PRECOMPILE_HEADERS");
  343. initProp("UNITY_BUILD");
  344. initPropValue("UNITY_BUILD_BATCH_SIZE", "8");
  345. #ifdef __APPLE__
  346. if (this->GetGlobalGenerator()->IsXcode()) {
  347. initProp("XCODE_SCHEME_ADDRESS_SANITIZER");
  348. initProp("XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN");
  349. initProp("XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING");
  350. initProp("XCODE_SCHEME_THREAD_SANITIZER");
  351. initProp("XCODE_SCHEME_THREAD_SANITIZER_STOP");
  352. initProp("XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER");
  353. initProp("XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP");
  354. initProp("XCODE_SCHEME_WORKING_DIRECTORY");
  355. initProp("XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER");
  356. initProp("XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP");
  357. initProp("XCODE_SCHEME_MALLOC_SCRIBBLE");
  358. initProp("XCODE_SCHEME_MALLOC_GUARD_EDGES");
  359. initProp("XCODE_SCHEME_GUARD_MALLOC");
  360. initProp("XCODE_SCHEME_ZOMBIE_OBJECTS");
  361. initProp("XCODE_SCHEME_MALLOC_STACK");
  362. initProp("XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE");
  363. initProp("XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS");
  364. initProp("XCODE_SCHEME_ENVIRONMENT");
  365. }
  366. #endif
  367. }
  368. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  369. initProp("FOLDER");
  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("LINK_LIBRARIES",
  854. this->GetDebugGeneratorExpressions(libName, llt));
  855. }
  856. if (cmGeneratorExpression::Find(lib) != std::string::npos || lib != libRef ||
  857. (tgt &&
  858. (tgt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  859. tgt->GetType() == cmStateEnums::OBJECT_LIBRARY)) ||
  860. (impl->Name == lib)) {
  861. return;
  862. }
  863. impl->OriginalLinkLibraries.emplace_back(lib, llt);
  864. // Add the explicit dependency information for libraries. This is
  865. // simply a set of libraries separated by ";". There should always
  866. // be a trailing ";". These library names are not canonical, in that
  867. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  868. // We shouldn't remove duplicates here because external libraries
  869. // may be purposefully duplicated to handle recursive dependencies,
  870. // and we removing one instance will break the link line. Duplicates
  871. // will be appropriately eliminated at emit time.
  872. if (impl->TargetType >= cmStateEnums::STATIC_LIBRARY &&
  873. impl->TargetType <= cmStateEnums::MODULE_LIBRARY &&
  874. (this->GetPolicyStatusCMP0073() == cmPolicies::OLD ||
  875. this->GetPolicyStatusCMP0073() == cmPolicies::WARN)) {
  876. std::string targetEntry = cmStrCat(impl->Name, "_LIB_DEPENDS");
  877. std::string dependencies;
  878. const char* old_val = mf.GetDefinition(targetEntry);
  879. if (old_val) {
  880. dependencies += old_val;
  881. }
  882. switch (llt) {
  883. case GENERAL_LibraryType:
  884. dependencies += "general";
  885. break;
  886. case DEBUG_LibraryType:
  887. dependencies += "debug";
  888. break;
  889. case OPTIMIZED_LibraryType:
  890. dependencies += "optimized";
  891. break;
  892. }
  893. dependencies += ";";
  894. dependencies += lib;
  895. dependencies += ";";
  896. mf.AddCacheDefinition(targetEntry, dependencies.c_str(),
  897. "Dependencies for the target", cmStateEnums::STATIC);
  898. }
  899. }
  900. void cmTarget::AddSystemIncludeDirectories(const std::set<std::string>& incs)
  901. {
  902. impl->SystemIncludeDirectories.insert(incs.begin(), incs.end());
  903. }
  904. std::set<std::string> const& cmTarget::GetSystemIncludeDirectories() const
  905. {
  906. return impl->SystemIncludeDirectories;
  907. }
  908. cmStringRange cmTarget::GetIncludeDirectoriesEntries() const
  909. {
  910. return cmMakeRange(impl->IncludeDirectoriesEntries);
  911. }
  912. cmBacktraceRange cmTarget::GetIncludeDirectoriesBacktraces() const
  913. {
  914. return cmMakeRange(impl->IncludeDirectoriesBacktraces);
  915. }
  916. cmStringRange cmTarget::GetCompileOptionsEntries() const
  917. {
  918. return cmMakeRange(impl->CompileOptionsEntries);
  919. }
  920. cmBacktraceRange cmTarget::GetCompileOptionsBacktraces() const
  921. {
  922. return cmMakeRange(impl->CompileOptionsBacktraces);
  923. }
  924. cmStringRange cmTarget::GetCompileFeaturesEntries() const
  925. {
  926. return cmMakeRange(impl->CompileFeaturesEntries);
  927. }
  928. cmBacktraceRange cmTarget::GetCompileFeaturesBacktraces() const
  929. {
  930. return cmMakeRange(impl->CompileFeaturesBacktraces);
  931. }
  932. cmStringRange cmTarget::GetCompileDefinitionsEntries() const
  933. {
  934. return cmMakeRange(impl->CompileDefinitionsEntries);
  935. }
  936. cmBacktraceRange cmTarget::GetCompileDefinitionsBacktraces() const
  937. {
  938. return cmMakeRange(impl->CompileDefinitionsBacktraces);
  939. }
  940. cmStringRange cmTarget::GetPrecompileHeadersEntries() const
  941. {
  942. return cmMakeRange(impl->PrecompileHeadersEntries);
  943. }
  944. cmBacktraceRange cmTarget::GetPrecompileHeadersBacktraces() const
  945. {
  946. return cmMakeRange(impl->PrecompileHeadersBacktraces);
  947. }
  948. cmStringRange cmTarget::GetSourceEntries() const
  949. {
  950. return cmMakeRange(impl->SourceEntries);
  951. }
  952. cmBacktraceRange cmTarget::GetSourceBacktraces() const
  953. {
  954. return cmMakeRange(impl->SourceBacktraces);
  955. }
  956. cmStringRange cmTarget::GetLinkOptionsEntries() const
  957. {
  958. return cmMakeRange(impl->LinkOptionsEntries);
  959. }
  960. cmBacktraceRange cmTarget::GetLinkOptionsBacktraces() const
  961. {
  962. return cmMakeRange(impl->LinkOptionsBacktraces);
  963. }
  964. cmStringRange cmTarget::GetLinkDirectoriesEntries() const
  965. {
  966. return cmMakeRange(impl->LinkDirectoriesEntries);
  967. }
  968. cmBacktraceRange cmTarget::GetLinkDirectoriesBacktraces() const
  969. {
  970. return cmMakeRange(impl->LinkDirectoriesBacktraces);
  971. }
  972. cmStringRange cmTarget::GetLinkImplementationEntries() const
  973. {
  974. return cmMakeRange(impl->LinkImplementationPropertyEntries);
  975. }
  976. cmBacktraceRange cmTarget::GetLinkImplementationBacktraces() const
  977. {
  978. return cmMakeRange(impl->LinkImplementationPropertyBacktraces);
  979. }
  980. void cmTarget::SetProperty(const std::string& prop, const char* value)
  981. {
  982. if (!cmTargetPropertyComputer::PassesWhitelist(
  983. this->GetType(), prop, impl->Makefile->GetMessenger(),
  984. impl->Makefile->GetBacktrace())) {
  985. return;
  986. }
  987. #define MAKE_STATIC_PROP(PROP) static const std::string prop##PROP = #PROP
  988. MAKE_STATIC_PROP(COMPILE_DEFINITIONS);
  989. MAKE_STATIC_PROP(COMPILE_FEATURES);
  990. MAKE_STATIC_PROP(COMPILE_OPTIONS);
  991. MAKE_STATIC_PROP(PRECOMPILE_HEADERS);
  992. MAKE_STATIC_PROP(PRECOMPILE_HEADERS_REUSE_FROM);
  993. MAKE_STATIC_PROP(CUDA_PTX_COMPILATION);
  994. MAKE_STATIC_PROP(EXPORT_NAME);
  995. MAKE_STATIC_PROP(IMPORTED_GLOBAL);
  996. MAKE_STATIC_PROP(INCLUDE_DIRECTORIES);
  997. MAKE_STATIC_PROP(LINK_OPTIONS);
  998. MAKE_STATIC_PROP(LINK_DIRECTORIES);
  999. MAKE_STATIC_PROP(LINK_LIBRARIES);
  1000. MAKE_STATIC_PROP(MANUALLY_ADDED_DEPENDENCIES);
  1001. MAKE_STATIC_PROP(NAME);
  1002. MAKE_STATIC_PROP(SOURCES);
  1003. MAKE_STATIC_PROP(TYPE);
  1004. #undef MAKE_STATIC_PROP
  1005. if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
  1006. impl->Makefile->IssueMessage(
  1007. MessageType::FATAL_ERROR,
  1008. "MANUALLY_ADDED_DEPENDENCIES property is read-only\n");
  1009. return;
  1010. }
  1011. if (prop == propNAME) {
  1012. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1013. "NAME property is read-only\n");
  1014. return;
  1015. }
  1016. if (prop == propTYPE) {
  1017. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1018. "TYPE property is read-only\n");
  1019. return;
  1020. }
  1021. if (prop == propEXPORT_NAME && this->IsImported()) {
  1022. std::ostringstream e;
  1023. e << "EXPORT_NAME property can't be set on imported targets (\""
  1024. << impl->Name << "\")\n";
  1025. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1026. return;
  1027. }
  1028. if (prop == propSOURCES && this->IsImported()) {
  1029. std::ostringstream e;
  1030. e << "SOURCES property can't be set on imported targets (\"" << impl->Name
  1031. << "\")\n";
  1032. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1033. return;
  1034. }
  1035. if (prop == propIMPORTED_GLOBAL && !this->IsImported()) {
  1036. std::ostringstream e;
  1037. e << "IMPORTED_GLOBAL property can't be set on non-imported targets (\""
  1038. << impl->Name << "\")\n";
  1039. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1040. return;
  1041. }
  1042. if (prop == propINCLUDE_DIRECTORIES) {
  1043. impl->IncludeDirectoriesEntries.clear();
  1044. impl->IncludeDirectoriesBacktraces.clear();
  1045. if (value) {
  1046. impl->IncludeDirectoriesEntries.emplace_back(value);
  1047. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1048. impl->IncludeDirectoriesBacktraces.push_back(lfbt);
  1049. }
  1050. } else if (prop == propCOMPILE_OPTIONS) {
  1051. impl->CompileOptionsEntries.clear();
  1052. impl->CompileOptionsBacktraces.clear();
  1053. if (value) {
  1054. impl->CompileOptionsEntries.emplace_back(value);
  1055. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1056. impl->CompileOptionsBacktraces.push_back(lfbt);
  1057. }
  1058. } else if (prop == propCOMPILE_FEATURES) {
  1059. impl->CompileFeaturesEntries.clear();
  1060. impl->CompileFeaturesBacktraces.clear();
  1061. if (value) {
  1062. impl->CompileFeaturesEntries.emplace_back(value);
  1063. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1064. impl->CompileFeaturesBacktraces.push_back(lfbt);
  1065. }
  1066. } else if (prop == propCOMPILE_DEFINITIONS) {
  1067. impl->CompileDefinitionsEntries.clear();
  1068. impl->CompileDefinitionsBacktraces.clear();
  1069. if (value) {
  1070. impl->CompileDefinitionsEntries.emplace_back(value);
  1071. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1072. impl->CompileDefinitionsBacktraces.push_back(lfbt);
  1073. }
  1074. } else if (prop == propLINK_OPTIONS) {
  1075. impl->LinkOptionsEntries.clear();
  1076. impl->LinkOptionsBacktraces.clear();
  1077. if (value) {
  1078. impl->LinkOptionsEntries.emplace_back(value);
  1079. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1080. impl->LinkOptionsBacktraces.push_back(lfbt);
  1081. }
  1082. } else if (prop == propLINK_DIRECTORIES) {
  1083. impl->LinkDirectoriesEntries.clear();
  1084. impl->LinkDirectoriesBacktraces.clear();
  1085. if (value) {
  1086. impl->LinkDirectoriesEntries.emplace_back(value);
  1087. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1088. impl->LinkDirectoriesBacktraces.push_back(lfbt);
  1089. }
  1090. } else if (prop == propPRECOMPILE_HEADERS) {
  1091. impl->PrecompileHeadersEntries.clear();
  1092. impl->PrecompileHeadersBacktraces.clear();
  1093. if (value) {
  1094. impl->PrecompileHeadersEntries.emplace_back(value);
  1095. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1096. impl->PrecompileHeadersBacktraces.push_back(lfbt);
  1097. }
  1098. } else if (prop == propLINK_LIBRARIES) {
  1099. impl->LinkImplementationPropertyEntries.clear();
  1100. impl->LinkImplementationPropertyBacktraces.clear();
  1101. if (value) {
  1102. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1103. impl->LinkImplementationPropertyEntries.emplace_back(value);
  1104. impl->LinkImplementationPropertyBacktraces.push_back(lfbt);
  1105. }
  1106. } else if (prop == propSOURCES) {
  1107. impl->SourceEntries.clear();
  1108. impl->SourceBacktraces.clear();
  1109. if (value) {
  1110. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1111. impl->SourceEntries.emplace_back(value);
  1112. impl->SourceBacktraces.push_back(lfbt);
  1113. }
  1114. } else if (prop == propIMPORTED_GLOBAL) {
  1115. if (!cmIsOn(value)) {
  1116. std::ostringstream e;
  1117. e << "IMPORTED_GLOBAL property can't be set to FALSE on targets (\""
  1118. << impl->Name << "\")\n";
  1119. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1120. return;
  1121. }
  1122. /* no need to change anything if value does not change */
  1123. if (!impl->ImportedGloballyVisible) {
  1124. impl->ImportedGloballyVisible = true;
  1125. this->GetGlobalGenerator()->IndexTarget(this);
  1126. }
  1127. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME") &&
  1128. !impl->CheckImportedLibName(prop, value ? value : "")) {
  1129. /* error was reported by check method */
  1130. } else if (prop == propCUDA_PTX_COMPILATION &&
  1131. this->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1132. std::ostringstream e;
  1133. e << "CUDA_PTX_COMPILATION property can only be applied to OBJECT "
  1134. "targets (\""
  1135. << impl->Name << "\")\n";
  1136. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1137. return;
  1138. } else if (prop == propPRECOMPILE_HEADERS_REUSE_FROM) {
  1139. if (this->GetProperty("PRECOMPILE_HEADERS")) {
  1140. std::ostringstream e;
  1141. e << "PRECOMPILE_HEADERS property is already set on target (\""
  1142. << impl->Name << "\")\n";
  1143. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1144. return;
  1145. }
  1146. auto reusedTarget =
  1147. impl->Makefile->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(
  1148. value);
  1149. if (!reusedTarget) {
  1150. const std::string e(
  1151. "PRECOMPILE_HEADERS_REUSE_FROM set with non existing target");
  1152. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  1153. return;
  1154. }
  1155. std::string reusedFrom = reusedTarget->GetSafeProperty(prop);
  1156. if (reusedFrom.empty()) {
  1157. reusedFrom = value;
  1158. }
  1159. impl->Properties.SetProperty(prop, reusedFrom.c_str());
  1160. reusedTarget->SetProperty("COMPILE_PDB_NAME", reusedFrom);
  1161. reusedTarget->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY",
  1162. cmStrCat(reusedFrom, ".dir/"));
  1163. this->SetProperty("COMPILE_PDB_NAME",
  1164. reusedTarget->GetProperty("COMPILE_PDB_NAME"));
  1165. this->AddUtility(reusedFrom, impl->Makefile);
  1166. } else {
  1167. impl->Properties.SetProperty(prop, value);
  1168. }
  1169. }
  1170. void cmTarget::AppendProperty(const std::string& prop, const char* value,
  1171. bool asString)
  1172. {
  1173. if (!cmTargetPropertyComputer::PassesWhitelist(
  1174. this->GetType(), prop, impl->Makefile->GetMessenger(),
  1175. impl->Makefile->GetBacktrace())) {
  1176. return;
  1177. }
  1178. if (prop == "NAME") {
  1179. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1180. "NAME property is read-only\n");
  1181. return;
  1182. }
  1183. if (prop == "EXPORT_NAME" && this->IsImported()) {
  1184. std::ostringstream e;
  1185. e << "EXPORT_NAME property can't be set on imported targets (\""
  1186. << impl->Name << "\")\n";
  1187. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1188. return;
  1189. }
  1190. if (prop == "SOURCES" && this->IsImported()) {
  1191. std::ostringstream e;
  1192. e << "SOURCES property can't be set on imported targets (\"" << impl->Name
  1193. << "\")\n";
  1194. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1195. return;
  1196. }
  1197. if (prop == "IMPORTED_GLOBAL") {
  1198. std::ostringstream e;
  1199. e << "IMPORTED_GLOBAL property can't be appended, only set on imported "
  1200. "targets (\""
  1201. << impl->Name << "\")\n";
  1202. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1203. return;
  1204. }
  1205. if (prop == "INCLUDE_DIRECTORIES") {
  1206. if (value && *value) {
  1207. impl->IncludeDirectoriesEntries.emplace_back(value);
  1208. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1209. impl->IncludeDirectoriesBacktraces.push_back(lfbt);
  1210. }
  1211. } else if (prop == "COMPILE_OPTIONS") {
  1212. if (value && *value) {
  1213. impl->CompileOptionsEntries.emplace_back(value);
  1214. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1215. impl->CompileOptionsBacktraces.push_back(lfbt);
  1216. }
  1217. } else if (prop == "COMPILE_FEATURES") {
  1218. if (value && *value) {
  1219. impl->CompileFeaturesEntries.emplace_back(value);
  1220. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1221. impl->CompileFeaturesBacktraces.push_back(lfbt);
  1222. }
  1223. } else if (prop == "COMPILE_DEFINITIONS") {
  1224. if (value && *value) {
  1225. impl->CompileDefinitionsEntries.emplace_back(value);
  1226. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1227. impl->CompileDefinitionsBacktraces.push_back(lfbt);
  1228. }
  1229. } else if (prop == "LINK_OPTIONS") {
  1230. if (value && *value) {
  1231. impl->LinkOptionsEntries.emplace_back(value);
  1232. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1233. impl->LinkOptionsBacktraces.push_back(lfbt);
  1234. }
  1235. } else if (prop == "LINK_DIRECTORIES") {
  1236. if (value && *value) {
  1237. impl->LinkDirectoriesEntries.emplace_back(value);
  1238. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1239. impl->LinkDirectoriesBacktraces.push_back(lfbt);
  1240. }
  1241. } else if (prop == "PRECOMPILE_HEADERS") {
  1242. if (this->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM")) {
  1243. std::ostringstream e;
  1244. e << "PRECOMPILE_HEADERS_REUSE_FROM property is already set on target "
  1245. "(\""
  1246. << impl->Name << "\")\n";
  1247. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1248. return;
  1249. }
  1250. if (value && *value) {
  1251. impl->PrecompileHeadersEntries.emplace_back(value);
  1252. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1253. impl->PrecompileHeadersBacktraces.push_back(lfbt);
  1254. }
  1255. } else if (prop == "LINK_LIBRARIES") {
  1256. if (value && *value) {
  1257. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1258. impl->LinkImplementationPropertyEntries.emplace_back(value);
  1259. impl->LinkImplementationPropertyBacktraces.push_back(lfbt);
  1260. }
  1261. } else if (prop == "SOURCES") {
  1262. cmListFileBacktrace lfbt = impl->Makefile->GetBacktrace();
  1263. impl->SourceEntries.emplace_back(value);
  1264. impl->SourceBacktraces.push_back(lfbt);
  1265. } else if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME")) {
  1266. impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1267. prop + " property may not be APPENDed.");
  1268. } else {
  1269. impl->Properties.AppendProperty(prop, value, asString);
  1270. }
  1271. }
  1272. void cmTarget::AppendBuildInterfaceIncludes()
  1273. {
  1274. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1275. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  1276. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1277. this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  1278. !this->IsExecutableWithExports()) {
  1279. return;
  1280. }
  1281. if (impl->BuildInterfaceIncludesAppended) {
  1282. return;
  1283. }
  1284. impl->BuildInterfaceIncludesAppended = true;
  1285. if (impl->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE")) {
  1286. std::string dirs = impl->Makefile->GetCurrentBinaryDirectory();
  1287. if (!dirs.empty()) {
  1288. dirs += ';';
  1289. }
  1290. dirs += impl->Makefile->GetCurrentSourceDirectory();
  1291. if (!dirs.empty()) {
  1292. this->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
  1293. ("$<BUILD_INTERFACE:" + dirs + ">"));
  1294. }
  1295. }
  1296. }
  1297. void cmTarget::InsertInclude(std::string const& entry,
  1298. cmListFileBacktrace const& bt, bool before)
  1299. {
  1300. auto position = before ? impl->IncludeDirectoriesEntries.begin()
  1301. : impl->IncludeDirectoriesEntries.end();
  1302. auto btPosition = before ? impl->IncludeDirectoriesBacktraces.begin()
  1303. : impl->IncludeDirectoriesBacktraces.end();
  1304. impl->IncludeDirectoriesEntries.insert(position, entry);
  1305. impl->IncludeDirectoriesBacktraces.insert(btPosition, bt);
  1306. }
  1307. void cmTarget::InsertCompileOption(std::string const& entry,
  1308. cmListFileBacktrace const& bt, bool before)
  1309. {
  1310. auto position = before ? impl->CompileOptionsEntries.begin()
  1311. : impl->CompileOptionsEntries.end();
  1312. auto btPosition = before ? impl->CompileOptionsBacktraces.begin()
  1313. : impl->CompileOptionsBacktraces.end();
  1314. impl->CompileOptionsEntries.insert(position, entry);
  1315. impl->CompileOptionsBacktraces.insert(btPosition, bt);
  1316. }
  1317. void cmTarget::InsertCompileDefinition(std::string const& entry,
  1318. cmListFileBacktrace const& bt)
  1319. {
  1320. impl->CompileDefinitionsEntries.push_back(entry);
  1321. impl->CompileDefinitionsBacktraces.push_back(bt);
  1322. }
  1323. void cmTarget::InsertLinkOption(std::string const& entry,
  1324. cmListFileBacktrace const& bt, bool before)
  1325. {
  1326. auto position =
  1327. before ? impl->LinkOptionsEntries.begin() : impl->LinkOptionsEntries.end();
  1328. auto btPosition = before ? impl->LinkOptionsBacktraces.begin()
  1329. : impl->LinkOptionsBacktraces.end();
  1330. impl->LinkOptionsEntries.insert(position, entry);
  1331. impl->LinkOptionsBacktraces.insert(btPosition, bt);
  1332. }
  1333. void cmTarget::InsertLinkDirectory(std::string const& entry,
  1334. cmListFileBacktrace const& bt, bool before)
  1335. {
  1336. auto position = before ? impl->LinkDirectoriesEntries.begin()
  1337. : impl->LinkDirectoriesEntries.end();
  1338. auto btPosition = before ? impl->LinkDirectoriesBacktraces.begin()
  1339. : impl->LinkDirectoriesBacktraces.end();
  1340. impl->LinkDirectoriesEntries.insert(position, entry);
  1341. impl->LinkDirectoriesBacktraces.insert(btPosition, bt);
  1342. }
  1343. void cmTarget::InsertPrecompileHeader(std::string const& entry,
  1344. cmListFileBacktrace const& bt)
  1345. {
  1346. impl->PrecompileHeadersEntries.push_back(entry);
  1347. impl->PrecompileHeadersBacktraces.push_back(bt);
  1348. }
  1349. static void cmTargetCheckLINK_INTERFACE_LIBRARIES(const std::string& prop,
  1350. const char* value,
  1351. cmMakefile* context,
  1352. bool imported)
  1353. {
  1354. // Look for link-type keywords in the value.
  1355. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  1356. if (!keys.find(value)) {
  1357. return;
  1358. }
  1359. // Support imported and non-imported versions of the property.
  1360. const char* base = (imported ? "IMPORTED_LINK_INTERFACE_LIBRARIES"
  1361. : "LINK_INTERFACE_LIBRARIES");
  1362. // Report an error.
  1363. std::ostringstream e;
  1364. e << "Property " << prop << " may not contain link-type keyword \""
  1365. << keys.match(2) << "\". "
  1366. << "The " << base << " property has a per-configuration "
  1367. << "version called " << base << "_<CONFIG> which may be "
  1368. << "used to specify per-configuration rules.";
  1369. if (!imported) {
  1370. e << " "
  1371. << "Alternatively, an IMPORTED library may be created, configured "
  1372. << "with a per-configuration location, and then named in the "
  1373. << "property value. "
  1374. << "See the add_library command's IMPORTED mode for details."
  1375. << "\n"
  1376. << "If you have a list of libraries that already contains the "
  1377. << "keyword, use the target_link_libraries command with its "
  1378. << "LINK_INTERFACE_LIBRARIES mode to set the property. "
  1379. << "The command automatically recognizes link-type keywords and sets "
  1380. << "the LINK_INTERFACE_LIBRARIES and LINK_INTERFACE_LIBRARIES_DEBUG "
  1381. << "properties accordingly.";
  1382. }
  1383. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1384. }
  1385. static void cmTargetCheckINTERFACE_LINK_LIBRARIES(const char* value,
  1386. cmMakefile* context)
  1387. {
  1388. // Look for link-type keywords in the value.
  1389. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  1390. if (!keys.find(value)) {
  1391. return;
  1392. }
  1393. // Report an error.
  1394. std::ostringstream e;
  1395. e << "Property INTERFACE_LINK_LIBRARIES may not contain link-type "
  1396. "keyword \""
  1397. << keys.match(2)
  1398. << "\". The INTERFACE_LINK_LIBRARIES "
  1399. "property may contain configuration-sensitive generator-expressions "
  1400. "which may be used to specify per-configuration rules.";
  1401. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1402. }
  1403. static void cmTargetCheckIMPORTED_GLOBAL(const cmTarget* target,
  1404. cmMakefile* context)
  1405. {
  1406. std::vector<cmTarget*> targets = context->GetOwnedImportedTargets();
  1407. auto it = std::find(targets.begin(), targets.end(), target);
  1408. if (it == targets.end()) {
  1409. std::ostringstream e;
  1410. e << "Attempt to promote imported target \"" << target->GetName()
  1411. << "\" to global scope (by setting IMPORTED_GLOBAL) "
  1412. "which is not built in this directory.";
  1413. context->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1414. }
  1415. }
  1416. void cmTarget::CheckProperty(const std::string& prop,
  1417. cmMakefile* context) const
  1418. {
  1419. // Certain properties need checking.
  1420. if (cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES")) {
  1421. if (const char* value = this->GetProperty(prop)) {
  1422. cmTargetCheckLINK_INTERFACE_LIBRARIES(prop, value, context, false);
  1423. }
  1424. }
  1425. if (cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES")) {
  1426. if (const char* value = this->GetProperty(prop)) {
  1427. cmTargetCheckLINK_INTERFACE_LIBRARIES(prop, value, context, true);
  1428. }
  1429. }
  1430. if (prop == "INTERFACE_LINK_LIBRARIES") {
  1431. if (const char* value = this->GetProperty(prop)) {
  1432. cmTargetCheckINTERFACE_LINK_LIBRARIES(value, context);
  1433. }
  1434. }
  1435. if (prop == "IMPORTED_GLOBAL") {
  1436. if (this->IsImported()) {
  1437. cmTargetCheckIMPORTED_GLOBAL(this, context);
  1438. }
  1439. }
  1440. }
  1441. const char* cmTarget::GetComputedProperty(
  1442. const std::string& prop, cmMessenger* messenger,
  1443. cmListFileBacktrace const& context) const
  1444. {
  1445. return cmTargetPropertyComputer::GetProperty(this, prop, messenger, context);
  1446. }
  1447. const char* cmTarget::GetProperty(const std::string& prop) const
  1448. {
  1449. #define MAKE_STATIC_PROP(PROP) static const std::string prop##PROP = #PROP
  1450. MAKE_STATIC_PROP(LINK_LIBRARIES);
  1451. MAKE_STATIC_PROP(TYPE);
  1452. MAKE_STATIC_PROP(INCLUDE_DIRECTORIES);
  1453. MAKE_STATIC_PROP(COMPILE_FEATURES);
  1454. MAKE_STATIC_PROP(COMPILE_OPTIONS);
  1455. MAKE_STATIC_PROP(COMPILE_DEFINITIONS);
  1456. MAKE_STATIC_PROP(LINK_OPTIONS);
  1457. MAKE_STATIC_PROP(LINK_DIRECTORIES);
  1458. MAKE_STATIC_PROP(PRECOMPILE_HEADERS);
  1459. MAKE_STATIC_PROP(IMPORTED);
  1460. MAKE_STATIC_PROP(IMPORTED_GLOBAL);
  1461. MAKE_STATIC_PROP(MANUALLY_ADDED_DEPENDENCIES);
  1462. MAKE_STATIC_PROP(NAME);
  1463. MAKE_STATIC_PROP(BINARY_DIR);
  1464. MAKE_STATIC_PROP(SOURCE_DIR);
  1465. MAKE_STATIC_PROP(SOURCES);
  1466. #undef MAKE_STATIC_PROP
  1467. static std::unordered_set<std::string> const specialProps{
  1468. propLINK_LIBRARIES,
  1469. propTYPE,
  1470. propINCLUDE_DIRECTORIES,
  1471. propCOMPILE_FEATURES,
  1472. propCOMPILE_OPTIONS,
  1473. propCOMPILE_DEFINITIONS,
  1474. propPRECOMPILE_HEADERS,
  1475. propLINK_OPTIONS,
  1476. propLINK_DIRECTORIES,
  1477. propIMPORTED,
  1478. propIMPORTED_GLOBAL,
  1479. propMANUALLY_ADDED_DEPENDENCIES,
  1480. propNAME,
  1481. propBINARY_DIR,
  1482. propSOURCE_DIR,
  1483. propSOURCES
  1484. };
  1485. if (specialProps.count(prop)) {
  1486. if (prop == propLINK_LIBRARIES) {
  1487. if (impl->LinkImplementationPropertyEntries.empty()) {
  1488. return nullptr;
  1489. }
  1490. static std::string output;
  1491. output = cmJoin(impl->LinkImplementationPropertyEntries, ";");
  1492. return output.c_str();
  1493. }
  1494. // the type property returns what type the target is
  1495. if (prop == propTYPE) {
  1496. return cmState::GetTargetTypeName(this->GetType());
  1497. }
  1498. if (prop == propINCLUDE_DIRECTORIES) {
  1499. if (impl->IncludeDirectoriesEntries.empty()) {
  1500. return nullptr;
  1501. }
  1502. static std::string output;
  1503. output = cmJoin(impl->IncludeDirectoriesEntries, ";");
  1504. return output.c_str();
  1505. }
  1506. if (prop == propCOMPILE_FEATURES) {
  1507. if (impl->CompileFeaturesEntries.empty()) {
  1508. return nullptr;
  1509. }
  1510. static std::string output;
  1511. output = cmJoin(impl->CompileFeaturesEntries, ";");
  1512. return output.c_str();
  1513. }
  1514. if (prop == propCOMPILE_OPTIONS) {
  1515. if (impl->CompileOptionsEntries.empty()) {
  1516. return nullptr;
  1517. }
  1518. static std::string output;
  1519. output = cmJoin(impl->CompileOptionsEntries, ";");
  1520. return output.c_str();
  1521. }
  1522. if (prop == propCOMPILE_DEFINITIONS) {
  1523. if (impl->CompileDefinitionsEntries.empty()) {
  1524. return nullptr;
  1525. }
  1526. static std::string output;
  1527. output = cmJoin(impl->CompileDefinitionsEntries, ";");
  1528. return output.c_str();
  1529. }
  1530. if (prop == propLINK_OPTIONS) {
  1531. if (impl->LinkOptionsEntries.empty()) {
  1532. return nullptr;
  1533. }
  1534. static std::string output;
  1535. output = cmJoin(impl->LinkOptionsEntries, ";");
  1536. return output.c_str();
  1537. }
  1538. if (prop == propLINK_DIRECTORIES) {
  1539. if (impl->LinkDirectoriesEntries.empty()) {
  1540. return nullptr;
  1541. }
  1542. static std::string output;
  1543. output = cmJoin(impl->LinkDirectoriesEntries, ";");
  1544. return output.c_str();
  1545. }
  1546. if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
  1547. if (impl->Utilities.empty()) {
  1548. return nullptr;
  1549. }
  1550. static std::string output;
  1551. output = cmJoin(impl->Utilities, ";");
  1552. return output.c_str();
  1553. }
  1554. if (prop == propPRECOMPILE_HEADERS) {
  1555. if (impl->PrecompileHeadersEntries.empty()) {
  1556. return nullptr;
  1557. }
  1558. static std::string output;
  1559. output = cmJoin(impl->PrecompileHeadersEntries, ";");
  1560. return output.c_str();
  1561. }
  1562. if (prop == propIMPORTED) {
  1563. return this->IsImported() ? "TRUE" : "FALSE";
  1564. }
  1565. if (prop == propIMPORTED_GLOBAL) {
  1566. return this->IsImportedGloballyVisible() ? "TRUE" : "FALSE";
  1567. }
  1568. if (prop == propNAME) {
  1569. return this->GetName().c_str();
  1570. }
  1571. if (prop == propBINARY_DIR) {
  1572. return impl->Makefile->GetStateSnapshot()
  1573. .GetDirectory()
  1574. .GetCurrentBinary()
  1575. .c_str();
  1576. }
  1577. if (prop == propSOURCE_DIR) {
  1578. return impl->Makefile->GetStateSnapshot()
  1579. .GetDirectory()
  1580. .GetCurrentSource()
  1581. .c_str();
  1582. }
  1583. }
  1584. const char* retVal = impl->Properties.GetPropertyValue(prop);
  1585. if (!retVal) {
  1586. const bool chain =
  1587. impl->Makefile->GetState()->IsPropertyChained(prop, cmProperty::TARGET);
  1588. if (chain) {
  1589. return impl->Makefile->GetStateSnapshot().GetDirectory().GetProperty(
  1590. prop, chain);
  1591. }
  1592. }
  1593. return retVal;
  1594. }
  1595. const char* cmTarget::GetSafeProperty(const std::string& prop) const
  1596. {
  1597. const char* ret = this->GetProperty(prop);
  1598. if (!ret) {
  1599. return "";
  1600. }
  1601. return ret;
  1602. }
  1603. bool cmTarget::GetPropertyAsBool(const std::string& prop) const
  1604. {
  1605. return cmIsOn(this->GetProperty(prop));
  1606. }
  1607. cmPropertyMap const& cmTarget::GetProperties() const
  1608. {
  1609. return impl->Properties;
  1610. }
  1611. bool cmTarget::IsDLLPlatform() const
  1612. {
  1613. return impl->IsDLLPlatform;
  1614. }
  1615. bool cmTarget::IsAIX() const
  1616. {
  1617. return impl->IsAIX;
  1618. }
  1619. bool cmTarget::IsImported() const
  1620. {
  1621. return impl->IsImportedTarget;
  1622. }
  1623. bool cmTarget::IsImportedGloballyVisible() const
  1624. {
  1625. return impl->ImportedGloballyVisible;
  1626. }
  1627. bool cmTarget::IsPerConfig() const
  1628. {
  1629. return impl->PerConfig;
  1630. }
  1631. const char* cmTarget::GetSuffixVariableInternal(
  1632. cmStateEnums::ArtifactType artifact) const
  1633. {
  1634. switch (this->GetType()) {
  1635. case cmStateEnums::STATIC_LIBRARY:
  1636. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  1637. case cmStateEnums::SHARED_LIBRARY:
  1638. switch (artifact) {
  1639. case cmStateEnums::RuntimeBinaryArtifact:
  1640. return "CMAKE_SHARED_LIBRARY_SUFFIX";
  1641. case cmStateEnums::ImportLibraryArtifact:
  1642. return "CMAKE_IMPORT_LIBRARY_SUFFIX";
  1643. }
  1644. break;
  1645. case cmStateEnums::MODULE_LIBRARY:
  1646. switch (artifact) {
  1647. case cmStateEnums::RuntimeBinaryArtifact:
  1648. return "CMAKE_SHARED_MODULE_SUFFIX";
  1649. case cmStateEnums::ImportLibraryArtifact:
  1650. return "CMAKE_IMPORT_LIBRARY_SUFFIX";
  1651. }
  1652. break;
  1653. case cmStateEnums::EXECUTABLE:
  1654. switch (artifact) {
  1655. case cmStateEnums::RuntimeBinaryArtifact:
  1656. // Android GUI application packages store the native
  1657. // binary as a shared library.
  1658. return (impl->IsAndroid && this->GetPropertyAsBool("ANDROID_GUI")
  1659. ? "CMAKE_SHARED_LIBRARY_SUFFIX"
  1660. : "CMAKE_EXECUTABLE_SUFFIX");
  1661. case cmStateEnums::ImportLibraryArtifact:
  1662. return (impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_SUFFIX"
  1663. : "CMAKE_IMPORT_LIBRARY_SUFFIX");
  1664. }
  1665. break;
  1666. default:
  1667. break;
  1668. }
  1669. return "";
  1670. }
  1671. const char* cmTarget::GetPrefixVariableInternal(
  1672. cmStateEnums::ArtifactType artifact) const
  1673. {
  1674. switch (this->GetType()) {
  1675. case cmStateEnums::STATIC_LIBRARY:
  1676. return "CMAKE_STATIC_LIBRARY_PREFIX";
  1677. case cmStateEnums::SHARED_LIBRARY:
  1678. switch (artifact) {
  1679. case cmStateEnums::RuntimeBinaryArtifact:
  1680. return "CMAKE_SHARED_LIBRARY_PREFIX";
  1681. case cmStateEnums::ImportLibraryArtifact:
  1682. return "CMAKE_IMPORT_LIBRARY_PREFIX";
  1683. }
  1684. break;
  1685. case cmStateEnums::MODULE_LIBRARY:
  1686. switch (artifact) {
  1687. case cmStateEnums::RuntimeBinaryArtifact:
  1688. return "CMAKE_SHARED_MODULE_PREFIX";
  1689. case cmStateEnums::ImportLibraryArtifact:
  1690. return "CMAKE_IMPORT_LIBRARY_PREFIX";
  1691. }
  1692. break;
  1693. case cmStateEnums::EXECUTABLE:
  1694. switch (artifact) {
  1695. case cmStateEnums::RuntimeBinaryArtifact:
  1696. // Android GUI application packages store the native
  1697. // binary as a shared library.
  1698. return (impl->IsAndroid && this->GetPropertyAsBool("ANDROID_GUI")
  1699. ? "CMAKE_SHARED_LIBRARY_PREFIX"
  1700. : "");
  1701. case cmStateEnums::ImportLibraryArtifact:
  1702. return (impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_PREFIX"
  1703. : "CMAKE_IMPORT_LIBRARY_PREFIX");
  1704. }
  1705. break;
  1706. default:
  1707. break;
  1708. }
  1709. return "";
  1710. }
  1711. std::string cmTarget::ImportedGetFullPath(
  1712. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1713. {
  1714. assert(this->IsImported());
  1715. // Lookup/compute/cache the import information for this
  1716. // configuration.
  1717. std::string desired_config = config;
  1718. if (config.empty()) {
  1719. desired_config = "NOCONFIG";
  1720. }
  1721. std::string result;
  1722. const char* loc = nullptr;
  1723. const char* imp = nullptr;
  1724. std::string suffix;
  1725. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  1726. this->GetMappedConfig(desired_config, &loc, &imp, suffix)) {
  1727. switch (artifact) {
  1728. case cmStateEnums::RuntimeBinaryArtifact:
  1729. if (loc) {
  1730. result = loc;
  1731. } else {
  1732. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  1733. if (const char* config_location = this->GetProperty(impProp)) {
  1734. result = config_location;
  1735. } else if (const char* location =
  1736. this->GetProperty("IMPORTED_LOCATION")) {
  1737. result = location;
  1738. }
  1739. }
  1740. break;
  1741. case cmStateEnums::ImportLibraryArtifact:
  1742. if (imp) {
  1743. result = imp;
  1744. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1745. this->IsExecutableWithExports()) {
  1746. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  1747. if (const char* config_implib = this->GetProperty(impProp)) {
  1748. result = config_implib;
  1749. } else if (const char* implib =
  1750. this->GetProperty("IMPORTED_IMPLIB")) {
  1751. result = implib;
  1752. }
  1753. }
  1754. break;
  1755. }
  1756. }
  1757. if (result.empty()) {
  1758. result = cmStrCat(this->GetName(), "-NOTFOUND");
  1759. }
  1760. return result;
  1761. }
  1762. bool cmTargetInternals::CheckImportedLibName(std::string const& prop,
  1763. std::string const& value) const
  1764. {
  1765. if (this->TargetType != cmStateEnums::INTERFACE_LIBRARY ||
  1766. !this->IsImportedTarget) {
  1767. this->Makefile->IssueMessage(
  1768. MessageType::FATAL_ERROR,
  1769. prop +
  1770. " property may be set only on imported INTERFACE library targets.");
  1771. return false;
  1772. }
  1773. if (!value.empty()) {
  1774. if (value[0] == '-') {
  1775. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1776. prop + " property value\n " + value +
  1777. "\nmay not start with '-'.");
  1778. return false;
  1779. }
  1780. std::string::size_type bad = value.find_first_of(":/\\;");
  1781. if (bad != std::string::npos) {
  1782. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  1783. prop + " property value\n " + value +
  1784. "\nmay not contain '" +
  1785. value.substr(bad, 1) + "'.");
  1786. return false;
  1787. }
  1788. }
  1789. return true;
  1790. }
  1791. bool cmTarget::GetMappedConfig(std::string const& desired_config,
  1792. const char** loc, const char** imp,
  1793. std::string& suffix) const
  1794. {
  1795. std::string config_upper;
  1796. if (!desired_config.empty()) {
  1797. config_upper = cmSystemTools::UpperCase(desired_config);
  1798. }
  1799. std::string locPropBase;
  1800. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1801. locPropBase = "IMPORTED_LIBNAME";
  1802. } else if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1803. locPropBase = "IMPORTED_OBJECTS";
  1804. } else {
  1805. locPropBase = "IMPORTED_LOCATION";
  1806. }
  1807. // Track the configuration-specific property suffix.
  1808. suffix = cmStrCat('_', config_upper);
  1809. std::vector<std::string> mappedConfigs;
  1810. {
  1811. std::string mapProp = cmStrCat("MAP_IMPORTED_CONFIG_", config_upper);
  1812. if (const char* mapValue = this->GetProperty(mapProp)) {
  1813. cmExpandList(mapValue, mappedConfigs, true);
  1814. }
  1815. }
  1816. // If we needed to find one of the mapped configurations but did not
  1817. // On a DLL platform there may be only IMPORTED_IMPLIB for a shared
  1818. // library or an executable with exports.
  1819. bool allowImp = (this->IsDLLPlatform() &&
  1820. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1821. this->IsExecutableWithExports())) ||
  1822. (this->IsAIX() && this->IsExecutableWithExports());
  1823. // If a mapping was found, check its configurations.
  1824. for (auto mci = mappedConfigs.begin();
  1825. !*loc && !*imp && mci != mappedConfigs.end(); ++mci) {
  1826. // Look for this configuration.
  1827. if (mci->empty()) {
  1828. // An empty string in the mapping has a special meaning:
  1829. // look up the config-less properties.
  1830. *loc = this->GetProperty(locPropBase);
  1831. if (allowImp) {
  1832. *imp = this->GetProperty("IMPORTED_IMPLIB");
  1833. }
  1834. // If it was found, set the suffix.
  1835. if (*loc || *imp) {
  1836. suffix.clear();
  1837. }
  1838. } else {
  1839. std::string mcUpper = cmSystemTools::UpperCase(*mci);
  1840. std::string locProp = cmStrCat(locPropBase, '_', mcUpper);
  1841. *loc = this->GetProperty(locProp);
  1842. if (allowImp) {
  1843. std::string impProp = cmStrCat("IMPORTED_IMPLIB_", mcUpper);
  1844. *imp = this->GetProperty(impProp);
  1845. }
  1846. // If it was found, use it for all properties below.
  1847. if (*loc || *imp) {
  1848. suffix = cmStrCat('_', mcUpper);
  1849. }
  1850. }
  1851. }
  1852. // If we needed to find one of the mapped configurations but did not
  1853. // then the target location is not found. The project does not want
  1854. // any other configuration.
  1855. if (!mappedConfigs.empty() && !*loc && !*imp) {
  1856. // Interface libraries are always available because their
  1857. // library name is optional so it is okay to leave *loc empty.
  1858. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  1859. }
  1860. // If we have not yet found it then there are no mapped
  1861. // configurations. Look for an exact-match.
  1862. if (!*loc && !*imp) {
  1863. std::string locProp = cmStrCat(locPropBase, suffix);
  1864. *loc = this->GetProperty(locProp);
  1865. if (allowImp) {
  1866. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  1867. *imp = this->GetProperty(impProp);
  1868. }
  1869. }
  1870. // If we have not yet found it then there are no mapped
  1871. // configurations and no exact match.
  1872. if (!*loc && !*imp) {
  1873. // The suffix computed above is not useful.
  1874. suffix.clear();
  1875. // Look for a configuration-less location. This may be set by
  1876. // manually-written code.
  1877. *loc = this->GetProperty(locPropBase);
  1878. if (allowImp) {
  1879. *imp = this->GetProperty("IMPORTED_IMPLIB");
  1880. }
  1881. }
  1882. // If we have not yet found it then the project is willing to try
  1883. // any available configuration.
  1884. if (!*loc && !*imp) {
  1885. std::vector<std::string> availableConfigs;
  1886. if (const char* iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS")) {
  1887. cmExpandList(iconfigs, availableConfigs);
  1888. }
  1889. for (auto aci = availableConfigs.begin();
  1890. !*loc && !*imp && aci != availableConfigs.end(); ++aci) {
  1891. suffix = cmStrCat('_', cmSystemTools::UpperCase(*aci));
  1892. std::string locProp = cmStrCat(locPropBase, suffix);
  1893. *loc = this->GetProperty(locProp);
  1894. if (allowImp) {
  1895. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  1896. *imp = this->GetProperty(impProp);
  1897. }
  1898. }
  1899. }
  1900. // If we have not yet found it then the target location is not available.
  1901. if (!*loc && !*imp) {
  1902. // Interface libraries are always available because their
  1903. // library name is optional so it is okay to leave *loc empty.
  1904. return this->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  1905. }
  1906. return true;
  1907. }