有没有办法强制重建目标(make -B)而不重建其依赖项?

时间:2017-02-09 14:25:32

标签: linux shell unix makefile gnu-make

GNU Make有-B选项,强制make忽略现有目标。它允许重建目标,但它也会重建目标的所有依赖关系树。我想知道是否有办法强制重建目标而不重建其依赖项(使用GNU Make选项,Makefile中的设置,兼容的make - 如软件等)?

问题说明:

$ mkdir test; cd test
$ unexpand -t4 >Makefile <<EOF
huge:
    @echo "rebuilding huge"; date >huge
small: huge
    @echo "rebuilding small"; sh -c 'cat huge; date' >small
EOF
$ make small
rebuilding huge
rebuilding small
$ ls
huge  Makefile  small
$ make small
make: 'small' is up to date.
$ make -B small
rebuilding huge  # how to get rid of this line?
rebuilding small
$ make --version | head -n3
GNU Make 4.0
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2013 Free Software Foundation, Inc.

当然可以rm small; make small,但是有内置的方法吗?

5 个答案:

答案 0 :(得分:3)

一种方法是对您不想重新制作的所有目标使用-o选项:

-o FILE, --old-file=FILE, --assume-old=FILE
        Consider FILE to be very old and don't remake it.

修改

我认为您误读了-B的文档;它说:

  -B, --always-make
        Unconditionally make all targets.

注意,所有目标在这里; huge肯定是一个目标,所以如果您使用-B,它将被重新制作。

然而,我也误解了你的问题。我认为您想重建small而不重建huge,即使huge是新的,但即使{{1}您正在尝试重建small 已更改,对吧?

您绝对不想使用huge。那个选项根本不是你想要的。

通常人们会通过删除-B

来实现这一目标
small

有一个强制重新创建给定目标的选项可能很有用,但该选项不存在。

您可以使用rm -f small make small ,但这意味着您需要知道先决条件的名称,而不仅仅是您想要构建的目标。

答案 1 :(得分:3)

这会有所帮助:

touch huge; make small

答案 2 :(得分:2)

文件在相当讨厌的黑客下:

huge:
    @echo "rebuilding huge"; date >huge

small: $(if $(filter just,${MAKECMDGOALS}),huge)
    @echo "rebuilding small"; sh -c 'cat huge; date' >small

.PHONY: just
just: ;

现在你可以

$ make -B just small

答案 3 :(得分:0)

好吧,到目前为止,我使用了这个部分解决方案:

$ cat >~/bin/remake <<'EOF'
#!/bin/sh
# http://stackoverflow.com/questions/42139227
for f in "$@"; do
  if [ -f "$f" ]; then
    # Make the file very old; it's safer than removing.
    touch --date=@0 "$f" 
  fi
done
make "$@"
EOF
$ chmod u+x ~/bin/remake
$ # PATH="$HOME/bin:$PATH" in .bashrc
$ remake --debug=b small
GNU Make 4.0
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Reading makefiles...
Updating goal targets....
 Prerequisite 'huge' is newer than target 'small'.
Must remake target 'small'.
rebuilding small

答案 4 :(得分:0)

我使用这样的东西:

remake()
{
  for f in "$@"
  do
    [ -f "$f" ] && rm -f "$f"
  done
  make "$@"
}