blob: 67b197bd2582e734df8fd181ac6ed1d92a446ec9 [file] [log] [blame] [view]
jcater89095eb2017-11-03 17:46:21 +01001---
2layout: documentation
3title: Platforms
4---
5
6# Platforms
7
spomorski5b406602017-11-03 22:05:53 +01008- [Overview](#overview)
brandjonb9a5f562018-10-02 14:53:02 -07009- [Defining constraints and platforms](#defining-constraints-and-platforms)
spomorski5b406602017-11-03 22:05:53 +010010- [Built-in constraints and platforms](#built-in-constraints-and-platforms)
11- [Specifying a platform for a build](#specifying-a-platform-for-a-build)
jcater89095eb2017-11-03 17:46:21 +010012
spomorski5b406602017-11-03 22:05:53 +010013## Overview
jcater89095eb2017-11-03 17:46:21 +010014
brandjonb9a5f562018-10-02 14:53:02 -070015Bazel can build and test code on a variety of hardware, operating systems, and
16system configurations, using many different versions of build tools such as
17linkers and compilers. To help manage this complexity, Bazel has a concept of
18*constraints* and *platforms*. A constraint is a dimension in which build or
19production environments may differ, such as CPU architecture, the presence or
20absence of a GPU, or the version of a system-installed compiler. A platform is a
21named collection of choices for these constraints, representing the particular
22resources that are available in some environment.
jcater89095eb2017-11-03 17:46:21 +010023
brandjonb9a5f562018-10-02 14:53:02 -070024Modeling the environment as a platform helps Bazel to automatically select the
25appropriate
26[toolchains](toolchains.html)
27for build actions. Platforms can also be used in combination with the
28[config_setting](be/general.html#config_setting)
29rule to write
John Cater17947032018-11-06 07:04:33 -080030<a href="configurable-attributes.html"> configurable attributes</a>.
jcater89095eb2017-11-03 17:46:21 +010031
brandjonb9a5f562018-10-02 14:53:02 -070032Bazel recognizes three roles that a platform may serve:
33
34* **Host** - the platform on which Bazel itself runs.
35* **Execution** - a platform on which build tools execute build actions to
36 produce intermediate and final outputs.
37* **Target** - a platform on which a final output resides and executes.
jcater89095eb2017-11-03 17:46:21 +010038
spomorski5b406602017-11-03 22:05:53 +010039Bazel supports the following build scenarios regarding platforms:
jcater89095eb2017-11-03 17:46:21 +010040
spomorski5b406602017-11-03 22:05:53 +010041* **Single-platform builds** (default) - host, execution, and target platforms
42 are the same. For example, building a Linux executable on Ubuntu running on
43 an Intel x64 CPU.
jcater89095eb2017-11-03 17:46:21 +010044
spomorski5b406602017-11-03 22:05:53 +010045* **Cross-compilation builds** - host and execution platforms are the same, but
46 the target platform is different. For example, building an iOS app on macOS
47 running on a MacBook Pro.
jcater89095eb2017-11-03 17:46:21 +010048
spomorski5b406602017-11-03 22:05:53 +010049* **Multi-platform builds** - host, execution, and target platforms are all
50 different.
jcater89095eb2017-11-03 17:46:21 +010051
brandjonb9a5f562018-10-02 14:53:02 -070052## Defining constraints and platforms
jcater89095eb2017-11-03 17:46:21 +010053
brandjonb9a5f562018-10-02 14:53:02 -070054The space of possible choices for platforms is defined by using the
55 [`constraint_setting`](be/platform.html#constraint_setting) and
56 [`constraint_value`](be/platform.html#constraint_value) rules within `BUILD` files. `constraint_setting` creates a new dimension, while
57`constraint_value` creates a new value for a given dimension; together they
58effectively define an enum and its possible values. For example, the following
59snippet of a `BUILD` file introduces a constraint for the system's glibc version
60with two possible values.
jcater89095eb2017-11-03 17:46:21 +010061
spomorski5b406602017-11-03 22:05:53 +010062```python
brandjonb9a5f562018-10-02 14:53:02 -070063constraint_setting(name = "glibc_version")
jcater89095eb2017-11-03 17:46:21 +010064
65constraint_value(
brandjonb9a5f562018-10-02 14:53:02 -070066 name = "glibc_2_25",
67 constraint_setting = ":glibc_version",
68)
jcater89095eb2017-11-03 17:46:21 +010069
70constraint_value(
brandjonb9a5f562018-10-02 14:53:02 -070071 name = "glibc_2_26",
72 constraint_setting = ":glibc_version",
73)
jcater89095eb2017-11-03 17:46:21 +010074```
75
brandjonb9a5f562018-10-02 14:53:02 -070076Constraints and their values may be defined across different packages in the
77workspace. They are referenced by label and subject to the usual visibility
78controls. If visibility allows, you can extend an existing constraint setting by
79defining your own value for it.
jcater89095eb2017-11-03 17:46:21 +010080
brandjonb9a5f562018-10-02 14:53:02 -070081The
82 [`platform`](be/platform.html#platform) rule introduces a new platform with certain choices of constraint values. The
83following creates a platform named `linux_x86`, and says that it describes any
84environment that runs a Linux operating system on an x86_64 architecture with a
85glibc version of 2.25. (See below for more on Bazel's built-in constraints.)
jcater89095eb2017-11-03 17:46:21 +010086
brandjonb9a5f562018-10-02 14:53:02 -070087```python
88platform(
89 name = "linux_x86",
90 constraint_values = [
hlopko71a213c2019-06-21 02:01:33 -070091 "@platforms//os:linux",
92 "@platforms//cpu:x86_64",
brandjonb9a5f562018-10-02 14:53:02 -070093 ":glibc_2_25",
94 ],
95)
96```
jcater89095eb2017-11-03 17:46:21 +010097
brandjonb9a5f562018-10-02 14:53:02 -070098Note that it is an error for a platform to specify more than one value of the
hlopko71a213c2019-06-21 02:01:33 -070099same constraint setting, such as `@platforms//cpu:x86_64` and
100`@platforms//cpu:arm` for `@platforms//cpu:cpu`.
jcater89095eb2017-11-03 17:46:21 +0100101
jcater89095eb2017-11-03 17:46:21 +0100102
Marcel Hlopkob4f3d832019-07-01 06:39:16 -0700103## Generally useful constraints and platforms
jcater89095eb2017-11-03 17:46:21 +0100104
Marcel Hlopkob4f3d832019-07-01 06:39:16 -0700105To keep the ecosystem consistent, Bazel team maintains a repository with
106constraint definitions for the most popular CPU architectures and operating
107systems. These are all located in
108[https://github.com/bazelbuild/platforms](https://github.com/bazelbuild/platforms).
jcater89095eb2017-11-03 17:46:21 +0100109
Marcel Hlopkob4f3d832019-07-01 06:39:16 -0700110Bazel ships with the following special platform definition:
111`@local_config_platform//:host`. This is the autodetected host platform value -
112represents autodetected platform for the system Bazel is running on.
jcater89095eb2017-11-03 17:46:21 +0100113
spomorski5b406602017-11-03 22:05:53 +0100114## Specifying a platform for a build
jcater89095eb2017-11-03 17:46:21 +0100115
brandjonb9a5f562018-10-02 14:53:02 -0700116You can specify the host and target platforms for a build using the following
spomorski5b406602017-11-03 22:05:53 +0100117command-line flags:
jcater89095eb2017-11-03 17:46:21 +0100118
John Cater283666d2018-01-03 09:19:35 -0800119* `--host_platform` - defaults to `@bazel_tools//platforms:host_platform`
jcater89095eb2017-11-03 17:46:21 +0100120
John Cater283666d2018-01-03 09:19:35 -0800121* `--platforms` - defaults to `@bazel_tools//platforms:target_platform`