在常见的编程语言中,使用条件控制结构诸如if ... else if ... else...是很寻常的事情,那么在GNU Makefile中如何使用呢?

  • ifeq
  • ifneq

例如:foo.sh

 #!/bin/bash

 ARCH=$(uname -p)
if [[ $ARCH == "x86_64" ]]; then
ARCH32="i686"
ARCH64="x86_64"
elif [[ $ARCH == "ppc64le" ]]; then
ARCH32=""
ARCH64="ppc64le"
else
ARCH32=$ARCH
ARCH64=""
fi if [[ -n $ARCH32 ]]; then
OUTPUT+=" ARCH32=$ARCH32"
fi if [[ -n $ARCH64 ]]; then
OUTPUT+=" ARCH64=$ARCH64"
fi echo $OUTPUT

将上述的foo.sh用Makefile实现就是:

 ARCH = $(shell uname -p)

 ifeq ($(ARCH), x86_64)
ARCH32 = i686
ARCH64 = x86_64
else ifeq ($(ARCH), ppc64le)
ARCH32 =
ARCH64 = ppc64le
else
ARCH32 = $(ARCH)
ARCH64 =
endif ifneq ($(ARCH32), )
OUTPUT += ARCH32=$(ARCH32)
endif ifneq ($(ARCH64), )
OUTPUT += ARCH64=$(ARCH64)
endif all: foo
foo:
@echo $(OUTPUT)

运行foo.sh 和 Makefile 结果如下:

$ uname -p
x86_64
$ bash foo.sh
ARCH32=i686 ARCH64=x86_64
$ make -f Makefile
ARCH32=i686 ARCH64=x86_64

由此可见,

  • ifeq($(VAR), )  等价于bash中的 [[ -z $VAR ]]
  • ifneq($(VAR), ) 等价于bash中的 [[ -n $VAR ]]

那么,在GNU Makefile中使用条件控制结构有什么用?很简单,对不同的平台提供不同的编译选项或者安装包支持。例如:

  • 一个针对不同的平台提供不同的安装包的Makefile
 ARCH = $(shell uname -p)

 ifeq ($(ARCH), x86_64)
ARCH32 = i686
ARCH64 = x86_64
else ifeq ($(ARCH), ppc64)
ARCH32 =
ARCH64 = ppc64
else ifeq ($(ARCH), ppc64le)
ARCH32 =
ARCH64 = ppc64le
else ifeq ($(ARCH), s390x)
ARCH32 =
ARCH64 = s390x
else ifeq ($(ARCH), aarch64)
ARCH32 =
ARCH64 = aarch64
else
ARCH32 = $(ARCH)
ARCH64 =
endif ifneq ($(ARCH32), )
CPKGS32 = libgcc.$(ARCH32)
CPKGS32 += glibc.$(ARCH32)
endif
ifneq ($(ARCH64), )
CPKGS64 = libgcc.$(ARCH64)
CPKGS64 += glibc.$(ARCH64)
endif
CPKGS = $(CPKGS32) $(CPKGS64) all: foo
foo:
@echo $(CPKGS)

参考资料:

05-11 19:26