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) |
| 9 | - [Defining a platform](#defining-a-platform) |
| 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 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 15 | Bazel can build and test code on a variety of operating systems and hardware |
| 16 | using many different build tools, such as linkers and compilers. These |
| 17 | combinations of software and hardware are what Bazel considers *platforms*. |
| 18 | One major use for specifying a platform for a build is automatic |
jcater | 0632505 | 2018-06-12 08:28:48 -0700 | [diff] [blame] | 19 | [toolchain](toolchains.html) |
| 20 | selection. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 21 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 22 | Bazel recognizes the following types of platforms: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 23 | |
spomorski | f69d079 | 2017-11-09 18:57:15 +0100 | [diff] [blame] | 24 | * **Host** - platforms on which Bazel runs. |
| 25 | * **Execution** - platforms on which build tools execute build actions. |
| 26 | * **Target** - platforms for which Bazel builds the output. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 27 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 28 | Bazel supports the following build scenarios regarding platforms: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 29 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 30 | * **Single-platform builds** (default) - host, execution, and target platforms |
| 31 | are the same. For example, building a Linux executable on Ubuntu running on |
| 32 | an Intel x64 CPU. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 33 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 34 | * **Cross-compilation builds** - host and execution platforms are the same, but |
| 35 | the target platform is different. For example, building an iOS app on macOS |
| 36 | running on a MacBook Pro. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 37 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 38 | * **Multi-platform builds** - host, execution, and target platforms are all |
| 39 | different. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 40 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 41 | ## Defining a platform |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 42 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 43 | A *Bazel platform* is a named collection of constraints that define a supported |
| 44 | software and/or hardware configuration through name-value pairs. For example, a |
| 45 | constraint can define the CPU architecture, GPU presence, or the specific |
| 46 | version of a build tool, such as a linker or compiler. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 47 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 48 | You define a platform in a `BUILD` file using the following Bazel rules: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 49 | |
jcater | 0632505 | 2018-06-12 08:28:48 -0700 | [diff] [blame] | 50 | * [`constraint_setting`](be/platform.html#constraint_setting) - defines a constraint. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 51 | |
jcater | 0632505 | 2018-06-12 08:28:48 -0700 | [diff] [blame] | 52 | * [`constraint_value`](be/platform.html#constraint_value) - defines an allowed value for a constraint. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 53 | |
jcater | 0632505 | 2018-06-12 08:28:48 -0700 | [diff] [blame] | 54 | * [`platform`](be/platform.html#platform) - defines a platform by specifying a set of constraints and their values. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 55 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 56 | The following example defines the `glibc_version` constraint and its two allowed |
| 57 | values. It then defines a platform that uses the `glibc_version` constraint |
| 58 | along with Bazel's [built-in constraints](#built-in-constraints-and-platforms) |
| 59 | for operating systems and CPU architecture: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 60 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 61 | ```python |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 62 | constraint_setting(name = 'glibc_version') |
| 63 | |
| 64 | constraint_value( |
| 65 | name = 'glibc_2_25', |
| 66 | constraint_setting = ':glibc_version') |
| 67 | |
| 68 | constraint_value( |
| 69 | name = 'glibc_2_26', |
| 70 | constraint_setting = ':glibc_version') |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 71 | |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 72 | platform( |
| 73 | name = 'linux_x86', |
| 74 | constraint_values = [ |
| 75 | '@bazel_tools//platforms:linux', |
| 76 | '@bazel_tools//platforms:x86_64', |
| 77 | ':glibc_2_25', |
| 78 | ]) |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 79 | ``` |
| 80 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 81 | Keep the following in mind when defining constraints and platforms that use |
| 82 | them: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 83 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 84 | * You can define constraints in any Bazel package within the project. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 85 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 86 | * Constraints follow the visibility settings of the package that contains them. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 87 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 88 | * You can use constraint values from multiple packages in the same platform |
| 89 | definition. However, using constraint values that share a constraint setting |
| 90 | will result in an error. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 91 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 92 | ## Built-in constraints and platforms |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 93 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 94 | Bazel ships with constraint definitions for the most popular CPU architectures |
spomorski | f69d079 | 2017-11-09 18:57:15 +0100 | [diff] [blame] | 95 | and operating systems. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 96 | |
spomorski | f69d079 | 2017-11-09 18:57:15 +0100 | [diff] [blame] | 97 | * `@bazel_tools//platforms:cpu` defines the following CPU architectures: |
| 98 | * `@bazel_tools//platforms:x86_32` |
| 99 | * `@bazel_tools//platforms:x86_64` |
| 100 | * `@bazel_tools//platforms:ppc` |
| 101 | * `@bazel_tools//platforms:arm` |
| 102 | * `@bazel_tools//platforms:s390x` |
| 103 | * `@bazel_tools//platforms:os` defines the following operating systems: |
| 104 | * `@bazel_tools//platforms:osx` |
| 105 | * `@bazel_tools//platforms:freebsd` |
| 106 | * `@bazel_tools//platforms:linux` |
| 107 | * `@bazel_tools//platforms:windows` |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 108 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 109 | Bazel also ships with the following platform definitions: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 110 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 111 | * `@bazel_tools//platforms:host_platform` - automatically detects the CPU |
| 112 | architecture and operating system for the host platform. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 113 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 114 | * `@bazel_tools//platforms:target_platform` - automatically detects the CPU |
| 115 | architecture and operating system for the target platform. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 116 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 117 | In these definitions, the CPU architecture constraint values are pulled from the |
| 118 | `--host_cpu` and `--cpu` flags. |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 119 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 120 | ## Specifying a platform for a build |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 121 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 122 | To select a specific host and target platform for a build, use the following |
| 123 | command-line flags: |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 124 | |
John Cater | 283666d | 2018-01-03 09:19:35 -0800 | [diff] [blame] | 125 | * `--host_platform` - defaults to `@bazel_tools//platforms:host_platform` |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 126 | |
John Cater | 283666d | 2018-01-03 09:19:35 -0800 | [diff] [blame] | 127 | * `--platforms` - defaults to `@bazel_tools//platforms:target_platform` |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 128 | |
spomorski | 5b40660 | 2017-11-03 22:05:53 +0100 | [diff] [blame] | 129 | Platforms can also be used with the `config_setting` rule to define configurable |
jcater | 0632505 | 2018-06-12 08:28:48 -0700 | [diff] [blame] | 130 | attributes. See |
| 131 | [config_setting](be/general.html#config_setting) |
| 132 | for more |
jcater | 89095eb | 2017-11-03 17:46:21 +0100 | [diff] [blame] | 133 | details. |