-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
conan.tools.android.android_abi()
(#12873)
* add conan.tools.android.android_abi * improve unit tests * improt unit test again * Dont fall back to settings if context settings are missing * Fix tests --------- Co-authored-by: Rubén Rincón Blanco <rubenrb@jfrog.com>
- Loading branch information
Showing
5 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from conan.tools.android.utils import android_abi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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(f"context argument must be either 'host', 'build' or 'target', was '{context}'") | ||
|
||
try: | ||
settings = getattr(conanfile, f"settings_{context}") | ||
except AttributeError: | ||
if context == "host": | ||
settings = conanfile.settings | ||
else: | ||
raise ConanException(f"settings_{context} not declared in recipe") | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from conans.test.utils.mocks import ConanFileMock, MockSettings | ||
from conans.errors import ConanException | ||
from conan.tools.android import android_abi | ||
|
||
from pytest import raises | ||
|
||
def test_tools_android_abi(): | ||
settings_linux = MockSettings({"os": "Linux", "arch": "foo"}) | ||
|
||
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}) | ||
|
||
# 1 profile (legacy native build) | ||
conanfile.settings = settings_android | ||
assert android_abi(conanfile) == expected | ||
assert android_abi(conanfile, context="host") == expected | ||
|
||
with raises(ConanException): | ||
assert android_abi(conanfile, context="build") == expected | ||
|
||
with raises(ConanException): | ||
assert android_abi(conanfile, context="target") == expected | ||
|
||
# 2 profiles | ||
## native build | ||
conanfile.settings = settings_android | ||
conanfile.settings_host = settings_android | ||
conanfile.settings_build = settings_android | ||
assert android_abi(conanfile) == expected | ||
assert android_abi(conanfile, context="host") == expected | ||
assert android_abi(conanfile, context="build") == expected | ||
|
||
with raises(ConanException): | ||
assert android_abi(conanfile, context="target") == expected | ||
|
||
## cross-build from Android to Linux (quite hypothetical) | ||
conanfile.settings = settings_linux | ||
conanfile.settings_host = settings_linux | ||
conanfile.settings_build = settings_android | ||
assert android_abi(conanfile) != expected | ||
assert android_abi(conanfile, context="host") != expected | ||
assert android_abi(conanfile, context="build") == expected | ||
|
||
with raises(ConanException): | ||
assert android_abi(conanfile, context="target") | ||
|
||
## cross-build a recipe from Linux to Android: | ||
### test android_abi in recipe itself | ||
conanfile.settings = settings_android | ||
conanfile.settings_host = settings_android | ||
conanfile.settings_build = settings_linux | ||
assert android_abi(conanfile) == expected | ||
assert android_abi(conanfile, context="host") == expected | ||
assert android_abi(conanfile, context="build") != expected | ||
with raises(ConanException): | ||
android_abi(conanfile, context="target") | ||
|
||
### test android_abi in "compiler recipe" (ie a android-ndk recipe in tool_requires of recipe being cross-build) | ||
conanfile.settings = settings_linux | ||
conanfile.settings_host = settings_linux | ||
conanfile.settings_build = settings_linux | ||
conanfile.settings_target = 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 |