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

Bazel build failed to detect clang compiler #732

Closed
hcoona opened this issue Jul 7, 2020 · 7 comments
Closed

Bazel build failed to detect clang compiler #732

hcoona opened this issue Jul 7, 2020 · 7 comments
Assignees

Comments

@hcoona
Copy link

hcoona commented Jul 7, 2020

Environment

  • gcc: 4.9.2
  • clang: 8.0.0
  • bazel: 3.3.1
  • abseil commit id: 38db52a

Expected

The target leak_check_api_enabled_for_testing defined as following:

cc_library(
    name = "leak_check_api_enabled_for_testing",
    testonly = 1,
    srcs = ["leak_check.cc"],
    hdrs = ["leak_check.h"],
    copts = select({
        "//absl:llvm_compiler": ["-DLEAK_SANITIZER"],
        "//conditions:default": [],
    }),
    linkopts = ABSL_DEFAULT_LINKOPTS,
    visibility = ["//visibility:private"],
    deps = [
        "//absl/base:config",
    ],
)

Building it, I expected -DLEAK_SANITIZER in clang command.

bazel build --action_env="a=1" -s //absl/debugging:leak_check_api_enabled_for_testing

Actual

bazel build --action_env="a=2" -s //absl/debugging:leak_check_api_enabled_for_testing
INFO: Build option --action_env has changed, discarding analysis cache.
INFO: Analyzed target //absl/debugging:leak_check_api_enabled_for_testing (3 packages loaded, 59 targets configured).
INFO: Found 1 target...
SUBCOMMAND: # //absl/debugging:leak_check_api_enabled_for_testing [action 'Compiling absl/debugging/leak_check.cc', configuration: ad1824ed492f6309894b0781fb74dc50dbf923d76e72e9e6369acd327b67e179]
(cd /data00/home/zhangshuai.ds/.cache/bazel/_bazel_zhangshuai.ds/28cefa5c67f76607bf7aac94fe5b35b3/execroot/com_google_absl && \
  exec env - \
    LD_LIBRARY_PATH=/usr/local/lib:/data00/home/zhangshuai.ds/.local/lib \
    PATH=/data00/home/zhangshuai.ds/.autojump/bin:/data00/home/zhangshuai.ds/.local/bin:/data00/home/zhangshuai.ds/.local/share/apache-maven-3.6.1/bin/:/data00/home/zhangshuai.ds/.yarn/bin:/opt/tiger/go/go1.11.4/bin:/opt/tiger/jdk/jdk1.8.0_131/bin:/data00/home/zhangshuai.ds/.vscode-server/bin/cd9ea6488829f560dc949a8b2fb789f3cdc05f5d/bin:/data00/home/zhangshuai.ds/.autojump/bin:/data00/home/zhangshuai.ds/.local/bin:/data00/home/zhangshuai.ds/.local/share/apache-maven-3.6.1/bin/:/data00/home/zhangshuai.ds/.yarn/bin:/opt/tiger/go/go1.11.4/bin:/opt/tiger/jdk/jdk1.8.0_131/bin:/data00/home/zhangshuai.ds/.vscode-server/bin/cd9ea6488829f560dc949a8b2fb789f3cdc05f5d/bin:/data00/home/zhangshuai.ds/.autojump/bin:/data00/home/zhangshuai.ds/anaconda3/condabin:/data00/home/zhangshuai.ds/.local/bin:/data00/home/zhangshuai.ds/.local/share/apache-maven-3.6.1/bin/:/data00/home/zhangshuai.ds/.yarn/bin:/opt/tiger/go/go1.11.4/bin:/opt/tiger/jdk/jdk1.8.0_131/bin:/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/tiger/dev_toolkit/bin:/opt/tiger/yarn_deploy/hadoop/bin/:/opt/tiger/hive_deploy/bin:/data00/home/zhangshuai.ds/GoEnlistments/bin:/data00/home/zhangshuai.ds/dotnet:/opt/tiger/dev_toolkit/bin:/opt/tiger/yarn_deploy/hadoop/bin/:/opt/tiger/hive_deploy/bin:/data00/home/zhangshuai.ds/GoEnlistments/bin:/data00/home/zhangshuai.ds/dotnet:/opt/tiger/dev_toolkit/bin:/opt/tiger/yarn_deploy/hadoop/bin/:/opt/tiger/hive_deploy/bin:/data00/home/zhangshuai.ds/GoEnlistments/bin:/data00/home/zhangshuai.ds/dotnet \
    PWD=/proc/self/cwd \
    a=1 \
  /data00/home/zhangshuai.ds/.local/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/k8-fastbuild/bin/absl/debugging/_objs/leak_check_api_enabled_for_testing/leak_check.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/absl/debugging/_objs/leak_check_api_enabled_for_testing/leak_check.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c absl/debugging/leak_check.cc -o bazel-out/k8-fastbuild/bin/absl/debugging/_objs/leak_check_api_enabled_for_testing/leak_check.pic.o)
