Parallelize make by Default

Last updated on December 13, 2016

The make utility is an standard utility on POSIX systems (GNU/Linux, macOS, etc.) that update files derived from other files, such as compiling source files to their binary forms. It is widely supported and used across different fields such as organizing and building C/C++/Fortran projects, building Sphinx documentation, etc.

The most popular implementation of the make utility is probably GNU make, which is usually the default make program on various GNU/Linux distributions. (On macOS, the version of the default GNU make is pretty old. Please consult Install and Use GNU Command Line Tools on macOS/OS X for a newer version.) It adds one very important feature besides the standard make specification: parallelization. The command line option -j can be used to specify the maximum number of jobs that it is allowed to run simultaneously. However, it is quite annoying to type up this option every time when using it—we want a setting such that the CPU can be fully utilized by default. To achieve this goal, add the following lines to your ~/.bashrc if you use bash or ~/.zshrc if you use zsh:

# set MAKEFLAGS
if type nproc &>/dev/null; then   # GNU/Linux
  export MAKEFLAGS="$MAKEFLAGS -j$(($(nproc)-1))"
elif type sysctl -n hw.ncpu &>/dev/null; then   # macOS, FreeBSD
  export MAKEFLAGS="$MAKEFLAGS -j$(($(sysctl -n hw.ncpu)-1))"
fi

The code above sets the envrionmental variable MAKEFLAGS, which specifies the command line arguments of any invoked make subprocesses. It is set such that the maximum number of jobs that is allowed to be run simultaneously is equal to the number of available CPU cores minus 1. In this way, the hardware is more or less fully utilized when using make, with one CPU core left for other potential tasks on the system.

Leave a Reply

Your email address will not be published. Required fields are marked *