首页 > 解决方案 > 如何通过 Makefile 中的分隔符拆分字符串?

问题描述

这是我的 Makefile 中的代码片段:

%/foo: %/bar.yaml
    $(BINARY) generate -g go \
        --package-name {COOL_VALUE}
# COOL_VALUE should be the parent folder of a `foo`, e.g., `foo1/foo2/foo -> foo2` 

问题是如何拆分$@字符串/以获得倒数第二个元素:

例如,

make foo1/foo2/foo
> ./binary generate -g go \
   --package-name foo2

make foo3/foo
> ./binary generate -g go \
   --package-name foo3

我的尝试:我想出了

$(eval package_name := $(word 1,$(subst /, ,$@)))
% pick second last element somehow

标签: makefile

解决方案


    $(eval package_folders := $(filter-out foo,$(subst /, ,$@)))
    $(eval package_name := $(word $(words $(package_folders)), $(package_folders)))
    @echo "$(package_name)"

推荐阅读