Skip to content

Commit

Permalink
[cc_toolchains] Cleanup libc versions.
Browse files Browse the repository at this point in the history
Summary: libc_version was being used for both the libc version and the abi. This defines abi separately from libc version.

Test Plan: Shouldn't be any functional changes. Relying on existing tests.

Reviewers: zasgar, vihang, michelle

Reviewed By: vihang

Signed-off-by: James Bartlett <jamesbartlett@pixielabs.ai>

Differential Revision: https://phab.corp.pixielabs.ai/D12848

GitOrigin-RevId: 11e3b3f5882a9435aaaf6f6741a3e79daf5de667
  • Loading branch information
JamesMBartlett authored and copybaranaut committed Jan 20, 2023
1 parent be31ed8 commit 7bd13ce
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ build:linux --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1

# Build for Clang:
build:clang --//bazel/cc_toolchains:compiler=clang
build:clang --//bazel/cc_toolchains:libc_version=gnu

# Build for Clang using Libc++.
build:libc++ --define use_libcpp=1
Expand All @@ -77,7 +76,6 @@ build:libc++ --copt -D_LIBCPP_NO_EXPERIMENTAL_DEPRECATION_WARNING_FILESYSTEM
build:libc++ --build_tag_filters=-no_libcpp
build:libc++ --test_tag_filters=-no_libcpp,-requires_root,-requires_bpf,-disabled
build:libc++ --//bazel/cc_toolchains:compiler=clang
build:libc++ --//bazel/cc_toolchains:libc_version=gnu
build:libc++ --features=libc++ --features=-libstdc++

# Build for GCC.
Expand All @@ -90,7 +88,6 @@ build:gcc --copt -Wno-error=maybe-uninitialized
build:gcc --build_tag_filters=-no_gcc
build:gcc --test_tag_filters=-no_gcc,-requires_root,-requires_bpf,-disabled
build:gcc --//bazel/cc_toolchains:compiler=gcc
build:gcc --//bazel/cc_toolchains:libc_version=gnu


# bazel build --config asan ...
Expand Down
9 changes: 6 additions & 3 deletions bazel/cc_toolchains/clang.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# SPDX-License-Identifier: Apache-2.0

load("//bazel:repository_locations.bzl", "REPOSITORY_LOCATIONS")
load("//bazel/cc_toolchains:settings.bzl", "HOST_GLIBC_VERSION")
load("//bazel/cc_toolchains:utils.bzl", "abi")

