I'm writing a Makefile for a Go application, and would like to be able to pass in an environment variable to specify debug flags for the go build command.
The script for building the service without debug flags looks like this:
cd ./authentication-service && env GOOS=linux CGO_ENABLED=0 go build -o ${AUTH_BINARY} ./cmd/api
I'd like to be able to inject these flags after the go build
part of the command, like so:
cd ./authentication-service && env GOOS=linux CGO_ENABLED=0 go build -gcflags=all="-N -l" ${AUTH_BINARY} ./cmd/api
I've read that you can use the echo
command to send arguments like this, but when I try the following, I get an error
cd ./authentication-service && env GOOS=linux CGO_ENABLED=0 go build `if [ "${DEBUG}" = 'auth' ]; then echo '-gcflags=all="-N -l"'; fi` -o authApp ./cmd/api
invalid value "all=\"-N" for flag -gcflags: unterminated " string
usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.
make: *** [build_broker] Error 2
How can I conditionally supply -gcflags=all="-N -l"
to this build command when a certain environment variable check returns true?