Skip to content

Commit

Permalink
[build/sysroots] Add toolchains for sysroots.
Browse files Browse the repository at this point in the history
Summary:
Adds toolchains for each of runtime, build and test sysroots. This way the correct sysroot will be selected based on the target platform and settings.
Any rule that requires a particular sysroot can rely on the corresponding toolchain (runtime, build, or test).

Test Plan: Tested with future sysroot diffs. No changes to existing x86_64 build.

Reviewers: zasgar, michelle, vihang

Reviewed By: zasgar

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

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

GitOrigin-RevId: 998e1d22b47a3085c6c51b7e5591a4590898f99f
  • Loading branch information
JamesMBartlett authored and copybaranaut committed Jan 27, 2023
1 parent f9e4ee6 commit b2bf680
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 8 deletions.
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ load("//bazel:repositories.bzl", "pl_cc_toolchain_deps", "pl_deps")

pl_cc_toolchain_deps()

load("//bazel/cc_toolchains/sysroots:sysroots.bzl", "pl_sysroot_deps")

pl_sysroot_deps()

load("//bazel/cc_toolchains:toolchains.bzl", "pl_register_cc_toolchains")

pl_register_cc_toolchains()
Expand Down
23 changes: 16 additions & 7 deletions bazel/cc_toolchains/llvm_libs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@
# SPDX-License-Identifier: Apache-2.0

load("@bazel_skylib//lib:selects.bzl", "selects")
load("//bazel/cc_toolchains/sysroots:sysroots.bzl", "sysroot_architectures", "sysroot_libc_versions")

def _llvm_variants():
variants = []
for arch in sysroot_architectures:
libc_versions = sysroot_libc_versions
if arch == "x86_64":
libc_versions = libc_versions + ["glibc_host"]

for libc_version in libc_versions:
for use_libcpp in [True, False]:
sanitizers = ["none"]

# Only run sanitizers on host x86_64 build.
if use_libcpp and arch == "x86_64" and libc_version == "glibc_host":
sanitizers = sanitizers + ["asan", "msan", "tsan"]

for san in sanitizers:
variants.append((arch, libc_version, use_libcpp, san))

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

def _llvm_variant_settings():
Expand Down
3 changes: 2 additions & 1 deletion bazel/cc_toolchains/settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
# SPDX-License-Identifier: Apache-2.0

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("//bazel/cc_toolchains/sysroots:sysroots.bzl", "sysroot_libc_versions")

HOST_GLIBC_VERSION = "glibc_host"

def _settings():
libc_versions = [
libc_versions = sysroot_libc_versions + [
HOST_GLIBC_VERSION,
"static_musl",
]
Expand Down
15 changes: 15 additions & 0 deletions bazel/cc_toolchains/sysroots/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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
20 changes: 20 additions & 0 deletions bazel/cc_toolchains/sysroots/build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)
73 changes: 73 additions & 0 deletions bazel/cc_toolchains/sysroots/build/sysroot.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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("@px//bazel/cc_toolchains/sysroots:sysroots.bzl", "sysroot_toolchain")

filegroup(
name = "all_files",
srcs = [
":compiler_files",
":linker_files",
],
visibility = ["//visibility:public"],
)

filegroup(
name = "compiler_files",
srcs = glob([
"usr/local/include/**",
"usr/include/{target_arch}-linux-{abi}/**",
"usr/include/**",
"usr/include/c++/12/**",
"usr/include/{target_arch}-linux-{abi}/c++/12/**",
"usr/include/c++/12/backward/**",
# We have to include all of /usr/lib/gcc/target/version/ b/c otherwise clang doesn't select this folder as a gcc installation.
"usr/lib/gcc/{target_arch}-linux-{abi}/12/**",
]),
visibility = ["//visibility:public"],
)

filegroup(
name = "linker_files",
srcs = glob([
"lib/**",
"lib64/**",
"usr/lib/{target_arch}-linux-{abi}/**",
"usr/lib/gcc/{target_arch}-linux-{abi}/**",
]),
visibility = ["//visibility:public"],
)

sysroot_toolchain(
name = "sysroot_toolchain",
architecture = "{target_arch}",
files = ":all_files",
path = "{path_to_this_repo}",
)

toolchain(
name = "toolchain",
target_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:{target_arch}",
],
target_settings = [
"@px//bazel/cc_toolchains:libc_version_{libc_version}",
],
toolchain = ":sysroot_toolchain",
toolchain_type = "@px//bazel/cc_toolchains/sysroots/build:toolchain_type",
visibility = ["//visibility:public"],
)
20 changes: 20 additions & 0 deletions bazel/cc_toolchains/sysroots/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)
46 changes: 46 additions & 0 deletions bazel/cc_toolchains/sysroots/runtime/sysroot.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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("@px//bazel/cc_toolchains/sysroots:sysroots.bzl", "sysroot_toolchain")

filegroup(
name = "all_files",
srcs = glob([
"**",
]),
visibility = ["//visibility:public"],
)

sysroot_toolchain(
name = "sysroot_toolchain",
architecture = "{target_arch}",
files = ":all_files",
path = "{path_to_this_repo}",
)

