Makefile中获取某个路径的父级路径的值

折腾:

【已解决】更新gitbook发布脚本Makefile忽略某些book

期间,已经获取到了当前makefile的路径:

define getCurrentDirAndDirName

$(eval MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST)))

$(eval MAKEFILE_LIST_FIRSTWORD = $(firstword $(MAKEFILE_LIST)))

 

$(eval MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_FIRSTWORD)))

$(eval MAKEFILE_DIR := $(dir $(MAKEFILE_PATH)))

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

$(eval MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST))

$(eval CURRENT_DIR_WITH_SLASH = $(MAKEFILE_DIR))

$(eval CURRENT_DIR = $(MAKEFILE_DIR_NOSLASH))

$(eval CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST)))

# $(eval GITBOOK_ROOT := $(abspath $(CURRENT_DIR))/..)

$(info MAKEFILE_PATH=$(MAKEFILE_PATH))

$(info MAKEFILE_DIR=$(MAKEFILE_DIR))

$(info MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST))

$(info MAKEFILE_DIR_NOSLASH=$(MAKEFILE_DIR_NOSLASH))

$(info CURRENT_DIR=$(CURRENT_DIR))

# $(info GITBOOK_ROOT=$(GITBOOK_ROOT))

endef

但是希望获取父级路径。

试了半天,用:

$(eval GITBOOK_ROOT := $(abspath $(CURRENT_DIR))/..)

$(info GITBOOK_ROOT=$(GITBOOK_ROOT))

输出:

CURRENT_DIR=/Users/crifan/dev/dev_root/gitbook/gitbook_src_root/scientific_network_summary
GITBOOK_ROOT=/Users/crifan/dev/dev_root/gitbook/gitbook_src_root/scientific_network_summary/..

但是希望得到绝对路径

Get parent dir, grandparent dir and so on in gnu make? – Stack Overflow

去找找parents

makefile parents

GNU make: Recursion

makefile – How to tell make to watch dependencies of a sub-make target? – Stack Overflow

gnu make – Sub-makefiles and passing variables upward – Stack Overflow

How to get a Makefile directory path « timmurphy.org

试试:realpath

makefile realpath

Makefile 关于realpath的研究 – magic 的专栏 – CSDN博客

GNU make: File Name Functions

“$(realpath names…)

For each file name in names return the canonical absolute name. A canonical name does not contain any . or .. components, nor any repeated path separators (/) or symlinks. In case of a failure the empty string is returned. Consult the realpath(3) documentation for a list of possible failure causes.

$(abspath names…)

For each file name in names return an absolute name that does not contain any . or .. components, nor any repeated path separators (/). Note that, in contrast to realpath function, abspath does not resolve symlinks and does not require the file names to refer to an existing file or directory. Use the wildcard function to test for existence.”

看这abspath,感觉返回的路径,不应该包含..啊

后来发现是:

自己漏写了个eval的最后的括号)

改为:

$(eval GITBOOK_ROOT := $(abspath $(CURRENT_DIR)/..))
$(info GITBOOK_ROOT=$(GITBOOK_ROOT))

即可正常输出路径:

CURRENT_DIR=/Users/crifan/dev/dev_root/gitbook/gitbook_src_root/scientific_network_summary
GITBOOK_ROOT=/Users/crifan/dev/dev_root/gitbook/gitbook_src_root

然后试试:

$(eval GITBOOK_ROOT_realpath := $(realpath $(CURRENT_DIR)/..))
$(eval GITBOOK_ROOT_abspath := $(abspath $(CURRENT_DIR)/..))
$(info GITBOOK_ROOT_realpath=$(GITBOOK_ROOT_realpath))
$(info GITBOOK_ROOT_abspath=$(GITBOOK_ROOT_abspath))

输出都是正确的,绝对路径:

GITBOOK_ROOT_realpath=/Users/crifan/dev/dev_root/gitbook/gitbook_src_root
GITBOOK_ROOT_abspath=/Users/crifan/dev/dev_root/gitbook/gitbook_src_root

【总结】

此处,已有:

CURRENT_DIR=/Users/crifan/dev/dev_root/gitbook/gitbook_src_root/scientific_network_summary

然后想要获取其父级目录,不带..,则可以用:

$(eval GITBOOK_ROOT_realpath := $(realpath $(CURRENT_DIR)/..))
$(eval GITBOOK_ROOT_abspath := $(abspath $(CURRENT_DIR)/..))

都可以得到:

/Users/crifan/dev/dev_root/gitbook/gitbook_src_root/

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值