aboutsummaryrefslogtreecommitdiffhomepage
path: root/site/docs/skylark/concepts.md
blob: 4935fc96639c8554a374ad6dcd1cb401cd677817 (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
134
135
136
137
138
139
# Concepts

## Loading a Skylark extension

Use the `load` statement to import a symbol from a <code>.bzl</code> Skylark
extension.

```python
load("/build_tools/rules/maprule", "maprule")
```

This code will load the file `build_tools/rules/maprule.bzl` and add the
`maprule` symbol to the environment. This can be used to load new rules,
functions or constants (e.g. a string, a list, etc.). Multiple symbols can be
imported by using additional arguments to the call to `load`. Arguments must
be string literals (no variable) and `load` statements must appear at
top-level, i.e. they cannot be in a function body.

`load` also supports aliases, i.e. you can assign different names to the
imported symbols.

```python
load("/build_tools/rules/maprule", maprule_alias = "maprule")
```

You define multiple aliases within one `load` statement. Moreover, the argument
list can contain both aliases and regular symbol names. The following example is
perfectly legal (please note when to use quotation marks).

```python
load("/path/to/my_rules", "some_rule", nice_alias = "some_other_rule")
```

Symbols starting with `_` are private and cannot be loaded from other files.
Visibility doesn't affect loading: you don't need to use `exports_files` to make
a Skylark file visible.

## Macros and rules

A [macro](macros.md) in Skylark is a function that instantiates rules. The
function is evaluated as soon as the BUILD file is read. Bazel has little
information about macros: if your macro generates a `genrule`, Bazel will behave
as if you wrote the `genrule`. As a result, `bazel query` will only list the
generated genrule.

A [rule](rules.md) in Skylark is more powerful than a macro, as it can access
Bazel internals and have full control over what is going on. It may for example
pass information to other rules. A rule defined in Skylark will behave in a
similar way as a native rule.

If a macro becomes complex, it is often a good idea to make it a rule.

## Evaluation model

A build consists of three phases.

* **Loading phase**. First, we load and evaluate all Skylark extensions and all BUILD
  files that are needed for the build. The execution of the BUILD files simply
  instantiates rules. This is where macros are evaluated.

* **Analysis phase**. The code of the rules is executed (their `implementation`
  function), and actions are instantiated. An action describes how to generate
  a set of outputs from a set of inputs, e.g. "run gcc on hello.c and get
  hello.o". It is important to note that we have to list explicitly which
  files will be generated before executing the actual commands.

* **Execution phase**. Actions are executed, when at least one of their outputs is
  required. If a file is missing or if a command fails to generate one output,
  the build fails. Tests are run during this phase, as they are actions.

## Language

Skylark is a superset of the core build language and its syntax is a subset of
Python. The following constructs have been added to the core build language:
`if` statements, `for` loops, and function definitions.
It is designed to be simple, thread-safe and integrated with the
BUILD language. It is not a general-purpose language and most Python
features are not included.


Some differences with Python should be noted:

* Although some data structures are mutable, all objects are recursively frozen
  and become recursively immutable before Bazel invokes Skylark
  and after it is done with such evaluation,
  i.e. when a .bzl file is loaded, when a BUILD file is processed,
  when a Skylark-defined rule is evaluated to create a configured target, or
  when a callback function is called to compute a configured target attribute.
  These objects that are frozen notably include the recursive contents of any
  global variable exported by a .bzl file or imported by a `load()` statement,
  and any parameter passed to a callback function or result returned by it.
  From the point of view of the Skylark code
  in given a Bazel-initiated evaluation,
  objects passed as input or present in the evaluation's initial environment
  are immutable, whereas objects created during the evaluation are mutable.
  From the point of view of the Bazel code that evaluates said Skylark code,
  all inputs and outputs of the evaluation are recursively immutable
  and all evaluations are deterministic,
  which guarantees the hermeticity of the build,
  and allows sharing of evaluations without any fear of side-effects.

* Lists are mutable, but dicts and sets are immutable.
  This is temporary: dicts will be made mutable in the near future;
  however there are no plans to make sets mutable at this time.

* All global values are constant (they cannot be reassigned).

* `x += y` is syntactic sugar for `x = x + y`. Even if `x` and `y` are lists,
  dicts or sets, the original value is not mutated, so references to `x`
  that were assigned before the operation will see the old value. This behavior
  is temporary, and will follow Python semantics in the future.

* The `+` operator is defined for dictionaries, returning an immutable
  concatenated dictionary created from the entries of the original
  dictionaries. In case of duplicate keys, we use values from the second
  operand. If you need compatibility with Python, we suggest this syntax:
  `dict(a.items() + b.items())`.

* Dictionary assignment has slightly different semantics: `d["x"] = y` is
  syntactic sugar for `d = d + {"x": y}` or `d += {"x": y}`. This behavior
  is temporary, and will follow Python semantics in the future.

* Dictionaries have deterministic order when iterating (sorted by key).

* Sets use a custom order when iterating (see
  [documentation](lib/Globals.html#set)).

* Recursion is not allowed.

The following Python features are not supported:

* `class` (see `struct` function)
* `import` (see `load` statement)
* `while`, `yield`
* `lambda`
* `try`, `raise`, `except`, `finally` (see `fail` for fatal errors).
* most builtin functions, most methods