toolchain(
name = "toolchain",
target_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:{target_arch}",
],
target_settings = [
"@px//bazel/cc_toolchains:libc_version_{libc_version}",
],
toolchain = ":sysroot_toolchain",
toolchain_type = "@px//bazel/cc_toolchains/sysroots/runtime:toolchain_type",
visibility = ["//visibility:public"],
)
136 changes: 136 additions & 0 deletions bazel/cc_toolchains/sysroots/sysroots.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# 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/cc_toolchains:utils.bzl", "abi")

SYSROOT_LOCATIONS = dict(
sysroot_x86_64_glibc2_36_runtime = dict(
sha256 = "85db7221f1334a802fb7f3651b2e27f596ddeeb0869d22678f0624cd9947b929",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/sysroots/pl1/sysroot-amd64-runtime.tar.gz"],
),
sysroot_x86_64_glibc2_36_build = dict(
sha256 = "150d0592aca950668330c01981fccef37ab2ac990fd66a5ec36359d54e95a13c",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/sysroots/pl1/sysroot-amd64-build.tar.gz"],
),
sysroot_x86_64_glibc2_36_test = dict(
sha256 = "a4e4dcfbfecef8710576fd977fb7f3d3a6bda4ebca526b411e4fda78870593d1",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/sysroots/pl1/sysroot-amd64-test.tar.gz"],
),
sysroot_aarch64_glibc2_36_runtime = dict(
sha256 = "84d102939126a557dcb6bad2d0c4a86f526b09f0e247b35544b9975a12aa7a98",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/sysroots/pl1/sysroot-arm64-runtime.tar.gz"],
),
sysroot_aarch64_glibc2_36_build = dict(
sha256 = "e61c8fa3e58a0f3b83145a807b13401cfe693c0672097cbbe125e68d92e7b0e9",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/sysroots/pl1/sysroot-arm64-build.tar.gz"],
),
sysroot_aarch64_glibc2_36_test = dict(
sha256 = "2bdd9a21d3b6a1ba05e63fa28235867be053748963fbe617376ad5b8d3592e4b",
strip_prefix = "",
urls = ["https://storage.googleapis.com/pixie-dev-public/sysroots/pl1/sysroot-arm64-test.tar.gz"],
),
)

_sysroot_architectures = ["aarch64", "x86_64"]
_sysroot_libc_versions = ["glibc2_36"]
_sysroot_variants = ["runtime", "build", "test"]

def _sysroot_repo_name(target_arch, libc_version, variant):
name = "sysroot_{target_arch}_{libc_version}_{variant}".format(
target_arch = target_arch,
libc_version = libc_version,
variant = variant,
)
if name in SYSROOT_LOCATIONS:
return name
return ""

def _sysroot_repo_impl(rctx):
loc = SYSROOT_LOCATIONS[rctx.attr.name]
rctx.download_and_extract(
url = loc["urls"],
sha256 = loc["sha256"],
stripPrefix = loc.get("strip_prefix", ""),
)
rctx.template(
"BUILD.bazel",
Label("@px//bazel/cc_toolchains/sysroots/{variant}:sysroot.BUILD".format(variant = rctx.attr.variant)),
substitutions = {
"{abi}": abi(rctx.attr.target_arch, rctx.attr.libc_version),
"{libc_version}": rctx.attr.libc_version,
"{path_to_this_repo}": "external/" + rctx.attr.name,
"{target_arch}": rctx.attr.target_arch,
},
)

_sysroot_repo = repository_rule(
implementation = _sysroot_repo_impl,
attrs = {
"libc_version": attr.string(mandatory = True, doc = "Libc version of the sysroot"),
"target_arch": attr.string(mandatory = True, doc = "CPU Architecture of the sysroot"),
"variant": attr.string(mandatory = True, doc = "Use case variant of the sysroot. One of 'runtime', 'build', or 'test'"),
},
)

SysrootInfo = provider(
doc = "Information about a sysroot.",
fields = ["files", "architecture", "path"],
)

def _sysroot_toolchain_impl(ctx):
return [
platform_common.ToolchainInfo(
sysroot = SysrootInfo(
files = ctx.attr.files.files,
architecture = ctx.attr.architecture,
path = ctx.attr.path,
),
),
]

sysroot_toolchain = rule(
implementation = _sysroot_toolchain_impl,
attrs = {
"architecture": attr.string(mandatory = True, doc = "CPU architecture targeted by this sysroot"),
"files": attr.label(mandatory = True, doc = "All sysroot files"),
"path": attr.string(mandatory = True, doc = "Path to sysroot relative to execroot"),
},
)

def _pl_sysroot_deps():
toolchains = []
for target_arch in _sysroot_architectures:
for libc_version in _sysroot_libc_versions:
for variant in _sysroot_variants:
repo = _sysroot_repo_name(target_arch, libc_version, variant)
_sysroot_repo(
name = repo,
target_arch = target_arch,
libc_version = libc_version,
variant = variant,
)
toolchains.append("@{repo}//:toolchain".format(repo = repo))
native.register_toolchains(*toolchains)

sysroot_repo_name = _sysroot_repo_name
sysroot_libc_versions = _sysroot_libc_versions
sysroot_architectures = _sysroot_architectures
pl_sysroot_deps = _pl_sysroot_deps
20 changes: 20 additions & 0 deletions bazel/cc_toolchains/sysroots/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)
Loading

0 comments on commit b2bf680

Please sign in to comment.