Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve Android tests #15808

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions conans/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@
'android_ndk': {
"platform": "Darwin",
"exe": "ndk-build",
"default": "system",
"system": {
"path": {'Darwin': f'{homebrew_root}/share/android-ndk'}
}
},
# TODO: Intel oneAPI is not installed in CI yet. Uncomment this line whenever it's done.
# "intel_oneapi": {
Expand Down Expand Up @@ -238,15 +242,12 @@ def update(d, u):
except ImportError as e:
user_default_profiles = None

homebrew_root = "/opt/homebrew" if platform.machine() == "arm64" else "/usr/local"


tools_environments = {
'mingw32': {'Windows': {'MSYSTEM': 'MINGW32'}},
'mingw64': {'Windows': {'MSYSTEM': 'MINGW64'}},
'ucrt64': {'Windows': {'MSYSTEM': 'UCRT64'}},
'msys2_clang64': {"Windows": {"MSYSTEM": "CLANG64"}},
'android_ndk': {'Darwin': {'TEST_CONAN_ANDROID_NDK': f'{homebrew_root}/share/android-ndk'}}
'msys2_clang64': {"Windows": {"MSYSTEM": "CLANG64"}}
}


Expand Down
38 changes: 0 additions & 38 deletions conans/test/functional/toolchains/android/_utils.py

This file was deleted.

84 changes: 29 additions & 55 deletions conans/test/functional/toolchains/android/test_using_cmake.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,44 @@
import os
import platform
import tempfile
import textwrap

import pytest

from conan.tools.cmake import CMakeToolchain
from conans.test.functional.toolchains.android._utils import create_library
from conans.test.conftest import tools_locations
from conans.test.utils.tools import TestClient


@pytest.fixture
def client():
t = TestClient()
create_library(t)
t.save({
'conanfile.py': textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain

class Library(ConanFile):
name = 'library'
settings = 'os', 'arch', 'compiler', 'build_type'
exports_sources = "CMakeLists.txt", "lib.h", "lib.cpp"
options = {'shared': [True, False]}
default_options = {'shared': False}

def generate(self):
tc = CMakeToolchain(self)
tc.generate()

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
cmake = CMake(self)
cmake.install()
"""),
'profile_host': textwrap.dedent("""
[settings]
os=Android
os.api_level=23
arch=x86_64
compiler=clang
compiler.version=9
compiler.libcxx=c++_shared
build_type=Release
[conf]
tools.android:ndk_path={ndk_path}
""".format(ndk_path=os.getenv("TEST_CONAN_ANDROID_NDK")))
})
return t


@pytest.mark.tool("cmake")
@pytest.mark.tool("cmake", "3.23") # Android complains if <3.19
@pytest.mark.tool("ninja") # so it easily works in Windows too
@pytest.mark.tool("android_ndk")
@pytest.mark.skipif(platform.system() != "Darwin", reason="NDK only installed on MAC")
def test_use_cmake_toolchain(client):
def test_use_cmake_toolchain():
""" This is the naive approach, we follow instruction from CMake in its documentation
https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android
"""
# Build in the cache
client.run('create . --name=library --version=version --profile:host=profile_host --profile:build=default')
# Overriding the default folders, so they are in the same unit drive in Windows
# otherwise AndroidNDK FAILS to build, it needs using the same unit drive
c = TestClient(cache_folder=tempfile.mkdtemp(),
current_folder=tempfile.mkdtemp())
c.run("new cmake_lib -d name=hello -d version=0.1")
ndk_path = tools_locations["android_ndk"]["system"]["path"][platform.system()]
android = textwrap.dedent(f"""
[settings]
os=Android
os.api_level=23
arch=x86_64
compiler=clang
compiler.version=12
compiler.libcxx=c++_shared
build_type=Release
[conf]
tools.android:ndk_path={ndk_path}
tools.cmake.cmaketoolchain:generator=Ninja
""")
c.save({"android": android})
c.run('create . --profile:host=android')
assert "hello/0.1 (test package): Running test()" in c.out

# Build locally
client.run('install . --name=library --version=version --profile:host=profile_host --profile:build=default')
client.run_command('cmake . -DCMAKE_TOOLCHAIN_FILE={}'.format(CMakeToolchain.filename))
client.run_command('cmake --build .')
c.run('build . --profile:host=android')
assert "conanfile.py (hello/0.1): Running CMake.build()" in c.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from conan.tools.apple.apple import _to_apple_arch, XCRun
from conans.test.assets.sources import gen_function_cpp, gen_function_h
from conans.test.conftest import tools_locations
from conans.test.utils.mocks import ConanFileMock
from conans.test.utils.tools import TestClient
from conans.util.runners import conan_run
Expand Down Expand Up @@ -175,9 +176,10 @@ def test_android_meson_toolchain_cross_compiling(arch, expected_arch):
hello_h = gen_function_h(name="hello")
hello_cpp = gen_function_cpp(name="hello", preprocessor=["STRING_DEFINITION"])
app = gen_function_cpp(name="main", includes=["hello"], calls=["hello"])
ndk_path = tools_locations["android_ndk"]["system"]["path"][platform.system()]
profile_host = profile_host.format(
arch=arch,
ndk_path=os.getenv("TEST_CONAN_ANDROID_NDK")
ndk_path=ndk_path
)

client = TestClient()
Expand Down