Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a bash script from a Makefile?

Tags:

bash

makefile

I have a Makefile from which I want to call another external bash script to do another part of the building. How would I best go about doing this?

like image 356
Matthew FL Avatar asked Mar 23 '10 04:03

Matthew FL


People also ask

How do I run a bash in makefile?

If this variable is not set in your makefile, the program `/bin/sh' is used as the shell. So put SHELL := /bin/bash at the top of your makefile, and you should be good to go. See "Target-specific Variable Values" in the documentation for more details.

How do I write a script in makefile?

Tips for writing shell scripts within makefiles: Escape the script's use of $ by replacing with $$ Convert the script to work as a single line by inserting ; between commands. If you want to write the script on multiple lines, escape end-of-line with \

How do you write shell commands in makefile?

If you're on GNU Make, use the := assignment instead of = . This assignment causes the right hand side to be expanded immediately, and stored in the left hand variable. FILES := $(shell ...) # expand now; FILES is now the result of $(shell ...)


3 Answers

Just like calling any other command from a makefile:

target: prerequisites     shell_script arg1 arg2 arg3 

Regarding your further explanation:

.PHONY: do_script  do_script:      shell_script arg1 arg2 arg3  prerequisites: do_script  target: prerequisites  
like image 198
Carl Norum Avatar answered Sep 28 '22 02:09

Carl Norum


Perhaps not the "right" way to do it like the answers already provided, but I came across this question because I wanted my makefile to run a script I wrote to generate a header file that would provide the version for a whole package of software. I have quite a bit of targets in this package, and didn't want to add a brand new prerequisite to them all. Putting this towards the beginning of my makefile worked for me

$(shell ./genVer.sh)

which tells make to simply run a shell command. ./genVer.sh is the path (same directory as the makefile) and name of my script to run. This runs no matter which target I specify (including clean, which is the downside, but ultimately not a huge deal to me).

like image 22
yano Avatar answered Sep 28 '22 02:09

yano


Each of the actions in the makefile rule is a command that will be executed in a subshell. You need to ensure that each command is independent, since each one will be run inside a separate subshell.

For this reason, you will often see line breaks escaped when the author wants several commands to run in the same subshell:

targetfoo:
        command_the_first foo bar baz
        command_the_second wibble wobble warble
        command_the_third which is rather too long \
            to fit on a single line so \
            intervening line breaks are escaped
        command_the_fourth spam eggs beans
like image 20
bignose Avatar answered Sep 28 '22 03:09

bignose