How to execute GNU make in parallel(-j) mode with shell shebang

(c)Wikipedia

Problem

GNU make can be used by shell shebang. You can create preferred named command other than Makefile.

#!/usr/bin/make 
all:
    @echo foobar

But shell shebang can accept only single argument since of shell specification.

This means that if you write a code such as bellow, The execution will not be succeed.

#!/usr/bin/make -j
all:foo bar

foo:
    sleep 5;echo Hello    
bar:
    sleep 5;echo World    

Solution

#!/bin/bash
make -j2 -f <(tail -n+3 $0) $@ ;exit $?

all:foo bar

foo:
    sleep 5;echo Hello    
bar:
    sleep 5;echo World    

The first 2 lines are magic words.

When you execute this script, Shell will treat this file as Bash script and executes 2nd line.

" <(tail -n+3 $0)" is shell pipeline. This creates pseudo file from this script which tailed from line 3 before "all:foo bar"-, then passes to GNU make.

Thus GNU make can reads 'pure' Makefile from pipeline.

This technics can be used on other than GNU make, such as sed.

Popular Articles from This Page

Top Page

Economizing Technology > How to execute GNU make in parallel(-j) mode with shell shebang