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+4 $0) $@ ;exit 0

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+4 $0)" is shell pipeline. This creates pseudo file from this script which tailed from line 4 "all:foo bar"-, and 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

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