为每个GNU Makefile使用Basename
我想知道如何在每个内部使用basename
?我认为Basename不起作用的原因可能是每个“ $ file”实际上是通往文件的途径,而不仅仅是文件名。
DOCKER_TAG=foobar
DOCKER_RUN=docker run -v `pwd`:/root -it --rm -w /root ${DOCKER_TAG}
TEST_DIR=test_cases
dockerbuild: App/App.csproj
docker build . -t ${DOCKER_TAG}
%.run: %.txt dockerbuild
${DOCKER_RUN} $<
%.out: %.txt dockerbuild
${DOCKER_RUN} $< > $@
%.test: %.out
python3 evaluate.py $<
.PHONY: regression
regression: $(TEST_DIR)/*.txt
for file in $^ ; do \
echo $(basename $${file}).test ; \
done
返回(我期望.txt
已替换为.test
)
test_cases/00_example.txt.test
test_cases/01_simplest_possible.txt.test
test_cases/02_two_users.txt.test
test_cases/03_five_users.txt.test
test_cases/04_one_interferer.txt.test
test_cases/05_equatorial_plane.txt.test
test_cases/06_partially_fullfillable.txt.test
test_cases/07_eighteen_planes.txt.test
test_cases/08_eighteen_planes_northern.txt.test
test_cases/09_ten_thousand_users.txt.test
test_cases/10_ten_thousand_users_geo_belt.txt.test
test_cases/11_one_hundred_thousand_users.txt.test
test_cases/test.txt.test
I am wondering how to use basename
inside for each? I think maybe the reason basename does not work is that each "$file" is actually a path to a file, not simply a filename.
DOCKER_TAG=foobar
DOCKER_RUN=docker run -v `pwd`:/root -it --rm -w /root ${DOCKER_TAG}
TEST_DIR=test_cases
dockerbuild: App/App.csproj
docker build . -t ${DOCKER_TAG}
%.run: %.txt dockerbuild
${DOCKER_RUN} lt;
%.out: %.txt dockerbuild
${DOCKER_RUN} lt; > $@
%.test: %.out
python3 evaluate.py lt;
.PHONY: regression
regression: $(TEST_DIR)/*.txt
for file in $^ ; do \
echo $(basename ${file}).test ; \
done
Returns (I expected .txt
to have been replaced with .test
)
test_cases/00_example.txt.test
test_cases/01_simplest_possible.txt.test
test_cases/02_two_users.txt.test
test_cases/03_five_users.txt.test
test_cases/04_one_interferer.txt.test
test_cases/05_equatorial_plane.txt.test
test_cases/06_partially_fullfillable.txt.test
test_cases/07_eighteen_planes.txt.test
test_cases/08_eighteen_planes_northern.txt.test
test_cases/09_ten_thousand_users.txt.test
test_cases/10_ten_thousand_users_geo_belt.txt.test
test_cases/11_one_hundred_thousand_users.txt.test
test_cases/test.txt.test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$(basename)
函数在 shell 运行循环之前由make
求值。因此,您有效地运行了shell 的
basename
命令也会删除路径,但您可能想用类似的方法来修复此问题,当然,更好的解决方案是使用
make
函数反而。如果您想做的不仅仅是打印文件名,更惯用的解决方案是创建一个模式规则并在每个输入文件上运行它。
The
$(basename)
function is evaluated bymake
before the shell runs the loop. So you are effectively runningThe shell's
basename
command also removes the path, but you might want to fix this with something likethough of course, a better solution is to use
make
functions instead.If you want to do something more than just print the file names, a more idiomatic solution is to create a pattern rule and run it on each of the input files.