cmExportBuildAndroidMKGenerator.cxx 6.6 KB

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