Skip to content

Commit 4565cfe

Browse files
committed
mirror: Fix flags usage for static build
The flag we've been using and documenting for static builds does the work, but is not correct: - It should be "-static" (compiler option, as expected by gcc for example) and not "--static" (linker option, gcc translates the former into the latter when invoking the linker). - It should not be passed with the CFLAGS (or in our case, EXTRA_CFLAGS), but instead with the LDFLAGS (or EXTRA_LDFLAGS), because it is a flag that will be passed to the linker. Fix it in the README.md and in CI/release workflows. We also need to adjust feature detection to take the EXTRA_LDFLAGS (so far ignored) into account. Signed-off-by: Quentin Monnet <[email protected]>
1 parent 81b1c1c commit 4565cfe

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
- name: Build bpftool, with libbfd, static build
7676
run: |
7777
make -C src clean
78-
EXTRA_CFLAGS=--static make -j -C src V=1
78+
EXTRA_LDFLAGS=-static make -j -C src V=1
7979
./src/bpftool 2>&1 | grep -q Usage
8080
./src/bpftool -p version | \
8181
tee /dev/stderr | \

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
env:
8888
LLVM_PATH: ${{ env[format('LLVM_{0}', matrix.arch)] }}
8989
run: |
90-
EXTRA_CFLAGS=--static \
90+
EXTRA_LDFLAGS=-static \
9191
LLVM_CONFIG="${GITHUB_WORKSPACE}/${{ env.LLVM_PATH }}/bin/llvm-config" \
9292
LLVM_STRIP="${GITHUB_WORKSPACE}/${{ env.LLVM_PATH }}/bin/llvm-strip" \
9393
HOSTAR="${GITHUB_WORKSPACE}/${{ env.LLVM_PATH }}/bin/llvm-ar" \
@@ -112,7 +112,7 @@ jobs:
112112
apt-get install -y make pkg-config gcc \
113113
libelf-dev libcap-dev libstdc++-11-dev zlib1g-dev && \
114114
cd /build/bpftool && \
115-
EXTRA_CFLAGS=--static \
115+
EXTRA_LDFLAGS=-static \
116116
LLVM_CONFIG='/build/${{ env.LLVM_PATH }}/bin/llvm-config' \
117117
LLVM_STRIP='/build/${{ env.LLVM_PATH }}/bin/llvm-strip' \
118118
CLANG='/build/${{ env.LLVM_PATH }}/bin/clang' \

.github/workflows/static-build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- name: Build bpftool (static build, default LLVM disassembler)
4444
working-directory: 'bpftool'
4545
run: |
46-
EXTRA_CFLAGS=--static \
46+
EXTRA_LDFLAGS=-static \
4747
LLVM_CONFIG="${GITHUB_WORKSPACE}/${LLVM_PATH}/bin/llvm-config" \
4848
LLVM_STRIP="${GITHUB_WORKSPACE}/${LLVM_PATH}/bin/llvm-strip" \
4949
make -j -C src V=1

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ example, we can create a static build with the following commands:
123123

124124
```console
125125
$ cd src
126-
$ EXTRA_CFLAGS=--static make
126+
$ EXTRA_LDFLAGS=-static make
127127
```
128128

