cmTarget.cxx 64 KB

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