在一个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)
让我们运行所有内容:
源文件:src / path / a.c src / path / b.c src / path / c.c src / path / d.c
测试文件:test / another_path / test_b.c test / another_path / test_c.c
结果文件:test / another_path / test_b.c test / another_path / test_c.csrc / path / a.c src / path / d.c
我们可以看到生成的文件是由:
要删除不需要的源文件,请使用以下行:$(filter-out src/path/b.c src/path/c.c,$(SRC_FILES))
如您所见,我要删除的文件是硬编码的。如何获得与上一行相同的结果,但不对文件路径/文件名进行硬编码?目的是删除存在等效测试文件(a.c test_a.c)的源文件。
不确定它是否真的更清洁,但是您可以使用...
RESULTING_FILES = \
$(TEST_FILES) \
$(filter-out $(patsubst test/another_path/test_%.c,src/path/%.c,$(TEST_FILES)),$(SRC_FILES))
因此,“过滤出”的模式只是$(TEST_FILES),其中test/another_path/test_%.c
映射到src/path/%.c
...
$(patsubst test/another_path/test_%.c,src/path/%.c,$(TEST_FILES))