确保从特定目录调用 make
Ensure that make is invoked from a specific directory
我希望我的所有食谱都从特定目录执行,Makefile
所在的目录。
这是在没有选项的情况下调用 make
时的默认行为,但用户始终可以 运行 :
(cd /somewhere; make -f /path/to/directory/Makefile)
要确保make
工作目录与Makefile
所在目录相同,有多种解决方案:
- 运行
make
没有选项(默认),来自这个特定目录 (cd /path/to/directory; make
)
- 使用
make -C /path/to/directory
每个食谱的 cd
到 /path/to/directory
,像这样:
MAKEFILE_DIR_LOCATION := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
a:
cd ${MAKEFILE_DIR_LOCATION} && do_something_from_makefile_folder
b:
cd ${MAKEFILE_DIR_LOCATION} && do_another_thing_from_makefile_folder
问题是前两个解决方案需要用户调用 Makefile 的操作,而最后一个会使 Makefile
.
混乱
有没有更漂亮的方法来确保所有配方都从 Makefile
所在的目录执行?
额外的解决方案(不起作用)
我也想过比较工作目录($(shell pwd)
)和${MAKEFILE_DIR_LOCATION}
,如果不匹配就退出(至少警告用户make
没有被正确调用),但我找不到如何执行此操作。我试过了:
MAKEFILE_DIR_LOCATION := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
WORKING_DIR := $(shell pwd)
ifneq (${MAKEFILE_DIR_LOCATION}, ${WORKING_DIR})
@error "Please run make from the directory of the Makefile, or use make -C"
endif
a:
do_something_from_makefile_folder
b:
do_another_thing_from_makefile_folder
但是我遇到了一个 missing separator
错误(第 @error
行),或者如果 @error
行缩进了 recipe commences before first target
。
您上次尝试的变体会在正确的目录中使用相同的目标重新调用 Make:
ifneq (${MAKEFILE_DIR_LOCATION},${WORKING_DIR})
%:
$(MAKE) -C ${MAKEFILE_DIR_LOCATION} $@
.PHONY: %
else
## rest of Makefile rules
endif
回答你问的问题而不评论它是否是个好主意,我不确定你在哪里找到这个语法:
@error "Please run make from the directory of the Makefile, or use make -C"
但这绝对是错误的。 error
是一个 make 函数,所以你想要这个:
$(error Please run make from the directory of the Makefile, or use make -C)
我希望我的所有食谱都从特定目录执行,Makefile
所在的目录。
这是在没有选项的情况下调用 make
时的默认行为,但用户始终可以 运行 :
(cd /somewhere; make -f /path/to/directory/Makefile)
要确保make
工作目录与Makefile
所在目录相同,有多种解决方案:
- 运行
make
没有选项(默认),来自这个特定目录 (cd /path/to/directory; make
) - 使用
make -C /path/to/directory
每个食谱的 cd
到/path/to/directory
,像这样:
MAKEFILE_DIR_LOCATION := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
a:
cd ${MAKEFILE_DIR_LOCATION} && do_something_from_makefile_folder
b:
cd ${MAKEFILE_DIR_LOCATION} && do_another_thing_from_makefile_folder
问题是前两个解决方案需要用户调用 Makefile 的操作,而最后一个会使 Makefile
.
有没有更漂亮的方法来确保所有配方都从 Makefile
所在的目录执行?
额外的解决方案(不起作用)
我也想过比较工作目录($(shell pwd)
)和${MAKEFILE_DIR_LOCATION}
,如果不匹配就退出(至少警告用户make
没有被正确调用),但我找不到如何执行此操作。我试过了:
MAKEFILE_DIR_LOCATION := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
WORKING_DIR := $(shell pwd)
ifneq (${MAKEFILE_DIR_LOCATION}, ${WORKING_DIR})
@error "Please run make from the directory of the Makefile, or use make -C"
endif
a:
do_something_from_makefile_folder
b:
do_another_thing_from_makefile_folder
但是我遇到了一个 missing separator
错误(第 @error
行),或者如果 @error
行缩进了 recipe commences before first target
。
您上次尝试的变体会在正确的目录中使用相同的目标重新调用 Make:
ifneq (${MAKEFILE_DIR_LOCATION},${WORKING_DIR})
%:
$(MAKE) -C ${MAKEFILE_DIR_LOCATION} $@
.PHONY: %
else
## rest of Makefile rules
endif
回答你问的问题而不评论它是否是个好主意,我不确定你在哪里找到这个语法:
@error "Please run make from the directory of the Makefile, or use make -C"
但这绝对是错误的。 error
是一个 make 函数,所以你想要这个:
$(error Please run make from the directory of the Makefile, or use make -C)