Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass option to cmake for future option to crosscompilation (CROSS_COMPILE)

IF(UNIX)
    # CROSS COMPILATION! ON/OFF
    #SET(CMAKE_C_COMPILER   /home/username/projects/buildroot/output/host/usr/bin/arm-linux-gcc)
    #SET(CMAKE_CXX_COMPILER /home/username/projects/buildroot/output/host/usr/bin/arm-linux-g++)
    #SET(CMAKE_C_COMPILER   /home/username/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-eabi-gcc)
    #SET(CMAKE_CXX_COMPILER /home/username/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-eabi-g++)

here is what I do now for cross-compilation. I want to add option to run it alike that:

make CROSS_COMPILE=~/projects/buildroot/output/host/usr/bin/arm-linux-

and if I do not path CROSS_COMPILE to make (not to cmake) it must use system defaults so cmake must path this option to makefile. How can I make it?

like image 690
cnd Avatar asked Dec 21 '22 22:12

cnd


2 Answers

Buildroot generates a CMake toolchain file for you. Depending on your Buildroot, it might be directly in the output directory, or in output/host/usr/share/buildroot. The file is named toolchainfile.cmake. Then to build your CMake applications, do:

cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/buildroot/output/host/usr/share/buildroot/toolchainfile.cmake

This file contains all the definitions of the cross-compiler paths, pkg-config environment variables, headers and libraries location, etc.

like image 66
Thomas Petazzoni Avatar answered Dec 27 '22 10:12

Thomas Petazzoni


For the simplest method, do this:

SET(CMAKE_C_COMPILER   $(CROSS_COMPILE)gcc)
SET(CMAKE_CXX_COMPILER $(CROSS_COMPILE)g++)

When the CROSS_COMPILE variable is passed to make, it will be substituted with the cross compiler path.

Now, the proper way. Ideally, the CROSS_COMPILE variable should be defined when CMake is run as it is meant to be cross-platform. Using the first solution could break if other CMake generators are used.

This can be done as:

IF(UNIX)
    SET(CMAKE_C_COMPILER   ${CROSS_COMPILE}gcc)
    SET(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++)

Then define the variable:

cmake -G "Unix Makefiles" -DCROSS_COMPILE=~/projects/buildroot/output/host/usr/bin/arm-linux-

In this case, CMake will generate proper build files, based on whether CROSS_COMPILE is defined or not.

like image 25
panickal Avatar answered Dec 27 '22 12:12

panickal