zstd
zstd copied to clipboard
Build fails depending on how LDFLAGS is passed
Describe the bug Building zstd can fail depending on how LDFLAGS is passed to make.
To Reproduce Steps to reproduce the behavior:
- Download source code.
- Run
make LDFLAGS="-L/some/extra/dir"and the build will fail. - Run
LDFLAGS="-L/some/extra/dir" makeand the build will succeed.
Expected behavior I expect the build to succeed regardless how LDFLAGS was passed to make.
Additional context
I was doing a static cross-compile of zstd and had zlib in a non-standard location.
The build wasn't detecting it, so I added the library directory via LDFLAGS.
The root cause appears to be the following lines from lib/Makefile:
$(LIBZSTD): CPPFLAGS += $(CPPFLAGS_DYNLIB)
$(LIBZSTD): CFLAGS += -fPIC -fvisibility=hidden
$(LIBZSTD): LDFLAGS += -shared $(LDFLAGS_DYNLIB)
Because of how GNU make handles variable precedence, passing LDFLAGS on the command line causes the LDFLAGS += to be entirely ignored. This behavior appears in a few other locations as well but doesn't seem to cause issues. My suggestion is that if the appended flags are critical to the build they should be appended using override VARNAME += extra. This will allow the build to succeed regardless if the flags were passed via command line or environment variable.
Example:
llamasoft:~/$ cat append.mk
all: no-override with-override ;
no-override: FOO += -extra
no-override:
@echo "$@ FOO = $(FOO)"
with-override: override FOO += -extra
with-override:
@echo "$@ FOO = $(FOO)"
llamasoft:~/$ make -f append.mk
no-override FOO = -extra
with-override FOO = -extra
llamasoft:~/$ FOO=-from-env make -f append.mk
no-override FOO = -from-env -extra
with-override FOO = -from-env -extra
llamasoft:~/$ make -f append.mk FOO=-from-cmd
no-override FOO = -from-cmd
with-override FOO = -from-cmd -extra