Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMAKE_SYSTEM_PROCESSOR value in toolchain file not visible in top level CMakeLists.txt file

Tags:

cmake

I have cmake project which I've successfully set up and used for cross compiling with a toolchain file.

I have a separate variable I want to set in my top level cmake file based on the value of the CMAKE_SYSTEM_PROCESSOR variable that is set in the tool chain file. But when referenced in the top level cmake file, the value of CMAKE_SYSTEM_PROCESSOR does not have the value from the toolchain file.

I can reproduce the conditions with a simple cmake file and one line toolchain file.

If I have the following cmake file:

project(ToolchainTest C)
message("CMAKE_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR}")

and run

cmake .

I get

CMAKE_SYSTEM_PROCESSOR x86_64

as I expect.

With the following one line toolchain file "toolchain.cmake":

set(CMAKE_SYSTEM_PROCESSOR aarch64)

when I run

cmake . -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake

I get exactly the same output, x86_64, and not aarch64 as I expected.

I looked over the following related posts, but as I'm just printing the value and not doing a string match (in the example) I don't think they apply.

Can I use CMAKE_SYSTEM_PROCESSOR, defined in a toolchain file, in CMakeLists?

CMake compare to empty string with STREQUAL failed

I'm using cmake version 3.13.4 on Ubuntu 19.04.

like image 350
M. Krajnak Avatar asked Sep 11 '25 17:09

M. Krajnak


1 Answers

I had a similar issue. The answer is that when CMAKE_SYSTEM_NAME is not set, CMake overwrites CMAKE_SYSTEM_PROCESSOR (source code). This is arguably a bug but regardless is how CMake behaves.

The solution is to set both, even if you're compiling for the same system:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
like image 103
Cyanfish Avatar answered Sep 16 '25 10:09

Cyanfish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!