最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Makefile中获取某个路径的父级路径的值

makefile crifan 4219浏览 0评论

折腾:

【已解决】更新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/

转载请注明:在路上 » 【已解决】Makefile中获取某个路径的父级路径的值

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
95 queries in 0.206 seconds, using 23.28MB memory