c4872ce644
The big change here is having the C++ toolchain use
Bazel platforms instead of the C++ specific flags/setup.
In Bazel, platforms are a general purpose way to define
things like os, cpu architecture, etc. We were not using
platforms previously, because the best documentation at
the time focused on the old ways.
However, the old ways were clumsy/difficult when trying
to manage cross-compilation, specifically when trying
to have a Mac host trigger a build on our Linux RBE
system targeting a Linux x64 system. Thus, rather than
keep investing in the legacy system, this CL migrates
us to using platforms where possible.
Suggested background reading to better understand this CL:
- https://bazel.build/concepts/platforms-intro
- https://bazel.build/docs/platforms
- https://bazel.build/docs/toolchains#registering-building-toolchains
The hermetic toolchain itself is not changing in this CL
(and likely does not need to), only how we tell Bazel
about it (i.e. registering it) and how Bazel decides
to use it (i.e. resolving toolchains).
Here is my understanding of how platforms and toolchains
interact (supported by some evidence from [1][2])
- Bazel needs to resolve platforms for the Host, Execution,
and Target.
- If not specified via flags, these are the machine from
which Bazel is invoked, aka "@local_config_platform//:host".
- With this CL, the Host could be a Mac laptop, the Execution
platform is our Linux RBE pool, and the Target is "a Linux
system with a x64 CPU"
- To specify the Host, that is, describe to Bazel the
capabilities of the system it is running on, one can
set --host_platform [3] with a label pointing to a platform()
containing the appropriate settings. Tip: have this
platform inherit from @local_config_platform//:host
so it can add to any of the constraint_settings and
constraint_values that Bazel deduces automatically.
- To specify the Target platform(s), that is, the system
on which a final output resides and can execute, one
can set the --platforms flag with a label referencing
a platform().
- Bazel will then choose an execution platform to fulfill
that request. Bazel will look through a list of available
platforms, which can be augmented* with the
--extra_execution_platforms. Platforms specified by this
flag will be considered higher than the default platforms!
- Having selected the appropriate platforms, Bazel now
needs to select a toolchain to actually run the actions
of the appropriate type.
- Bazel looks through the list of available toolchains
and finds one that "matches" the Execution and the Target
platform. This means, the toolchain's exec_compatible_with
is a strict subset of the Execution platform and
the toolchain's target_compatible_with is a strict subset
of the Target platform. To register toolchains* (i.e. add
them to the resolution list), we use --extra_toolchains.
Once Bazel finds a match, it stops looking.
Using --toolchain_resolution_debug=".*" makes Bazel log
how it is resolving these toolchains and what execution
platform it picked.
* We can also register execution platforms and toolchains in
WORKSPACE.bazel [4], but the flags come with higher priority
and that made resolution a bit tricky. Also, when we want
to conditionally add them (e.g. --config=linux_rbe), we
cannot remove them conditionally in the WORKSPACE.bazel file.
The above resolution flow directly necessitated the changes
in this CL.
Example usage of the new configs and platforms:
# Can be run on a x64 Linux host and uses the hermetic toolchain.
bazel build //:skia_public
# Can be run on Mac or Linux and uses the Linux RBE system along
# with the hermetic toolchain to compile a binary for Linux x64.
bazel build //:skia_public --config=linux_rbe --config=for_linux_x64
# Shorthand for above
bazel build //:skia_public --config=for_linux_x64_with_rbe
Notice we don't have to type out --config=clang_linux anymore!
That was due to me reading the Bazel docs more carefully and
realizing we can set options for *all* Bazel build commands.
Current Limitations:
- Targets which require a py_binary (e.g. Dawn's genrules)
will not work on RBE when cross compiling because the
python runtime we download is for the host machine, not
the executor. This means //example:hello_world_dawn does
not work on Mac when cross-compiling via linux_rbe.
- Mac M1 linking not quite working with SkOpts settings.
Probably need to set -target [5]
Suggested Review order:
- toolchain/BUILD.bazel Notice how we do away with
cc_toolchain_suite for toolchain. These have the same
role: giving Bazel the information about where a toolchain
can run. The platforms one is more expressive (IMO), allowing
us to say both where to run the toolchain and what it can
make. In order to more easily force the use of our hermetic
toolchain, but also allow the hermetic toolchain to be used
on RBE, we specify "use_hermetic_toolchain" only on the target,
because the RBE image does not have the hermetic toolchain
on it by default (but can certainly run it).
- bazel/platform/BUILD.bazel to see the custom constraint_setting
and corresponding constraint_value. The names for both of these
are completely arbitrary - they do not need to have any deeper
meaning or relation to any file or Docker image or system or
any other constraints. Think of the constraint_setting as
an Enum and the constraint_value being the one and only member.
We need to pass around a constant value, not a type, so we
need to provide the constraint_value (e.g. in toolchain/BUILD.bazel)
but not a constraint_setting. However we need a
constraint_setting declared so we can make a constraint_value
of that "type".
Notice the platform declared here - it allows us to force
Bazel to use the hermetic toolchain because of the extra
constraint_value.
- .bazelrc I set a few flags that will be on for all
bazel build commands. Importantly, this causes the C++
build logic to use platforms and not the old, bespoke way.
I also found a way to avoid using the local toolchain on
the host, which will hopefully lead to clearer errors
if platforms are mis-specified instead of odd compile
errors because the host toolchain is too old or something.
There are also a few RBE settings tweaked to be a bit
more modern, as well the new shorthands for specifying
target platforms (e.g. for_linux_x64).
- bazel/buildrc where we have to turn off the platforms
logic for emscripten https://github.com/emscripten-core/emsdk/issues/984
- bazel/rbe/BUILD.bazel for a fix in the platform description
that makes it work on Mac.
- Notice that _m1 has been removed from the mac-related toolchain
files because the same toolchain should work on both
architectures.
- All other changes in any order.
[1] https://bazel.build/docs/toolchains#debugging-toolchains
[2] https://bazel.build/docs/toolchains#toolchain-resolution
[3] https://bazel.build/reference/command-line-reference
[4] https://bazel.build/docs/toolchains#registering-building-toolchains
[5] 17dc3f16fc/gn/skia/BUILD.gn (L258-L271)
Change-Id: I515c114099d659639a808f74e47d489a68b7af62
Bug: skia:12541
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/549737
Reviewed-by: Erik Rose <erikrose@google.com>
Reviewed-by: Jorge Betancourt <jmbetancourt@google.com>
290 lines
10 KiB
Plaintext
290 lines
10 KiB
Plaintext
workspace(name = "skia")
|
|
|
|
load("//toolchain:download_toolchains.bzl", "download_toolchains_for_skia")
|
|
|
|
download_toolchains_for_skia("clang_linux_amd64", "clang_mac")
|
|
|
|
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
|
|
|
# See https://github.com/emscripten-core/emsdk/tree/85d27a4a2a60d591613a305b14ae438c2bb3ce11/bazel#setup-instructions
|
|
http_archive(
|
|
name = "emsdk",
|
|
sha256 = "99de90a1827044c5430b926db947bcb266c8bdab1a0f20fe34a62431d9174376",
|
|
strip_prefix = "emsdk-3.1.9/bazel",
|
|
urls = [
|
|
"https://github.com/emscripten-core/emsdk/archive/refs/tags/3.1.9.tar.gz",
|
|
"https://storage.googleapis.com/skia-world-readable/bazel/99de90a1827044c5430b926db947bcb266c8bdab1a0f20fe34a62431d9174376.tar.gz",
|
|
],
|
|
)
|
|
|
|
load("@emsdk//:deps.bzl", emsdk_deps = "deps")
|
|
|
|
# One of the deps here is build_bazel_rules_nodejs, currently version 4.4.1
|
|
# If we try to install it ourselves after this, it won't work.
|
|
emsdk_deps()
|
|
|
|
load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
|
|
|
|
emsdk_emscripten_deps(emscripten_version = "3.1.9")
|
|
|
|
http_archive(
|
|
name = "bazel_skylib",
|
|
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
|
|
urls = [
|
|
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
|
|
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
|
|
],
|
|
)
|
|
|
|
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
|
|
|
|
bazel_skylib_workspace()
|
|
|
|
http_archive(
|
|
name = "bazel_toolchains",
|
|
sha256 = "e52789d4e89c3e2dc0e3446a9684626a626b6bec3fde787d70bae37c6ebcc47f",
|
|
strip_prefix = "bazel-toolchains-5.1.1",
|
|
urls = [
|
|
"https://github.com/bazelbuild/bazel-toolchains/archive/refs/tags/v5.1.1.tar.gz",
|
|
"https://storage.googleapis.com/skia-world-readable/bazel/e52789d4e89c3e2dc0e3446a9684626a626b6bec3fde787d70bae37c6ebcc47f.tar.gz",
|
|
],
|
|
)
|
|
|
|
load("@bazel_toolchains//repositories:repositories.bzl", bazel_toolchains_repositories = "repositories")
|
|
|
|
bazel_toolchains_repositories()
|
|
|
|
#######################################################################################
|
|
# Python
|
|
#######################################################################################
|
|
|
|
# https://github.com/bazelbuild/rules_python
|
|
http_archive(
|
|
name = "rules_python",
|
|
sha256 = "9fcf91dbcc31fde6d1edb15f117246d912c33c36f44cf681976bd886538deba6",
|
|
strip_prefix = "rules_python-0.8.0",
|
|
urls = [
|
|
"https://github.com/bazelbuild/rules_python/archive/refs/tags/0.8.0.tar.gz",
|
|
"https://storage.googleapis.com/skia-world-readable/bazel/9fcf91dbcc31fde6d1edb15f117246d912c33c36f44cf681976bd886538deba6.tar.gz",
|
|
],
|
|
)
|
|
|
|
# This sets up a hermetic python3, rather than depending on what is installed.
|
|
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
|
|
|
|
python_register_toolchains(
|
|
name = "python3_9",
|
|
# https://github.com/bazelbuild/rules_python/blob/main/python/versions.bzl
|
|
python_version = "3.9",
|
|
)
|
|
|
|
load("@python3_9//:defs.bzl", "interpreter")
|
|
load("@rules_python//python:pip.bzl", "pip_install")
|
|
|
|
pip_install(
|
|
name = "py_deps",
|
|
python_interpreter_target = interpreter,
|
|
requirements = "//:requirements.txt",
|
|
)
|
|
|
|
#######################################################################################
|
|
# Gazelle
|
|
#######################################################################################
|
|
http_archive(
|
|
name = "io_bazel_rules_go",
|
|
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
|
|
urls = [
|
|
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
|
|
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
|
|
],
|
|
)
|
|
|
|
http_archive(
|
|
name = "bazel_gazelle",
|
|
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
|
|
urls = [
|
|
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
|
|
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
|
|
],
|
|
)
|
|
|
|
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
|
|
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
|
|
load("//:go_repositories.bzl", "go_repositories")
|
|
|
|
# gazelle:repository_macro go_repositories.bzl%go_repositories
|
|
go_repositories()
|
|
|
|
go_rules_dependencies()
|
|
|
|
go_register_toolchains(version = "1.17.2")
|
|
|
|
gazelle_dependencies(go_repository_default_config = "//:WORKSPACE.bazel")
|
|
|
|
# Because the skia infra repo has a dependency on google.golang.org/grpc (aka
|
|
# @org_golang_google_grpc), we need to have this library available to build protos.
|
|
# https://github.com/bazelbuild/rules_go#protobuf-and-grpc
|
|
http_archive(
|
|
name = "com_google_protobuf",
|
|
sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
|
|
strip_prefix = "protobuf-3.14.0",
|
|
urls = [
|
|
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
|
|
"https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
|
|
],
|
|
)
|
|
|
|
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
|
|
|
|
protobuf_deps()
|
|
|
|
###################################################
|
|
# JavaScript / TypeScript rules and dependencies. #
|
|
###################################################
|
|
|
|
# The npm_install rule runs anytime the package.json or package-lock.json file changes. It also
|
|
# extracts any Bazel rules distributed in an npm package.
|
|
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
|
|
|
|
# Manages the node_modules directory.
|
|
npm_install(
|
|
name = "npm",
|
|
package_json = "//:package.json",
|
|
package_lock_json = "//:package-lock.json",
|
|
)
|
|
|
|
# io_bazel_rules_webtesting allows us to download browsers in a hermetic, repeatable way. This
|
|
# currently includes Chromium and Firefox. Note that the version on this does not necessarily
|
|
# match the version below of the browsers-X.Y.Z below that is available.
|
|
http_archive(
|
|
name = "io_bazel_rules_webtesting",
|
|
sha256 = "e9abb7658b6a129740c0b3ef6f5a2370864e102a5ba5ffca2cea565829ed825a",
|
|
urls = [
|
|
"https://github.com/bazelbuild/rules_webtesting/releases/download/0.3.5/rules_webtesting.tar.gz",
|
|
"https://storage.googleapis.com/skia-world-readable/bazel/e9abb7658b6a129740c0b3ef6f5a2370864e102a5ba5ffca2cea565829ed825a.tar.gz",
|
|
],
|
|
)
|
|
|
|
# https://github.com/bazelbuild/rules_webtesting/blob/e9cf17123068b1123c68219edf9b274bf057b9cc/web/versioned/browsers-0.3.3.bzl
|
|
load("@io_bazel_rules_webtesting//web/versioned:browsers-0.3.3.bzl", "browser_repositories")
|
|
|
|
browser_repositories(
|
|
chromium = True,
|
|
firefox = True,
|
|
)
|
|
|
|
###################################################
|
|
# External C++ deps with Bazel support #
|
|
###################################################
|
|
# https://bazel.build/reference/be/workspace#local_repository
|
|
local_repository(
|
|
name = "abseil_cpp",
|
|
path = "third_party/externals/abseil-cpp",
|
|
)
|
|
|
|
local_repository(
|
|
name = "spirv_tools",
|
|
path = "third_party/externals/spirv-tools",
|
|
)
|
|
|
|
local_repository(
|
|
# This name is important because spirv_tools expects @spirv_headers to exist by that name.
|
|
name = "spirv_headers",
|
|
path = "third_party/externals/spirv-headers",
|
|
)
|
|
|
|
###############################################################################
|
|
# External C++ deps with NO Bazel support #
|
|
# In this case, we need to supply a BUILD file which exposes targets/files #
|
|
# we need. If these deps depend on other deps (e.g. @dawn -> @spirv_tools), #
|
|
# then we need to specify those here as well. The workspace_file_content is #
|
|
# generally ignored; external repos (i.e. the ones starting with @) will be #
|
|
# resolved with Skia's version of those repos, which is how Bazel avoids #
|
|
# multiple versions of the same project being compiled together. #
|
|
###############################################################################
|
|
# https://bazel.build/reference/be/workspace#new_local_repository
|
|
new_local_repository(
|
|
name = "dawn",
|
|
build_file = "bazel/external/dawn/BUILD.bazel",
|
|
path = "third_party/externals/dawn",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "dng_sdk",
|
|
build_file = "bazel/external/dng_sdk/BUILD.bazel",
|
|
path = "third_party/externals/dng_sdk",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "libjpeg_turbo",
|
|
build_file = "bazel/external/libjpeg_turbo/BUILD.bazel",
|
|
path = "third_party/externals/libjpeg-turbo",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "libpng",
|
|
build_file = "bazel/external/libpng/BUILD.bazel",
|
|
path = "third_party/externals/libpng",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "libwebp",
|
|
build_file = "bazel/external/libwebp/BUILD.bazel",
|
|
path = "third_party/externals/libwebp",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "perfetto",
|
|
build_file = "bazel/external/perfetto/BUILD.bazel",
|
|
path = "third_party/externals/perfetto",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "piex",
|
|
build_file = "bazel/external/piex/BUILD.bazel",
|
|
path = "third_party/externals/piex",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "vulkan_headers",
|
|
build_file = "bazel/external/vulkan_headers/BUILD.bazel",
|
|
path = "third_party/externals/vulkan-headers",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "vulkan_tools",
|
|
build_file = "bazel/external/vulkan_tools/BUILD.bazel",
|
|
path = "third_party/externals/vulkan-tools",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "vulkanmemoryallocator",
|
|
build_file = "bazel/external/vulkanmemoryallocator/BUILD.bazel",
|
|
path = "third_party/externals/vulkanmemoryallocator",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
name = "wuffs",
|
|
build_file = "bazel/external/wuffs/BUILD.bazel",
|
|
path = "third_party/externals/wuffs",
|
|
workspace_file_content = "",
|
|
)
|
|
|
|
new_local_repository(
|
|
# Some other dependency downloads zlib but with their own rules
|
|
name = "zlib_skia",
|
|
build_file = "bazel/external/zlib/BUILD.bazel",
|
|
path = "third_party/externals/zlib",
|
|
workspace_file_content = "",
|
|
)
|