Noz.i.kor
Noz.i.kor

Reputation: 3747

Makefile: run binaries differently depending on their name

I have this in my Makefile:

BINS = $(shell echo *.bin)

.PHONY: $(BINS)
run: $(BINS)

*.bin:
    ./$@

I run it as make -j 8

This way it looks for all files ending with .bin, and runs them in parallel using make's -j option (Makefile run processes in background)

I need to modify the makefile so that it runs all files of the type mpi*.bin as mpirun -np 2 ./mpi*.bin, and the remaining executable files as ./<filename>.bin

Thanks for your help.

Upvotes: 0

Views: 1067

Answers (1)

Keith Layne
Keith Layne

Reputation: 3775

Here is a simple example I used to test my answer:

touch {a,b,c,d}.bin mpi{a,b,c,d}.bin

to create some empty test files, and my Makefile based on yours:

BINS = $(shell echo *.bin)

.PHONY: $(BINS)
run: $(BINS)

*.bin:
    echo "bin file: " ./$@

mpi*.bin:
    echo "mpi file: " ./$@

It is key here that the rule for the prefixed files follows the non-prefixed rule. If not, the prefixed rule will be overridden.

This seems to work for distinguishing between the prefixed and non-prefixed files, but gives the following output:

~/tmp$ make
Makefile:10: warning: overriding commands for target `mpia.bin'
Makefile:7: warning: ignoring old commands for target `mpia.bin'
Makefile:10: warning: overriding commands for target `mpib.bin'
Makefile:7: warning: ignoring old commands for target `mpib.bin'
Makefile:10: warning: overriding commands for target `mpic.bin'
Makefile:7: warning: ignoring old commands for target `mpic.bin'
Makefile:10: warning: overriding commands for target `mpid.bin'
Makefile:7: warning: ignoring old commands for target `mpid.bin'
echo "bin file: " ./a.bin
bin file:  ./a.bin
echo "bin file: " ./b.bin
bin file:  ./b.bin
echo "bin file: " ./c.bin
bin file:  ./c.bin
echo "bin file: " ./d.bin
bin file:  ./d.bin
echo "mpi file: " ./mpia.bin
mpi file:  ./mpia.bin
echo "mpi file: " ./mpib.bin
mpi file:  ./mpib.bin
echo "mpi file: " ./mpic.bin
mpi file:  ./mpic.bin
echo "mpi file: " ./mpid.bin
mpi file:  ./mpid.bin

I'm sure that there's a way to suppress those warnings or do it better, but this approach seems to work.

Upvotes: 1

Related Questions