Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android build #137

Merged
merged 1 commit into from
Nov 6, 2018
Merged

Fix Android build #137

merged 1 commit into from
Nov 6, 2018

Conversation

tals
Copy link
Contributor

@tals tals commented Nov 6, 2018

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 :)

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
@yse yse merged commit caac93c into yse:develop Nov 6, 2018
@yse
Copy link
Owner

yse commented Nov 6, 2018

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

@tals
Copy link
Contributor Author

tals commented Nov 6, 2018

@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
Copy link

ndesai commented Jan 15, 2019

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

@tals
Copy link
Contributor Author

tals commented Jan 17, 2019

@ndesai was not aware of that! Thanks!

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.

3 participants