bazelbuild / bazelbuild/bazel

Compiler Specific COPTS

Open
#25,795 2 comments 0 reactions 0 assignees View on GitHub
P3 team-Rules-CPP type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

Hi,

I have various modules I'm maintaining on BCR, and have the reoccurring issue that certain copts in cc_ targets require a variation depending on compiler (ie MSVC `/02` vs GCC `-02`). Additionally, certain flags are needed with certain compilers for workarounds, such as `-mno-avx256-split-unaligned-load` for gcc.

After some searching, on [this page](https://bazel.build/rules/lib/toplevel/cc_common) I found:

> The compiler string (e.g. "gcc"). The current toolchain's compiler is exposed to `@bazel_tools//tools/cpp:compiler (compiler_flag)` as a flag value. Targets that require compiler-specific flags can use the config_settings in https://github.com/bazelbuild/rules_cc/blob/main/cc/compiler/BUILD in select() statements or create custom config_setting if the existing settings don't suffice.

So currently I'm starting to use it like:
```starlark
copts = select({
"@rules_cc//cc/compiler:gcc": [
"-mno-avx256-split-unaligned-load",
],
"//conditions:default": [],
})
```

If this is indeed the correct way to do this, did I miss somewhere it was more explicitly demonstrated in the docs (rather than just the build file deep in the repo) as I think this quite a common pattern which mostly gets replaced with:

```starlark
copts = select({
"@platforms//os:linux": ["-mno-avx256-split-unaligned-load"],
"//conditions:default": [],
}),
```

Which may not behave as expected when using MinGW or Clang on linux?

Some clearer documentation (if it doesn't yet exist, maybe I missed it😅) with examples would be great IMO

EDIT - Just found [this file in BCR](https://github.com/bazelbuild/bazel-central-registry/blob/main/modules/jemalloc/5.3.0-bcr.alpha.1/overlay/tools/cc.bzl) which demonstrates it nicely

Contributor guide

Open the contributing guide

Research direction

Start with the linked cc_common documentation, rules_cc/cc/compiler/BUILD, and the BCR jemalloc tools/cc.bzl example to identify the current compiler-specific select pattern. Update the relevant Bazel documentation with clear examples and explain why compiler selection differs from selecting only the target operating system.

Written by the indexing model from the issue text.

Assessment

Domain
build-system, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.