#set minimum cmake version required for cross-compiling to work
cmake_minimum_required(VERSION 2.6)
#Build with rm CMakeCache.txt; cmake -DCMAKE_TOOLCHAIN_FILE=./CMakeCross.txt . 

# set target system name
SET (CMAKE_SYSTEM_NAME Linux)
SET (CMAKE_SYSTEM_PROCESSOR $ENV{DEB_HOST_ARCH})

#set compiler name
SET (CMAKE_C_COMPILER $ENV{DEB_HOST_GNU_TYPE}-gcc)
SET (CMAKE_CXX_COMPILER $ENV{DEB_HOST_GNU_TYPE}-g++)

#set path(s) to search for libraries/binaries/headers
SET (CMAKE_FIND_ROOT_PATH /usr/$ENV{DEB_HOST_GNU_TYPE})
# ensure only cross-dirs are searched
SET (ONLY_CMAKE_FIND_ROOT_PATH TRUE)
# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

MESSAGE ("Target system:${CMAKE_SYSTEM_NAME}, Host system:${CMAKE_HOST_SYSTEM} ")
IF (CMAKE_CROSSCOMPILING)
  MESSAGE("CROSS COMPILING for ${CMAKE_C_COMPILER}")
  INCLUDE_DIRECTORIES(BEFORE ${CMAKE_FIND_ROOT_PATH}/include)
  #Make pkg-config look in the right place
  FIND_PROGRAM(DPKG_CROSS_PKG_CONFIG $ENV{DEB_HOST_GNU_TYPE}-pkg-config)
  IF (DPKG_CROSS_PKG_CONFIG)
    # for UsePkgConfig
    SET(PKGCONFIG_EXECUTABLE ${DPKG_CROSS_PKG_CONFIG})
    # for FindPkgConfig
    SET(PKG_CONFIG_EXECUTABLE ${DPKG_CROSS_PKG_CONFIG})
  ELSE ()
    SET(ENV{PKG_CONFIG_LIBDIR} ${CMAKE_FIND_ROOT_PATH}/lib/pkgconfig/)
  ENDIF ()
ELSE (CMAKE_CROSSCOMPILING)
  MESSAGE("Native Compile")
ENDIF (CMAKE_CROSSCOMPILING)
