cmExportBuildAndroidMKGenerator.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "cmExportBuildAndroidMKGenerator.h"
  4. #include <map>
  5. #include <sstream>
  6. #include <utility>
  7. #include <vector>
  8. #include <cmext/algorithm>
  9. #include "cmGeneratorTarget.h"
  10. #include "cmLinkItem.h"
  11. #include "cmList.h"
  12. #include "cmMakefile.h"
  13. #include "cmMessageType.h"
  14. #include "cmPolicies.h"
  15. #include "cmStateTypes.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmTarget.h"
  19. cmExportBuildAndroidMKGenerator::cmExportBuildAndroidMKGenerator()
  20. {
  21. this->LG = nullptr;
  22. this->ExportSet = nullptr;
  23. }
  24. void cmExportBuildAndroidMKGenerator::GenerateImportHeaderCode(
  25. std::ostream& os, const std::string&)
  26. {
  27. os << "LOCAL_PATH := $(call my-dir)\n\n";
  28. }
  29. void cmExportBuildAndroidMKGenerator::GenerateImportFooterCode(std::ostream&)
  30. {
  31. }
  32. void cmExportBuildAndroidMKGenerator::GenerateExpectedTargetsCode(
  33. std::ostream&, const std::string&)
  34. {
  35. }
  36. void cmExportBuildAndroidMKGenerator::GenerateImportTargetCode(
  37. std::ostream& os, cmGeneratorTarget const* target,
  38. cmStateEnums::TargetType /*targetType*/)
  39. {
  40. std::string targetName = cmStrCat(this->Namespace, target->GetExportName());
  41. os << "include $(CLEAR_VARS)\n";
  42. os << "LOCAL_MODULE := ";
  43. os << targetName << "\n";
  44. os << "LOCAL_SRC_FILES := ";
  45. std::string const noConfig; // FIXME: What config to use here?
  46. std::string path =
  47. cmSystemTools::ConvertToOutputPath(target->GetFullPath(noConfig));
  48. os << path << "\n";
  49. }
  50. void cmExportBuildAndroidMKGenerator::GenerateImportPropertyCode(
  51. std::ostream&, const std::string&, cmGeneratorTarget const*,
  52. ImportPropertyMap const&)
  53. {
  54. }
  55. void cmExportBuildAndroidMKGenerator::GenerateMissingTargetsCheckCode(
  56. std::ostream&)
  57. {
  58. }
  59. void cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
  60. const cmGeneratorTarget* target, std::ostream& os,
  61. const ImportPropertyMap& properties)
  62. {
  63. std::string config;
  64. if (!this->Configurations.empty()) {
  65. config = this->Configurations[0];
  66. }
  67. cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
  68. target, os, properties, cmExportBuildAndroidMKGenerator::BUILD, config);
  69. }
  70. void cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
  71. const cmGeneratorTarget* target, std::ostream& os,
  72. const ImportPropertyMap& properties, GenerateType type,
  73. std::string const& config)
  74. {
  75. const bool newCMP0022Behavior =
  76. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  77. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  78. if (!newCMP0022Behavior) {
  79. std::ostringstream w;
  80. if (type == cmExportBuildAndroidMKGenerator::BUILD) {
  81. w << "export(TARGETS ... ANDROID_MK) called with policy CMP0022";
  82. } else {
  83. w << "install( EXPORT_ANDROID_MK ...) called with policy CMP0022";
  84. }
  85. w << " set to OLD for target " << target->Target->GetName() << ". "
  86. << "The export will only work with CMP0022 set to NEW.";
  87. target->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  88. }
  89. if (!properties.empty()) {
  90. os << "LOCAL_CPP_FEATURES := rtti exceptions\n";
  91. for (auto const& property : properties) {
  92. if (property.first == "INTERFACE_COMPILE_OPTIONS") {
  93. os << "LOCAL_CPP_FEATURES += ";
  94. os << (property.second) << "\n";
  95. } else if (property.first == "INTERFACE_LINK_LIBRARIES") {
  96. std::string staticLibs;
  97. std::string sharedLibs;
  98. std::string ldlibs;
  99. cmLinkInterfaceLibraries const* linkIFace =
  100. target->GetLinkInterfaceLibraries(
  101. config, target, cmGeneratorTarget::LinkInterfaceFor::Link);
  102. for (cmLinkItem const& item : linkIFace->Libraries) {
  103. cmGeneratorTarget const* gt = item.Target;
  104. std::string const& lib = item.AsStr();
  105. if (gt) {
  106. if (gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  107. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  108. sharedLibs += " " + lib;
  109. } else {
  110. staticLibs += " " + lib;
  111. }
  112. } else {
  113. bool relpath = false;
  114. if (type == cmExportBuildAndroidMKGenerator::INSTALL) {
  115. relpath = cmHasLiteralPrefix(lib, "../");
  116. }
  117. // check for full path or if it already has a -l, or
  118. // in the case of an install check for relative paths
  119. // if it is full or a link library then use string directly
  120. if (cmSystemTools::FileIsFullPath(lib) ||
  121. cmHasLiteralPrefix(lib, "-l") || relpath) {
  122. ldlibs += " " + lib;
  123. // if it is not a path and does not have a -l then add -l
  124. } else if (!lib.empty()) {
  125. ldlibs += " -l" + lib;
  126. }
  127. }
  128. }
  129. if (!sharedLibs.empty()) {
  130. os << "LOCAL_SHARED_LIBRARIES :=" << sharedLibs << "\n";
  131. }
  132. if (!staticLibs.empty()) {
  133. os << "LOCAL_STATIC_LIBRARIES :=" << staticLibs << "\n";
  134. }
  135. if (!ldlibs.empty()) {
  136. os << "LOCAL_EXPORT_LDLIBS :=" << ldlibs << "\n";
  137. }
  138. } else if (property.first == "INTERFACE_INCLUDE_DIRECTORIES") {
  139. std::string includes = property.second;
  140. cmList includeList{ includes };
  141. os << "LOCAL_EXPORT_C_INCLUDES := ";
  142. std::string end;
  143. for (std::string const& i : includeList) {
  144. os << end << i;
  145. end = "\\\n";
  146. }
  147. os << "\n";
  148. } else if (property.first == "INTERFACE_LINK_OPTIONS") {
  149. os << "LOCAL_EXPORT_LDFLAGS := ";
  150. cmList linkFlagsList{ property.second };
  151. os << linkFlagsList.join(" ") << "\n";
  152. } else {
  153. os << "# " << property.first << " " << (property.second) << "\n";
  154. }
  155. }
  156. }
  157. // Tell the NDK build system if prebuilt static libraries use C++.
  158. if (target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  159. cmLinkImplementation const* li = target->GetLinkImplementation(
  160. config, cmGeneratorTarget::LinkInterfaceFor::Link);
  161. if (cm::contains(li->Languages, "CXX")) {
  162. os << "LOCAL_HAS_CPP := true\n";
  163. }
  164. }
  165. switch (target->GetType()) {
  166. case cmStateEnums::SHARED_LIBRARY:
  167. case cmStateEnums::MODULE_LIBRARY:
  168. os << "include $(PREBUILT_SHARED_LIBRARY)\n";
  169. break;
  170. case cmStateEnums::STATIC_LIBRARY:
  171. os << "include $(PREBUILT_STATIC_LIBRARY)\n";
  172. break;
  173. case cmStateEnums::EXECUTABLE:
  174. case cmStateEnums::UTILITY:
  175. case cmStateEnums::OBJECT_LIBRARY:
  176. case cmStateEnums::GLOBAL_TARGET:
  177. case cmStateEnums::INTERFACE_LIBRARY:
  178. case cmStateEnums::UNKNOWN_LIBRARY:
  179. break;
  180. }
  181. os << "\n";
  182. }