You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Makefile适配不同长度路径,提取测试名并创建目录?

解决方案

你可以利用Makefile内置的notdir和basename函数来实现,这两个函数能自动适配任意长度的路径,不需要手动拆分路径的每一部分:

修改后的Makefile代码如下:

test_path := /dir1/dir2/dir3/dir4/test.sv

# 提取路径中的文件名(带后缀)
test_filename := $(notdir $(test_path))
# 去掉文件名的后缀,得到测试名
test_name := $(basename $(test_filename))

all:
    echo test_filename = $(test_filename)
    echo test_name = $(test_name)
    mkdir $(test_name)

关键函数说明

  • $(notdir $(test_path)):自动剥离路径中的所有目录部分,只保留最后一个文件名(比如输入/dir1/dir2/test.sv时返回test.sv,输入/a/b/c/d/e/f.sv时返回f.sv)
  • $(basename $(test_filename)):去掉文件名的后缀部分,只保留主文件名(比如输入test.sv返回test,输入demo.txt返回demo)

这样不管你的test_path是3层目录还是更多层级,都能准确提取到测试名并创建对应目录,不会再出现误创建目录的问题。

内容的提问来源于stack exchange,提问作者VELAMALA NITHIN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 05:28:22