Nan Xiao
Nan Xiao

Reputation: 17487

The make on FreeBSD doesn't support "ifdef" directives

My FreeBSD is 11.0, and find the make can't process the ifdef directives. For example:

ifdef VERBOSE
Q :=
else
Q := @
endif

The make will complain:

make: "/root/Project/powermon/Makefile" line 13: Need an operator
make: "/root/Project/powermon/Makefile" line 15: Need an operator
make: "/root/Project/powermon/Makefile" line 17: Need an operator

My current solution is using gmake instead. So does any make port on FreeBSD support processing ifdef?

Upvotes: 7

Views: 2249

Answers (2)

Ismael Luceno
Ismael Luceno

Reputation: 2118

A more portable way to write something like that is:

Q$(VERBOSE) := @

Upvotes: 0

user2371524
user2371524

Reputation:

BSD make uses different syntax and has different features than GNU make. The snippet you show should look like the following for BSD make:

.ifdef VERBOSE
Q :=
.else
Q := @
.endif

You have basically three options:

  1. If your software targets specifically BSD, write your Makefile in BSD make syntax. man make(1) has the complete manual for FreeBSD's make.
  2. Write a portable Makefile. This would only use the most basic features of make that every known make tool implements (e.g. don't use any pattern rules etc). This can be tedious and there are other tools helping to manage this by generating the Makefile like cmake or the GNU autotools.
  3. write a GNU make Makefile (might be a good idea to name it GNUmakefile, so it is never interpreted by any other make than GNU make) and rely on the fact that GNU make is available nearly everywhere. For FreeBSD, this would mean installing the appropriate port.

If you go with the third option, you can add a "wrapper" Makefile like e.g. this:

GNUMAKE?= gmake

all:
    ${GNUMAKE} $@

.DEFAULT:
    ${GNUMAKE} $@

.PHONY: all

Typing make on BSD will cause the BSD make to read this file and accordingly call gmake. On a GNU system (where make is GNU make), this file will be ignored when there is a GNUmakefile -- GNU make prefers this over just Makefile.

Upvotes: 9

Related Questions