'How do you handle include dependencies passed as #defines in Bazel?

I am attempting to build a legacy C/C++ embedded code base using Bazel. The code is separated into software sets. Because the system is embedded, there is an environment header include passed as an argument to the compiler for each software set. The header path is defined using a #define:

The source file of software_set_b may begin with:

#include MY_ENV

The compile instruction would define MY_ENV to the absolute path of the environment header in software_set_a, e.g.:

gcc -DMY_ENV=/path/to/software_set_a/headers/MyEnvironmentHeader.h

Is it possible to achieve this using Bazel without explicitly passing /path/to/software_set_a/headers/MyEnvironment.h in the --define argument in bazel build, or hardcoding the value in software_set_b's BUILD file, e.g.:

cc_library(
  name = 'software_set_b',
  defines = [
    'MY_ENV=/path/to/software_set_a/headers/MyEnvironment.h'
  ],
  ...
)

Ideally, the programmer could select the package with an argument, e.g. bazel build //:software_set_b --//:from_env=software_set_a with a fragment similar to the following in the BUILD script:

File: software_set_b/BUILD

string_flag(
  name = 'from_env',
  build_setting_default = ''
)

def deps_from_env():
  from_env = get_flag_value('from_env') # A function that gets the value of the flag.
  return '@' + from_env + '//:env' # Evaluate to e.g. '@software_set_a//:env'

cc_library(
  name = 'software_set_b',
  deps = [
    deps_from_env()
  ]
)

File: software_set_a/BUILD

cc_library(
  name = 'env',
  defines = [
    # Something to give me '/path/to/software_set_a/headers/MyEnvironment.h'
    'MY_ENV=$(rootpath)/headers/MyEnvironment.h'
  ],
  ...
)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source