Paul D. Smith在“多体系结构构建”一文中对如何分离源目录和目标目录进行了出色的研究。描述了以下方法(有其缺点):
但是,我找到了更简单的解决方案-使用较小的样板,而无需递归调用make
。如果使用带有Guile的GNU Make,我们可以使用Guilechdir
函数从Makefile更改当前的工作目录。同样,我们可以mkdir
在此之前通过创建目录。
data ?= ./data/
# Create $(data) directory if it is not exist (just for example)
$(guile (if (not (access? "$(data)" F_OK)) (mkdir "$(data)") ))
# Set the new correct value of CURDIR (before changing directory)
CURDIR := $(abspath $(data))
# Change the current directory to $(data)
$(guile (chdir "$(data)"))
# Another way of updating CURDIR
# — via sub-shell call after changing directory
# CURDIR := $(shell pwd)
# Don't try to recreate Makefile file
# that is disappeared now from the current directory
Makefile : ;
$(info CURDIR = $(CURDIR) )
$(info PWD = $(shell pwd) )
假设:data
变量在上下文中可用,$(data)
目录的父级可以访问,路径可以是相对的。
srcdir := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
ifeq (,$(filter guile,$(.FEATURES)))
$(warning Guile is required to change the current directory.)
$(error Your Make version $(MAKE_VERSION) is not built with support for Guile)
endif
$(MAKEFILE_LIST): ;
$(guile (if (not (file-exists? "$(data)")) (mkdir "$(data)") ))
ORIGCURDIR := $(CURDIR)
CURDIR := $(realpath $(data))
$(guile (chdir "$(data)"))
ifneq ($(CURDIR),$(realpath .))
$(error Cannot change the current directory)
endif
$(warning CURDIR is changed to "$(data)")
请记住,include
指令中的相对路径默认情况下是从当前目录计算得出的,因此它取决于位置-是在此样板之前还是之后使用。
注意:$(data)
不得在规则中使用;$(srcdir)
可用于指定相对于此Makefile文件位置的文件。
此方法已在GNU Make 4.0和4.2.1中进行了测试。
观察到一个小问题。abspath
更改当前目录后,该函数无法正常工作-它将继续按照旧的路径解析相对路径CURDIR
;realpath
正常工作。
同样,该方法可能具有其他未知的缺点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句