Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile as an executable script with shebang?

Tags:

Is it possible to create an executable script that would be interpreted by make?

I tried this:

#!/usr/bin/env make --makefile=/dev/stdin

main:
        @echo Hello!

but it does not work - hangs until press Ctrl-c.

like image 606
Aleksandr Levchuk Avatar asked Aug 19 '11 14:08

Aleksandr Levchuk


2 Answers

#!/usr/bin/make -f

main:
        @echo Hello World!

Is normally all you need in a standard make file. The filename is implicitly passed as the last argument. /dev/stdin here is (usually) the tty. You can do the whole env thing if there's a reason to, but often there's no need.

ajw@rapunzel:~/code/videocc/tools > vi Makefile                       
ajw@rapunzel:~/code/videocc/tools > chmod a+x Makefile         
ajw@rapunzel:~/code/videocc/tools > ./Makefile                 
Hello World!
like image 100
Flexo Avatar answered Sep 22 '22 18:09

Flexo


The following adds a level of indirection but it's the best solution I've come up with for self-executing makefiles not called "makefile":

#!/bin/sh
exec make -f- "$@" << 'eof'

.PHONY: all
all:
    @echo 'hello world!'

I'm trying to collect #! env hacks for each language / program here.

like image 38
Stephen Niedzielski Avatar answered Sep 24 '22 18:09

Stephen Niedzielski