CMake Cross Compile

From www.chip-community.org
Jump to: navigation, search

Goals

Prepare your environment

Pre-Requisites

  • A linux distribution.
  • GCC/G++ - Version < 6.x (else BUILDROOT will not compile).

Setup BUILDROOT

  git clone https://github.com/NextThingCo/CHIP-buildroot
  cd ~/CHIP-buildroot
  make chip_defconfig
  • Customize BUILDROOT (be sure to include the TOOLCHAIN):
  make nconfig
  • Build BUILDROOT by:
  make

Setup CMake

  • Create a file named chip_toolchain.cmake with the following content:
  # C.H.I.P. / ARM

  set(BUILDROOT_DIR ${_BUILDROOT_DIR}/CHIP-buildroot)

  set(BUILD_DIR     ${BUILDROOT_DIR}/output)
  set(STAGING_DIR   ${BUILD_DIR}/staging)
  set(HOST_DIR      ${BUILD_DIR}/host) 
  set(TARGET_DIR    ${BUILD_DIR}/target)

  # Symbol define for conditional C/C++ pre-processing

  add_definitions(-D_PLATFORM_CHIP)

  # CMake toolchain

  set(CMAKE_SYSTEM_NAME CHIP)
  set(CMAKE_SYSTEM_VERSION 1)

  set(TOOLCHAIN_DIR      ${HOST_DIR}/opt/ext-toolchain/bin)

  set(CMAKE_C_COMPILER   ${TOOLCHAIN_DIR}/arm-linux-gnueabihf-gcc)
  set(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/arm-linux-gnueabihf-g++)

  set(
    CMAKE_FIND_ROOT_PATH 
    ${TARGET_DIR}
    ${TARGET_DIR}/usr/lib
  )

  include_directories(
    ${HOST_DIR}/usr/include
    ${TARGET_DIR}/usr/include
    ${HOST_DIR}/usr/arm-buildroot-linux-gnueabihf/sysroot/usr/include
  )

  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  • In your project CMakeLists.txt file set the variable _BUILDROOT_DIR to the path of your CHIP-buildroot folder.

Cross compile your project

Before run CMake, add to its command line the parameter:

   -DCMAKE_TOOLCHAIN_FILE=chip_toolchain.cmake

That's all, I hope! :)