In common programming language, using control structures such as if ... else if ... else ... is a very unusual thing, then how to use GNU Makefile in it?
- ifeq
- ifneq
For example: 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
The above foo.sh is achieved with a 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 and run results are as follows:
$ uname -p x86_64 $ bash foo.sh ARCH32=i686 ARCH64=x86_64 $ make -f Makefile ARCH32=i686 ARCH64=x86_64
Thus,
- ifeq ($ (VAR),) is equivalent to the bash [[-z $ VAR]]
- ifneq ($ (on VAR),) is equivalent to the bash [[-n $ VAR]]