aboutsummaryrefslogtreecommitdiffhomepage
path: root/site/docs/platforms.md
blob: c746b189c23e51072f5b4d54dbd9fb5ac14158ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
layout: documentation
title: Platforms
---

# Platforms

- [Overview](#overview)
- [Defining a platform](#defining-a-platform)
- [Built-in constraints and platforms](#built-in-constraints-and-platforms)
- [Specifying a platform for a build](#specifying-a-platform-for-a-build)

## Overview

Bazel can build and test code on a variety of operating systems and hardware
using many different build tools, such as linkers and compilers. These
combinations of software and hardware are what Bazel considers *platforms*.
One major use for specifying a platform for a build is automatic
[toolchain](toolchains.html)
selection.

Bazel recognizes the following types of platforms:

*  **Host** - platforms on which Bazel runs.
*  **Execution** - platforms on which build tools execute build actions.
*  **Target** - platforms for which Bazel builds the output.

Bazel supports the following build scenarios regarding platforms:

*  **Single-platform builds** (default) - host, execution, and target platforms
   are the same. For example, building a Linux executable on Ubuntu running on
   an Intel x64 CPU.

*  **Cross-compilation builds** - host and execution platforms are the same, but
   the target platform is different. For example, building an iOS app on macOS
   running on a MacBook Pro.

*  **Multi-platform builds** - host, execution, and target platforms are all
   different.

## Defining a platform

A *Bazel platform* is a named collection of constraints that define a supported
software and/or hardware configuration through name-value pairs. For example, a
constraint can define the CPU architecture, GPU presence, or the specific
version of a build tool, such as a linker or compiler.

You define a platform in a `BUILD` file using the following Bazel rules:

*   [`constraint_setting`](be/platform.html#constraint_setting)  - defines a constraint.

*   [`constraint_value`](be/platform.html#constraint_value)  - defines an allowed value for a constraint.

*   [`platform`](be/platform.html#platform)  - defines a platform by specifying a set of constraints and their values.

The following example defines the `glibc_version` constraint and its two allowed
values. It then defines a platform that uses the `glibc_version` constraint
along with Bazel's [built-in constraints](#built-in-constraints-and-platforms)
for operating systems and CPU architecture:

```python
constraint_setting(name = 'glibc_version')

constraint_value(
    name = 'glibc_2_25',
    constraint_setting = ':glibc_version')

constraint_value(
    name = 'glibc_2_26',
    constraint_setting = ':glibc_version')

platform(
    name = 'linux_x86',
    constraint_values = [
      '@bazel_tools//platforms:linux',
      '@bazel_tools//platforms:x86_64',
      ':glibc_2_25',
    ])
```

Keep the following in mind when defining constraints and platforms that use
them:

*  You can define constraints in any Bazel package within the project.

*  Constraints follow the visibility settings of the package that contains them.

*  You can use constraint values from multiple packages in the same platform
   definition. However, using constraint values that share a constraint setting
   will result in an error.

## Built-in constraints and platforms

Bazel ships with constraint definitions for the most popular CPU architectures
and operating systems.

*  `@bazel_tools//platforms:cpu` defines the following CPU architectures:
   *  `@bazel_tools//platforms:x86_32`
   *  `@bazel_tools//platforms:x86_64`
   *  `@bazel_tools//platforms:ppc`
   *  `@bazel_tools//platforms:arm`
   *  `@bazel_tools//platforms:s390x`
*   `@bazel_tools//platforms:os` defines the following operating systems:
   *  `@bazel_tools//platforms:osx`
   *  `@bazel_tools//platforms:freebsd`
   *  `@bazel_tools//platforms:linux`
   *  `@bazel_tools//platforms:windows`

Bazel also ships with the following platform definitions:

*  `@bazel_tools//platforms:host_platform` - automatically detects the CPU
   architecture and operating system for the host platform.

*  `@bazel_tools//platforms:target_platform` - automatically detects the CPU
   architecture and operating system for the target platform.

In these definitions, the CPU architecture constraint values are pulled from the
`--host_cpu` and `--cpu` flags.

## Specifying a platform for a build

To select a specific host and target platform for a build, use the following
command-line flags:

*  `--host_platform` - defaults to `@bazel_tools//platforms:host_platform`

*  `--platforms` - defaults to `@bazel_tools//platforms:target_platform`

Platforms can also be used with the `config_setting` rule to define configurable
attributes. See
[config_setting](be/general.html#config_setting)
for more
details.