This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

CMAKE_BUILD_TYPE and NCS

In your "Configuring build types" guide you specify "ZDebug" and "ZRelease" build types, but do not explain why these are used instead of the standard CMake "Debug" and "Release".

In this response to a question an engineer wrote:

The CMAKE_BUILD_TYPE is actually a special variable in cmake. Zephyr does not handle it, so if you use one of the values from the documentation cmake will take over and add some changes to variables used during the building process (for example cflags). You don't want this to happen as it may contradict definitions from kconfig.

I didn't want to introduce yet another variable so I reused cmake_build_type with "Z" prefix to build chose configuration. Since these are unknown to cmake it won't add anything specific by itself.

(At the moment I tend to think that it might have been better to introduce the new variable to have more flexibility and avoid confusion.)

I understand the reasoning behind this. If you use "Debug", Zephyr's CMake script emits a warning:

The CMake build type was set to 'Debug', but the optimization flag was set to '-Og'.
        This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'

However, it is desirable to use the standard CMake build types – if you are adding custom CMake libraries to your target, these may do things in their CMakeLists.txt that expect the Debug and Release configurations to be used. For example:

add_compile_options($<$<CONFIG:Debug>:-DDEBUG=1>)

The warning from Zephyr makes it seem like we could just set the CMake variable "NO_BUILD_TYPE_WARNING" and use Debug and Release anyway. Looking at the pull request where this warning was added, if the current optimization flag matches CMake's default flags for the build type, the warning won't even be emitted, which suggests it's OK to use Debug and Release. I guess Zephyr's CMake scripts override everything in the predefined build type variables anyway, so there would be no conflict with predefined variables?

  • Hi Nick, 

    I guess Zephyr's CMake scripts override everything in the predefined build type variables anyway, so there would be no conflict with predefined variables?

    In the "Configuring build types" guide,  it says:

    Build types enable you to use different sets of configuration options for each board. You can create several build type .conf files per board and select one of them when building the application. This means that you do not have to use one prj.conf file for your project and modify it each time to fit your needs. 

    Therefore, the answer is Yes, the concept is the same to set configurations in the prj.conf file to overwrite the variables. See the 1.5 Configurations section in the tutorial

    -Amanda H.

  • This wasn’t the question - the question was about CMake configuration - the logic used in the quote in my question for using ‘ZDebug’ instead of ‘Debug’ is 

    if you use one of the values from the documentation cmake will take over and add some changes to variables used during the building process (for example cflags). You don't want this to happen as it may contradict definitions from kconfig.

    I’m questioning this logic because the Zephyr Cmake warning only occurs if the optimisation flags are different. I think it should be fine to use ‘Debug’ and ‘Release’ build types (it seems to work fine) and I’m asking for confirmation of this. 

  • Hi Nick, 

    Sorry for misunderstanding your question. 

    nrbrook said:
    I’m questioning this logic because the Zephyr Cmake warning only occurs if the optimisation flags are different. I think it should be fine to use ‘Debug’ and ‘Release’ build types (it seems to work fine) and I’m asking for confirmation of this. 

    Since it could be that something changed from the last time the engineer tried that. That was nearly 3 years ago...

    Verifying if it works (in a specific environment) should be easy. It should be enough to inspect the constructed build.ninja files for both cases. If there are no changes (flags, files, etc are the same) it means it worked. I don't know if that would work for any environment. If you know what you are doing you could go with it.

    -Amanda H.

  • Thanks, I checked the output flags (for unix makefiles) – it seems the default CMake flags are not reset by Zephyr, and do make it into the output flags, so the original answer is still valid and to use "Debug" and "Release" I will need to clear flags manually to ensure flags only come from Zephyr.

Related