Go to file
Kevin Lubick c4872ce644 [bazel] Add support for Macs to make Linux RBE builds
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>
2022-06-23 12:00:43 +00:00
bazel [bazel] Add support for Macs to make Linux RBE builds 2022-06-23 12:00:43 +00:00
bench [graphite] Add HybridBoundsManager and use as default impl 2022-06-21 15:52:22 +00:00
bin Update SKPs documentation link 2022-05-18 15:21:44 +00:00
build/fuchsia [fuchsia] Fix fidlc command. 2020-06-10 03:29:24 +00:00
build_overrides Set dawn_vulkan_tools_dir in Dawn's GN overrides 2022-04-14 17:05:33 +00:00
client_utils/android Revert "Move SkCamera.h to client_utils/android" 2022-04-25 12:33:01 +00:00
demos.skia.org [canvaskit] Add in fillText to spreadsheet demo for comparison 2022-02-15 15:35:43 +00:00
dm [graphite] Use CombinationBuilder to create SkPaintParamsKeys directly 2022-06-16 17:16:30 +00:00
docker Update Perfetto reference following fix for b/234001590 2022-06-16 15:16:55 +00:00
docs/examples Replace SK_ARRAY_COUNT with std::size() for skia/docs 2022-06-16 20:09:58 +00:00
example reland "set up GL sample app to build through Bazel Mac toolchain" 2022-06-21 19:22:16 +00:00
experimental [bazel] Add "skia_internal" target that exposes private API for tests/tools. 2022-06-02 11:23:50 +00:00
fuzz Remove SkMakeSpan 2022-06-15 17:26:32 +00:00
gm Replace SK_ARRAY_COUNT with std::size() for skia/gm 2022-06-21 18:09:30 +00:00
gn Reland "Move SkSubRun to src/text" 2022-06-18 17:20:42 +00:00
include Deprecate SK_ARRAY_COUNT and docuemnt the old and new methods 2022-06-21 18:01:49 +00:00
infra [bazel] Add support for Macs to make Linux RBE builds 2022-06-23 12:00:43 +00:00
modules [bazel] Add support for Macs to make Linux RBE builds 2022-06-23 12:00:43 +00:00
platform_tools Fix bug where SkQP test that are always to be excluded are not. 2022-04-29 20:26:54 +00:00
resources Added preliminary SkSL Metal compute shader support 2022-06-15 18:52:42 +00:00
samplecode Replace SK_ARRAY_COUNT with std::size() for skia/samplecode 2022-06-22 14:02:56 +00:00
site reland "set up GL sample app to build through Bazel Mac toolchain" 2022-06-21 19:22:16 +00:00
specs [infra] Port serve.py from Python2 to 3 2021-08-31 15:28:19 +00:00
src [graphite] Fix assert in Viewer when displaying anisomips GM. 2022-06-22 21:39:29 +00:00
tests Add support for int-typed data payloads. 2022-06-22 03:06:16 +00:00
third_party Reland "Add Perfetto library (gn & bazel) and bare-bones SkPerfTrace class" 2022-06-15 16:35:05 +00:00
toolchain [bazel] Add support for Macs to make Linux RBE builds 2022-06-23 12:00:43 +00:00
tools [graphite] Fix assert in Viewer when displaying anisomips GM. 2022-06-22 21:39:29 +00:00
.bazelrc [bazel] Add support for Macs to make Linux RBE builds 2022-06-23 12:00:43 +00:00
.bazelversion Reland "[bazel] Update to v5.2.0" 2022-06-21 18:49:10 +00:00
.clang-format no more bin packing in .clang-format 2021-03-04 17:22:50 +00:00
.clang-tidy Disable ClangTidy namespace comments for short blocks. 2020-08-24 14:01:22 +00:00
.gitignore [infra] Add BazelBuild task to build CanvasKit on the CI with Bazel 2022-05-06 17:54:08 +00:00
.gn Disable wayland in ANGLE 2022-06-15 18:24:40 +00:00
.vpython [python3] Reland recipes -> python3 changes 2021-12-01 13:04:03 +00:00
AUTHORS Add mike@reedtribe.org to AUTHORS 2022-05-19 12:49:11 +00:00
BUILD.bazel [bazel] Add "skia_internal" target that exposes private API for tests/tools. 2022-06-02 11:23:50 +00:00
BUILD.gn [skottie_tool] GPU backend support 2022-06-22 20:17:13 +00:00
codereview.settings Make uploading to Gerrit the default for Skia 2016-11-09 19:07:56 +00:00
CONTRIBUTING Fix references to https://sites.google.com/site/skiadocs/. 2015-02-03 13:12:54 -02:00
CQ_COMMITTERS Moved committer list to chrome-infra-auth and deleted it from the repo 2015-09-02 13:37:54 -07:00
DEPS Roll vulkan-deps from 87af9564b246 to 470172656108 (1 revision) 2022-06-23 07:03:13 +00:00
DIR_METADATA Move metadata in OWNERS files to DIR_METADATA files 2021-02-02 23:41:54 +00:00
go_repositories.bzl [infra] Update dep of infra repo 2022-04-28 13:20:10 +00:00
go.mod [infra] Update dep of infra repo 2022-04-28 13:20:10 +00:00
go.sum [infra] Fix go.sum 2022-04-28 18:01:34 +00:00
LICENSE LICENSE file: clean up 2019-11-22 21:59:03 +00:00
OWNERS [infra] Add wildcard OWNERS 2021-08-30 19:19:34 +00:00
OWNERS.android Update OWNERS.android with more details 2022-03-16 18:13:33 +00:00
package-lock.json [bazel] Make custom karma_test rule 2022-02-23 14:53:01 +00:00
package.json [bazel] Make custom karma_test rule 2022-02-23 14:53:01 +00:00
PRESUBMIT.py [bazel] Add support for Macs to make Linux RBE builds 2022-06-23 12:00:43 +00:00
public.bzl Reland "Move SkSubRun to src/text" 2022-06-18 17:20:42 +00:00
README Fix references to https://sites.google.com/site/skiadocs/. 2015-02-03 13:12:54 -02:00
README.chromium Update README.chromium. 2015-06-11 13:19:24 -07:00
RELEASE_NOTES.txt Add SkSurfaceProps parameter to several APIs 2022-06-02 13:12:33 +00:00
requirements.txt Added expected hash for MarkupSafe for the MacOS platform with the M1 chip 2022-05-19 17:54:32 +00:00
whitespace.txt Push shaders.skia.org. 2022-01-19 21:46:16 +00:00
WORKSPACE.bazel [bazel] Add support for Macs to make Linux RBE builds 2022-06-23 12:00:43 +00:00

Skia is a complete 2D graphic library for drawing Text, Geometries, and Images.

See full details, and build instructions, at https://skia.org.