Skip to content

[EXPERIMENTAL] allow test_packages run in emulated environment#19983

Open
perseoGI wants to merge 2 commits into
conan-io:develop2from
perseoGI:pgi/config/emulator-run
Open

[EXPERIMENTAL] allow test_packages run in emulated environment#19983
perseoGI wants to merge 2 commits into
conan-io:develop2from
perseoGI:pgi/config/emulator-run

Conversation

@perseoGI
Copy link
Copy Markdown
Contributor

Changelog: Feature: Allow running test_package in an emulated environment
Docs: https://github.com/conan-io/docs/pull/XXXX

Directly related to #19940

This PR makes use of the "incubating" emulator key in order to allow running the test package in a cross compilation.
This could be used in Conan Center Index in order to compile and test new configurations (e.g. emscripten, android).

Examples

Create a basic CMake library:

$ conan new cmake_lib -d name="cmake_lib" -d version=1.0 -o cmake_lib

Android NDK

[settings]
arch=armv8
build_type=Release
compiler=clang
compiler.cppstd=17
compiler.libcxx=c++_static
compiler.version=19
os=Android
os.api_level=28
[tool_requires]
*: android-ndk/[*]

[conf]
tools.build:compiler_executables={'emulator': '{{profile_dir}}/android_emulator.sh'}

android_emulator.sh -> wrapper to call Android Studio emulator (this could be far simpler if we use qemu-user emulator from a Linux environment (Macos only supports qemu-system which does not allow direct invokation)

For this, we create a wrapper:

#!/bin/bash
set -e

BINARY="$1"
shift

BINARY_NAME="$(basename "$BINARY")"
REMOTE_PATH="/data/local/tmp/$BINARY_NAME"

echo "Pushing $BINARY to emulator at $REMOTE_PATH"
adb push "$BINARY" "$REMOTE_PATH" >/dev/null 2>&1
adb shell chmod +x "$REMOTE_PATH"

echo "Executing $REMOTE_PATH with arguments: $@"
OUTPUT=$(adb shell "$REMOTE_PATH" "$@" 2>&1; echo "EXIT_CODE:$?")
EXIT_CODE=$(echo "$OUTPUT" | grep -o 'EXIT_CODE:[0-9]*' | cut -d: -f2)
echo "$OUTPUT" | grep -v 'EXIT_CODE:'

adb shell rm -f "$REMOTE_PATH" >/dev/null 2>&1
exit ${EXIT_CODE:-1}
$ conan create -pr android
...

======== Testing the package: Executing test ========
cmake_lib/1.0 (test package): Running test()
cmake_lib/1.0 (test package): RUN: /Users/perseo/sources/test/meson/cmake_emulator/android_emulator.sh ./example
Pushing ./example to emulator at /data/local/tmp/example
Executing /data/local/tmp/example with arguments: 
cmake_lib/1.0: Hello World Release!
  cmake_lib/1.0: __aarch64__ defined
  cmake_lib/1.0: __cplusplus201703
  cmake_lib/1.0: __GNUC__4
  cmake_lib/1.0: __GNUC_MINOR__2
  cmake_lib/1.0: __clang_major__21
cmake_lib/1.0 test_package

The test_package will be passed to the emulator wrapper, which will push the binary to the emulator and run from it!

Emscripten/Node

An easier example will be to use emscripten toolchain to cross-compile to WASM and then, invoke node directly.

[settings]
arch=wasm
build_type=Release
compiler=emcc
compiler.cppstd=17
compiler.libcxx=libc++
compiler.version=5.0.3
os=Emscripten

[conf]
tools.build:compiler_executables={'c':'emcc', 'cpp':'em++', 'emulator': '/tmp/node_wrapper.sh --trace-exit'}
$ conan create -pr wasm
...
======== Testing the package: Executing test ========
cmake_lib/1.0 (test package): Running test()
cmake_lib/1.0 (test package): RUN: node ./example
[EMULATOR] node --trace-exit ./example
cmake_lib/1.0: Hello World Release!
  cmake_lib/1.0: __cplusplus201703
  cmake_lib/1.0: __GNUC__4
  cmake_lib/1.0: __GNUC_MINOR__2
  cmake_lib/1.0: __clang_major__23
cmake_lib/1.0 test_package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant