cmake - add support to run ctest on android #2347

Signed-off-by: Ralph Giles <giles@thaumas.net>
This commit is contained in:
Marcus Asteborg 2020-12-22 00:33:56 -08:00 committed by Ralph Giles
parent 2985a40afe
commit dfd6c88aaa
No known key found for this signature in database
GPG key ID: 9259A8F2D2D44C84
3 changed files with 90 additions and 19 deletions

View file

@ -579,7 +579,7 @@ if(OPUS_BUILD_PROGRAMS)
target_link_libraries(opus_compare PRIVATE opus ${OPUS_REQUIRED_LIBRARIES})
endif()
if(BUILD_TESTING)
if(BUILD_TESTING AND NOT BUILD_SHARED_LIBS)
enable_testing()
# tests
@ -590,16 +590,20 @@ if(BUILD_TESTING)
if(OPUS_FIXED_POINT)
target_compile_definitions(test_opus_decode PRIVATE DISABLE_FLOAT_API)
endif()
add_test(NAME test_opus_decode COMMAND $<TARGET_FILE:test_opus_decode> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME test_opus_decode COMMAND ${CMAKE_COMMAND}
-DTEST_EXECUTABLE=$<TARGET_FILE:test_opus_decode>
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P "${PROJECT_SOURCE_DIR}/cmake/RunTest.cmake")
add_executable(test_opus_padding ${test_opus_padding_sources})
target_include_directories(test_opus_padding
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(test_opus_padding PRIVATE opus)
add_test(NAME test_opus_padding COMMAND $<TARGET_FILE:test_opus_padding> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME test_opus_padding COMMAND ${CMAKE_COMMAND}
-DTEST_EXECUTABLE=$<TARGET_FILE:test_opus_padding>
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P "${PROJECT_SOURCE_DIR}/cmake/RunTest.cmake")
if(NOT BUILD_SHARED_LIBS)
# disable tests that depends on private API when building shared lib
add_executable(test_opus_api ${test_opus_api_sources})
target_include_directories(test_opus_api
PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
@ -607,12 +611,17 @@ if(BUILD_TESTING)
if(OPUS_FIXED_POINT)
target_compile_definitions(test_opus_api PRIVATE DISABLE_FLOAT_API)
endif()
add_test(NAME test_opus_api COMMAND $<TARGET_FILE:test_opus_api> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME test_opus_api COMMAND ${CMAKE_COMMAND}
-DTEST_EXECUTABLE=$<TARGET_FILE:test_opus_api>
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P "${PROJECT_SOURCE_DIR}/cmake/RunTest.cmake")
add_executable(test_opus_encode ${test_opus_encode_sources})
target_include_directories(test_opus_encode
PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
target_link_libraries(test_opus_encode PRIVATE opus)
add_test(NAME test_opus_encode COMMAND $<TARGET_FILE:test_opus_encode> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()
add_test(NAME test_opus_encode COMMAND ${CMAKE_COMMAND}
-DTEST_EXECUTABLE=$<TARGET_FILE:test_opus_encode>
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P "${PROJECT_SOURCE_DIR}/cmake/RunTest.cmake")
endif()

View file

@ -222,6 +222,7 @@ EXTRA_DIST = opus.pc.in \
cmake/OpusFunctions.cmake \
cmake/OpusPackageVersion.cmake \
cmake/OpusSources.cmake \
cmake/RunTest.cmake \
cmake/config.h.cmake.in \
cmake/vla.c \
meson/get-version.py \

61
cmake/RunTest.cmake Normal file
View file

@ -0,0 +1,61 @@
if(NOT EXISTS ${TEST_EXECUTABLE})
message(FATAL_ERROR "Error could not find ${TEST_EXECUTABLE}, ensure that you built the test binary")
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
# support to run plain old binary on android devices
# requires android debug bridge to be installed
find_program(adb_executable adb)
if(NOT adb_executable)
message(FATAL_ERROR "Error could not find adb")
endif()
# check if any device emulator is attached
execute_process(COMMAND ${adb_executable} shell echo RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error adb: no devices/emulators found")
endif()
# push binary
set(android_path /data/local/tmp)
execute_process(COMMAND ${adb_executable} push ${TEST_EXECUTABLE} ${android_path} RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error running ${adb_executable} push ${TEST_EXECUTABLE} ${android_path} failed with result ${CMD_RESULT}")
endif()
# set permissions
get_filename_component(test_executable ${TEST_EXECUTABLE} NAME)
set(test_executable_on_android /data/local/tmp/${test_executable})
execute_process(COMMAND ${adb_executable} shell chmod 555 ${test_executable_on_android} RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error running ${adb_executable} shell chmod 555 ${test_executable_on_android} failed with result ${CMD_RESULT}")
endif()
# run executable
execute_process(COMMAND ${adb_executable} shell ${test_executable_on_android} RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error running ${adb_executable} shell ${test_executable_on_android} failed with result ${CMD_RESULT}")
endif()
# clean up binary
execute_process(COMMAND ${adb_executable} shell rm ${test_executable_on_android} RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error running ${adb_executable} shell rm ${test_executable_on_android} failed with result ${CMD_RESULT}")
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS")
# CTest doesn't support iOS
message(FATAL_ERROR "Error CTest is not supported on iOS")
else()
# for other platforms just execute test binary on host
execute_process(COMMAND ${TEST_EXECUTABLE} RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error running ${TEST_EXECUTABLE} failed with result ${CMD_RESULT}")
endif()
endif()