Target //absl/debugging:leak_check_api_enabled_for_testing up-to-date:
  bazel-bin/absl/debugging/libleak_check_api_enabled_for_testing.a
  bazel-bin/absl/debugging/libleak_check_api_enabled_for_testing.so
INFO: Elapsed time: 0.324s, Critical Path: 0.09s
INFO: 1 process: 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions
@rogeeff
Copy link
Member

rogeeff commented Jul 7, 2020

Is llvm the default compiler for the build? Does building with "--compiler=llvm" do anything different?

My best guess is whatever environment you are running into doesn't default to the llvm compiler, which is why it wouldn't choose the select() path that adds that define.

@hcoona
Copy link
Author

hcoona commented Jul 8, 2020

Is llvm the default compiler for the build? Does building with "--compiler=llvm" do anything different?

My best guess is whatever environment you are running into doesn't default to the llvm compiler, which is why it wouldn't choose the select() path that adds that define.

The default compiler in my environment (aka. /usr/bin/cc) is not clang. However, I already exposed CC=clang CXX=clang++ to the environment variables.

Here's the logs building with --compiler=clang (toolchains renamed from llvm to clang in higher version of bazel):

bazel build --compiler=clang --action_env="a=2" -s //absl/debugging:leak_check_api_enabled_for_testing
INFO: Build option --compiler has changed, discarding analysis cache.
INFO: Analyzed target //absl/debugging:leak_check_api_enabled_for_testing (0 packages loaded, 59 targets configured).
INFO: Found 1 target...
SUBCOMMAND: # //absl/debugging:leak_check_api_enabled_for_testing [action 'Compiling absl/debugging/leak_check.cc', configuration: a362aae818b2e036a010ee52eba8e3a03be433042292ee27b78736e7912454ee]
(cd /data00/home/zhangshuai.ds/.cache/bazel/_bazel_zhangshuai.ds/28cefa5c67f76607bf7aac94fe5b35b3/execroot/com_google_absl && \
  exec env - \
    LD_LIBRARY_PATH=/usr/local/lib:/data00/home/zhangshuai.ds/.local/lib \
    PATH=/data00/home/zhangshuai.ds/.autojump/bin:/data00/home/zhangshuai.ds/.local/bin:/data00/home/zhangshuai.ds/.local/share/apache-maven-3.6.1/bin/:/data00/home/zhangshuai.ds/.yarn/bin:/opt/tiger/go/go1.11.4/bin:/opt/tiger/jdk/jdk1.8.0_131/bin:/data00/home/zhangshuai.ds/.autojump/bin:/data00/home/zhangshuai.ds/anaconda3/condabin:/data00/home/zhangshuai.ds/.local/bin:/data00/home/zhangshuai.ds/.local/share/apache-maven-3.6.1/bin/:/data00/home/zhangshuai.ds/.yarn/bin:/opt/tiger/go/go1.11.4/bin:/opt/tiger/jdk/jdk1.8.0_131/bin:/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/tiger/dev_toolkit/bin:/opt/tiger/yarn_deploy/hadoop/bin/:/opt/tiger/hive_deploy/bin:/data00/home/zhangshuai.ds/GoEnlistments/bin:/data00/home/zhangshuai.ds/dotnet:/opt/tiger/dev_toolkit/bin:/opt/tiger/yarn_deploy/hadoop/bin/:/opt/tiger/hive_deploy/bin:/data00/home/zhangshuai.ds/GoEnlistments/bin:/data00/home/zhangshuai.ds/dotnet \
    PWD=/proc/self/cwd \
    a=2 \
  /data00/home/zhangshuai.ds/.local/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/k8-fastbuild/bin/absl/debugging/_objs/leak_check_api_enabled_for_testing/leak_check.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/absl/debugging/_objs/leak_check_api_enabled_for_testing/leak_check.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c absl/debugging/leak_check.cc -o bazel-out/k8-fastbuild/bin/absl/debugging/_objs/leak_check_api_enabled_for_testing/leak_check.pic.o)
