CMake on STM32 | Episode 3: handle warnings in 3rd party files
Tags: embedded, cpp, microcontrollers, cmake
Published: 2020-05-01 11:23:57

Something funny happens sometimes with 3rd party files: they generate warnings when compiled with your project settings. If you have quality indicators about the build health (automatically updated by Jenkins maybe), this is quite annoying. If you compile with an option such as [`-Werror`](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html) to treat warnings as errors, this is a blocking issue.

In this article, we will add compiler options that will raise warnings in ST's generated files and we will ask CMake to suppress these warnings but only for these files.

## Add compilers options

In [episode 1](https://dev.to/younup/cmake-on-stm32-the-beginning-3766), we simply used the compiler options from ST's generated Makefiles. The only option to control GCC warnings is `-Wall`. If this is not clear for you yet, I will say it very clearly : **`-Wall` does not enable all warnings and compiling with `-Wall` is not enough.** I suggest that you use at least `-Wall -Wextra`. This [GitHub repository](https://github.com/jonathanpoelen/cpp-compiler-options) has lists of options to enable warnings. You might be scared ^^

For the example here, let's add `-Wextra -pedantic`. Simply modify the call to `target_compile_options()` in `CMakeLists.txt`:

```cmake

target_compile_options(${EXECUTABLE} PRIVATE
        -mcpu=cortex-m4
        -mthumb
        -mfpu=fpv4-sp-d16
        -mfloat-abi=hard

        -fdata-sections
        -ffunction-sections

        -Wall
        -Wextra
        -pedantic

        $<$<CONFIG:Debug>:-Og>
        )
```

When you recompile the project, you will see warnings that you didn't get before:

```bash
[ 48%] Building C object CMakeFiles/nucleo-f413zh.out.dir/BSP/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c.obj
D:\cmake_stm32\BSP\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr.c: In function 'HAL_PWR_EnterSLEEPMode':
D:\cmake_stm32\BSP\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr.c:365:38: warning: unused parameter 'Regulator' [-Wunused-parameter]
  365 | void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
      |                             ~~~~~~~~~^~~~~~~~~
```

## Change compiler options for a specific file

At this point, we are facing a dilemma:

1. It is out of the question that we modify generated / 3rd party files.
2. There is no way that we remove `-Wextra` ([that adds `-Wunused-parameter`](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html)).

We may add `-Wno-unused-parameter` to `target_compile_options()` to counter `-Wunused-parameter`. It will suppress the warnings on ST's generated file but also for our own files. This is not what we want because it would be useful to be warned if a function we wrote doesn't use one of its parameters.

The solution is to add `-Wno-unused-parameter` only to these generated files. Our own code will then still compile with `-Wunused-parameter`.

The CMake function to do this is [`set_source_files_properties()`](https://cmake.org/cmake/help/latest/command/set_source_files_properties.html).

We modify our `CMakeLists.txt` again to add this:

```cmake
set_source_files_properties(BSP/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c
            BSP/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c
            BSP/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c
            PROPERTIES
            COMPILE_FLAGS -Wno-unused-parameter)
```

The project builds again with no warning! Great!

## Conclusion

It is always a good idea to configure the compiler with many options to be warned about suspicious code. In C and C++, warnings are often potential bugs / not portable constructs / performance issues / etc. We should rely on the compiler to tell us about such code.

If adding options raise warnings in files that we don't own and can't modify, we can use CMake's `set_source_files_properties()` function to apply specific options to these files to suppress the warnings.