1

我正在尝试获取 Makefile 中文件名的基本名称;但它总是失败:basename 命令不起作用。这是我的代码:

    list = a.xlib b.lib
    all:
        for each_lib in $(notdir $(list)); \
            do \
                if [[ $$each_lib == *.xlib ]]; then \
                    *** Here I need to get the basename of $$each_lib to some variable, say base_lib *** \
                    cp -p $$base_lib-un.xlib ../../dist/lib/$$each_lib \
                else \
                    cp -p $$each_lib ../../dist/lib/$$each_lib \
                fi; \
            done;

如果我们可以使用列表中每个文件的 basename 的值创建一个变量,那将是完美的。谢谢。

4

3 回答 3

3

$(basename ...) is a make function. It cannot operate on shell variables, because they are expanded later than make commands. You can use the shells $(...) substitution, but you have to double the dollar sign, so makefile does not interpret it as a function.

于 2013-08-02T14:39:04.313 回答
2

查看以下选项之一是否符合您的要求:

list = a.xlib b.lib

libs = $(filter-out %.xlib,$(list))
xlibs = $(filter %.xlib,$(list))

V ?= @

allsh:
    $(V)for each_lib in $(notdir $(libs)); do \
        echo cp -p $$each_lib ../../dist/lib/$$each_lib; \
    done;
    $(V)for each_lib in $(notdir $(xlibs)); do \
        base_lib="$$(basename $$each_lib .xlib)"; \
        echo cp -p $$base_lib-un.xlib ../../dist/lib/$$each_lib; \
    done;

xlibsstripped = $(patsubst %.xlib,%,$(xlibs))
allmake:
    $(V)for each_lib in $(notdir $(libs)); do \
        echo cp -p $$each_lib ../../dist/lib/$$each_lib; \
    done;
    $(V)for each_lib in $(notdir $(xlibsstripped)); do \
        echo cp -p $$each_lib-un.xlib ../../dist/lib/$$each_lib.xlib; \
    done;

运行make allsh/allmake V=以查看正在运行的命令。

于 2013-08-06T14:47:03.563 回答
0

如果您想做的是获取基本名称,我很惊讶这basename不起作用。(您使用的是哪个版本的 Make?)请注意,它是一个 Make 函数,因此它超出了规则:

list = a.xlib b.lib
BASES = $(basename $(list))

您可以使用其他 Make 函数来执行此操作(注意冒号):

list := a.xlib b.lib
list := $(patsubst %.lib,%,$(list))
list := $(patsubst %.xlib,%,$(list))

如果您真正想要做的是移动文件并修改 xlib 文件的名称 ( a-un.xlib=> .../a.xlib),则有一种更简单的方法:

targets := $(addprefix ../../dist/lib/,$(list))

all: $(targets)

../../dist/lib/%.lib: %.lib
    mv $< $@

../../dist/lib/%.xlib: %-un.xlib
    mv $< $@
于 2013-08-06T15:16:48.093 回答