Target //absl/debugging:leak_check_api_enabled_for_testing up-to-date:
  bazel-bin/absl/debugging/libleak_check_api_enabled_for_testing.a
  bazel-bin/absl/debugging/libleak_check_api_enabled_for_testing.so
INFO: Elapsed time: 0.278s, Critical Path: 0.09s
INFO: 1 process: 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions

@derekmauro
Copy link
Member

Abseil relies on the compiler string being llvm. At one point Bazel simply set the compiler string to compiler, so for testing we set an environment variable BAZEL_COMPILER=llvm. From my experimentation, it looks like Bazel is now setting this string to clang when clang is used, and it is setting it to compiler when gcc is used. We may have to adjust this.

For now, try working around this using BAZEL_COMPILER=llvm bazel build ..., although this won't work if your project relies on the compiler string being clang somwhere.

@hcoona
Copy link
Author

hcoona commented Jul 9, 2020

Abseil relies on the compiler string being llvm. At one point Bazel simply set the compiler string to compiler, so for testing we set an environment variable BAZEL_COMPILER=llvm. From my experimentation, it looks like Bazel is now setting this string to clang when clang is used, and it is setting it to compiler when gcc is used. We may have to adjust this.

For now, try working around this using BAZEL_COMPILER=llvm bazel build ..., although this won't work if your project relies on the compiler string being clang somwhere.

@derekmauro Thanks for your explanation. Unfortunately, the workaround didn't work on Bazel 3.3.1.

$ env BAZEL_COMPILER=llvm bazel build --compiler=clang --action_env="a=1" -s //absl/debugging:leak_check_api_enabled_for_testing
Starting local Bazel server and connecting to it...
ERROR: /data00/home/zhangshuai.ds/.cache/bazel/_bazel_zhangshuai.ds/28cefa5c67f76607bf7aac94fe5b35b3/external/local_config_cc/BUILD:47:19: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'k8' and compiler 'clang'.
ERROR: While resolving configuration keys for //absl/debugging:leak_check_api_enabled_for_testing: Analysis of target '@local_config_cc//:toolchain' failed
ERROR: Analysis of target '//absl/debugging:leak_check_api_enabled_for_testing' failed; build aborted: Analysis of target '@local_config_cc//:toolchain' failed
INFO: Elapsed time: 3.685s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (14 packages loaded, 42 targets configured)
$ bazel build --compiler=llvm --action_env="a=1" -s //absl/debugging:leak_check_api_enabled_for_testing
INFO: Build option --compiler has changed, discarding analysis cache.
ERROR: /data00/home/zhangshuai.ds/.cache/bazel/_bazel_zhangshuai.ds/28cefa5c67f76607bf7aac94fe5b35b3/external/local_config_cc/BUILD:47:19: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'k8' and compiler 'llvm'.
ERROR: While resolving configuration keys for //absl/debugging:leak_check_api_enabled_for_testing: Analysis of target '@local_config_cc//:toolchain' failed
ERROR: Analysis of target '//absl/debugging:leak_check_api_enabled_for_testing' failed; build aborted: Analysis of target '@local_config_cc//:toolchain' failed
INFO: Elapsed time: 1.271s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 42 targets configured)

@hcoona
Copy link
Author

hcoona commented Jul 9, 2020

Abseil relies on the compiler string being llvm. At one point Bazel simply set the compiler string to compiler, so for testing we set an environment variable BAZEL_COMPILER=llvm. From my experimentation, it looks like Bazel is now setting this string to clang when clang is used, and it is setting it to compiler when gcc is used. We may have to adjust this.

For now, try working around this using BAZEL_COMPILER=llvm bazel build ..., although this won't work if your project relies on the compiler string being clang somwhere.

I got the point. I can patch //absl/compiler_config_setting.bzl in my environment to workaround it.

diff --git a/absl/compiler_config_setting.bzl b/absl/compiler_config_setting.bzl
index 6696229..c03301c 100644
--- a/absl/compiler_config_setting.bzl
+++ b/absl/compiler_config_setting.bzl
@@ -26,7 +26,7 @@ def create_llvm_config(name, visibility):
         native.config_setting(
             name = name,
             flag_values = {
-                "@bazel_tools//tools/cpp:compiler": "llvm",
+                "@bazel_tools//tools/cpp:compiler": "clang",
             },
             visibility = visibility,
         )

@hcoona
Copy link
Author

hcoona commented Jul 9, 2020

Is it possible to detect the Bazel version and make another else-if branch for this issue?

This could help to detect Bazel version: https://github.com/bazelbuild/bazel-skylib/blob/master/lib/versions.bzl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@hcoona @derekmauro @rogeeff and others