Other Target variable assignment in make

I'd like to do the following target variable assignment in make. It works in GNU Make but not in BSD Make.

Code:
test: clean debug_compile

debug_compile: ERLCFLAGS += -DTEST
debug_compile: compile compile_test;

Make reports "don't know how to make ERLCFLAGS". Any suggestions would be appreciated.
 
Makefile:
OPTS?="-c"
all:compile
debug_compile:
    make "OPTS=${OPTS} -DDEBUG"

compile:
    @echo ${OPTS}
or
Makefile:
OPTS="-c"
.if !empty(.TARGETS:Mdebug_compile)
OPTS+= -DDEBUG
.endif

all:compile
debug_compile:  compile
compile:
    @echo ${OPTS}
 
BSD make and GNU make compatible:
Makefile:
sources += main.c

system != uname -s
# Optional OS-dependent flags
sysflags = /$(system)
sysflags := $(sysflags:/Linux=-std=c99 -pedantic -D_XOPEN_SOURCE=500 -D_BSD_SOURCE)
sysflags := $(sysflags:/FreeBSD=-std=c99 -pedantic)

# Optional OS-dependent sources
xtrasrc = /$(system)
xtrasrc := $(xtrasrc:/Linux=)
xtrasrc := $(xtrasrc:/FreeBSD=extra.c)
sources += $(xtrasrc)

# Build-related flags
build ?= debug
buildflags = /$(build)
buildflags := $(buildflags:/debug=-D_DEBUG -O0)
buildflags := $(buildflags:/release=-DNDEBUG -O2)

CFLAGS += $(buildflags) $(sysflags) -g
LDFLAGS += -g

objects = $(sources:.c=.o)

all: foo

foo: $(objects)
    $(CC) $(LDFLAGS) -o $@ $(objects)

clean:
    rm -f foo $(objects)

make for debug build, make build=release for release build.
 
Back
Top