最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】makefile中获取当前文件夹名

makefile crifan 6879浏览 0评论

折腾:

【已解决】Mac中写Makefile或脚步实现自动化执行命令

期间,需要在Makefile中获取当前文件夹名

makefile – Get base name of folder in GNU make – Stack Overflow

directory – How to get the basename of the current folder as variable in a makefile? – Stack Overflow

gnu make – How to get current relative directory of your Makefile? – Stack Overflow

makefile get folder name

在此期间,想要:

【已解决】makefile中如何打印时只输出一次变量的值

【总结】

然后使用:

# MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))

# CURRENT_DIR := $(notdir $(patsubst %/,%,$(MAKEFILE_DIR))

MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))

MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_LASTWORD))

MAKEFILE_DIR := $(dir $(MAKEFILE_PATH))

MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR))

CURRENT_DIR := $(notdir $(MAKEFILE_DIR_PATSUBST))

.PHONY : cur_dir

cur_dir:

  @echo MAKEFILE_LIST=$(MAKEFILE_LIST)

  @echo MAKEFILE_LIST_LASTWORD=$(MAKEFILE_LIST_LASTWORD)

  @echo MAKEFILE_PATH=$(MAKEFILE_PATH)

  @echo MAKEFILE_DIR=$(MAKEFILE_DIR)

  @echo MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST)

  @echo CURRENT_DIR=$(CURRENT_DIR)

可以得到所有的希望得到的值:

➜  http_summary git:(master) ✗ make cur_dir

MAKEFILE_LIST= Makefile

MAKEFILE_LIST_LASTWORD=Makefile

MAKEFILE_PATH=/Users/crifan/GitBook/Library/Import/http_summary/Makefile

MAKEFILE_DIR=/Users/crifan/GitBook/Library/Import/http_summary/

MAKEFILE_DIR_PATSUBST=/Users/crifan/GitBook/Library/Import/http_summary

CURRENT_DIR=http_summary

另外换成:

<code>CURDIR_NOTDIR = $(notdir $(CURDIR))
</code>

cur_dir:

  @echo CURDIR=$(CURDIR)

  @echo CURDIR_NOTDIR=$(CURDIR_NOTDIR)

也可以:

<code>➜  http_summary git:(master) ✗ make cur_dir
CURDIR=/Users/crifan/GitBook/Library/Import/http_summary
</code>

CURDIR_NOTDIR=http_summary

但是这个方法有个缺陷,换成其他目录(比如父目录)调用,结果就是调用时的目录的名字了:

<code>➜  http_summary git:(master) ✗ cd ..
➜  Import ll
total 14320
drwxr-xr-x  12 crifan  staff   384B 12  7 10:27 http_summary
drwxr-xr-x  14 crifan  staff   448B 11 20 19:20 httpzhi_shi_zheng_li
-rw-r--r--   1 crifan  staff   7.0M 11 20 17:59 httpzhi_shi_zheng_li.zip
➜  Import make -f http_summary/Makefile cur_dir
MAKEFILE_LIST= http_summary/Makefile
MAKEFILE_LIST= http_summary/Makefile
MAKEFILE_LIST_LASTWORD=http_summary/Makefile
MAKEFILE_PATH=/Users/crifan/GitBook/Library/Import/http_summary/Makefile
MAKEFILE_DIR=/Users/crifan/GitBook/Library/Import/http_summary/
MAKEFILE_DIR_PATSUBST=/Users/crifan/GitBook/Library/Import/http_summary
CURRENT_DIR=http_summary
CURDIR=/Users/crifan/GitBook/Library/Import
CURDIR_NOTDIR=Import
</code>

所以,还是前面的办法比较保险。

转载请注明:在路上 » 【已解决】makefile中获取当前文件夹名

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.157 seconds, using 22.12MB memory