|
| 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. |
| 6 | + |
| 7 | +Starting with Rust 1.76.0, `rustc` will warn if a jobserver appears to be |
| 8 | +available but is not accessible, e.g.: |
| 9 | + |
| 10 | +```console |
| 11 | +$ echo 'fn main() {}' | MAKEFLAGS=--jobserver-auth=3,4 rustc - |
| 12 | +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) |
| 13 | + | |
| 14 | + = note: the build environment is likely misconfigured |
| 15 | +``` |
| 16 | + |
| 17 | +## Integration with build systems |
| 18 | + |
| 19 | +The following subsections contain recommendations on how to integrate `rustc` |
| 20 | +with build systems so that the jobserver is handled appropriately. |
| 21 | + |
| 22 | +### GNU Make |
| 23 | + |
| 24 | +When calling `rustc` from GNU Make, it is recommended that all `rustc` |
| 25 | +invocations are marked as recursive in the `Makefile` (by prefixing the command |
| 26 | +line with the `+` indicator), so that GNU Make enables the jobserver for them. |
| 27 | +For instance: |
| 28 | + |
| 29 | +<!-- ignore-tidy-tab --> |
| 30 | + |
| 31 | +```make |
| 32 | +x: |
| 33 | + +@echo 'fn main() {}' | rustc - |
| 34 | +``` |
| 35 | + |
| 36 | +In particular, GNU Make 4.3 (a widely used version as of 2024) passes a simple |
| 37 | +pipe jobserver in `MAKEFLAGS` even when it was not made available for the child |
| 38 | +process, which in turn means `rustc` will warn about it. For instance, if the |
| 39 | +`+` indicator is removed from the example above and GNU Make is called with e.g. |
| 40 | +`make -j2`, then the aforementioned warning will trigger. |
| 41 | + |
| 42 | +For calls to `rustc` inside `$(shell ...)` inside a recursive Make, one can |
| 43 | +disable the jobserver manually by clearing the `MAKEFLAGS` variable, e.g.: |
| 44 | + |
| 45 | +```make |
| 46 | +S := $(shell MAKEFLAGS= rustc --print sysroot) |
| 47 | + |
| 48 | +x: |
| 49 | + @$(MAKE) y |
| 50 | + |
| 51 | +y: |
| 52 | + @echo $(S) |
| 53 | +``` |
| 54 | + |
| 55 | +[GNU Make jobserver]: https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html |
0 commit comments