Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect that I am cross-compiling in CMakeLists.txt?

Tags:

cmake

The CMake documentation suggests that CMAKE_CROSSCOMPILING is set when cross-compiling. In my CMakeLists.txt I have the lines:

IF(CMAKE_CROSSCOMPILING)
    message(STATUS "Cross-compiling so skipping unit tests.")
    option(GAME_PORTAL_UNIT_TEST "Enable unit testing of Game Portal code" OFF)
ELSE()
    message(STATUS "Enabling unit testing of Game Portal code")
    option(GAME_PORTAL_UNIT_TEST "Enable unit testing of Game Portal code" ON)
ENDIF()

The output from running:

cmake -DCMAKE_TOOLCHAIN_FILE=../crosscompile/raspberry_pi/CMakeCross.txt .

Includes the text "Enabling unit testing of Game Portal code", so clearly this variable is not being set, or not so it evaluates to true anyway.

I tried modifying CMakeCross.txt to include:

set(CMAKE_CROSSCOMPILING ON CACHE BOOL "Cross-compiling" FORCE)

and after cleaning the old CMakeCache.txt and rerunning my cmake command I can see that the new CMakeCache.txt now includes this variable, but I still get the same result as previously with regards to the unit tests being enabled.

How can I reliably detect that I am cross-compiling so I can properly disable the unit tests?

As requested, the full cross-compile file is:

# 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=/home/crosscompile/dev/raspberry_pi/CMakeCross.txt .. 

# Set target system name.
SET (CMAKE_SYSTEM_NAME Linux)

# Set compiler name.
SET (CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
SET (CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)

# Set path(s) to search for libraries/binaries/headers.
SET (CMAKE_FIND_ROOT_PATH /home/crosscompile/dev/raspberry_pi/rootfs/)

# Ensure only cross-compiler directories 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)

# Set output/install directory to safe place.
SET (CMAKE_INSTALL_PREFIX /home/crosscompile/dev/raspberry_pi/install/)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rpath-link=/lib/arm-linux-gnueabihf")
set(THREADS_PTHREAD_ARG 0)
set(CMAKE_CROSSCOMPILING ON CACHE BOOL "Cross-compiling" FORCE)
like image 584
AlastairG Avatar asked Nov 10 '16 12:11

AlastairG


People also ask

What is cmakelists TXT?

CMakeLists.txt. . CMakeLists.txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists.txt file automatically and places it in the project root directory.

Is CMake_crosscompiling set when cross-compiling?

The CMake documentation suggests that CMAKE_CROSSCOMPILING is set when cross-compiling. In my CMakeLists.txt I have the lines:

How do I open a project from a cmakelists file?

When you create a new project, CLion generates CMakeLists.txt file automatically and places it in the project root directory. To open a project, you can point CLion to the top-level CMakeLists.txt and choose Open as Project. Example below shows the CMakeLists.txt file of a simple "Hello, World!" project: Copied!

How do I know which compiler is installed with CMake?

If you run CMake in the console (or in IDE which shows CMake output) you can usually see, that in the beginning there are some log messages reporting what is the detected compiler. Below you can find sample output from one of my projects:


2 Answers

The test for CMAKE_CROSSCOMPILING must come after the "project" instruction in CMakeLists.txt.

like image 124
AlastairG Avatar answered Nov 15 '22 08:11

AlastairG


With in-source builds, one need to manually cleanup build files when change configuration parameters a lot.

E.g., if you did native build before, and then decide to cross-compile, you need to perform manual cleanup: CMake cannot automatically adjust build directory from one build type to another.

This is one of the reasons why in-source builds are not recommended and should be replaced with out-of-source builds.

like image 44
Tsyvarev Avatar answered Nov 15 '22 07:11

Tsyvarev