Multiple stems on makefile target

爷,独闯天下 提交于 2021-01-01 09:22:50

问题


Assume ls produces a b c d and I want to create files a-b b-d c-b, etc. To make a-b, I'd use a command such as cat a b > a-b, and similarly for the other ones. I wanted to use a makefile, but couldn't figure out how. I needed something such as:

FILES := a-b b-d c-b

$(FILES): %1-%2: %1 %2
     cat $^ > $@

Here %1 and %2 would be something like \1 and \2 in some regex notations. Is there any simple way to do this? I found this answer, but it seemed to me too complicated and ugly for such a simple task. Maybe I'm asking too much, though.


回答1:


It's similar to this question, "You basically have two options: you can use auto-generated makefiles,or you can use $(eval ...).", here is the solution using auto-generated method,

FILE =a-b b-c c-d

all: $(FILE)

-include generated.mk

generated.mk: Makefile
        @for f in $(FILE); do \
                echo $$f: `echo $$f | sed 's/-/ /'`; \
                echo '\t'"@cat $$^ > $$"@; \
                echo ;\
        done > $@



回答2:


The following would seem to do what you are asking with a modicum of hassle.

FILES := a-b b-d c-b

$(foreach y,$(FILES),$(eval $y: $(subst -, ,$y)))

$(FILES): %:
    cat $^ >$@

I broke out the dependencies to keep this simple, but I imagine you could get exactly what you were asking for if you really wanted to,



来源:https://stackoverflow.com/questions/35092905/multiple-stems-on-makefile-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!