From af80ff94a5b56a2fc8aa32cf8b50d1e8a013a84c Mon Sep 17 00:00:00 2001 From: Liu Yiqun Date: Tue, 2 May 2017 07:19:45 +0000 Subject: [PATCH] Allow the case user sets CMAKE_C_COMPILER directly instead of standalone toolchain. --- cmake/cross_compiling/android.cmake | 93 ++++++++++++++++++----------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/cmake/cross_compiling/android.cmake b/cmake/cross_compiling/android.cmake index 3b13fca51..0be136163 100644 --- a/cmake/cross_compiling/android.cmake +++ b/cmake/cross_compiling/android.cmake @@ -41,7 +41,8 @@ IF(NOT DEFINED ANDROID_STANDALONE_TOOLCHAIN) CACHE PATH "Folder holds the standalone toolchain of Android NDK") ENDIF() IF(NOT ANDROID_STANDALONE_TOOLCHAIN) - MESSAGE(FATAL_ERROR "Set ANDROID_STANDALONE_TOOLCHAIN to use a standalone toolchain.\n" + MESSAGE(WARNING "It is recommened to set ANDROID_STANDALONE_TOOLCHAIN to " + "use a standalone toolchain.\n" "To cross-compile for Android, you need to:\n" "1. Download an Android NDK from" " https://developer.android.com/ndk/downloads/index.html\n" @@ -65,11 +66,6 @@ ENDIF() IF(NOT DEFINED ANDROID_ARM_MODE) SET(ANDROID_ARM_MODE ON) ENDIF() - -IF(NOT DEFINED ANDROID_ARM_NEON) - SET(ANDROID_ARM_NEON ON) -ENDIF() - IF(NOT ANDROID_ARM_MODE OR ANDROID_ARM_MODE STREQUAL "thumb") SET(ANDROID_ARM_MODE OFF) SET(ANDROID_ARM_MODE_NAME "thumb") @@ -78,6 +74,10 @@ ELSEIF(ANDROID_ARM_MODE OR ANDROID_ARM_MODE STREQUAL "arm") SET(ANDROID_ARM_MODE_NAME "arm") ENDIF() +IF(NOT DEFINED ANDROID_ARM_NEON) + SET(ANDROID_ARM_NEON ON) +ENDIF() + IF(${CMAKE_VERSION} VERSION_LESS "3.7.0") IF(${CMAKE_VERSION} VERSION_LESS "3.1.0") SET(CMAKE_SYSTEM_NAME "Linux") @@ -85,47 +85,70 @@ IF(${CMAKE_VERSION} VERSION_LESS "3.7.0") MESSAGE(WARNING "It is recommended to use CMake >= 3.7.0 (current version: " "${CMAKE_VERSION}), when cross-compiling for Android.") - SET(CMAKE_SYSROOT "${ANDROID_STANDALONE_TOOLCHAIN}/sysroot") - - IF(NOT CMAKE_SYSTEM_VERSION) - SET(ANDROID_STANDALONE_TOOLCHAIN_API "") - SET(ANDROID_API_LEVEL_H_REGEX "^[\t ]*#[\t ]*define[\t ]+__ANDROID_API__[\t ]+([0-9]+)") - FILE(STRINGS "${ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h" - ANDROID_API_LEVEL_H_CONTENT REGEX "${ANDROID_API_LEVEL_H_REGEX}") - IF(ANDROID_API_LEVEL_H_CONTENT MATCHES "${ANDROID_API_LEVEL_H_REGEX}") - SET(ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}") + IF(ANDROID_STANDALONE_TOOLCHAIN) + SET(CMAKE_SYSROOT "${ANDROID_STANDALONE_TOOLCHAIN}/sysroot") + + IF(NOT CMAKE_SYSTEM_VERSION) + SET(ANDROID_STANDALONE_TOOLCHAIN_API "") + SET(ANDROID_API_LEVEL_H_REGEX "^[\t ]*#[\t ]*define[\t ]+__ANDROID_API__[\t ]+([0-9]+)") + FILE(STRINGS "${ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h" + ANDROID_API_LEVEL_H_CONTENT REGEX "${ANDROID_API_LEVEL_H_REGEX}") + IF(ANDROID_API_LEVEL_H_CONTENT MATCHES "${ANDROID_API_LEVEL_H_REGEX}") + SET(ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}") + ENDIF() + SET(CMAKE_SYSTEM_VERSION ${ANDROID_STANDALONE_TOOLCHAIN_API}) ENDIF() - SET(CMAKE_SYSTEM_VERSION ${ANDROID_STANDALONE_TOOLCHAIN_API}) - ENDIF() - # Toolchain - SET(ANDROID_TOOLCHAIN "gcc") - SET(ANDROID_TOOLCHAIN_ROOT ${ANDROID_STANDALONE_TOOLCHAIN}) - IF(ANDROID_ABI MATCHES "^armeabi(-v7a)?$") - SET(ANDROID_TOOLCHAIN_NAME arm-linux-androideabi) - IF(ANDROID_ABI STREQUAL "armeabi") - SET(CMAKE_SYSTEM_PROCESSOR armv5te) - ELSEIF(ANDROID_ABI STREQUAL "armeabi-v7a") - SET(CMAKE_SYSTEM_PROCESSOR armv7-a) + # Toolchain + SET(ANDROID_TOOLCHAIN "gcc") + SET(ANDROID_TOOLCHAIN_ROOT ${ANDROID_STANDALONE_TOOLCHAIN}) + IF(ANDROID_ABI MATCHES "^armeabi(-v7a)?$") + SET(ANDROID_TOOLCHAIN_NAME arm-linux-androideabi) + IF(ANDROID_ABI STREQUAL "armeabi") + SET(CMAKE_SYSTEM_PROCESSOR armv5te) + ELSEIF(ANDROID_ABI STREQUAL "armeabi-v7a") + SET(CMAKE_SYSTEM_PROCESSOR armv7-a) + ENDIF() ENDIF() + SET(ANDROID_TOOLCHAIN_PREFIX "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_NAME}-") ENDIF() - SET(ANDROID_TOOLCHAIN_PREFIX "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_NAME}-") - IF(EXISTS "${ANDROID_TOOLCHAIN_PREFIX}gcc") - SET(CMAKE_C_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}gcc" CACHE PATH "C compiler") + # C compiler + IF(NOT CMAKE_C_COMPILER) + SET(ANDROID_C_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}gcc") ELSE() - MESSAGE(FATAL_ERROR "Cannot found C compiler: ${ANDROID_TOOLCHAIN_PREFIX}gcc") + GET_FILENAME_COMPONENT(ANDROID_C_COMPILER_PATH ${CMAKE_C_COMPILER} PROGRAM) + SET(ANDROID_C_COMPILER ${ANDROID_C_COMPILER_PATH}) + ENDIF() + IF(NOT EXISTS ${ANDROID_C_COMPILER}) + MESSAGE(FATAL_ERROR "Cannot found C compiler: ${ANDROID_C_COMPILER}") ENDIF() - IF(EXISTS "${ANDROID_TOOLCHAIN_PREFIX}g++") - SET(CMAKE_CXX_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}g++" CACHE PATH "CXX compiler") + # CXX compiler + IF(NOT CMAKE_CXX_COMPILER) + SET(ANDROID_CXX_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}g++") ELSE() - MESSAGE(FATAL_ERROR "Cannot found CXX compiler: ${ANDROID_TOOLCHAIN_PREFIX}g++") + GET_FILENAME_COMPONENT(ANDROID_CXX_COMPILER_PATH ${CMAKE_CXX_COMPILER} PROGRAM) + SET(ANDROID_CXX_COMPILER ${ANDROID_CXX_COMPILER_PATH}) + ENDIF() + IF(NOT EXISTS ${ANDROID_CXX_COMPILER}) + MESSAGE(FATAL_ERROR "Cannot found CXX compiler: ${ANDROID_CXX_COMPILER}") ENDIF() - IF(EXISTS "${ANDROID_TOOLCHAIN_PREFIX}gfortran") - SET(CMAKE_Fortran_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}gfortran" CACHE PATH "Fortran compiler") + # Fortran compiler + IF(NOT CMAKE_Fortran_COMPILER) + SET(ANDROID_Fortran_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}gfortran") + ELSE() + GET_FILENAME_COMPONENT(ANDROID_Fortran_COMPILER_PATH ${CMAKE_Fortran_COMPILER}) + SET(ANDROID_Fortran_COMPILER ${ANDROID_Fortran_COMPILER_PATH}) ENDIF() + IF(NOT EXISTS ${ANDROID_Fortran_COMPILER}) + SET(ANDROID_Fortran_COMPILER "") + ENDIF() + + SET(CMAKE_C_COMPILER ${ANDROID_C_COMPILER} CACHE PATH "C compiler" FORCE) + SET(CMAKE_CXX_COMPILER ${ANDROID_CXX_COMPILER} CACHE PATH "CXX compiler" FORCE) + SET(CMAKE_Fortran_COMPILER ${ANDROID_Fortran_COMPILER} CACHE PATH "Fortran compiler" FORCE) # Toolchain and ABI specific flags. SET(ANDROID_COMPILER_FLAGS "-ffunction-sections -fdata-sections -finline-limit=64") -- GitLab