cmake for arm and x86

There is a project that needs to be built for two platforms x86 and arm7.

For each platform separately, CMakeLists looks something like this:

cmake_minimum_required(VERSION 2.8)
project(asio_test_x86)

set(CMAKE_CXX_COMPILER "/usr/bin/g++" CACHE INTERNAL "")
set(boost_libdir_x86 "/path2boost/boost86/lib")
include_directories("/path2boost/boost86/include")

find_library(x86_LBSys boost_system PATH ${boost_libdir_x86})
find_library(x86_LBThread boost_thread PATH ${boost_libdir_x86})

set (LIBRARYS_x86)
list (APPEND LIBRARYS_x86 ${x86_LBSys} ${x86_LBThread})

add_executable(asio_test_x86 ${SOURCES})
target_link_libraries(asio_test_x86 ${LIBRARYS_x86})

And for arm

cmake_minimum_required(VERSION 2.8)
project(asio_test_arm)

set(CMAKE_CXX_COMPILER "/usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-g++" CACHE INTERNAL "")
set(boost_libdir_arm "/path2boost/boostarm/lib")
include_directories("/path2boost/boostarm/include")

find_library(arm_LBSys boost_system PATH ${boost_libdir_arm})
find_library(arm_LBThread boost_thread PATH ${boost_libdir_arm})

set (LIBRARYS_arm)
list (APPEND LIBRARYS_arm ${arm_LBSys} ${arm_LBThread})

add_executable(asio_test_arm ${SOURCES})
target_link_libraries(asio_test_x86 ${LIBRARYS_arm})

The sources are the same, the only difference is in the compilers and Boost versions. Is it possible to somehow write a common cmake file to build with one command?

Author: Denis, 2015-12-17

1 answers

Use the CMake Toolchain File:

In the toolchain, you can define the necessary variables and use them for branching in the main CMakeLists.txt. Or declare paths, just using common variables, and basically CMakeLists.txt just refer to them.

Or, as an option, the compiler can be overridden via an environment variableCC, CXX when calling cmake, and the rest of the options are set via -DПАРАМЕТР=значение and use conditions inside CMakeLists.txt, for example:

env CXX=/usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-g++ \
    cmake -DBOOST_PREFIX=/path2boost/boostarm

And inside:

set(boost_libdir "${BOOST_PREFIX}/lib")
include_directories("${BOOST_PREFIX}/include")

But it's better to go through the tulchain. Although, I am more impressed with the option of using some kind of default (with the ability to override via -DPARAM=VAL) for the host machine and using toolchains for various cross-build options.

 2
Author: Monah Tuk, 2015-12-17 14:31:00