From d7c303cacda4496eb07a31c6c272058c2a78b1f7 Mon Sep 17 00:00:00 2001 From: "rf.wu" Date: Mon, 14 Oct 2019 09:16:58 +0800 Subject: [PATCH] CMake: Add Android dynamic library cross compile support. --- CMakeLists.txt | 14 ++++++++++++++ README.md | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c23308..682c177 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,20 @@ option(YAML_CPP_BUILD_CONTRIB "Enable yaml-cpp contrib in library" ON) option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON) option(YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${BUILD_SHARED_LIBS}) +option(YAML_BUILD_ANDROID_LIBS "Build android dynamic library" OFF) + +if (YAML_BUILD_ANDROID_LIBS) + set(YAML_BUILD_SHARED_LIBS ON) + + if("${ANDROID_STL}" STREQUAL "c++_shared") + message(STATUS "Configuring STL libc++_shared.so for ${ANDROID_ABI}") + configure_file( + "${ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ABI}/libc++_shared.so" + "${CMAKE_BINARY_DIR}/libc++_shared.so" + COPYONLY) + endif() +endif() + cmake_dependent_option(YAML_CPP_BUILD_TESTS "Enable yaml-cpp tests" ON "BUILD_TESTING;CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF) diff --git a/README.md b/README.md index 92bc90e..175ae74 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,26 @@ cmake [-G generator] [-DYAML_BUILD_SHARED_LIBS=ON|OFF] .. 5. To clean up, just remove the `build` directory. +## Cross Compile Dynamic Library for Android + +* The 3rd step above can do like this: + + ```bash + cmake -DCMAKE_TOOLCHAIN_FILE=${NDKROOT}/build/cmake/android.toolchain.cmake -DANDROID_STL=c++_shared -DANDROID_PLATFORM=android-26 -DANDROID_ABI=x86_64 -DYAML_BUILD_ANDROID_LIBS=ON .. + ``` + + - To do cross-compiling, `ndk-bundle` OR `android-sdk` is needed. Also `NDKROOT` should be exported in your `bash` config file, just like: + + ```bash + export NDKROOT=/path/to/Android/sdk/ndk-bundle + ``` + + - The argument `ANDROID_ABI` can be set to `arm64-v8a`, `armeabi-v7a`, `armeabi`, `x86`, `x86_64`, etc. If NOT set, the default value is `armeabi-v7a`. + + - The argument `ANDROID_PLATFORM` can be set to `android-xx` OR `latest`, `xx` can be replaced to the android api number, the default value will be the minimum supported version of your `ndk-bundle`, while the value `latest` will using the maximum supported version. + + - The argument `YAML_BUILD_ANDROID_LIBS=ON` will default set `YAML_BUILD_SHARED_LIBS=ON`, and copy the `libc++_shared.so` to `build` directory from `ndk-bundle`, which is needed by `libyaml-cpp.so`. + # Recent Release # [yaml-cpp 0.6.0](https://github.com/jbeder/yaml-cpp/releases/tag/yaml-cpp-0.6.0) has been released! This release requires C++11, and no longer depends on Boost. @@ -50,6 +70,6 @@ cmake [-G generator] [-DYAML_BUILD_SHARED_LIBS=ON|OFF] .. **The old API will continue to be supported, and will still receive bugfixes!** The 0.3.x and 0.4.x versions will be old API releases, and 0.5.x and above will all be new API releases. -# API Documentation +# API Documentation The autogenerated API reference is hosted on [CodeDocs](https://codedocs.xyz/jbeder/yaml-cpp/index.html)