在旧 make 版本上使用非标准 makefile 名称的递归 make

Recursive make with nonstandard makefile name on old make version

我有一个会调用自身的 makefile,以便在编译任何东西之前获得编译器的许可证,并在编译失败时释放许可证,如下所示:

.PHONY main_target
main_target:
    @license_grab &
    @sleep 2
    -@$(MAKE) real_target
    @license_release

如果 makefile 被命名为 "makefile",这将非常有效。但是,如果我复制 makefile 来试验某些东西,并使用 make -f makefile_copy 调用它,那么在递归调用中会使用错误的 makefile。如果不在 makefile 本身中对 makefile 名称进行硬编码,如何防止这种情况发生?

编辑: 不幸的是我一直在使用 GNU Make 版本 3.79.1,所以我不能使用 MAKEFILE_LIST,这显然 introduced in version 3.80. Therefore none of the answers in this question 可以工作对我来说。

您可以使用 MAKEFILE_LIST 变量:

THIS_MAKEFILE := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))

.PHONY main_target
main_target:
        @license_grab &
        @sleep 2
        -@$(MAKE) -f $(THIS_MAKEFILE) real_target
        @license_release

您可以在 makefile 外部设置 MAKE 变量,以包含 makefile 名称(当然,除非它被覆盖)。像这样(bash):

MAKE="make -f makefile_copy" make -e -f makefile_copy

或这个(在几乎所有 shell 中):

make MAKE="make -f makefile_copy" -f makefile_copy