In my makefile I have many cd $(d)
commands, one for each simple command of a target. Is there any way to reduce the number of cd $(d)
commands?
all:
cd $(d); command1
cd $(d); command2
cd $(d); command3
cd $(d); command4
cd $(d); command5
Use a shell script that CDs to $d
first as make's ${SHELL}
.
.PHONY: all
all: SHELL := ./cd-to-d
all: .SHELLFLAGS :=
all:
command1
command2
command3
command4
We use target specific variables so as not to disturb other commands in the makefile.
It gets a bit messy if we add the rule to create cd-to-d
though.
cd-to-d: SHELL := ${SHELL}
cd-to-d: .SHELLFLAGS := ${.SHELLFLAGS}
cd-to-d:
echo '#!/bin/bash' >$@-tmp
echo 'cd "$d"' >>$@-tmp
echo 'exec "$$@"' >>$@-tmp
chmod a+x $@-tmp
mv $@-tmp $@
.PHONY: all
all: SHELL := ./cd-to-d
all: .SHELLFLAGS :=
all: cd-to-d
all:
command1
command2
command3
command4
Why all the $@-tmp
noise? If we get an error while creating cd-to-d
, we don't want to leave an old one lying around. You could use .DELETE_ON_ERROR:
instead (I always do).
Why the SHELL := ${SHELL}
noise? When make builds cd-to-d
as a dependency of all
, it will inherit all
's target specific defines. We need to cancel those concerned with changing the recipe shell.
Yuk.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments