如何找出 GNU make 的启动位置?

How to find out where GNU make was started?

如果我使用 -C 选项启动 (GNU) make,它首先进入该目录,然后 $(CURDIR) 将被设置为那个。我想知道是否有任何机制可以在 makefile 中找出最初调用 make 的地方?

也就是说,如果我在 /some/location 中有一个 makefile:

all:
  @echo $(CURDIR)
  @echo $(SOME_MAGIC_VARIABLE_I_AM_LOOKING_FOR)

然后我做:

$ cd /other/place
$ make -C /some/location

我会得到

/some/location
/other/place

印刷。

谢谢!

make中没有这样的变量。

但是,POSIX shell 维护并导出一个 PWD 变量到它们调用的命令。 make 将所有环境变量导入为 make 个变量。

因此,如果您查看 make 变量 $(PWD),您很可能会找到调用 make 的 shell 使用的原始路径。