129129
Note that to use the LLVM disassembler with static builds, we need a static
@@ -150,12 +150,12 @@ version of the LLVM library installed on the system:
150150
$ make -j -C llvm_build llvm-config llvm-libraries
151151
```
152152

153-
2. Build bpftool with `EXTRA_CFLAGS` set to `--static`, and by passing the
153+
2. Build bpftool with `EXTRA_LDFLAGS` set to `-static`, and by passing the
154154
path to the relevant `llvm-config`.
155155

156156
```console
157157
$ cd bpftool
158-
$ LLVM_CONFIG=../../llvm_build/bin/llvm-config EXTRA_CFLAGS=--static make -j -C src
158+
$ LLVM_CONFIG=../../llvm_build/bin/llvm-config EXTRA_LDFLAGS=-static make -j -C src
159159
```
160160

161161
### Build bpftool's man pages

src/Makefile.feature

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ CFLAGS_BACKUP := $(CFLAGS)
66
CFLAGS := $(EXTRA_CFLAGS)
77
CFLAGS += -Wno-unused-command-line-argument
88

9+
LDFLAGS_BACKUP := $(LDFLAGS)
10+
LDFLAGS := $(EXTRA_LDFLAGS)
11+
912
ifeq ($(V),1)
1013
LOG=$(warning $(1))
1114
LOG_RES = (echo $(1) && >&2 echo result: $(1))
@@ -43,7 +46,7 @@ LIBBFD_PROBE += ' bfd_demangle(0, 0, 0);'
4346
LIBBFD_PROBE += ' return 0;'
4447
LIBBFD_PROBE += '}'
4548
LIBBFD_PROBE_CMD = printf '%b\n' $(LIBBFD_PROBE) | \
46-
$(CC) $(CFLAGS) -Wall -Werror -x c -DPACKAGE='"bpftool"' - $(1) -o /dev/null >/dev/null
49+
$(CC) $(CFLAGS) -Wall -Werror -x c -DPACKAGE='"bpftool"' - $(LDFLAGS) $(1) -o /dev/null >/dev/null
4750

4851
define libbfd_build
4952
$(call detect,$(LIBBFD_PROBE_CMD))
@@ -76,7 +79,7 @@ DISASSEMBLER_PROBE += ' return 0;'
7679
DISASSEMBLER_PROBE += '}'
7780

7881
DISASSEMBLER_PROBE_CMD = printf '%b\n' $(1) | \
79-
$(CC) $(CFLAGS) -Wall -Werror -x c -DPACKAGE='"bpftool"' - -lbfd -lopcodes -S -o - >/dev/null
82+
$(CC) $(CFLAGS) -Wall -Werror -x c -DPACKAGE='"bpftool"' - $(LDFLAGS) -lbfd -lopcodes -S -o - >/dev/null
8083
define disassembler_build
8184
$(call detect,$(DISASSEMBLER_PROBE_CMD))
8285
endef
@@ -109,7 +112,7 @@ LIBCAP_PROBE += ' cap_free(0);'
109112
LIBCAP_PROBE += ' return 0;'
110113
LIBCAP_PROBE += '}'
111114
LIBCAP_PROBE_CMD = printf '%b\n' $(LIBCAP_PROBE) | \
112-
$(CC) $(CFLAGS) -Wall -Werror -x c - -lcap -S -o - >/dev/null
115+
$(CC) $(CFLAGS) -Wall -Werror -x c - $(LDFLAGS) -lcap -S -o - >/dev/null
113116

114117
define libcap_build
115118
$(call detect,$(LIBCAP_PROBE_CMD))
@@ -130,19 +133,15 @@ LLVM_PROBE += ' LLVMDisposeMessage(triple);'
130133
LLVM_PROBE += ' return 0;'
131134
LLVM_PROBE += '}'
132135

133-
# We need some adjustments for the flags.
134-
# - $(CFLAGS) was set to parent $(EXTRA_CFLAGS) at the beginning of this file.
135-
# - $(EXTRA_LDFLAGS) from parent Makefile should be kept as well.
136-
# - Libraries to use depend on whether we have a static or shared version of
137-
# LLVM, pass the llvm-config flag and adjust the list of libraries
138-
# accordingly.
136+
# Libraries to use depend on whether we have a static or shared version of
137+
# LLVM, pass the llvm-config flag and adjust the list of libraries accordingly.
139138
FEATURE_LLVM_CFLAGS := $(CFLAGS) $(shell $(LLVM_CONFIG) --cflags 2>/dev/null)
140139
FEATURE_LLVM_LIBS := $(shell $(LLVM_CONFIG) --libs target 2>/dev/null)
141140
ifeq ($(shell $(LLVM_CONFIG) --shared-mode 2>/dev/null),static)
142141
FEATURE_LLVM_LIBS += $(shell $(LLVM_CONFIG) --system-libs target 2>/dev/null)
143142
FEATURE_LLVM_LIBS += -lstdc++
144143
endif
145-
FEATURE_LDFLAGS := $(EXTRA_LDFLAGS) $(shell $(LLVM_CONFIG) --ldflags 2>/dev/null)
144+
FEATURE_LDFLAGS := $(LDFLAGS) $(shell $(LLVM_CONFIG) --ldflags 2>/dev/null)
146145

147146
LLVM_PROBE_CMD = printf '%b\n' $(LLVM_PROBE) | \
148147
$(CC) $(FEATURE_LLVM_CFLAGS) $(FEATURE_LDFLAGS) \
@@ -174,4 +173,5 @@ $(foreach feature,$(filter-out libbfd%,$(FEATURE_DISPLAY)), \
174173
$(call feature_print_status,$(feature-$(feature)),$(feature)))
175174

176175
CFLAGS := $(CFLAGS_BACKUP)
176+
LDFLAGS := $(LDFLAGS_BACKUP)
177177
undefine LOG LOG_RES

0 commit comments

Comments
 (0)