Skip to content

Commit

Permalink
[build/sysroots] Add generic llvm variant handling.
Browse files Browse the repository at this point in the history
Summary:
Instead of explicitly listing select options for the different llvm variants, this diff hides it behind a few starlark macros.

This generic handling allows easier additions of new llvm variants, for example, for the variants for the new sysroots that will be added in a future diff.

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

Reviewers: vihang, zasgar, michelle, #third_party_approvers

Reviewed By: vihang, #third_party_approvers

Signed-off-by: James Bartlett <[email protected]>

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

GitOrigin-RevId: 3aef9d9f5db74d61634fd2d7d340801c646189cd
  • Loading branch information
JamesMBartlett authored and copybaranaut committed Jan 20, 2023
1 parent ae40d03 commit f5de1c0
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ build:linux --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
build:clang --//bazel/cc_toolchains:compiler=clang

# Build for Clang using Libc++.
build:libc++ --define use_libcpp=1
build:libc++ --//bazel:enable_libcpp
# TODO(zasgar): Remove this in next diff.
build:libc++ --copt -D_LIBCPP_NO_EXPERIMENTAL_DEPRECATION_WARNING_FILESYSTEM
build:libc++ --build_tag_filters=-no_libcpp
Expand Down
5 changes: 3 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("//bazel:repositories.bzl", "list_pl_deps")
load("//bazel/cc_toolchains:llvm_libs.bzl", "llvm_variant_repo_name", "llvm_variant_setting_label", "llvm_variants")
load("//bazel/external/ubuntu_packages:packages.bzl", "packages")

licenses(["restricted"])
Expand Down Expand Up @@ -172,8 +173,8 @@ container_image(
alias(
name = "llvm",
actual = select({
"//bazel:use_libcpp": "@com_llvm_lib_libcpp//:llvm",
"//conditions:default": "@com_llvm_lib//:llvm",
llvm_variant_setting_label(variant): "@{repo}//:llvm".format(repo = llvm_variant_repo_name(variant))
for variant in llvm_variants()
}),
visibility = ["//visibility:public"],
)
Expand Down
17 changes: 16 additions & 1 deletion bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -45,9 +46,23 @@ config_setting(
},
)

bool_flag(
name = "enable_libcpp",
build_setting_default = False,
)

config_setting(
name = "use_libcpp",
values = {"define": "use_libcpp=1"},
flag_values = {
"//bazel:enable_libcpp": "True",
},
)

config_setting(
name = "use_libstdcpp",
flag_values = {
"//bazel:enable_libcpp": "False",
},
)

