简体   繁体   English

Makefile 中变量扩展中的斩波后缀

[英]Chopping suffixes in variable expansion within a Makefile

As the title states, I have a problem being able to remove suffix from a package name if I need to provide several suffixes (pattern-list)如标题所述,如果我需要提供多个后缀(模式列表),我无法从 package 名称中删除后缀

Note: I have forced bash usage in the Makefile ( SHELL=/bin/bash )注意:我在 Makefile ( SHELL=/bin/bash ) 中强制使用 bash

  • DEST_DIR : Makefile variable DEST_DIR : Makefile 变量

  • PKG_LIST : Makefile variable, hold a list of folders names (eg "packageFsOPT1 packageAdc packageMemOPT2....."). PKG_LIST :Makefile 变量,保存文件夹名称列表(例如“packageFsOPT1 packageAdc packageMemOPT2 .....”)。 Some of the names might have OPT1 suffix, some other don't.有些名称可能有 OPT1 后缀,有些则没有。

  • tmp : folder that is existing and contains the folder packageFs, packageAdc, packageMem..... tmp :存在的文件夹,包含文件夹 packageFs、packageAdc、packageMem .....

Here the part of the Makefile that perform copy from tmp to dest along with the name chopping:这是 Makefile 的一部分,它执行从 tmp 到 dest 的复制以及名称截断:

for pkg in $(PKG_LIST) ; do \
    cd tmp && tar xzf $$pkg-$(MAJOR).$(MINOR).$(TARGET).tar.gz tmp ; \
    cp -a tmp/$${pkg%OPT1} $(DEST_DIR) ; \
done

This is "working", meaning that OPT1 is indeed chopped from packageFsOPT1.这是“有效的”,意味着 OPT1 确实是从 packageFsOPT1 中切下来的。 But what I need is to be able to chop different suffixes.但我需要的是能够截断不同的后缀。 In this case OPT1 or OPT2在这种情况下 OPT1 或 OPT2

I tried this:我试过这个:

cp -a tmp/$${pkg%+(OPT1|OPT2)} $(DEST_DIR) ; \

Which is working if tested directly in console but not within the Makefile (not able to chop => OPT1 stays in the folder name, which doesn't exist => copy fail)如果直接在控制台中测试而不是在 Makefile 中测试,这是有效的(无法切碎 => OPT1 保留在文件夹名称中,该文件夹名称不存在 => 复制失败)

I some kind of stuff like this (but lead to pkg_stripped being empty):我有点像这样的东西(但导致 pkg_stripped 为空):

pkg_stripped=$(shell echo ${pkg%+(OPT1|OPT2)}) ; \
cp -a tmp/$$pkg_stripped $(DEST_DIR) ; \

Does anyone have an idea?有人有想法吗?

Edit: Solutions编辑:解决方案

Either (using extended global)要么(使用扩展全局)

for pkg in $(PKG_LIST) ; do \
    cd tmp && tar xzf $$pkg-$(MAJOR).$(MINOR).$(TARGET).tar.gz tmp ; \
    shopt -s extglob ; \
    cp -a tmp/$${pkg%+(OPT1|OPT2)} $(DEST_DIR) ; \
done

Or (using sed)或者(使用 sed)

See MadScientist answer查看MadScientist 的回答

Maybe it's simpler to use GNU make functions?也许使用 GNU make 函数更简单? Something like this:是这样的:

for pkg in $(patsubst %OPT1,%,$(patsubst %OPT2,%,$(PKG_LIST))) ; do \
    cp -a tmp/$${pkg} $(DEST_DIR) ; \
done

This will do the wrong thing (maybe?) if you have names like fooOPT1OPT2 (both will be stripped) but that can be solved as well.如果您有像 fooOPT1OPT2 这样的名称(两者都将被删除),这将做错事(也许?),但这也可以解决。

ETA预计到达时间

If you need both you'll have to do it in the shell. You can't use $(shell...) in a recipe: that's a make function so it's expanded by make before the shell is started: it can't operate on the value of any shell for loop variables of course.如果您需要两者,则必须在 shell 中完成。您不能在食谱中使用$(shell...) :这是一个 make function 所以它在 shell 启动之前由 make 扩展:它不能当然,对循环变量的任何 shell 的值进行操作。

I would simply use sed for this:为此,我会简单地使用sed

for pkg in $(PKG_LIST) ; do \
    prefix=$$(echo "$$pkg" | sed 's/OPT[12]$$//'); \
    cd tmp && tar xzf $$pkg-$(MAJOR).$(MINOR).$(TARGET).tar.gz tmp ; \
    cp -a tmp/$$prefix $(DEST_DIR) ; \
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM