jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | title: Platforms |
| 4 | --- |
| 5 | |
| 6 | # Platforms |
| 7 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 8 | - [Overview](#overview) |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 9 | - [Defining constraints and platforms](#defining-constraints-and-platforms) |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 10 | - [Built-in constraints and platforms](#built-in-constraints-and-platforms) |
| 11 | - [Specifying a platform for a build](#specifying-a-platform-for-a-build) |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 12 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 13 | ## Overview |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 14 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 15 | Bazel can build and test code on a variety of hardware, operating systems, and |
| 16 | system configurations, using many different versions of build tools such as |
| 17 | linkers 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 |
| 19 | production environments may differ, such as CPU architecture, the presence or |
| 20 | absence of a GPU, or the version of a system-installed compiler. A platform is a |
| 21 | named collection of choices for these constraints, representing the particular |
| 22 | resources that are available in some environment. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 23 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 24 | Modeling the environment as a platform helps Bazel to automatically select the |
| 25 | appropriate |
| 26 | [toolchains](toolchains.html) |
| 27 | for build actions. Platforms can also be used in combination with the |
| 28 | [config_setting](be/general.html#config_setting) |
| 29 | rule to write |
John Cater | 1794703 | 2018-11-06 07:04:33 -0800 | [diff] [blame] | 30 | <a href="configurable-attributes.html"> configurable attributes</a>. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 31 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 32 | Bazel 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. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 38 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 39 | Bazel supports the following build scenarios regarding platforms: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 40 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 41 | * **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. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 44 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 45 | * **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. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 48 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 49 | * **Multi-platform builds** - host, execution, and target platforms are all |
| 50 | different. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 51 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 52 | ## Defining constraints and platforms |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 53 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 54 | The 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 |
| 58 | effectively define an enum and its possible values. For example, the following |
| 59 | snippet of a `BUILD` file introduces a constraint for the system's glibc version |
| 60 | with two possible values. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 61 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 62 | ```python |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 63 | constraint_setting(name = "glibc_version") |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 64 | |
| 65 | constraint_value( |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 66 | name = "glibc_2_25", |
| 67 | constraint_setting = ":glibc_version", |
| 68 | ) |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 69 | |
| 70 | constraint_value( |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 71 | name = "glibc_2_26", |
| 72 | constraint_setting = ":glibc_version", |
| 73 | ) |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 74 | ``` |
| 75 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 76 | Constraints and their values may be defined across different packages in the |
| 77 | workspace. They are referenced by label and subject to the usual visibility |
| 78 | controls. If visibility allows, you can extend an existing constraint setting by |
| 79 | defining your own value for it. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 80 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 81 | The |
| 82 | [`platform`](be/platform.html#platform) rule introduces a new platform with certain choices of constraint values. The |
| 83 | following creates a platform named `linux_x86`, and says that it describes any |
| 84 | environment that runs a Linux operating system on an x86_64 architecture with a |
| 85 | glibc version of 2.25. (See below for more on Bazel's built-in constraints.) |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 86 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 87 | ```python |
| 88 | platform( |
| 89 | name = "linux_x86", |
| 90 | constraint_values = [ |
hlopko | 71a213c | 2019-06-21 02:01:33 -0700 | [diff] [blame] | 91 | "@platforms//os:linux", |
| 92 | "@platforms//cpu:x86_64", |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 93 | ":glibc_2_25", |
| 94 | ], |
| 95 | ) |
| 96 | ``` |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 97 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 98 | Note that it is an error for a platform to specify more than one value of the |
hlopko | 71a213c | 2019-06-21 02:01:33 -0700 | [diff] [blame] | 99 | same constraint setting, such as `@platforms//cpu:x86_64` and |
| 100 | `@platforms//cpu:arm` for `@platforms//cpu:cpu`. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 101 | |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 102 | |
Marcel Hlopko | b4f3d83 | 2019-07-01 06:39:16 -0700 | [diff] [blame] | 103 | ## Generally useful constraints and platforms |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 104 | |
Marcel Hlopko | b4f3d83 | 2019-07-01 06:39:16 -0700 | [diff] [blame] | 105 | To keep the ecosystem consistent, Bazel team maintains a repository with |
| 106 | constraint definitions for the most popular CPU architectures and operating |
| 107 | systems. These are all located in |
| 108 | [https://github.com/bazelbuild/platforms](https://github.com/bazelbuild/platforms). |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 109 | |
Marcel Hlopko | b4f3d83 | 2019-07-01 06:39:16 -0700 | [diff] [blame] | 110 | Bazel ships with the following special platform definition: |
| 111 | `@local_config_platform//:host`. This is the autodetected host platform value - |
| 112 | represents autodetected platform for the system Bazel is running on. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 113 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 114 | ## Specifying a platform for a build |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 115 | |
brandjon | b9a5f56 | 2018-10-02 14:53:02 -0700 | [diff] [blame] | 116 | You can specify the host and target platforms for a build using the following |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 117 | command-line flags: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 118 | |
John Cater | 283666d | 2018-01-03 09:19:35 -0800 | [diff] [blame] | 119 | * `--host_platform` - defaults to `@bazel_tools//platforms:host_platform` |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 120 | |
John Cater | 283666d | 2018-01-03 09:19:35 -0800 | [diff] [blame] | 121 | * `--platforms` - defaults to `@bazel_tools//platforms:target_platform` |