facebook / facebook/zstd

Build fails depending on how LDFLAGS is passed

Open
#3,190 0 comments 1 reaction 1 assignee Claimed by @felixhandte View on GitHub
build
Dominant language
C
Stars
27.9k
Forks
2.6k
Avg merge
1d 3h
Merged PRs (30d)
8

Description

**Describe the bug**
Building zstd can fail depending on how LDFLAGS is passed to make.

**To Reproduce**
Steps to reproduce the behavior:

1. Download source code.
2. Run `make LDFLAGS="-L/some/extra/dir"` and the build will fail.
3. Run `LDFLAGS="-L/some/extra/dir" make` and 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](https://github.com/facebook/zstd/blob/02ef78be58a222ad21c6c0780b211eb5acd6f6f0/lib/Makefile#L140-L142):

```
$(LIBZSTD): CPPFLAGS += $(CPPFLAGS_DYNLIB)
$(LIBZSTD): CFLAGS += -fPIC -fvisibility=hidden
$(LIBZSTD): LDFLAGS += -shared $(LDFLAGS_DYNLIB)
```

Because of how GNU make handles [variable precedence](https://www.gnu.org/software/make/manual/html_node/Environment.html), 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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.