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