在常见的编程语言中,使用条件控制结构诸如if ... else if ... else...是很寻常的事情,那么在GNU Makefile中如何使用呢?
- ifeq
- ifneq
例如:foo.sh
1 #!/bin/bash 2 3 ARCH=$(uname -p) 4 if [[ $ARCH == "x86_64" ]]; then 5 ARCH32="i686" 6 ARCH64="x86_64" 7 elif [[ $ARCH == "ppc64le" ]]; then 8 ARCH32="" 9 ARCH64="ppc64le" 10 else 11 ARCH32=$ARCH 12 ARCH64="" 13 fi 14 15 if [[ -n $ARCH32 ]]; then 16 OUTPUT+=" ARCH32=$ARCH32" 17 fi 18 19 if [[ -n $ARCH64 ]]; then 20 OUTPUT+=" ARCH64=$ARCH64" 21 fi 22 23 echo $OUTPUT
将上述的foo.sh用Makefile实现就是:
1 ARCH = $(shell uname -p) 2 3 ifeq ($(ARCH), x86_64) 4 ARCH32 = i686 5 ARCH64 = x86_64 6 else ifeq ($(ARCH), ppc64le) 7 ARCH32 = 8 ARCH64 = ppc64le 9 else 10 ARCH32 = $(ARCH) 11 ARCH64 = 12 endif 13 14 ifneq ($(ARCH32), ) 15 OUTPUT += ARCH32=$(ARCH32) 16 endif 17 18 ifneq ($(ARCH64), ) 19 OUTPUT += ARCH64=$(ARCH64) 20 endif 21 22 all: foo 23 foo: 24 @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
1 ARCH = $(shell uname -p) 2 3 ifeq ($(ARCH), x86_64) 4 ARCH32 = i686 5 ARCH64 = x86_64 6 else ifeq ($(ARCH), ppc64) 7 ARCH32 = 8 ARCH64 = ppc64 9 else ifeq ($(ARCH), ppc64le) 10 ARCH32 = 11 ARCH64 = ppc64le 12 else ifeq ($(ARCH), s390x) 13 ARCH32 = 14 ARCH64 = s390x 15 else ifeq ($(ARCH), aarch64) 16 ARCH32 = 17 ARCH64 = aarch64 18 else 19 ARCH32 = $(ARCH) 20 ARCH64 = 21 endif 22 23 ifneq ($(ARCH32), ) 24 CPKGS32 = libgcc.$(ARCH32) 25 CPKGS32 += glibc.$(ARCH32) 26 endif 27 ifneq ($(ARCH64), ) 28 CPKGS64 = libgcc.$(ARCH64) 29 CPKGS64 += glibc.$(ARCH64) 30 endif 31 CPKGS = $(CPKGS32) $(CPKGS64) 32 33 all: foo 34 foo: 35 @echo $(CPKGS)
参考资料: