From 39f9056c775d4a46d2a5785266517e659d1f24ee Mon Sep 17 00:00:00 2001 From: Liu Yiqun Date: Fri, 28 Apr 2017 11:17:12 +0000 Subject: [PATCH] Config HOST_C/CXX_COMPILER for cross-compiling. --- cmake/cross_compiling/android.cmake | 10 +++--- cmake/cross_compiling/host.cmake | 49 +++++++++++++++++++++++++++++ cmake/external/openblas.cmake | 6 ++-- cmake/system.cmake | 1 + 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 cmake/cross_compiling/host.cmake diff --git a/cmake/cross_compiling/android.cmake b/cmake/cross_compiling/android.cmake index 4dd81bf29..3b13fca51 100644 --- a/cmake/cross_compiling/android.cmake +++ b/cmake/cross_compiling/android.cmake @@ -112,19 +112,19 @@ IF(${CMAKE_VERSION} VERSION_LESS "3.7.0") 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") + SET(CMAKE_C_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}gcc" CACHE PATH "C compiler") ELSE() - MESSAGE(FATAL_ERROR "Cannot find C compiler: ${ANDROID_TOOLCHAIN_PREFIX}gcc") + MESSAGE(FATAL_ERROR "Cannot found C compiler: ${ANDROID_TOOLCHAIN_PREFIX}gcc") ENDIF() IF(EXISTS "${ANDROID_TOOLCHAIN_PREFIX}g++") - SET(CMAKE_CXX_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}g++") + SET(CMAKE_CXX_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}g++" CACHE PATH "CXX compiler") ELSE() - MESSAGE(FATAL_ERROR "Cannot find CXX compiler: ${ANDROID_TOOLCHAIN_PREFIX}g++") + MESSAGE(FATAL_ERROR "Cannot found CXX compiler: ${ANDROID_TOOLCHAIN_PREFIX}g++") ENDIF() IF(EXISTS "${ANDROID_TOOLCHAIN_PREFIX}gfortran") - SET(CMAKE_Fortran_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}gfortran") + SET(CMAKE_Fortran_COMPILER "${ANDROID_TOOLCHAIN_PREFIX}gfortran" CACHE PATH "Fortran compiler") ENDIF() # Toolchain and ABI specific flags. diff --git a/cmake/cross_compiling/host.cmake b/cmake/cross_compiling/host.cmake new file mode 100644 index 000000000..62f3196b1 --- /dev/null +++ b/cmake/cross_compiling/host.cmake @@ -0,0 +1,49 @@ +# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# find host C compiler +IF(HOST_C_COMPILER) + SET(HOST_C_COMPILER_NAME ${HOST_C_COMPILER}) +ELSEIF(NOT $ENV{CC} STREQUAL "") + SET(HOST_C_COMPILER_NAME $ENV{CC}) +ELSE() + SET(HOST_C_COMPILER_NAME cc) +ENDIF() + +GET_FILENAME_COMPONENT(HOST_C_COMPILER_PATH ${HOST_C_COMPILER_NAME} PROGRAM) +IF(NOT HOST_C_COMPILER_PATH OR NOT EXISTS ${HOST_C_COMPILER_PATH}) + MESSAGE(FATAL_ERROR "Cannot found host C compiler, set host C compiler:\n" + "\tcmake .. -DHOST_C_COMPILER=...") +ENDIF() + +# find host CXX compiler +IF(HOST_CXX_COMPILER) + SET(HOST_CXX_COMPILER_NAME ${HOST_CXX_COMPILER}) +ELSEIF(NOT $ENV{CXX} STREQUAL "") + SET(HOST_CXX_COMPILER_NAME $ENV{CXX}) +ELSE() + SET(HOST_CXX_COMPILER_NAME c++) +ENDIF() + +GET_FILENAME_COMPONENT(HOST_CXX_COMPILER_PATH ${HOST_CXX_COMPILER_NAME} PROGRAM) +IF(NOT HOST_CXX_COMPILER_PATH OR NOT EXISTS ${HOST_CXX_COMPILER_PATH}) + MESSAGE(FATAL_ERROR "Cannot found host CXX compiler, set host CXX compiler:\n" + "\tcmake .. -DHOST_CXX_COMPILER=...") +ENDIF() + +SET(HOST_C_COMPILER ${HOST_C_COMPILER_PATH} CACHE PATH "Host C compiler") +SET(HOST_CXX_COMPILER ${HOST_CXX_COMPILER_PATH} CACHE PATH "Host CXX compiler") + +MESSAGE(STATUS "Found host C compiler: " ${HOST_C_COMPILER}) +MESSAGE(STATUS "Found host CXX compiler: " ${HOST_CXX_COMPILER}) diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake index c5d457d3e..ece0f0d44 100644 --- a/cmake/external/openblas.cmake +++ b/cmake/external/openblas.cmake @@ -57,11 +57,9 @@ IF(NOT ${CBLAS_FOUND}) SET(OPENBLAS_COMMIT "v0.2.19") SET(OPENBLAS_ARGS FC=${CMAKE_Fortran_COMPILER} DYNAMIC_ARCH=1 libs netlib) - SET(HOST_C_COMPILER ${CMAKE_C_COMPILER}) ELSE() SET(OPENBLAS_COMMIT "b5c96fcfcdc82945502a2303116a64d89985daf5") - SET(OPTIONAL_ARGS TARGET=ARMV7 ARM_SOFTFP_ABI=1 NOFORTRAN=1 USE_THREAD=0 libs) - SET(HOST_C_COMPILER gcc) + SET(OPTIONAL_ARGS HOSTCC=${HOST_C_COMPILER} TARGET=ARMV7 ARM_SOFTFP_ABI=1 NOFORTRAN=1 USE_THREAD=0 libs) ENDIF() ExternalProject_Add( @@ -72,7 +70,7 @@ IF(NOT ${CBLAS_FOUND}) PREFIX ${CBLAS_SOURCES_DIR} INSTALL_DIR ${CBLAS_INSTALL_DIR} BUILD_IN_SOURCE 1 - BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} CC=${CMAKE_C_COMPILER} HOSTCC=${HOST_C_COMPILER} NO_SHARED=1 ${OPTIONAL_ARGS} + BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} CC=${CMAKE_C_COMPILER} NO_SHARED=1 ${OPTIONAL_ARGS} INSTALL_COMMAND ${CMAKE_MAKE_PROGRAM} install NO_SHARED=1 PREFIX= UPDATE_COMMAND "" CONFIGURE_COMMAND "" diff --git a/cmake/system.cmake b/cmake/system.cmake index 22f74b2e3..6c381458d 100644 --- a/cmake/system.cmake +++ b/cmake/system.cmake @@ -74,6 +74,7 @@ MESSAGE(STATUS "Found Paddle host system's CPU: ${CPU_CORES} cores") # configuration for cross-compiling IF(DEFINED CMAKE_SYSTEM_NAME) + INCLUDE(cross_compiling/host) IF(${CMAKE_SYSTEM_NAME} STREQUAL "Android") SET(ANDROID TRUE) INCLUDE(cross_compiling/android) -- GitLab