Search code examples
cmakeyoctobitbake

Yocto conditional debug flag addition


Context

I'm currently working on yocto on a build for raspberrypi 4 (not really relevant). I would like to create two recipes for two different images:

  1. An image with the bare minimum to run fast. Mostly for production phase
  2. An image with a lot of extra tools and debug options

Basically the core idea is pretty easy to sort out, I just create two image files, the first one requires the basic recipes for my image, the second one requires the first file and requires a few additional modules (such as apt, valgrind, gdb, etc...).

Question

Now here is the issue: Most of my recipes are built with cmake. I would like to add extra flags (especially -g) when building for the debug image.

What is the best way or most common way to do that? Is there a good practice?

To me this is a little tricky due to the fact that bitbake does not supports the if statement.

My solution

For now here is the solution I found: In the image recipe for the dev file add a variable to define the debug mode:

DEBUG_MODE = "1"

Then in every file that compiles a module with cmake conditionally add the flag (-g for example):

TARGET_CXXFLAGS += "${@'-g' if d.getVar('DEBUG_MODE') == '1' else ''}"

However I'm not sure how this apply to dependencies, and I'm not sure if it is the best way to do it. It seems like a lot of duplicate code and tedious to maintain. Is there anything easier to manage?


Solution

  • Currently you set

    DEBUG_MODE = "1"
    

    but then check for DEV_MODE in your if condition, so this approach should not work at all.

    Suggestion: Set -g globally

    One step could be to define TARGET_CPPFLAGS in a *.conf for your debug-image to include -g.

    TARGET_CPPFLAGS +=  "-g"
    

    That way there should be no need to add it on recipe base, but can be done globally. Also when it is added in a conf only used for your debug image, there is no need for any if/else.

    I would also use TARGET_CPPFLAGS, as those are then used for c and c++. But bear in min that some recipes might unset the compiler flags again.