Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint value @bazel_tools//tools/cpp:compiler:gcc not true when using GCC compiler #12707

Closed
pudelkoM opened this issue Dec 16, 2020 · 5 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: bug

Comments

@pudelkoM
Copy link

pudelkoM commented Dec 16, 2020

Description of the problem:

The constraint value @bazel_tools//tools/cpp:compiler:gcc is not detected/enabled correctly when the GCC compiler is used and can not be targeted with a config_setting.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

The following Bazel repo demonstrates the issue. It has a dummy target that is intended to be compiled with different copts, depending on the compiler. We use config_settings which match on the @bazel_tools//tools/cpp:compiler flag and select on them later.

WORKSPACE:

workspace(name = "test-bar")

BUILD:

config_setting(
    name = "llvm_compiler",
    flag_values = {"@bazel_tools//tools/cpp:compiler": "clang"},
)

config_setting(
    name = "gcc_compiler",
    flag_values = {"@bazel_tools//tools/cpp:compiler": "gcc"},
)

# Only to show the bug(?) in the local `cc_toolchain_config`.
config_setting(
    name = "compiler_compiler",
    flag_values = {"@bazel_tools//tools/cpp:compiler": "compiler"},
)

# non-existent flags to show which condition matched.
DEFAULT_COPTS = select({
    ":llvm_compiler": ["-fail_llvm"],
    ":gcc_compiler": ["-fail_gcc"],
    ":compiler_compiler": ["-fail_compiler"],
    "//conditions:default": ["-fail_default"],
})

cc_binary(
    name = "dummy",
    srcs = ["empty.cc"],
    copts = DEFAULT_COPTS,
    deps = [],
)

empty.cc

The system has both gcc and clang installed and available in $PATH:

> gcc -v
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

> clang -v
Ubuntu clang version 12.0.0-++20201207052627+22558c8501e-1~exp1~20201207163316.246

Now try to compile the dummy target.

With no $CC/$CXX set, we expect the build to fail with the -fail_gcc message, as GCC is the default. Bazel found and used gcc as seen in the error, but with the wrong flags (unrecognized command line option '-fail_compiler'):

> bazel build -s //...
INFO: Invocation ID: bc64f64e-a338-4f97-a7ba-005dc3f6cb3a
INFO: Analyzed target //:dummy (1 packages loaded, 21 targets configured).
INFO: Found 1 target...
SUBCOMMAND: # //:dummy [action 'Compiling empty.cc', configuration: a5d130b0966b4a9ca2d32725aa5baf40e215bcfc4d5cdcdc60f5cc5b4918903b, execution platform: @local_config_platform//:host]
(cd /home/max/.cache/bazel/_bazel_max/2ba0fe81f3bf905bcacad82a417d35f2/execroot/test-bar && \
  exec env - \
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin \
    PWD=/proc/self/cwd \
  /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/k8-fastbuild/bin/_objs/dummy/empty.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/dummy/empty.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -fail_compiler -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c empty.cc -o bazel-out/k8-fastbuild/bin/_objs/dummy/empty.pic.o)
ERROR: /tmp/bazel_compiler_test/BUILD:23:10: C++ compilation of rule '//:dummy' failed (Exit 1): gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 21 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 21 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
gcc: error: unrecognized command line option '-fail_compiler'
Target //:dummy failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.012s, Critical Path: 0.08s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

Snippet from <bazel cache dir>/external/local_config_cc/BUILD:

