Skip to content

Commit

Permalink
add conan.tools.android.android_abi
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceIm committed Jan 9, 2023
1 parent 6e1f2bb commit 624ef22
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions conan/tools/android/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from conan.tools.android.utils import android_abi
25 changes: 25 additions & 0 deletions conan/tools/android/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from conan.errors import ConanException


def android_abi(conanfile, context="host"):
"""
Returns Android-NDK ABI
:param conanfile: ConanFile instance
:param context: either "host", "build" or "target"
:return: Android-NDK ABI
"""
if context not in ["host", "build", "target"]:
raise ConanException("context argument must be either 'host', 'build' or 'target'")

settings = getattr(conanfile, f"settings_{context}", conanfile.settings)
arch = settings.get_safe("arch")
# https://cmake.org/cmake/help/latest/variable/CMAKE_ANDROID_ARCH_ABI.html
return {
"armv5el": "armeabi",
"armv5hf": "armeabi",
"armv5": "armeabi",
"armv6": "armeabi-v6",
"armv7": "armeabi-v7a",
"armv7hf": "armeabi-v7a",
"armv8": "arm64-v8a",
}.get(arch, arch)
8 changes: 2 additions & 6 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from jinja2 import Template

from conan.tools._compilers import architecture_flag, libcxx_flags
from conan.tools.android.utils import android_abi
from conan.tools.apple.apple import is_apple_os, to_apple_arch
from conan.tools.build import build_jobs
from conan.tools.build.cross_building import cross_building
Expand Down Expand Up @@ -286,11 +287,6 @@ def context(self):
if os_ != "Android":
return

android_abi = {"x86": "x86",
"x86_64": "x86_64",
"armv7": "armeabi-v7a",
"armv8": "arm64-v8a"}.get(str(self._conanfile.settings.arch))

# TODO: only 'c++_shared' y 'c++_static' supported?
# https://developer.android.com/ndk/guides/cpp-support
libcxx_str = self._conanfile.settings.get_safe("compiler.libcxx")
Expand All @@ -302,7 +298,7 @@ def context(self):

ctxt_toolchain = {
'android_platform': 'android-' + str(self._conanfile.settings.os.api_level),
'android_abi': android_abi,
'android_abi': android_abi(self._conanfile),
'android_stl': libcxx_str,
'android_ndk_path': android_ndk_path,
}
Expand Down
Empty file.
36 changes: 36 additions & 0 deletions conans/test/unittests/tools/android/test_android_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from conans.test.utils.mocks import ConanFileMock, MockSettings
from conan.tools.android import android_abi


def test_tools_android_abi():
settings_linux = MockSettings({"os": "Linux", "arch": "x86_64"})

for (arch, expected) in [
("armv5el", "armeabi"),
("armv5hf", "armeabi"),
("armv5", "armeabi"),
("armv6", "armeabi-v6"),
("armv7", "armeabi-v7a"),
("armv7hf", "armeabi-v7a"),
("armv8", "arm64-v8a"),
("x86", "x86"),
("x86_64", "x86_64"),
("mips", "mips"),
("mips_64", "mips_64"),
]:
conanfile = ConanFileMock()
settings_android = MockSettings({"os": "Android", "arch": arch})

conanfile.settings = settings_android
assert android_abi(conanfile) == expected
assert android_abi(conanfile, context="host") == expected
assert android_abi(conanfile, context="build") == expected
assert android_abi(conanfile, context="target") == expected

conanfile.settings = settings_linux
conanfile.settings_build = settings_android
assert android_abi(conanfile, context="build") == expected

conanfile.settings_build = settings_linux
conanfile.settings_target = settings_android
assert android_abi(conanfile, context="target") == expected

0 comments on commit 624ef22

Please sign in to comment.