How to specify preprocessor includes in Bazel? (-include common_header.h)

  bazel, c++

I am trying to use code in Bazel, which uses the -include flag (https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html) to always include a config header.
The config header sets a number of #defines that are required by the header files, ie enabling/disabling certain APIs.

The config header looks something like this:

#ifndef CONFIG_H_
#define CONFIG_H_

// part of library, defines some stuff to use in this file
#include "config_defs.h"

<#define several config settings>

// also part of library, has a defaults via `#ifndef`->`#define`
#include "config_defaults.h" 

#endif  // CONFIG_H_

Unfortunately, adding that header to every file is not an option.
I would like to avoid having to convert the complete config and all defaults to -D (ie via defines = [...]), due to the number of settings.

The "naive" way of adding copts = ["-include config.h"] does not work, giving the following error when i add the file to deps:
source file '[...]:config.h' is misplaced here (expected .ld, .lds or .ldscript)

From the looks of it, the config would need to end up in CcToolchainConfigInfo.includes (https://docs.bazel.build/versions/master/cc-toolchain-config-reference.html#cctoolchainconfiginfo-build-variables), but I didn’t figure out how to get it there.

I also tried to do add the -include in a toolchain (via feature flags), but couldn’t figure out how to tell it where to find the file.

Source: Windows Questions C++

LEAVE A COMMENT