Skip to content

Commit 930193a

Browse files
authored
chore: support dynamically link against libpcap (#260)
1 parent a871ce9 commit 930193a

File tree

8 files changed

+61
-6
lines changed

8 files changed

+61
-6
lines changed

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ permissions:
1212

1313
jobs:
1414

15+
build-with-dynamic-link:
16+
runs-on: ubuntu-latest
17+
timeout-minutes: 5
18+
steps:
19+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
20+
21+
- name: Set up Go
22+
uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5
23+
with:
24+
# renovate: datasource=golang-version depName=golang
25+
go-version: '1.23.6'
26+
27+
- name: Install Deps
28+
run: |
29+
sudo apt-get install -y gcc flex bison make libelf-dev autoconf libpcap-dev
30+
31+
- name: Build
32+
run: |
33+
make build-dynamic-link
34+
35+
- name: Test
36+
run: |
37+
file ./ptcpdump | grep 'dynamically linked'
38+
ldd ./ptcpdump | grep 'libpcap.so'
39+
sudo ./ptcpdump -i any -c 2
40+
41+
1542
build:
1643
runs-on: ubuntu-latest
1744
timeout-minutes: 5

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ GOOS ?= $(shell go env GOOS)
2323
LDFLAGS := -linkmode "external" -extldflags "-static"
2424
LDFLAGS += -X github.com/mozillazg/ptcpdump/internal.Version=$(VERSION)
2525
LDFLAGS += -X github.com/mozillazg/ptcpdump/internal.GitCommit=$(GIT_COMMIT)
26-
COVERAGE_FLAG ?= ''
27-
COVERAGE_ARGS ?= ''
26+
LDFLAGS_DYNAMIC := -X github.com/mozillazg/ptcpdump/internal.Version=$(VERSION)
27+
LDFLAGS_DYNAMIC += -X github.com/mozillazg/ptcpdump/internal.GitCommit=$(GIT_COMMIT)
28+
COVERAGE_FLAG ?=
29+
COVERAGE_ARGS ?=
2830

2931
CARCH ?= $(shell uname -m)
3032
LIBPCAP_ARCH = $(CARCH)-unknown-linux-gnu
@@ -62,6 +64,11 @@ build: libpcap
6264
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \
6365
CGO_ENABLED=1 go build -tags static -ldflags "$(LDFLAGS)" $(COVERAGE_FLAG)
6466

67+
68+
.PHONY: build-dynamic-link
69+
build-dynamic-link:
70+
CGO_ENABLED=1 go build -tags dynamic -ldflags "$(LDFLAGS_DYNAMIC)" $(COVERAGE_FLAG)
71+
6572
.PHONY: test
6673
test:
6774
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ replace (
108108
github.com/cilium/ebpf => github.com/mozillazg/ebpf v0.17.3-0.20250207013525-5746abfb2b20
109109
// github.com/cilium/ebpf => ../../cilium/ebpf
110110
github.com/gopacket/gopacket => github.com/mozillazg/gopacket v0.0.0-20241026043817-048341de5231
111+
112+
github.com/jschwinger233/elibpcap => github.com/mozillazg/elibpcap v0.0.0-20250314124526-312841a0730a
111113
// github.com/gopacket/gopacket => ../../gopacket/gopacket
112114
github.com/x-way/pktdump => github.com/mozillazg/pktdump v0.0.9-0.20241102131745-63c34f34f0d1
113115
// github.com/x-way/pktdump => ../../x-way/pktdump

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ github.com/josharian/native v0.0.0-20200817173448-b6b71def0850/go.mod h1:7X/rasw
116116
github.com/josharian/native v1.0.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
117117
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
118118
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
119-
github.com/jschwinger233/elibpcap v1.0.0 h1:UQuP3XT9orz26lW6DPTu9B9OBYirB57LmhPNGxQqU2U=
120-
github.com/jschwinger233/elibpcap v1.0.0/go.mod h1:fUmq00C6Pechtr089JDPhvIc6TxrbUHDlZ5QCYc9tJQ=
121119
github.com/jsimonetti/rtnetlink v0.0.0-20190606172950-9527aa82566a/go.mod h1:Oz+70psSo5OFh8DBl0Zv2ACw7Esh6pPUphlvZG9x7uw=
122120
github.com/jsimonetti/rtnetlink v0.0.0-20200117123717-f846d4f6c1f4/go.mod h1:WGuG/smIU4J/54PblvSbh+xvCZmpJnFgr3ds6Z55XMQ=
123121
github.com/jsimonetti/rtnetlink v0.0.0-20201009170750-9c6f07d100c1/go.mod h1:hqoO/u39cqLeBLebZ8fWdE96O7FxrAsRYhnVOdgHxok=
@@ -185,6 +183,8 @@ github.com/mozillazg/cri-client v0.31.0-alpha.0.0.20241019023238-87687176fd67 h1
185183
github.com/mozillazg/cri-client v0.31.0-alpha.0.0.20241019023238-87687176fd67/go.mod h1:pFm23AAi/gIlW9FGrWPTPnGe1xsyGHPFFO/zezc4w90=
186184
github.com/mozillazg/ebpf v0.17.3-0.20250207013525-5746abfb2b20 h1:DrBwhJyOCDIDLvzWcwpK2rj3OLCEIjK/EJdKkgtBQOc=
187185
github.com/mozillazg/ebpf v0.17.3-0.20250207013525-5746abfb2b20/go.mod h1:9X5VAsIOck/nCAp0+nCSVzub1Q7x+zKXXItTMYfNE+E=
186+
github.com/mozillazg/elibpcap v0.0.0-20250314124526-312841a0730a h1:mLZ2OOjxRLMDG0Ug2fhBeylG+ntdZOGEOjAW2QFtLO4=
187+
github.com/mozillazg/elibpcap v0.0.0-20250314124526-312841a0730a/go.mod h1:fUmq00C6Pechtr089JDPhvIc6TxrbUHDlZ5QCYc9tJQ=
188188
github.com/mozillazg/gopacket v0.0.0-20241026043817-048341de5231 h1:uvhf0oGPfJ24Lc5/N2ysh9sQc71Pog67zxKiLVS4/Qg=
189189
github.com/mozillazg/gopacket v0.0.0-20241026043817-048341de5231/go.mod h1:WnFrU1Xkf5lWKV38uKNR9+yYtppn+ZYzOyNqMeH4oNE=
190190
github.com/mozillazg/pktdump v0.0.9-0.20241102131745-63c34f34f0d1 h1:kVgiW9P9tfqPXrIU/ZKC8XwkhHkri3s439D+RqSpF6g=

vendor/github.com/jschwinger233/elibpcap/README.md

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/jschwinger233/elibpcap/compile.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/jschwinger233/elibpcap/compile_static.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ github.com/inconshreveable/mousetrap
276276
# github.com/josharian/native v1.1.0
277277
## explicit; go 1.13
278278
github.com/josharian/native
279-
# github.com/jschwinger233/elibpcap v1.0.0
279+
# github.com/jschwinger233/elibpcap v1.0.0 => github.com/mozillazg/elibpcap v0.0.0-20250314124526-312841a0730a
280280
## explicit; go 1.21.0
281281
github.com/jschwinger233/elibpcap
282282
# github.com/klauspost/compress v1.16.7
@@ -607,6 +607,7 @@ rsc.io/binaryregexp
607607
rsc.io/binaryregexp/syntax
608608
# github.com/cilium/ebpf => github.com/mozillazg/ebpf v0.17.3-0.20250207013525-5746abfb2b20
609609
# github.com/gopacket/gopacket => github.com/mozillazg/gopacket v0.0.0-20241026043817-048341de5231
610+
# github.com/jschwinger233/elibpcap => github.com/mozillazg/elibpcap v0.0.0-20250314124526-312841a0730a
610611
# github.com/x-way/pktdump => github.com/mozillazg/pktdump v0.0.9-0.20241102131745-63c34f34f0d1
611612
# k8s.io/cri-api => github.com/mozillazg/cri-api v0.32.0-alpha.1.0.20241019013855-3dc36f8743df
612613
# k8s.io/cri-client => github.com/mozillazg/cri-client v0.31.0-alpha.0.0.20241019023238-87687176fd67

0 commit comments

Comments
 (0)