cmExportBuildAndroidMKGenerator.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "cmGeneratorExpressionDAGChecker.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmLinkItem.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmPolicies.h"
  15. #include "cmStateTypes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include "cmake.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, const cmGeneratorTarget* target)
  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. // evaluate any generator expressions with the current
  95. // build type of the makefile
  96. cmGeneratorExpression ge;
  97. cmGeneratorExpressionDAGChecker dagChecker(
  98. target->GetName(), "INTERFACE_LINK_LIBRARIES", nullptr, nullptr);
  99. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  100. ge.Parse(property.second);
  101. std::string evaluated = cge->Evaluate(
  102. target->GetLocalGenerator(), config, false, target, &dagChecker);
  103. // need to look at list in pi->second and see if static or shared
  104. // FindTargetToLink
  105. // target->GetLocalGenerator()->FindGeneratorTargetToUse()
  106. // then add to LOCAL_CPPFLAGS
  107. std::vector<std::string> libraries;
  108. cmSystemTools::ExpandListArgument(evaluated, libraries);
  109. std::string staticLibs;
  110. std::string sharedLibs;
  111. std::string ldlibs;
  112. for (std::string const& lib : libraries) {
  113. cmGeneratorTarget* gt =
  114. target->GetLocalGenerator()->FindGeneratorTargetToUse(lib);
  115. if (gt) {
  116. if (gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  117. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  118. sharedLibs += " " + lib;
  119. } else {
  120. staticLibs += " " + lib;
  121. }
  122. } else {
  123. bool relpath = false;
  124. if (type == cmExportBuildAndroidMKGenerator::INSTALL) {
  125. relpath = lib.substr(0, 3) == "../";
  126. }
  127. // check for full path or if it already has a -l, or
  128. // in the case of an install check for relative paths
  129. // if it is full or a link library then use string directly
  130. if (cmSystemTools::FileIsFullPath(lib) ||
  131. lib.substr(0, 2) == "-l" || relpath) {
  132. ldlibs += " " + lib;
  133. // if it is not a path and does not have a -l then add -l
  134. } else if (!lib.empty()) {
  135. ldlibs += " -l" + lib;
  136. }
  137. }
  138. }
  139. if (!sharedLibs.empty()) {
  140. os << "LOCAL_SHARED_LIBRARIES :=" << sharedLibs << "\n";
  141. }
  142. if (!staticLibs.empty()) {
  143. os << "LOCAL_STATIC_LIBRARIES :=" << staticLibs << "\n";
  144. }
  145. if (!ldlibs.empty()) {
  146. os << "LOCAL_EXPORT_LDLIBS :=" << ldlibs << "\n";
  147. }
  148. } else if (property.first == "INTERFACE_INCLUDE_DIRECTORIES") {
  149. std::string includes = property.second;
  150. std::vector<std::string> includeList;
  151. cmSystemTools::ExpandListArgument(includes, includeList);
  152. os << "LOCAL_EXPORT_C_INCLUDES := ";
  153. std::string end;
  154. for (std::string const& i : includeList) {
  155. os << end << i;
  156. end = "\\\n";
  157. }
  158. os << "\n";
  159. } else {
  160. os << "# " << property.first << " " << (property.second) << "\n";
  161. }
  162. }
  163. }
  164. // Tell the NDK build system if prebuilt static libraries use C++.
  165. if (target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  166. cmLinkImplementation const* li = target->GetLinkImplementation(config);
  167. if (std::find(li->Languages.begin(), li->Languages.end(), "CXX") !=
  168. li->Languages.end()) {
  169. os << "LOCAL_HAS_CPP := true\n";
  170. }
  171. }
  172. switch (target->GetType()) {
  173. case cmStateEnums::SHARED_LIBRARY:
  174. case cmStateEnums::MODULE_LIBRARY:
  175. os << "include $(PREBUILT_SHARED_LIBRARY)\n";
  176. break;
  177. case cmStateEnums::STATIC_LIBRARY:
  178. os << "include $(PREBUILT_STATIC_LIBRARY)\n";
  179. break;
  180. case cmStateEnums::EXECUTABLE:
  181. case cmStateEnums::UTILITY:
  182. case cmStateEnums::OBJECT_LIBRARY:
  183. case cmStateEnums::GLOBAL_TARGET:
  184. case cmStateEnums::INTERFACE_LIBRARY:
  185. case cmStateEnums::UNKNOWN_LIBRARY:
  186. break;
  187. }
  188. os << "\n";
  189. }