Detecting (not) GNU Do when someone runs `make`

I have a project whose makefile uses functions exclusive to GNU Make. Unfortunately, there are platforms that we must support, where GNU make is still not standard at startup make.

One of my colleagues was bitten by this when non-GNU makes an implementation that could not build our code correctly (it expanded the automatic variable to an empty string). I want this to not happen again by creating an explicit error message instead.

What can I write Makefileto distinguish GNU make from non-GNU make, print a clear error, and exit?

I already looked at a workaround for renaming my real make file to GNUmakefileand put a little stub in Makefile, but I would prefer something more direct.

The answers from Beta and Dan Molding look very nice and simple, but on AIX 6.1, the make implementation cannot handle any of them:

$ cat testmake

foo:
        touch foo

ifeq ($(shell $(MAKE) -v | grep GNU),)
$(error this is not GNU Make)
endif

ifeq "${MAKE_VERSION}" ""
$(info GNU Make not detected)
$(error ${MIN_MAKE_VER_MSG})
endif


$ /usr/bin/make -f testmake
"testmake", line 5: make: 1254-055 Dependency line needs colon or double colon operator.
"testmake", line 6: make: 1254-055 Dependency line needs colon or double colon operator.
"testmake", line 7: make: 1254-055 Dependency line needs colon or double colon operator.
"testmake", line 8: make: 1254-055 Dependency line needs colon or double colon operator.
"testmake", line 11: make: 1254-055 Dependency line needs colon or double colon operator.
"testmake", line 12: make: 1254-055 Dependency line needs colon or double colon operator.
"testmake", line 13: make: 1254-055 Dependency line needs colon or double colon operator.
make: 1254-058 Fatal errors encountered -- cannot continue.

I encounter similar problems in both archaic and modern versions (Solaris 8 and 10) of Sun make. This is less critical, but it would be nice to manage.

+5
source share
2 answers

, GNU make GNUmakefile makefile makefile, , , (decoy) makefile, /

default:
    @echo "This requires GNU make, run gmake instead"
    exit 70

GNU GNUmakefile, makefile GNU, , .

, make Makefile, , . FreeBSD BSDmakefile, makefile ( , , make ). AFAICT AIX Solaris make , .

makefile, GNU make, .

-, ( , OSF1, BSD Solaris), SOMETHING=$(shell ...) GNU make, GNU SOMETHING. - , . , $() (.. $(shell foo) /, function, ).

, , , , , :

GNUMAKE=$(shell echo GNUMAKE)

default: gnumake all

gnumake:
        @[ "$(GNUMAKE)" = "GNUMAKE" ] || { echo GNU make required ; exit 70; }

, POSIX sh.

( , $(MAKE) -v , GNU make "make", make GNU make... PATH, make , , SHELL .)

+6

, GNUMake, kludge: "make -v" "GNU" ( , GNU Make have MAKE GNU Make):

ifeq ($(shell $(MAKE) -v | grep GNU),)
$(error this is not GNU Make)
endif

EDIT:
, . , Makefile all Make. Sun Make ( ), , , , , , .

, . , - :

default:
  ./runGNUMake.pl

, makefile. runGNUMake script Perl ( bash ), - "make -v" kludge, , "make -f realMakefile".

+2

All Articles