cmExportBuildAndroidMKGenerator.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <algorithm>
  5. #include <memory> // IWYU pragma: keep
  6. #include <sstream>
  7. #include <utility>
  8. #include "cmGeneratorExpression.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmLinkItem.h"
  11. #include "cmLocalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmPolicies.h"
  14. #include "cmStateTypes.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cmake.h"
  18. cmExportBuildAndroidMKGenerator::cmExportBuildAndroidMKGenerator()
  19. {
  20. this->LG = nullptr;
  21. this->ExportSet = nullptr;
  22. }
  23. void cmExportBuildAndroidMKGenerator::GenerateImportHeaderCode(
  24. std::ostream& os, const std::string&)
  25. {
  26. os << "LOCAL_PATH := $(call my-dir)\n\n";
  27. }
  28. void cmExportBuildAndroidMKGenerator::GenerateImportFooterCode(std::ostream&)
  29. {
  30. }
  31. void cmExportBuildAndroidMKGenerator::GenerateExpectedTargetsCode(
  32. std::ostream&, const std::string&)
  33. {
  34. }
  35. void cmExportBuildAndroidMKGenerator::GenerateImportTargetCode(
  36. std::ostream& os, cmGeneratorTarget const* target,
  37. cmStateEnums::TargetType /*targetType*/)
  38. {
  39. std::string targetName = this->Namespace;
  40. targetName += target->GetExportName();
  41. os << "include $(CLEAR_VARS)\n";
  42. os << "LOCAL_MODULE := ";
  43. os << targetName << "\n";
  44. os << "LOCAL_SRC_FILES := ";
  45. std::string path = cmSystemTools::ConvertToOutputPath(target->GetFullPath());
  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&, const std::vector<std::string>&)
  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(cmake::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. // need to look at list in pi->second and see if static or shared
  95. // FindTargetToLink
  96. // target->GetLocalGenerator()->FindGeneratorTargetToUse()
  97. // then add to LOCAL_CPPFLAGS
  98. std::vector<std::string> libraries;
  99. cmSystemTools::ExpandListArgument(property.second, libraries);
  100. std::string staticLibs;
  101. std::string sharedLibs;
  102. std::string ldlibs;
  103. for (std::string const& lib : libraries) {
  104. cmGeneratorTarget* gt =
  105. target->GetLocalGenerator()->FindGeneratorTargetToUse(lib);
  106. if (gt) {
  107. if (gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  108. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  109. sharedLibs += " " + lib;
  110. } else {
  111. staticLibs += " " + lib;
  112. }
  113. } else {
  114. // evaluate any generator expressions with the current
  115. // build type of the makefile
  116. cmGeneratorExpression ge;
  117. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(lib);
  118. std::string evaluated =
  119. cge->Evaluate(target->GetLocalGenerator(), config);
  120. bool relpath = false;
  121. if (type == cmExportBuildAndroidMKGenerator::INSTALL) {
  122. relpath = lib.substr(0, 3) == "../";
  123. }
  124. // check for full path or if it already has a -l, or
  125. // in the case of an install check for relative paths
  126. // if it is full or a link library then use string directly
  127. if (cmSystemTools::FileIsFullPath(evaluated) ||
  128. evaluated.substr(0, 2) == "-l" || relpath) {
  129. ldlibs += " " + evaluated;
  130. // if it is not a path and does not have a -l then add -l
  131. } else if (!evaluated.empty()) {
  132. ldlibs += " -l" + evaluated;
  133. }
  134. }
  135. }
  136. if (!sharedLibs.empty()) {
  137. os << "LOCAL_SHARED_LIBRARIES :=" << sharedLibs << "\n";
  138. }
  139. if (!staticLibs.empty()) {
  140. os << "LOCAL_STATIC_LIBRARIES :=" << staticLibs << "\n";
  141. }
  142. if (!ldlibs.empty()) {
  143. os << "LOCAL_EXPORT_LDLIBS :=" << ldlibs << "\n";
  144. }
  145. } else if (property.first == "INTERFACE_INCLUDE_DIRECTORIES") {
  146. std::string includes = property.second;
  147. std::vector<std::string> includeList;
  148. cmSystemTools::ExpandListArgument(includes, includeList);
  149. os << "LOCAL_EXPORT_C_INCLUDES := ";
  150. std::string end;
  151. for (std::string const& i : includeList) {
  152. os << end << i;
  153. end = "\\\n";
  154. }
  155. os << "\n";
  156. } else {
  157. os << "# " << property.first << " " << (property.second) << "\n";
  158. }
  159. }
  160. }
  161. // Tell the NDK build system if prebuilt static libraries use C++.
  162. if (target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  163. cmLinkImplementation const* li = target->GetLinkImplementation(config);
  164. if (std::find(li->Languages.begin(), li->Languages.end(), "CXX") !=
  165. li->Languages.end()) {
  166. os << "LOCAL_HAS_CPP := true\n";
  167. }
  168. }
  169. switch (target->GetType()) {
  170. case cmStateEnums::SHARED_LIBRARY:
  171. case cmStateEnums::MODULE_LIBRARY:
  172. os << "include $(PREBUILT_SHARED_LIBRARY)\n";
  173. break;
  174. case cmStateEnums::STATIC_LIBRARY:
  175. os << "include $(PREBUILT_STATIC_LIBRARY)\n";
  176. break;
  177. case cmStateEnums::EXECUTABLE:
  178. case cmStateEnums::UTILITY:
  179. case cmStateEnums::OBJECT_LIBRARY:
  180. case cmStateEnums::GLOBAL_TARGET:
  181. case cmStateEnums::INTERFACE_LIBRARY:
  182. case cmStateEnums::UNKNOWN_LIBRARY:
  183. break;
  184. }
  185. os << "\n";
  186. }