blob: c746b189c23e51072f5b4d54dbd9fb5ac14158ba [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)
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)
jcater89095eb2017-11-03 17:46:21 +010012
spomorski5b406602017-11-03 22:05:53 +010013## Overview
jcater89095eb2017-11-03 17:46:21 +010014
spomorski5b406602017-11-03 22:05:53 +010015Bazel can build and test code on a variety of operating systems and hardware
16using many different build tools, such as linkers and compilers. These
17combinations of software and hardware are what Bazel considers *platforms*.
18One major use for specifying a platform for a build is automatic
jcater06325052018-06-12 08:28:48 -070019[toolchain](toolchains.html)
20selection.
jcater89095eb2017-11-03 17:46:21 +010021
spomorski5b406602017-11-03 22:05:53 +010022Bazel recognizes the following types of platforms:
jcater89095eb2017-11-03 17:46:21 +010023
spomorskif69d0792017-11-09 18:57:15 +010024* **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.
jcater89095eb2017-11-03 17:46:21 +010027
spomorski5b406602017-11-03 22:05:53 +010028Bazel supports the following build scenarios regarding platforms:
jcater89095eb2017-11-03 17:46:21 +010029
spomorski5b406602017-11-03 22:05:53 +010030* **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.
jcater89095eb2017-11-03 17:46:21 +010033
spomorski5b406602017-11-03 22:05:53 +010034* **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.
jcater89095eb2017-11-03 17:46:21 +010037
spomorski5b406602017-11-03 22:05:53 +010038* **Multi-platform builds** - host, execution, and target platforms are all
39 different.
jcater89095eb2017-11-03 17:46:21 +010040
spomorski5b406602017-11-03 22:05:53 +010041## Defining a platform
jcater89095eb2017-11-03 17:46:21 +010042
spomorski5b406602017-11-03 22:05:53 +010043A *Bazel platform* is a named collection of constraints that define a supported
44software and/or hardware configuration through name-value pairs. For example, a
45constraint can define the CPU architecture, GPU presence, or the specific
46version of a build tool, such as a linker or compiler.
jcater89095eb2017-11-03 17:46:21 +010047
spomorski5b406602017-11-03 22:05:53 +010048You define a platform in a `BUILD` file using the following Bazel rules:
jcater89095eb2017-11-03 17:46:21 +010049
jcater06325052018-06-12 08:28:48 -070050* [`constraint_setting`](be/platform.html#constraint_setting) - defines a constraint.
jcater89095eb2017-11-03 17:46:21 +010051
jcater06325052018-06-12 08:28:48 -070052* [`constraint_value`](be/platform.html#constraint_value) - defines an allowed value for a constraint.
jcater89095eb2017-11-03 17:46:21 +010053
jcater06325052018-06-12 08:28:48 -070054* [`platform`](be/platform.html#platform) - defines a platform by specifying a set of constraints and their values.
jcater89095eb2017-11-03 17:46:21 +010055
spomorski5b406602017-11-03 22:05:53 +010056The following example defines the `glibc_version` constraint and its two allowed
57values. It then defines a platform that uses the `glibc_version` constraint
58along with Bazel's [built-in constraints](#built-in-constraints-and-platforms)
59for operating systems and CPU architecture:
jcater89095eb2017-11-03 17:46:21 +010060
spomorski5b406602017-11-03 22:05:53 +010061```python
jcater89095eb2017-11-03 17:46:21 +010062constraint_setting(name = 'glibc_version')
63
64constraint_value(
65 name = 'glibc_2_25',
66 constraint_setting = ':glibc_version')
67
68constraint_value(
69 name = 'glibc_2_26',
70 constraint_setting = ':glibc_version')
jcater89095eb2017-11-03 17:46:21 +010071
jcater89095eb2017-11-03 17:46:21 +010072platform(
73 name = 'linux_x86',
74 constraint_values = [
75 '@bazel_tools//platforms:linux',
76 '@bazel_tools//platforms:x86_64',
77 ':glibc_2_25',
78 ])
jcater89095eb2017-11-03 17:46:21 +010079```
80
spomorski5b406602017-11-03 22:05:53 +010081Keep the following in mind when defining constraints and platforms that use
82them:
jcater89095eb2017-11-03 17:46:21 +010083
spomorski5b406602017-11-03 22:05:53 +010084* You can define constraints in any Bazel package within the project.
jcater89095eb2017-11-03 17:46:21 +010085
spomorski5b406602017-11-03 22:05:53 +010086* Constraints follow the visibility settings of the package that contains them.
jcater89095eb2017-11-03 17:46:21 +010087
spomorski5b406602017-11-03 22:05:53 +010088* 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.
jcater89095eb2017-11-03 17:46:21 +010091
spomorski5b406602017-11-03 22:05:53 +010092## Built-in constraints and platforms
jcater89095eb2017-11-03 17:46:21 +010093
spomorski5b406602017-11-03 22:05:53 +010094Bazel ships with constraint definitions for the most popular CPU architectures
spomorskif69d0792017-11-09 18:57:15 +010095and operating systems.
jcater89095eb2017-11-03 17:46:21 +010096
spomorskif69d0792017-11-09 18:57:15 +010097* `@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`
jcater89095eb2017-11-03 17:46:21 +0100108
spomorski5b406602017-11-03 22:05:53 +0100109Bazel also ships with the following platform definitions:
jcater89095eb2017-11-03 17:46:21 +0100110
spomorski5b406602017-11-03 22:05:53 +0100111* `@bazel_tools//platforms:host_platform` - automatically detects the CPU
112 architecture and operating system for the host platform.
jcater89095eb2017-11-03 17:46:21 +0100113
spomorski5b406602017-11-03 22:05:53 +0100114* `@bazel_tools//platforms:target_platform` - automatically detects the CPU
115 architecture and operating system for the target platform.
jcater89095eb2017-11-03 17:46:21 +0100116
spomorski5b406602017-11-03 22:05:53 +0100117In these definitions, the CPU architecture constraint values are pulled from the
118`--host_cpu` and `--cpu` flags.
jcater89095eb2017-11-03 17:46:21 +0100119
spomorski5b406602017-11-03 22:05:53 +0100120## Specifying a platform for a build
jcater89095eb2017-11-03 17:46:21 +0100121
spomorski5b406602017-11-03 22:05:53 +0100122To select a specific host and target platform for a build, use the following
123command-line flags:
jcater89095eb2017-11-03 17:46:21 +0100124
John Cater283666d2018-01-03 09:19:35 -0800125* `--host_platform` - defaults to `@bazel_tools//platforms:host_platform`
jcater89095eb2017-11-03 17:46:21 +0100126
John Cater283666d2018-01-03 09:19:35 -0800127* `--platforms` - defaults to `@bazel_tools//platforms:target_platform`
jcater89095eb2017-11-03 17:46:21 +0100128
spomorski5b406602017-11-03 22:05:53 +0100129Platforms can also be used with the `config_setting` rule to define configurable
jcater06325052018-06-12 08:28:48 -0700130attributes. See
131[config_setting](be/general.html#config_setting)
132for more
jcater89095eb2017-11-03 17:46:21 +0100133details.