开发者

Eliminating duplication in a Makefile

This is the same Makefile as in Makefile command substitution problem, but a different question.

REBAR=./rebar
REBAR_DEBUG=$(REBAR) -C rebar.debug.config
REBAR_COMPILE=$(REBAR) get-deps compile
LAST_CONFIG:=$(shell cat config.tmp)
PLT=dialyzer/sqlite3.plt

all: config_normal compile

compile:
    $(REBAR_COMPILE)

test:
    $(REBAR_COMPILE) eunit

clean:
    -rm -rf deps ebin priv doc/* .eunit c_src/*.o

docs:
    $(REBAR_COMPILE) doc

static: config_debug
    $(REBAR_DEBUG) get-deps compile
ifeq ($(wildcard $(PLT)),)
    dialyzer --build_plt --apps kernel stdlib erts --output_plt $(PLT) 
else
    dialyzer --plt $(PLT) -r ebin
endif

cross_compile: config_cross
    $(REBAR_COMPILE) -C rebar.cross_compile.config

valgrind: clean
    $(REBAR_DEBUG) get-deps compile
    valgrind --tool=memcheck --leak-check=yes --num-callers=20 ./test.sh

ifeq ($(LAST_CONFIG),normal)
config_normal:
    echo "$(LAST_CONFIG) == normal"
else
config_normal: clean
    echo "$(LAST_CONFIG) != normal"
    rm -f config.tmp
    echo "normal" > config.tmp
endif

ifeq ($(LAST_CONFIG),debug)
config_debug: ;
else
config_debug: clean
    rm -f config.tmp
    echo "debug" > config.tmp
endif

ifeq ($(LAST_CONFIG),cross)
config_cross: ;
else
config_cross: clean
    rm -f config.tmp
    echo "cross" > config.tmp
en开发者_StackOverflowdif

.PHONY: all compile test clean docs static valgrind config_normal config_debug config_cross

How can I eliminate (or significantly decrease) the duplication between config_normal, config_debug, and config_cross?


If you are using at least version 3.80 of GNU make, you can use $(eval) to create the rules from a template, like this:

define templ
ifeq ($$(LAST_CONFIG),$(config))
config_$(config):
        echo "$$(LAST_CONFIG) == $(config)"
else
config_$(config):
        echo "$$(LAST_CONFIG) != $(config)"
        rm -f config.tmp
        echo "$(config)" > config.tmp
endif
endef

$(foreach config,normal debug cross,$(eval $(templ)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