在 FreeBSD 下,Make 在错误的目录中启动

马哈茂德·库德西

我有一个非常简单的 Makefile,它只是输出到另一个 Makefile:

all:
    cd src && make all

我的目录结构(Makefile在顶级目录中):

[I] mqudsi@php ~/bbcp> tree -d
.
├── bin
│   └── FreeBSD
├── obj
│   └── FreeBSD
├── src
└── utils

这在 Linux 下工作得很好,但在 FreeBSD 下,它给出了一个关于src找不到的错误

为了调试,我更新了的Makefile命令pwd; cd src && make all,我发现,不知何故,当我运行make在顶级目录,它被下执行./obj,而不是,这意味着它在寻找./obj/src/cd成。

除了我不知道它为什么这样做这一事实之外,我确信调用gmake而不是make在 FreeBSD 下会处理它,但事实并非如此(我松了一口气,因为我不敢相信在核心操作方面,BSD make 和 GNU make 之间存在巨大差异)。

奇怪的是,删除obj使一切正常。所以在存在obj目录的情况下makecds 进入./obj第一个;否则它会按照您的预期执行。

马哈茂德·库德西

在这里回答我自己的问题。

从 FreeBSDmake手册页:

.OBJDIR      A path to the directory where the targets are built.  Its
             value is determined by trying to chdir(2) to the follow-
             ing directories in order and using the first match:

             1.   ${MAKEOBJDIRPREFIX}${.CURDIR}

              (Only if `MAKEOBJDIRPREFIX' is set in the environ-
              ment or on the command line.)

             2.   ${MAKEOBJDIR}

              (Only if `MAKEOBJDIR' is set in the environment or
              on the command line.)

             3.   ${.CURDIR}/obj.${MACHINE}

             4.   ${.CURDIR}/obj

             5.   /usr/obj/${.CURDIR}

             6.   ${.CURDIR}

             Variable expansion is performed on the value before it's
             used, so expressions such as
               ${.CURDIR:S,^/usr/src,/var/obj,}
             may be used.  This is especially useful with
             `MAKEOBJDIR'.

             `.OBJDIR' may be modified in the makefile via the special
             target `.OBJDIR'.  In all cases, make will chdir(2) to
             the specified directory if it exists, and set `.OBJDIR'
             and `PWD' to that directory before executing any targets.

关键部分是

在所有情况下,如果指定目录存在,make 将 chdir(2) 到指定目录,并.OBJDIR'在执行任何目标之前PWD' 设置为该目录。

相比之下,GNU make 手册页没有提及任何类型的 自动确定OBJDIR,只是在设置时才会使用。

解决方案是OBJDIR通过伪目标覆盖变量.OBJDIR

.OBJDIR: ./

all:
    cd src && make
clean:
    cd src && make clean

另一种解决方案是在cd目标前面加上${CURDIR},在chdirinto之后不会修改OBJDIR

gmake然而,我不明白为什么表现得如此。这对我来说几乎就像一个错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章