Incrementing Makefile Variable When Executing Rule

时间:2015-07-28 15:50:29

标签: makefile

My dilemma is the following: I need to create a symbolic link to a different serial port for each of the items in the Makefile variable 'LINKS'. I have the following code.

LINK_PATH = ~/some/path/
LINKS = $(LINK_PATH)/SomeLinkName $(LINK_PATH)/AnotherLinkName $(LINK_PATH)/TheseLinkNamesUnchangeable    
COUNT = 0    

install: $(LINKS)
    #Do other stuff

$(LINKS): $(LINK_PATH)
    ln -s /dev/ttyS$(COUNT) $@

$(LINK_PATH):
    mkdir -p $@

I know that, as it is now, it will just create a bunch of links pointing to /dev/ttyS0. But I need them to be different, preferably sequential. Any ideas? Thanks.

1 个答案:

答案 0 :(得分:2)

if all of the serial ports are defined ahead of time, you can enumerate them and store that list in another variable then use that variable as a target dependency:

LINKS=/path/to/bar /path/to/baz /path/to/woz
COUNTS=$(shell v=`echo $(LINKS) | wc -w`; echo `seq 0 $$(expr $$v - 1)`)

install: $(COUNTS)

$(COUNTS):
    @echo ln -s /dev/ttyS$@ $(shell v=($(LINKS)); echo $${v[$@]})

then, when run:

[user@host: ~]$ make install
ln -s /dev/ttyS0 /path/to/bar
ln -s /dev/ttyS1 /path/to/baz
ln -s /dev/ttyS2 /path/to/woz