cmExportBuildAndroidMKGenerator.cxx 7.3 KB

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