1

I have a CMake Toolchain file that cross compiles for arm-linux on an x86_64-linux host. During configuration, my toolchain file is parsed and it (correcty) finds a GNU compiler. Therefore it will load GNU.cmake (in my case /usr/share/cmake-3.10/Modules/Compiler/GNU.cmake). The including hierarchy here is unclear to me (could not find documentation. Any link is appreceated). However it seems like this:

  • root CMakeLists.txt to project()
  • my toolchain.cmake
  • stuff in modules dir (including GNU.cmake)
  • loads %_INIT variables to % Cache variables
  • continue after project()

Say i want this target to build only with -O2 optimization. I set it in my toolchain, because it is then global for many project.

set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O2")

but then GNU.cmake is loaded and appends it with "-O3". Therefore my setting is overwritten. After this i can only overwrite it in my projects CMakeLists.txt files. I would have to do this for each project, and developers will miss this!

So, my question is (repeating the headline): How to overwrite settings from CMake modules from toolchain file?

kuga
  • 1,483
  • 1
  • 17
  • 38

1 Answers1

2

For the reasons you pointed out you cannot do this. Maybe this could be considered a limitation of toolchain files but it is how it currently works.

Now digging into the modules, the module that loads GNU.cmake is CMakeCInformation.cmake and CMakeCXXInformation.cmake.

It may be possible to override these settings by using a different file that gets loaded after GNU.cmake by using the variable CMAKE_USER_MAKE_RULES_OVERRIDE_<lang>. I found this old email that talks about it: https://cmake.org/pipermail/cmake/2008-April/021337.html

It is also mentioned here: https://cmake.org/cmake/help/latest/variable/CMAKE_USER_MAKE_RULES_OVERRIDE.html?highlight=cmake_user_make_rules_override

So what you can then do is change the value by doing something like this from within this override file:

string(REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE_INIT ${CMAKE_CXX_FLAGS_RELEASE_INIT })

I've never heard of these variables before so I'm not entirely sure how they work regarding path locations when setting CMAKE_USER_MAKE_RULES_OVERRIDE_<lang>.

I suspect that you can set CMAKE_USER_MAKE_RULES_OVERRIDE_<lang> from within the toolchain file instead of the CMakeLists.txt file.

fdk1342
  • 3,274
  • 1
  • 16
  • 17
  • Today, I tried to use it. It did not work. Neither in toolchain, nor before project. I guess the optimization is set after the overrides are loaded. – kuga Aug 31 '21 at 17:08