SOLVED: Overriding Compiler Options in a Component

jcolebaker
Posts: 64
Joined: Thu Mar 18, 2021 12:23 am

SOLVED: Overriding Compiler Options in a Component

Postby jcolebaker » Thu Aug 19, 2021 9:36 pm

Hi, I'm trying to enable some compiler warnings and "treat warnings as errors" for our custom components.

I have added this to the CMakeLists.txt for the component:

Code: Select all

target_compile_options(${COMPONENT_LIB} PRIVATE -Wall -Wextra -Werror)
I discovered that my extra options are being fed to the compiler, BUT it still doesn't fail on various warning such as unused function parameters. When I look at the full list of options when compiling one source file in the component, I see that it already has a number of warnings explicitly turned off, e.g.:

Code: Select all

-Wno-sign-compare  -Wno-unused-parameter
I guess these are overriding my attempt to turn these warnings ON?

How do I REMOVE these existing (inherited) compiler options that I don't want? I can't see where they are coming from.

Note I don't want to remove all existing compiler options (even if I could figure out how) because there are some which look important.
Last edited by jcolebaker on Mon Sep 02, 2024 12:52 am, edited 1 time in total.

mark.jeronimus
Posts: 6
Joined: Sun Jan 31, 2021 10:11 am

Re: Overriding Compiler Options in a Component

Postby mark.jeronimus » Mon Aug 28, 2023 10:12 am

Unfortunately, IDF *relies* on these parameters.

The way you change the 'forced' parameters is by editing `esp-idf/tools/cmake/build.cmake`. However if you disable even one of these, the entirety of IDF can't compile anymore.

I was searching for a fix for this too because I found a bug in my own software that was caused by a signed/unsigned compare.

jcolebaker
Posts: 64
Joined: Thu Mar 18, 2021 12:23 am

WORKAROUND: Overriding Compiler Options in a Component

Postby jcolebaker » Mon Sep 02, 2024 12:49 am

I have finally found a solution to this problem.

I have been able to add extra compiler flags with (for example):

Code: Select all

target_compile_options(${COMPONENT_LIB} PRIVATE  -Wunused-function -Werror)
However, it didn't work for some errors (such as unused function) because the ESP-IDF build system had already added flags to explicitly turn OFF these errors.

When I looked at the compiler command line as printed out during the build, and split it into readable lines, I saw this:

Code: Select all

...
-Wall
-Werror=all
-Wno-error=unused-function
-Wno-error=unused-variable
-Wno-error=unused-but-set-variable
-Wno-error=deprecated-declarations
-Wextra
-Wno-unused-parameter
-Wno-sign-compare
-Wno-enum-conversion
...etc.
So "no-error=" is used to explicitly disable some errors including unused-function. I found that if I added the following to my extra flags to explicitly turn these errors back ON, it worked:

Code: Select all

-Werror=unused-function
-Werror=unused-variable
-Werror=unused-but-set-variable
-Werror=deprecated-declarations
I.e. my build now fails if there is an unused function or variable (yay!!).

This introduces another problem, though: certain system headers won't compile now, because they generate these errors (hence why they were disabled).

The only problem I've found so far is actually with freertos/FreeRTOS.h, and it fails due to a sign-compare warning.

The work-around is to use a wrapper for freertos/FreeRTOS.h, and use #pragma directives to temporarily disable the warning, just for that header, as follows:

Code: Select all

// my_FreeRTOS.h:

#ifndef MY_FREERTOS_H_
#define MY_FREERTOS_H_

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"

    #include "freertos/FreeRTOS.h"

#pragma GCC diagnostic pop

#endif // MY_FREERTOS_H_
Then include this header instead of freertos/FreeRTOS.h

Who is online

Users browsing this forum: No registered users and 310 guests