Makefile:从另一个列表中删除与多个截断文件名匹配的文件列表

Makefile: Removing a list of files matching several truncated filenames from another list

在 Makefile 中,我想从另一个列表中删除与文件名前缀为 test_ 的模式相匹配的文件列表。

让我们来看看这个 Makefile,它正在做我想做的事情(只是不是我想要的方式):

SRC_FILES = \
    src/path/a.c \
    src/path/b.c \
    src/path/c.c \
    src/path/d.c

TEST_FILES = \
    test/another_path/test_b.c \
    test/another_path/test_c.c \

RESULTING_FILES = \
    $(TEST_FILES) \
    $(filter-out src/path/b.c src/path/c.c,$(SRC_FILES))

all:
    @echo SOURCE FILES:
    @echo $(SRC_FILES)
    @echo ""
    @echo TEST FILES:
    @echo $(TEST_FILES)
    @echo ""
    @echo RESULTING FILES:
    @echo $(RESULTING_FILES)

让我们运行全部:

SOURCE FILES: src/path/a.c src/path/b.c src/path/c.c src/path/d.c

TEST FILES: test/another_path/test_b.c test/another_path/test_c.c

RESULTING FILES: test/another_path/test_b.c test/another_path/test_c.c src/path/a.c src/path/d.c

我们可以看到生成的文件由:

要删除我不想要的源文件,我使用以下行: $(filter-out src/path/b.c src/path/c.c,$(SRC_FILES))

如您所见,我要删除的文件是硬编码的。 如果不对 filepath/filenames 进行硬编码,我如何才能获得与前一行相同的结果? 目标是删除具有等效测试文件 (a.c <-> test_a.c) 的源文件。

编辑:我最初没有解释,但每个文件的源文件路径可能不同,例如:

SRC_FILES = \
    src/path_1/a.c \
    src/path_2/b.c \
    src/path_1/next_path/c.c \
    src/path_4/d.c

将测试文件转换为与文件名匹配的模式:

TEST_PATTERNS = $(addprefix %/,$(patsubst test_%,%,$(notdir $(TEST_FILES))))

现在您可以使用过滤器了:

$(filter-out $(TEST_PATTERNS),$(SRC_FILES))

或者如果你真的想要一个特定的路径而不是一个模式,你可以使用 $(addprefix src/path/,$(...))