Skip to content

Commit

Permalink
Mark gcc as gcc instead of compiler in Unix CC toolchain
Browse files Browse the repository at this point in the history
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 #12707

Closes #16297.

PiperOrigin-RevId: 475467838
Change-Id: I9fa98ddd3c7f29742357596280dfc98c939f0253
  • Loading branch information
fmeum authored and copybara-github committed Sep 20, 2022
1 parent 1bf2246 commit ef3f058
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1708,4 +1708,51 @@ EOF
expect_log "in pkg/library.cpp: ''"
}

function test_compiler_flag_gcc() {
# The default macOS toolchain always uses XCode's clang.
[ "$PLATFORM" != "darwin" ] || return 0
type -P gcc || return 0

cat > BUILD.bazel <<'EOF'
config_setting(
name = "gcc_compiler",
flag_values = {"@bazel_tools//tools/cpp:compiler": "gcc"},
)
cc_binary(
name = "main",
srcs = select({":gcc_compiler": ["main.cc"]}),
)
EOF
cat > main.cc <<'EOF'
int main() {}
EOF

bazel build //:main --repo_env=CC=gcc || fail "Expected compiler flag to have value 'gcc'"
}

function test_compiler_flag_clang() {
# TODO: The default toolchain always uses XCode's clang, but sets the compiler name to the generic
# "compiler".
[ "$PLATFORM" != "darwin" ] || return 0
type -P clang || return 0

cat > BUILD.bazel <<'EOF'
config_setting(
name = "clang_compiler",
flag_values = {"@bazel_tools//tools/cpp:compiler": "clang"},
)
cc_binary(
name = "main",
srcs = select({":clang_compiler": ["main.cc"]}),
)
EOF
cat > main.cc <<'EOF'
int main() {}
EOF

bazel build //:main --repo_env=CC=clang || fail "Expected compiler flag to have value 'clang'"
}

run_suite "cc_integration_test"
14 changes: 13 additions & 1 deletion tools/cpp/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ def _coverage_flags(repository_ctx, darwin):
def _is_clang(repository_ctx, cc):
return "clang" in repository_ctx.execute([cc, "-v"]).stderr

def _is_gcc(repository_ctx, cc):
# GCC's version output uses the basename of argv[0] as the program name:
# https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/gcc.cc;h=158461167951c1b9540322fb19be6a89d6da07fc;hb=HEAD#l8728
return repository_ctx.execute([cc, "--version"]).stdout.startswith("gcc ")

def _get_compiler_name(repository_ctx, cc):
if _is_clang(repository_ctx, cc):
return "clang"
if _is_gcc(repository_ctx, cc):
return "gcc"
return "compiler"

def _find_generic(repository_ctx, name, env_name, overriden_tools, warn = False, silent = False):
"""Find a generic C++ toolchain tool. Doesn't %-escape the result."""

Expand Down Expand Up @@ -492,7 +504,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
"%{compiler}": escape_string(get_env_var(
repository_ctx,
"BAZEL_COMPILER",
"clang" if is_clang else "compiler",
_get_compiler_name(repository_ctx, cc),
False,
)),
"%{abi_version}": escape_string(get_env_var(
Expand Down

0 comments on commit ef3f058

Please sign in to comment.