set(CMAKE_SYSTEM_NAME "Linux")

set(CROSS arm-funkey-linux-musleabihf-)

set(ARM_CFLAGS "${ARM_CFLAGS} -march=armv7-a+neon-vfpv4 -mtune=cortex-a7 -mfpu=neon-vfpv4 -Ofast -fno-PIC -fdata-sections -ffunction-sections -fsingle-precision-constant -fno-common -fno-builtin -DFUNKEY")
set(ARM_CXXFLAGS "${ARM_CXXFLAGS} -fno-rtti")

set(TOOL_OS_SUFFIX "")
set(TOOLCHAIN_PREFIX "/opt/FunKey-sdk-2.0.0")
set(TOOLCHAIN_BINARY_DIR "${TOOLCHAIN_PREFIX}/bin/")

execute_process(COMMAND "${TOOLCHAIN_BINARY_DIR}${CROSS}gcc${TOOL_OS_SUFFIX}" -v RESULT_VARIABLE C_CHECK_RESULT OUTPUT_QUIET ERROR_QUIET)

set(CMAKE_SYSTEM_PROCESSOR "arm")
set(CMAKE_C_COMPILER   "${TOOLCHAIN_BINARY_DIR}${CROSS}gcc${TOOL_OS_SUFFIX}"     CACHE PATH "C compiler")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BINARY_DIR}${CROSS}g++${TOOL_OS_SUFFIX}"     CACHE PATH "C++ compiler")
set(CMAKE_ASM_COMPILER "${TOOLCHAIN_BINARY_DIR}${CROSS}gcc${TOOL_OS_SUFFIX}"     CACHE PATH "assembler")
set(CMAKE_STRIP        "${TOOLCHAIN_BINARY_DIR}${CROSS}strip${TOOL_OS_SUFFIX}"   CACHE PATH "strip")
set(CMAKE_AR           "${TOOLCHAIN_BINARY_DIR}${CROSS}ar${TOOL_OS_SUFFIX}"      CACHE PATH "archive")
set(CMAKE_LINKER       "${TOOLCHAIN_BINARY_DIR}${CROSS}ld${TOOL_OS_SUFFIX}"      CACHE PATH "linker")
set(CMAKE_NM           "${TOOLCHAIN_BINARY_DIR}${CROSS}nm${TOOL_OS_SUFFIX}"      CACHE PATH "nm")
set(CMAKE_OBJCOPY      "${TOOLCHAIN_BINARY_DIR}${CROSS}objcopy${TOOL_OS_SUFFIX}" CACHE PATH "objcopy")
set(CMAKE_OBJDUMP      "${TOOLCHAIN_BINARY_DIR}${CROSS}objdump${TOOL_OS_SUFFIX}" CACHE PATH "objdump")
set(CMAKE_RANLIB       "${TOOLCHAIN_BINARY_DIR}${CROSS}ranlib${TOOL_OS_SUFFIX}"  CACHE PATH "ranlib")

set(CMAKE_C_FLAGS          "${CMAKE_C_FLAGS} ${ARM_CFLAGS}"                     CACHE STRING "C flags")
set(CMAKE_CXX_FLAGS        "${CMAKE_CXX_FLAGS} ${ARM_CFLAGS} ${ARM_CXXFLAGS}"  CACHE STRING "C++ flags")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-no-undefined" CACHE STRING "Executable linker flags")

# No runtime cpu detect for mipsel-linux-gcc.
set(CONFIG_RUNTIME_CPU_DETECT 0 CACHE BOOL "")

execute_process(COMMAND ${CMAKE_C_COMPILER} -print-sysroot OUTPUT_VARIABLE ARM_SYSROOT_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CMAKE_FIND_ROOT_PATH "${ARM_SYSROOT_PATH}")

if(NOT CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
endif()
if(NOT CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endif()