...
cc_toolchain_config(
    name = "local",
    cpu = "k8",
    compiler = "compiler",  # should be gcc?
...

With $CC=clang/$CXX=clang++, the build fails as expected: clang: error: unknown argument: '-fail_llvm'
And the local config seems correct, too:

...
cc_toolchain_config(
    name = "local",
    cpu = "k8",
    compiler = "clang",
...

Setting $CC=gcc/$CXX=g++ does not help, same error as above: gcc: error: unrecognized command line option '-fail_compiler'

...
cc_toolchain_config(
    name = "local",
    cpu = "k8",
    compiler = "compiler",
...

What operating system are you running Bazel on?

20.04.1 LTS (Focal Fossa)

Linux 5.4.0-53-generic #59-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux

What's the output of bazel info release?

release 3.7.1

Have you found anything relevant by searching the web?

Abseil uses the same technique to detect the compiler, but uses the default condition for GCC:

@aiuto aiuto added team-Rules-CPP Issues for C++ rules untriaged labels Jan 7, 2021
@pudelkoM pudelkoM changed the title Constraint value @bazel_tools//tools/cpp:compiler:gccnot true when using GCC compiler Constraint value @bazel_tools//tools/cpp:compiler:gcc not true when using GCC compiler Jan 7, 2021
@oquenchil oquenchil added P3 We're not considering working on this, but happy to review a PR. (No assignee) type: bug and removed untriaged labels Jan 12, 2021
jrajahalme added a commit to cilium/proxy that referenced this issue Jan 28, 2021
It is impossible to turn on the "//bazel/gcc_build" config setting, see:

   bazelbuild/bazel#12707

GCC 9, which is the default in Ubuntu 20.04, requires the -Wno-maybe-uninitialized for
@com_googlesource_quiche//:quic_core_framer_lib to compile successfully.

Mitigate by assuming the default "compiler" is GCC.

Signed-off-by: Jarno Rajahalme <jarno@covalent.io>
lizan pushed a commit to envoyproxy/envoy that referenced this issue Feb 23, 2021
It is impossible to turn on the "//bazel/gcc_build" config setting on Bazel 3.7.x, see:

   bazelbuild/bazel#12707

GCC 9, which is the default in Ubuntu 20.04, requires the -Wno-maybe-uninitialized for
@com_googlesource_quiche//:quic_core_framer_lib to compile successfully.

Mitigate by assuming the default "compiler" is GCC. Clang is properly recognized by Bazel, so it is not affected by this.

Additional Description: See bazelbuild/bazel#12707
Risk Level: Medium, non-GCC compilers may not implement -Wno-maybe-uninitialized, and may start to fail if not recognized by Bazel.
Testing: Tested compilations with both GCC and clang, and observed that -Wno-maybe-uninitialized is present with GCC but not with clang.
Docs Changes:
Release Notes: Fix compilation with GCC by assuming it is the default compiler.

Signed-off-by: Jarno Rajahalme <jarno@covalent.io>
@fmeum
Copy link
Collaborator

fmeum commented Aug 14, 2021

@oquenchil I have a (simple) fix for this bug, but am wondering about your thoughts on backwards compatibility here: If compiler starts to have the value "gcc" instead of "compiler" for gcc, this could mess with existing uses of select.

Envoy may break, but would certainly welcome the fix based on the discussion on that PR. Abseil would not break as they treat gcc as the default case.

rexengineering pushed a commit to rexengineering/istio-envoy that referenced this issue Oct 15, 2021
It is impossible to turn on the "//bazel/gcc_build" config setting on Bazel 3.7.x, see:

   bazelbuild/bazel#12707

GCC 9, which is the default in Ubuntu 20.04, requires the -Wno-maybe-uninitialized for
@com_googlesource_quiche//:quic_core_framer_lib to compile successfully.

Mitigate by assuming the default "compiler" is GCC. Clang is properly recognized by Bazel, so it is not affected by this.

Additional Description: See bazelbuild/bazel#12707
Risk Level: Medium, non-GCC compilers may not implement -Wno-maybe-uninitialized, and may start to fail if not recognized by Bazel.
Testing: Tested compilations with both GCC and clang, and observed that -Wno-maybe-uninitialized is present with GCC but not with clang.
Docs Changes:
Release Notes: Fix compilation with GCC by assuming it is the default compiler.

Signed-off-by: Jarno Rajahalme <jarno@covalent.io>
@aaron-michaux
Copy link

@fmeum , can you describe what your simple fix is? Is there a way to simply tell bazel which compiler to use?

@fmeum
Copy link
Collaborator

fmeum commented Aug 31, 2022

@aaron-michaux If you just want to have Bazel use a particular compiler, add --repo_env=CC=<path to your compilet> to your .bazelrc and run bazel sync --configure.

The fix I was talking about above was for getting GCC recognized by Bazel as gcc rather than the generic compiler. Fixing that comes down to parsing the output of --version, but the backwards compatibility concerns are real.

@oquenchil Friendly ping, do you think reporting GCC as gcc would be too disruptive?

copybara-service bot pushed a commit to abseil/abseil-cpp that referenced this issue Sep 8, 2022
… to "gcc",

instead of just detecting Bazel's default "compiler" string.

When Bazel auto-configures GCC, it sets the compiler string to
"compiler", probably for backwards compatibility. Some users manually
set the string to "gcc".

This should address the backwards compatibility issues described in
bazelbuild/bazel#12707 and hopefully fix
#1263

PiperOrigin-RevId: 473069817
Change-Id: I8a24721f63f9d61447b22b3b05b06a9dde7d34d8
@oquenchil
Copy link
Contributor

No. I think it's fine. This would be a bug fix. Ideally the change would be checked in together with a script that runs buildozer (or some regex) over a project and fixes the places that need fixing. But if that's not possible then properly describing how it can break and what the instructions for fixing it are should be enough.

aiuto pushed a commit to aiuto/bazel that referenced this issue Oct 12, 2022
This makes it possible to detect gcc specifically via a `config_setting` for the `@bazel_tools//tools/cpp:compiler` flag with value `gcc`.

RELNOTES: The `@bazel_tools//tools/cpp:compiler` flag now has the value `gcc` if the configured compiler is detected to be gcc rather than the generic value `compiler`. A branch for `gcc` may have to be added to `select` statements that do not have a default case that handles gcc appropriately.

Fixes bazelbuild#12707

Closes bazelbuild#16297.

PiperOrigin-RevId: 475467838
Change-Id: I9fa98ddd3c7f29742357596280dfc98c939f0253
@jbms
Copy link

jbms commented Mar 15, 2023

Note: This isn't fully fixed: #17794

netkex pushed a commit to netkex/abseil-cpp that referenced this issue Apr 3, 2024
… to "gcc",

instead of just detecting Bazel's default "compiler" string.

When Bazel auto-configures GCC, it sets the compiler string to
"compiler", probably for backwards compatibility. Some users manually
set the string to "gcc".

This should address the backwards compatibility issues described in
bazelbuild/bazel#12707 and hopefully fix
abseil#1263

PiperOrigin-RevId: 473069817
Change-Id: I8a24721f63f9d61447b22b3b05b06a9dde7d34d8
anilhbtiit pushed a commit to anilhbtiit/envoyClone that referenced this issue May 27, 2024
It is impossible to turn on the "//bazel/gcc_build" config setting, see:

   bazelbuild/bazel#12707

GCC 9, which is the default in Ubuntu 20.04, requires the -Wno-maybe-uninitialized for
@com_googlesource_quiche//:quic_core_framer_lib to compile successfully.

Mitigate by assuming the default "compiler" is GCC.

Signed-off-by: Jarno Rajahalme <jarno@covalent.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants