INCLUDE (CMakeForceCompiler)

#This file defines needed options for native GCC compiler.

# Read environment variables
SET (TOOLCHAIN_DIR $ENV{YOCTO_DIR})
SET (TOOLCHAIN_SYSROOT $ENV{TOOLCHAIN_SYSROOT})
SET (TOOLCHAIN_ROOT $ENV{TOOLCHAIN_ROOT})
SET (TARGET_TRIPLET $ENV{TARGET})

IF (NOT TOOLCHAIN_DIR)
    MESSAGE (FATAL_ERROR "***Please set YOCTO_DIR in environment variables***. Yocto SDK or Yocto build dir.")
ENDIF ()

IF (NOT TOOLCHAIN_SYSROOT)
    MESSAGE (STATUS "***'TOOLCHAIN_SYSROOT' in environment variable not set. Setting default to 'tmp/sysroots'***")
    SET (TOOLCHAIN_SYSROOT "tmp/sysroots")
ENDIF ()

IF (NOT TOOLCHAIN_ROOT)
  MESSAGE (STATUS "***'TOOLCHAIN_ROOT' environment variable not set. Setting default target 'x86_64-linux'***")
  SET (TOOLCHAIN_ROOT "x86_64-linux")
ENDIF ()

IF (NOT TARGET_TRIPLET)
  MESSAGE (STATUS "***'TARGET' environment variable not set. Setting default target 'raspberrypi3'***")
  SET (TARGET_TRIPLET "raspberrypi3")
ENDIF ()

# Set for cross compile
SET (HOST_TRIPLET "arm-poky-linux-gnueabi")
SET (CMAKE_SYSTEM_NAME Generic)
SET (CMAKE_SYSTEM_PROCESSOR arm)
SET (TOOLCHAIN_BIN_DIR ${TOOLCHAIN_DIR}/${TOOLCHAIN_SYSROOT}/${TOOLCHAIN_ROOT}/usr/bin)
SET (CMAKE_SYSROOT ${TOOLCHAIN_DIR}/${TOOLCHAIN_SYSROOT}/${TARGET_TRIPLET})

CMAKE_FORCE_C_COMPILER (${TOOLCHAIN_BIN_DIR}/${HOST_TRIPLET}/${HOST_TRIPLET}-gcc GNU)
CMAKE_FORCE_CXX_COMPILER (${TOOLCHAIN_BIN_DIR}/${HOST_TRIPLET}/${HOST_TRIPLET}-g++ GNU)
SET (CMAKE_ASM_COMPILER ${TOOLCHAIN_BIN_DIR}/${HOST_TRIPLET}/${HOST_TRIPLET}-gcc)

SET (CMAKE_OBJCOPY ${TOOLCHAIN_BIN_DIR}/${HOST_TRIPLET}/${HOST_TRIPLET}-objcopy CACHE INTERNAL "objcopy tool")
SET (CMAKE_OBJDUMP ${TOOLCHAIN_BIN_DIR}/${HOST_TRIPLET}/${HOST_TRIPLET}-objdump CACHE INTERNAL "objdump tool")

MACRO (SET_COMPILER_ALL_FLAG value)
  SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
  SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
ENDMACRO (SET_COMPILER_ALL_FLAG)

# Compiler flags
IF (NOT CMAKE_C_FLAGS)
  MESSAGE (STATUS "Flags not defined. Defining them.")

  # All
  SET_COMPILER_ALL_FLAG("-Wall")
  SET_COMPILER_ALL_FLAG("-mfloat-abi=hard")
  SET_COMPILER_ALL_FLAG("-fno-common")
  SET_COMPILER_ALL_FLAG("-ffunction-sections")
  SET_COMPILER_ALL_FLAG("-fdata-sections")
  SET_COMPILER_ALL_FLAG("-ffreestanding")
  SET_COMPILER_ALL_FLAG("-fno-builtin")

  # ASM
  SET (CMAKE_ASM_FLAGS "-mthumb -mapcs")

  # C
  SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -MMD -MP")

  # C Debug
  SET (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")

  # Set C optimizations to debug level, inlined functions disappers causing linker failure
  SET (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Og")

  # CXX Debug
  SET (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG -Og")

  # Set CXX optimizations to debug level, inlined functions disappers causing linker failure
  SET (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og")

  # Linker
  SET (CMAKE_EXE_LINKER_FLAGS "-Xlinker --gc-sections")

ELSE ()
  MESSAGE (STATUS "Flags defined.")
ENDIF ()

SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

MESSAGE (STATUS "TOOLCHAIN_DIR     : " ${TOOLCHAIN_DIR})
MESSAGE (STATUS "CMAKE FIND ROOT   : " ${CMAKE_FIND_ROOT_PATH})
MESSAGE (STATUS "CMAKE SYSROOT     : " ${CMAKE_SYSROOT})
MESSAGE (STATUS "CROSS COMPILING   : " ${CMAKE_CROSSCOMPILING})

MESSAGE (STATUS "C FLAGS           : " ${CMAKE_C_FLAGS})
MESSAGE (STATUS "C DEBUG FLAGS     : " ${CMAKE_C_FLAGS_DEBUG})
MESSAGE (STATUS "C RELEASE FLAGS   : " ${CMAKE_C_FLAGS_RELEASE})
MESSAGE (STATUS "CXX FLAGS         : " ${CMAKE_CXX_FLAGS})
MESSAGE (STATUS "CXX DEBUG FLAGS   : " ${CMAKE_CXX_FLAGS_DEBUG})
MESSAGE (STATUS "CXX RELEASE FLAGS : " ${CMAKE_CXX_FLAGS_RELEASE})
MESSAGE (STATUS "ASM FLAGS         : " ${CMAKE_ASM_FLAGS})
MESSAGE (STATUS "ASM DEBUG FLAGS   : " ${CMAKE_ASM_FLAGS_DEBUG})
MESSAGE (STATUS "ASM RELEASE FLAGS : " ${CMAKE_ASM_FLAGS_RELEASE})
MESSAGE (STATUS "LNK FLAGS         : " ${CMAKE_EXE_LINKER_FLAGS})
MESSAGE (STATUS "LNK DEBUG FLAGS   : " ${CMAKE_EXE_LINKER_FLAGS_DEBUG})
MESSAGE (STATUS "LNK RELEASE FLAGS : " ${CMAKE_EXE_LINKER_FLAGS_RELEASE})

MESSAGE (STATUS "BUILD_TYPE        : " ${CMAKE_BUILD_TYPE})