config_setting(
Expand Down
3 changes: 3 additions & 0 deletions bazel/cc_toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#
# SPDX-License-Identifier: Apache-2.0
load("//bazel/cc_toolchains:gcc.bzl", "gcc_x86_64_gnu", "gcc_x86_64_static_musl")
load("//bazel/cc_toolchains:llvm_libs.bzl", "llvm_variant_settings")
load("//bazel/cc_toolchains:settings.bzl", "settings")

filegroup(
Expand All @@ -22,6 +23,8 @@ filegroup(

settings()

llvm_variant_settings()

gcc_x86_64_gnu()

gcc_x86_64_static_musl()
Expand Down
70 changes: 70 additions & 0 deletions bazel/cc_toolchains/llvm_libs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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

load("@bazel_skylib//lib:selects.bzl", "selects")

def _llvm_variants():
variants = []

# TODO(james): add sysroot variants.
# Add variants for our non sysroot config.
variants.append(("x86_64", "glibc_host", True))
variants.append(("x86_64", "glibc_host", False))
return variants

def _llvm_variant_settings():
for variant in _llvm_variants():
arch, libc_version, use_libcpp = variant
configs_to_match = [
":libc_version_" + libc_version,
"@platforms//cpu:" + arch,
]
if use_libcpp:
configs_to_match.append("//bazel:use_libcpp")
else:
configs_to_match.append("//bazel:use_libstdcpp")
selects.config_setting_group(
name = _llvm_variant_setting_name(variant),
match_all = configs_to_match,
visibility = ["//visibility:public"],
)

def _llvm_variant_setting_name(variant):
arch, libc_version, use_libcpp = variant
name = "llvm_variant_{arch}_{libc_version}".format(
arch = arch,
libc_version = libc_version,
)
if use_libcpp:
name = name + "_libcpp"
return name

def _llvm_variant_setting_label(variant):
name = _llvm_variant_setting_name(variant)
return "@px//bazel/cc_toolchains:" + name

def _llvm_variant_repo_name(variant):
arch, libc_version, use_libcpp = variant
return "com_llvm_lib{libcpp}_{arch}_{libc_version}".format(
arch = arch,
libc_version = libc_version.replace(".", "_"),
libcpp = "_libcpp" if use_libcpp else "",
)

llvm_variant_settings = _llvm_variant_settings
llvm_variant_setting_label = _llvm_variant_setting_label
llvm_variant_repo_name = _llvm_variant_repo_name
llvm_variants = _llvm_variants
19 changes: 8 additions & 11 deletions bazel/llvm_cmake.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
load("//bazel/cc_toolchains:llvm_libs.bzl", "llvm_variant_repo_name", "llvm_variant_setting_label", "llvm_variants")

def add_llvm_cache_entries(cache_entries):
return select({
"@px//bazel:use_libcpp": dict(
llvm_variant_setting_label(variant): dict(
cache_entries,
LLVM_ROOT = "$EXT_BUILD_ROOT/external/com_llvm_lib_libcpp",
Clang_ROOT = "$EXT_BUILD_ROOT/external/com_llvm_lib_libcpp",
),
"//conditions:default": dict(
cache_entries,
LLVM_ROOT = "$EXT_BUILD_ROOT/external/com_llvm_lib",
Clang_ROOT = "$EXT_BUILD_ROOT/external/com_llvm_lib",
),
LLVM_ROOT = "$EXT_BUILD_ROOT/external/" + llvm_variant_repo_name(variant),
Clang_ROOT = "$EXT_BUILD_ROOT/external/" + llvm_variant_repo_name(variant),
)
for variant in llvm_variants()
})

def llvm_build_data_deps():
return select({
"@px//bazel:use_libcpp": ["@com_llvm_lib_libcpp//:cmake"],
"//conditions:default": ["@com_llvm_lib//:cmake"],
llvm_variant_setting_label(variant): ["@{repo}//:cmake".format(repo = llvm_variant_repo_name(variant))]
for variant in llvm_variants()
})
4 changes: 2 additions & 2 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def _include_all_repo(name, **kwargs):
_http_archive_repo_impl(name, **kwargs)

def _com_llvm_lib():
_bazel_repo("com_llvm_lib", build_file = "//bazel/external:llvm.BUILD")
_bazel_repo("com_llvm_lib_libcpp", build_file = "//bazel/external:llvm.BUILD")
_bazel_repo("com_llvm_lib_x86_64_glibc_host", build_file = "//bazel/external:llvm.BUILD")
_bazel_repo("com_llvm_lib_libcpp_x86_64_glibc_host", build_file = "//bazel/external:llvm.BUILD")

def _cc_deps():
# Dependencies with native bazel build files.
Expand Down
4 changes: 2 additions & 2 deletions bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ REPOSITORY_LOCATIONS = dict(
sha256 = "fa1198d6fcfb25a43b3ce326f14a874177ede9df1315c2826556742324fc6428",
urls = ["https://storage.googleapis.com/pixie-dev-public/clang/15.0-pl8/clang-min-15.0-pl8.tar.gz"],
),
com_llvm_lib = dict(
com_llvm_lib_x86_64_glibc_host = dict(
sha256 = "9106b0694ec4f16e10425bab2357f861e32c3e49041f0bac51f56c5bba9ea966",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/clang/15.0-pl8/llvm-15.0-pl8.tar.gz"],
),
com_llvm_lib_libcpp = dict(
com_llvm_lib_libcpp_x86_64_glibc_host = dict(
sha256 = "041598998b833611efa0673c5a2795583257a4171b996ee50e2e130e60c439d7",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/clang/15.0-pl8/llvm-15.0-pl8-libcxx.tar.gz"],
Expand Down

0 comments on commit f5de1c0

Please sign in to comment.