Skip to content

Commit 7e4955e

Browse files
committed
rustc: document the jobserver
Explicitly document that the jobserver may be used by `rustc` and show the warning to increase the chances that this document is found when searching for solutions online. In particular, add a section about the interaction with build systems, which is intended to contain recommendations on how to integrate `rustc` with different built systems. For GNU Make, recommend using the `+` indicator. In addition, add a note about the issue with GNU Make 4.3 since it is important that users realize they should do this even if they do not expect parallelism from `rustc`. Finally, show how to workaround the issue of `$(shell ...)` calls in recursive Make (which e.g. was needed for the Linux kernel). The GNU Make 4.4 case under `--jobserver-style=pipe` is not added since it got fixed after Rust 1.76.0 already (i.e. `rustc` will not warn if it finds the negative file descriptors). For CMake, recommend using `JOB_SERVER_AWARE` and show a workaround using `$(MAKE)` for earlier versions (when using the Makefile generator). From: #120515 Cc: @petrochenkov @belovdv @weihanglo @bjorn3 Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 57da90e commit 7e4955e

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/doc/rustc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [What is rustc?](what-is-rustc.md)
44
- [Command-line Arguments](command-line-arguments.md)
55
- [Codegen Options](codegen-options/index.md)
6+
- [Jobserver](jobserver.md)
67
- [Lints](lints/index.md)
78
- [Lint Levels](lints/levels.md)
89
- [Lint Groups](lints/groups.md)

src/doc/rustc/src/jobserver.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Jobserver
2+
3+
Internally, `rustc` may take advantage of parallelism. `rustc` will coordinate
4+
with the build system calling it if a [GNU Make jobserver] is passed in the
5+
`MAKEFLAGS` environment variable. Other flags may have an effect as well, such
6+
as [`CARGO_MAKEFLAGS`]. If a jobserver is not passed, then `rustc` will choose
7+
the number of jobs to use.
8+
9+
Starting with Rust 1.76.0, `rustc` will warn if a jobserver appears to be
10+
available but is not accessible, e.g.:
11+
12+
```console
13+
$ echo 'fn main() {}' | MAKEFLAGS=--jobserver-auth=3,4 rustc -
14+
warning: failed to connect to jobserver from environment variable `MAKEFLAGS="--jobserver-auth=3,4"`: cannot open file descriptor 3 from the jobserver environment variable value: Bad file descriptor (os error 9)
15+
|
16+
= note: the build environment is likely misconfigured
17+
```
18+
19+
## Integration with build systems
20+
21+
The following subsections contain recommendations on how to integrate `rustc`
22+
with build systems so that the jobserver is handled appropriately.
23+
24+
### GNU Make
25+
26+
When calling `rustc` from GNU Make, it is recommended that all `rustc`
27+
invocations are marked as recursive in the `Makefile` (by prefixing the command
28+
line with the `+` indicator), so that GNU Make enables the jobserver for them.
29+
For instance:
30+
31+
<!-- ignore-tidy-tab -->
32+
33+
```make
34+
x:
35+
+@echo 'fn main() {}' | rustc -
36+
```
37+
38+
In particular, GNU Make 4.3 (a widely used version as of 2024) passes a simple
39+
pipe jobserver in `MAKEFLAGS` even when it was not made available for the child
40+
process, which in turn means `rustc` will warn about it. For instance, if the
41+
`+` indicator is removed from the example above and GNU Make is called with e.g.
42+
`make -j2`, then the aforementioned warning will trigger.
43+
44+
For calls to `rustc` inside `$(shell ...)` inside a recursive Make, one can
45+
disable the jobserver manually by clearing the `MAKEFLAGS` variable, e.g.:
46+
47+
```make
48+
S := $(shell MAKEFLAGS= rustc --print sysroot)
49+
50+
x:
51+
@$(MAKE) y
52+
53+
y:
54+
@echo $(S)
55+
```
56+
57+
### CMake
58+
59+
CMake 3.28 supports the `JOB_SERVER_AWARE` option in its [`add_custom_target`]
60+
command, e.g.:
61+
62+
```cmake
63+
cmake_minimum_required(VERSION 3.28)
64+
project(x)
65+
add_custom_target(x
66+
JOB_SERVER_AWARE TRUE
67+
COMMAND echo 'fn main() {}' | rustc -
68+
)
69+
```
70+
71+
For earlier versions, when using CMake with the Makefile generator, one
72+
workaround is to have [`$(MAKE)`] somewhere in the command so that GNU Make
73+
treats it as a recursive Make call, e.g.:
74+
75+
```cmake
76+
cmake_minimum_required(VERSION 3.22)
77+
project(x)
78+
add_custom_target(x
79+
COMMAND DUMMY_VARIABLE=$(MAKE) echo 'fn main() {}' | rustc -
80+
)
81+
```
82+
83+
[GNU Make jobserver]: https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html
84+
[`CARGO_MAKEFLAGS`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html
85+
[`add_custom_target`]: https://cmake.org/cmake/help/latest/command/add_custom_target.html
86+
[`$(MAKE)`]: https://www.gnu.org/software/make/manual/html_node/MAKE-Variable.html

0 commit comments

Comments
 (0)