Override commandline options for certain targets

374 views
Skip to first unread message

Xu Hu

unread,
Oct 13, 2022, 11:44:57 PM10/13/22
to bazel-discuss
Hi Bazel experts,

TLDR: what's the best way to override or nullify certain bazel commandline flags for a target?

We are using bazel to build C++ code and CUDA code. Due to certain platform limitation, we use C++ 17 for C++ code, but C++ 14 for CUDA (since the cuda compiler nvcc doesn't support C++ 17, but we still need C++ 17 features in regular C++ code). We use rules_cuda to build CUDA targets.

To do this, currently we pass -std=c++17 to all the cc targets (by copts), and no special flag for CUDA targets. Without any flag the compilers (gcc and nvcc) default to use c++ 14. We don't use --cxxopt=-std=c++17 flag, since this flag will be passed to nvcc and cause failures. This has been working fine.

Now we are introducing abseil to the codebase. Abseil recommends --cxxopt=-std=c++17 to set what C++ dialect to use. As mentioned above, this will break CUDA build.

Ideally I'd like to modify cuda targets (cuda_library rule) such that they can override the -std=c++17 flag or nullify it. I have been looking into this discussion on transition and general discussions Google gives me, but have not got a clear solution yet.

Xu Hu

unread,
Oct 15, 2022, 2:28:39 PM10/15/22
to bazel-discuss
To answer my own question, I figured out a hack:
  1. Don't use global flags like --cxxopt, so that nvcc won't be broken
  2. Modify the cpp toolchain to add a new feature for c++17. Enable this feature by default.
  3. Modify rules_cuda implementation to turn off c++17 feature.
For 2, since we are using rules_cc's unix toolchain, I created a patch to modify rules_cc/cc/private/toolchain/unix_cc_toolchain_config.bzl. In function _impl, add this new feature, set enabled to True, and add just one flag -std=c++17. Add this feature to features list.

For 3, rules_cuda provides a macro cuda_library, which is a wrapper around cc_library. I created a patch to explicitly disable the feature in the macro implementation.



Reply all
Reply to author
Forward
0 new messages