cmExportBuildAndroidMKGenerator.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, cmGeneratorTarget const* target,
  38. cmStateEnums::TargetType /*targetType*/)
  39. {
  40. std::string targetName = this->Namespace;
  41. targetName += target->GetExportName();
  42. os << "include $(CLEAR_VARS)\n";
  43. os << "LOCAL_MODULE := ";
  44. os << targetName << "\n";
  45. os << "LOCAL_SRC_FILES := ";
  46. std::string path = cmSystemTools::ConvertToOutputPath(target->GetFullPath());
  47. os << path << "\n";
  48. }
  49. void cmExportBuildAndroidMKGenerator::GenerateImportPropertyCode(
  50. std::ostream&, const std::string&, cmGeneratorTarget const*,
  51. ImportPropertyMap const&)
  52. {
  53. }
  54. void cmExportBuildAndroidMKGenerator::GenerateMissingTargetsCheckCode(
  55. std::ostream&, const std::vector<std::string>&)
  56. {
  57. }
  58. void cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
  59. const cmGeneratorTarget* target, std::ostream& os,
  60. const ImportPropertyMap& properties)
  61. {
  62. std::string config;
  63. if (!this->Configurations.empty()) {
  64. config = this->Configurations[0];
  65. }
  66. cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
  67. target, os, properties, cmExportBuildAndroidMKGenerator::BUILD, config);
  68. }
  69. void cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
  70. const cmGeneratorTarget* target, std::ostream& os,
  71. const ImportPropertyMap& properties, GenerateType type,
  72. std::string const& config)
  73. {
  74. const bool newCMP0022Behavior =
  75. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  76. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  77. if (!newCMP0022Behavior) {
  78. std::ostringstream w;
  79. if (type == cmExportBuildAndroidMKGenerator::BUILD) {
  80. w << "export(TARGETS ... ANDROID_MK) called with policy CMP0022";
  81. } else {
  82. w << "install( EXPORT_ANDROID_MK ...) called with policy CMP0022";
  83. }
  84. w << " set to OLD for target " << target->Target->GetName() << ". "
  85. << "The export will only work with CMP0022 set to NEW.";
  86. target->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  87. }
  88. if (!properties.empty()) {
  89. os << "LOCAL_CPP_FEATURES := rtti exceptions\n";
  90. for (auto const& property : properties) {
  91. if (property.first == "INTERFACE_COMPILE_OPTIONS") {
  92. os << "LOCAL_CPP_FEATURES += ";
  93. os << (property.second) << "\n";
  94. } else if (property.first == "INTERFACE_LINK_LIBRARIES") {
  95. // evaluate any generator expressions with the current
  96. // build type of the makefile
  97. cmGeneratorExpression ge;
  98. cmGeneratorExpressionDAGChecker dagChecker(
  99. target->GetName(), "INTERFACE_LINK_LIBRARIES", nullptr, nullptr);
  100. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  101. ge.Parse(property.second);
  102. std::string evaluated = cge->Evaluate(
  103. target->GetLocalGenerator(), config, false, target, &dagChecker);
  104. // need to look at list in pi->second and see if static or shared
  105. // FindTargetToLink
  106. // target->GetLocalGenerator()->FindGeneratorTargetToUse()
  107. // then add to LOCAL_CPPFLAGS
  108. std::vector<std::string> libraries;
  109. cmSystemTools::ExpandListArgument(evaluated, libraries);
  110. std::string staticLibs;
  111. std::string sharedLibs;
  112. std::string ldlibs;
  113. for (std::string const& lib : libraries) {
  114. cmGeneratorTarget* gt =
  115. target->GetLocalGenerator()->FindGeneratorTargetToUse(lib);
  116. if (gt) {
  117. if (gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  118. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  119. sharedLibs += " " + lib;
  120. } else {
  121. staticLibs += " " + lib;
  122. }
  123. } else {
  124. bool relpath = false;
  125. if (type == cmExportBuildAndroidMKGenerator::INSTALL) {
  126. relpath = lib.substr(0, 3) == "../";
  127. }
  128. // check for full path or if it already has a -l, or
  129. // in the case of an install check for relative paths
  130. // if it is full or a link library then use string directly
  131. if (cmSystemTools::FileIsFullPath(lib) ||
  132. lib.substr(0, 2) == "-l" || relpath) {
  133. ldlibs += " " + lib;
  134. // if it is not a path and does not have a -l then add -l
  135. } else if (!lib.empty()) {
  136. ldlibs += " -l" + lib;
  137. }
  138. }
  139. }
  140. if (!sharedLibs.empty()) {
  141. os << "LOCAL_SHARED_LIBRARIES :=" << sharedLibs << "\n";
  142. }
  143. if (!staticLibs.empty()) {
  144. os << "LOCAL_STATIC_LIBRARIES :=" << staticLibs << "\n";
  145. }
  146. if (!ldlibs.empty()) {
  147. os << "LOCAL_EXPORT_LDLIBS :=" << ldlibs << "\n";
  148. }
  149. } else if (property.first == "INTERFACE_INCLUDE_DIRECTORIES") {
  150. std::string includes = property.second;
  151. std::vector<std::string> includeList;
  152. cmSystemTools::ExpandListArgument(includes, includeList);
  153. os << "LOCAL_EXPORT_C_INCLUDES := ";
  154. std::string end;
  155. for (std::string const& i : includeList) {
  156. os << end << i;
  157. end = "\\\n";
  158. }
  159. os << "\n";
  160. } else {
  161. os << "# " << property.first << " " << (property.second) << "\n";
  162. }
  163. }
  164. }
  165. // Tell the NDK build system if prebuilt static libraries use C++.
  166. if (target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  167. cmLinkImplementation const* li = target->GetLinkImplementation(config);
  168. if (std::find(li->Languages.begin(), li->Languages.end(), "CXX") !=
  169. li->Languages.end()) {
  170. os << "LOCAL_HAS_CPP := true\n";
  171. }
  172. }
  173. switch (target->GetType()) {
  174. case cmStateEnums::SHARED_LIBRARY:
  175. case cmStateEnums::MODULE_LIBRARY:
  176. os << "include $(PREBUILT_SHARED_LIBRARY)\n";
  177. break;
  178. case cmStateEnums::STATIC_LIBRARY:
  179. os << "include $(PREBUILT_STATIC_LIBRARY)\n";
  180. break;
  181. case cmStateEnums::EXECUTABLE:
  182. case cmStateEnums::UTILITY:
  183. case cmStateEnums::OBJECT_LIBRARY:
  184. case cmStateEnums::GLOBAL_TARGET:
  185. case cmStateEnums::INTERFACE_LIBRARY:
  186. case cmStateEnums::UNKNOWN_LIBRARY:
  187. break;
  188. }
  189. os << "\n";
  190. }