README.md
1<!---
2Copyright (C) 2015 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15-->
16
17makeparallel
18============
19makeparallel communicates with the [GNU make jobserver](http://make.mad-scientist.net/papers/jobserver-implementation/)
20in order claim all available jobs, and then passes the number of jobs
21claimed to a subprocess with `-j<jobs>`.
22
23The number of available jobs is determined by reading tokens from the jobserver
24until a read would block. If the makeparallel rule is the only one running the
25number of jobs will be the total size of the jobserver pool, i.e. the value
26passed to make with `-j`. Any jobs running in parallel with with the
27makeparellel rule will reduce the measured value, and thus reduce the
28parallelism available to the subprocess.
29
30To run a multi-thread or multi-process binary inside GNU make using
31makeparallel, add
32```Makefile
33 +makeparallel subprocess arguments
34```
35to a rule. For example, to wrap ninja in make, use something like:
36```Makefile
37 +makeparallel ninja -f build.ninja
38```
39
40To determine the size of the jobserver pool, add
41```Makefile
42 +makeparallel echo > make.jobs
43```
44to a rule that is guarantee to run alone (i.e. all other rules are either
45dependencies of the makeparallel rule, or the depend on the makeparallel
46rule. The output file will contain the `-j<num>` flag passed to the parent
47make process, or `-j1` if no flag was found. Since GNU make will run
48makeparallel during the execution phase, after all variables have been
49set and evaluated, it is not possible to get the output of makeparallel
50into a make variable. Instead, use a shell substitution to read the output
51file directly in a recipe. For example:
52```Makefile
53 echo Make was started with $$(cat make.jobs)
54```
55