yse / easy_profiler

Lightweight profiler library for c++
MIT License
2.15k stars 184 forks source link

Fix Android build #137

Closed tals closed 5 years ago

tals commented 5 years ago

Android does not have libpthread, but instead exposes the pthread functions via Bionic See: https://android.googlesource.com/platform/bionic/+/10ce969/libc/bionic/pthread.c

This enables Android builds out of the box :)

yse commented 5 years ago

Thanks for contribution =) By the way can you advice CI for building on android?

tals commented 5 years ago

@yse Yeah! But there are a few steps :)

The NDK has a feature that extract a stand-alone toolchain, which makes is easy to integrate with something like CMake as a custom toolchain. See https://developer.android.com/ndk/guides/standalone_toolchain

Setup instructions

Step 1: install NDK

Step 2: Extract the toolchain

$ python $ANDROID_NDK_HOME/build/tools/make_standalone_toolchain.py \
 --arch arm64 \
 --api 21 \
 --stl libc++ \
 --install-dir some/directory/android-21-arm64

Step 3: Drop android.cmake somewhere (lets say cmake/android.cmake) that looks something like:

set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_NAME  Android)
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
set(ANDROID_NATIVE_API_LEVEL 21)

# Lock down sysroot
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# Output folders
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# PIE is delicious
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")

# Configure toolchain
get_filename_component(TOOLCHAIN /some/directory ABSOLUTE)
set(CMAKE_C_COMPILER    ${TOOLCHAIN}/bin/clang CACHE PATH "c")
set(CMAKE_CXX_COMPILER  ${TOOLCHAIN}/bin/clang++ CACHE PATH "cxx")
set(CMAKE_LINKER        ${TOOLCHAIN}/bin/llvm-ld CACHE PATH "linker")
set(CMAKE_OBJCOPY       ${TOOLCHAIN}/bin/aarch64-linux-android-objcopy CACHE PATH "objcopy")
set(CMAKE_OBJDUMP       ${TOOLCHAIN}/bin/aarch64-linux-android-objdump CACHE PATH "objdump")
set(CMAKE_NM            ${TOOLCHAIN}/bin/llvm-nm CACHE PATH "nm")
set(CMAKE_RANLIB        ${TOOLCHAIN}/bin/aarch64-linux-android-ranlib CACHE PATH "ranlib")
set(CMAKE_AR            ${TOOLCHAIN}/bin/llvm-ar CACHE PATH "archive")
set(CMAKE_STRIP         ${TOOLCHAIN}/bin/aarch64-linux-android-strip CACHE PATH "strip")
set(CMAKE_SYSROOT       ${TOOLCHAIN}/sysroot CACHE PATH "sysroot")
set(LIB_CXX             ${TOOLCHAIN}/aarch64-linux-android/lib/libc++_shared.so)
set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -fcolor-diagnostics -fdiagnostics-absolute-paths")

and when you build easy_profiler, run:

$ mkdir build_android && cd build_android && cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/android.cmake ..

building should just work now :)

Hope this helps!

ndesai commented 5 years ago

@tals you can also use the android.toolchain.cmake that exists in the NDK folder

tals commented 5 years ago

@ndesai was not aware of that! Thanks!