From ea29a664cd974b407752ff5251d436ddc040020c Mon Sep 17 00:00:00 2001 From: daquexian Date: Sat, 25 May 2019 07:26:32 +0800 Subject: [PATCH] Fix android build when API<23, fix android test, update build doc and pipeline (#884) --- BUILD.md | 14 ++++++++++++++ onnxruntime/core/framework/path_lib.h | 4 ++-- onnxruntime/core/platform/posix/env.cc | 3 ++- .../test/providers/cpu/generator/random_test.cc | 4 ++-- tools/ci_build/github/linux/run_build.sh | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/BUILD.md b/BUILD.md index 06d878f922..e9f48890be 100644 --- a/BUILD.md +++ b/BUILD.md @@ -326,3 +326,17 @@ ls -l /code/onnxruntime/build/Linux/MinSizeRel/dist/*.whl ### Using other compilers (TODO) + +## Android Builds + +### Cross compiling on Linux + +1. Get Android NDK from https://developer.android.com/ndk/downloads. Please unzip it after downloading. + +2. Get a pre-compiled protoc: + + You may get it from https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip. Please unzip it after downloading. + +3. Denote the unzip destination in step 1 as $ANDROID_NDK, append `-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DONNX_CUSTOM_PROTOC_EXECUTABLE=path/to/protoc` to your cmake args, run cmake and make to build it. + +Note: For 32-bit devices, replace `-DANDROID_ABI=arm64-v8a` to `-DANDROID_ABI=armeabi-v7a`. diff --git a/onnxruntime/core/framework/path_lib.h b/onnxruntime/core/framework/path_lib.h index 3832fd5522..a7dd3878d6 100644 --- a/onnxruntime/core/framework/path_lib.h +++ b/onnxruntime/core/framework/path_lib.h @@ -236,7 +236,7 @@ void LoopDir(const std::string& dir_name, T func) { auto e = errno; char buf[1024]; char* msg; -#if defined(__GLIBC__) && defined(_GNU_SOURCE) +#if defined(__GLIBC__) && defined(_GNU_SOURCE) && !defined (__ANDROID__) msg = strerror_r(e, buf, sizeof(buf)); #else if (strerror_r(e, buf, sizeof(buf)) != 0) { @@ -270,4 +270,4 @@ inline T ReplaceFilename(const T& input, const T& new_value) { return ConcatPathComponent(ret, new_value); } -} // namespace onnxruntime \ No newline at end of file +} // namespace onnxruntime diff --git a/onnxruntime/core/platform/posix/env.cc b/onnxruntime/core/platform/posix/env.cc index 92375f6f31..2b7e50dee3 100644 --- a/onnxruntime/core/platform/posix/env.cc +++ b/onnxruntime/core/platform/posix/env.cc @@ -180,9 +180,10 @@ class PosixEnv : public Env { char buf[1024]; const char* msg = ""; if (e > 0) { -#if defined(__GLIBC__) && defined(_GNU_SOURCE) +#if defined(__GLIBC__) && defined(_GNU_SOURCE) && !defined (__ANDROID__) msg = strerror_r(e, buf, sizeof(buf)); #else + // for Mac OS X and Android lower than API 23 if (strerror_r(e, buf, sizeof(buf)) != 0) { buf[0] = '\0'; } diff --git a/onnxruntime/test/providers/cpu/generator/random_test.cc b/onnxruntime/test/providers/cpu/generator/random_test.cc index 350efbab23..9e5a804054 100644 --- a/onnxruntime/test/providers/cpu/generator/random_test.cc +++ b/onnxruntime/test/providers/cpu/generator/random_test.cc @@ -246,7 +246,7 @@ TEST(Random, MultinomialGoodCase) { const std::vector output_dims{batch_size, num_samples}; #ifdef _WIN32 const std::vector expected_output{2, 0, 0, 2, 2, 2, 0, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 0}; -#elif defined(__MACH__) +#elif defined(__MACH__) || defined (__ANDROID__) const std::vector expected_output{1, 1, 2, 2, 0, 2, 2, 2, 0, 2, 1, 1, 2, 0, 2, 2, 0, 2, 1, 1}; #else const std::vector expected_output{2, 0, 0, 1, 0, 1, 2, 0, 1, 0, 0, 1, 1, 0, 1, 0, 2, 0, 2, 0}; @@ -274,7 +274,7 @@ TEST(Random, MultinomialDefaultDType) { const std::vector output_dims{batch_size, num_samples}; #ifdef _WIN32 const std::vector expected_output{2, 0, 0, 2, 2, 2, 0, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 0}; -#elif defined(__MACH__) +#elif defined(__MACH__) || defined (__ANDROID__) const std::vector expected_output{1, 1, 2, 2, 0, 2, 2, 2, 0, 2, 1, 1, 2, 0, 2, 2, 0, 2, 1, 1}; #else const std::vector expected_output{2, 0, 0, 1, 0, 1, 2, 0, 1, 0, 0, 1, 1, 0, 1, 0, 2, 0, 2, 0}; diff --git a/tools/ci_build/github/linux/run_build.sh b/tools/ci_build/github/linux/run_build.sh index 97279f9336..d784ed265a 100755 --- a/tools/ci_build/github/linux/run_build.sh +++ b/tools/ci_build/github/linux/run_build.sh @@ -17,7 +17,7 @@ done if [ $BUILD_OS = "android" ]; then pushd /onnxruntime_src mkdir build-android && cd build-android - /opt/cmake/bin/cmake -DCMAKE_TOOLCHAIN_FILE=/android-ndk/build/cmake/android.toolchain.cmake -DANDROID_CPP_FEATURES=exceptions -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_BUILD_TYPE=Release -Donnxruntime_CROSS_COMPILING=ON -Donnxruntime_BUILD_x86=OFF -DONNX_CUSTOM_PROTOC_EXECUTABLE=/usr/bin/protoc ../cmake + /opt/cmake/bin/cmake -DCMAKE_TOOLCHAIN_FILE=/android-ndk/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DONNX_CUSTOM_PROTOC_EXECUTABLE=/usr/bin/protoc ../cmake /opt/cmake/bin/cmake --build . -- -j$(nproc) else COMMON_BUILD_ARGS="--skip_submodule_sync --enable_onnx_tests --parallel --build_shared_lib --use_openmp"