def _download_repo(rctx, repo_name, output):
loc = REPOSITORY_LOCATIONS[repo_name]
Expand Down Expand Up @@ -52,10 +54,11 @@ def _clang_toolchain_impl(rctx):
substitutions = {
"{clang_major_version}": rctx.attr.clang_version.split(".")[0],
"{clang_version}": rctx.attr.clang_version,
"{host_abi}": abi(rctx.attr.host_arch, rctx.attr.host_libc_version),
"{host_arch}": rctx.attr.host_arch,
"{host_libc_version}": rctx.attr.host_libc_version,
"{libc_version}": rctx.attr.libc_version,
"{libcxx_path}": libcxx_path,
"{target_abi}": abi(rctx.attr.target_arch, rctx.attr.libc_version),
"{target_arch}": rctx.attr.target_arch,
"{this_repo}": rctx.attr.name,
"{toolchain_path}": toolchain_path,
Expand Down Expand Up @@ -83,9 +86,9 @@ def _clang_register_toolchain(
libcxx_repo,
target_arch,
clang_version,
libc_version = "gnu",
libc_version = HOST_GLIBC_VERSION,
host_arch = "x86_64",
host_libc_version = "gnu",
host_libc_version = HOST_GLIBC_VERSION,
use_for_host_tools = False):
clang_toolchain(
name = name,
Expand Down
18 changes: 5 additions & 13 deletions bazel/cc_toolchains/clang/toolchain.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,13 @@ includes = [
"{libcxx_path}/include/c++/v1",
]

_LIBC_VERSION_TO_LONG_NAME = {
"gnu": "glibc_unknown",
}

_PLATFORMS = {
"x86_64": "@platforms//cpu:x86_64",
}

_LIBC_CONSTRAINTS = {
"gnu": "@px//bazel/cc_toolchains:libc_version_gnu",
}

cc_toolchain_config(
name = "toolchain_config",
abi_libc_version = _LIBC_VERSION_TO_LONG_NAME["{libc_version}"],
abi_libc_version = "{libc_version}",
abi_version = "clang",
compile_flags = [
"-fstack-protector",
Expand All @@ -85,7 +77,7 @@ cc_toolchain_config(
],
dbg_compile_flags = ["-g"],
enable_sanitizers = not {use_for_host_tools},
host_system_name = "{host_arch}-unknown-linux-{host_libc_version}",
host_system_name = "{host_arch}-unknown-linux-{host_abi}",
libclang_rt_path = "external/{this_repo}/{toolchain_path}/lib/clang/{clang_version}/lib/linux",
libcxx_path = "external/{this_repo}/{libcxx_path}",
link_flags = [
Expand All @@ -106,8 +98,8 @@ cc_toolchain_config(
],
opt_link_flags = ["-Wl,--gc-sections"],
supports_start_end_lib = True,
target_libc = _LIBC_VERSION_TO_LONG_NAME["{libc_version}"],
target_system_name = "{target_arch}-unknown-linux-{libc_version}",
target_libc = "{libc_version}",
target_system_name = "{target_arch}-unknown-linux-{target_abi}",
tool_paths = tool_paths,
toolchain_identifier = toolchain_identifier,
unfiltered_compile_flags = [
Expand Down Expand Up @@ -206,7 +198,7 @@ toolchain(
] + ["@px//bazel/cc_toolchains:is_exec_true"] if {use_for_host_tools} else ["@px//bazel/cc_toolchains:is_exec_false"],
target_settings = [
"@px//bazel/cc_toolchains:compiler_clang",
_LIBC_CONSTRAINTS["{libc_version}"],
"@px//bazel/cc_toolchains:libc_version_{libc_version}",
],
toolchain = ":cc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
Expand Down
2 changes: 1 addition & 1 deletion bazel/cc_toolchains/gcc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _gcc_x86_64_gnu():
],
target_settings = [
":compiler_gcc",
":libc_version_gnu",
":libc_version_glibc_host",
],
toolchain = ":cc-compiler-gcc-x86_64-gnu",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
Expand Down
33 changes: 16 additions & 17 deletions bazel/cc_toolchains/settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,27 @@

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

HOST_GLIBC_VERSION = "glibc_host"

def _settings():
libc_versions = [
HOST_GLIBC_VERSION,
"static_musl",
]
string_flag(
name = "libc_version",
build_setting_default = "gnu",
values = [
"gnu",
"static_musl",
],
)
native.config_setting(
name = "libc_version_gnu",
flag_values = {
"//bazel/cc_toolchains:libc_version": "gnu",
},
)
native.config_setting(
name = "libc_version_static_musl",
flag_values = {
"//bazel/cc_toolchains:libc_version": "static_musl",
},
build_setting_default = HOST_GLIBC_VERSION,
values = libc_versions,
)

for version in libc_versions:
native.config_setting(
name = "libc_version_" + version,
flag_values = {
"//bazel/cc_toolchains:libc_version": version,
},
)

string_flag(
name = "compiler",
build_setting_default = "clang",
Expand Down
29 changes: 29 additions & 0 deletions bazel/cc_toolchains/utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

# abi returns the expected abi for the given arch libc_version pair.
# Currently, doesn't support musl.
def abi(arch, libc_version):
if arch == "aarch64":
return "eabi"
if libc_version.startswith("glibc"):
return "gnu"

# We should support musl in the future.
fail("Cannot determine abi from arch ({arch}) and libc version ({libc_version})".format(
arch = arch,
libc_version = libc_version,
))

0 comments on commit 7bd13ce

Please sign in to comment.