How can I avoid repetitive commands, such as cd in a target rule?

Robber Pen

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
bobbogo

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I combine the ssh and cd commands?

How can I change NLog rule target during runtime?

How can I avoid writing repetitive code in javascript for my Tic-Tac-Toe game?

How to avoid repetitive XAML?

How can I limit CD drive speed while on the live CD to avoid drive noise?

In Firestore I can not create rule to avoid overwrite

How can I generalize these repetitive blocks of code?

Python: How can I improve the repetitive code?

How can i cut a lot of repetitive code?

SQL Query repetitive `AND` query, how do I avoid it?

How do I avoid repetitive code in this event handler?

How to avoid repetitive codes in cypress

How to avoid repetitive JUnit tests

How can i avoid the don't repeat your self rule with this function

How can I make my own "shell commands" (e.g. mkdir/cd combo)?

R: How can I code repetitive output into an Rmarkdown file?

How can I make imports less repetitive in Java

How can I simplify this two lines of JavaScript so that it's not repetitive?

How can I reduce/compact this repetitive VBA code?

How can i reduce repetitive code in spring boot controllers

Repetitive sublayout - How can I make it unique - to meet ADA requirements

How i can optimize this code that contain some repetitive line?

How can I remove the repetitive characters in Histogram [JAVA]

how can I condense my repetitive/redundant php code?

How can I simplify this Makefile to make it less repetitive?

How can I make syntax less repetitive (DRY)?

How can I implement a repetitive promise call chain?

How can I delete repetitive values in vector with order?

How can I refactor repetitive conditional Vue.js code?