4

I'd like to add Raspberry Pi as a cross compilation target to a C++ project which uses CMake. Following the accepted answer to this question, I've set up the environment successfully.

The project has many build targets already, all of them defined in the main CMakeLists.txt in a quite ugly way (it's an old project). In this file, there are some compiler flags set, depending on the CMAKE_SYSTEM_PROCESSOR variable, for example:

if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm")
    # do something
endif ()

According to the guide, I should set this in my Raspberry toolchain file. I've added SET(CMAKE_SYSTEM_PROCESSOR arm), but I can't access this from the CMakeLists, because this becomes an empty string there. I don't have this problem with the other variables, like CMAKE_SYSTEM_NAME. Using message(), I can see that it is still available in the toolchain file, but becomes empty after that.

Is there any way to use CMAKE_SYSTEM_PROCESSOR in CMakeLists.txt? Or, is it available only in the toolchain file intentionally?

I'm using CMake 2.8.12.2.

mmatyas
  • 923
  • 1
  • 7
  • 9
  • Did you actually load the toolchain file? I suggest you generate your project from a clean build directory using cmake-gui: it will prompt you asking if you want to use a toolchain the first time you configure. – Antonio May 01 '15 at 20:09
  • A clean build fixed this issue, thanks! The toolchain file was loaded correctly, but looks like CMake cached the variable before. – mmatyas May 01 '15 at 22:09

2 Answers2

3

Looks like you have fallen victim to CMake's double expansion. Try using this instead:

if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
    # do something
endif ()
Community
  • 1
  • 1
sakra
  • 62,199
  • 16
  • 168
  • 151
2

It seems this was a caching issue after all - building in a clean directory fixed the problem. Though I still wonder how did it become an empty string...

mmatyas
  • 923
  • 1
  • 7
  • 9
  • May be when you didn't specify the toolchain file when you did cmake top_dir/CMakeLists.txt for the first time. – leodotcloud May 02 '15 at 00:06