From 378a10a327d50f5ce75d4dbc62fd624b2f5d3aac Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Tue, 26 Mar 2024 17:37:33 +0000 Subject: [PATCH 1/5] Statically link to libxml2 when `static_build` tag is defined There's an unofficial convention in the Go ecosystem that the `static_build` tag indicates a desire to link statically to external libraries detected with pkg-config - examples include: * [LXC](https://github.com/lxc/go-lxc/blob/ccae595aa49e779f7ecc9250329967aa546acd31/linking_static.go) * [containers-storage](https://github.com/containers/storage/blob/6c835719e98e403dae4a437e3809e247c9561b29/pkg/devicemapper/devmapper_wrapper_static.go) Support this convention by invoking pkg-config with the `--static` option if the `static_build` tag is defined, or without if it isn't. --- README.md | 8 ++++++-- clib/clib.go | 1 - clib/link_dynamic.go | 6 ++++++ clib/link_static.go | 7 +++++++ 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 clib/link_dynamic.go create mode 100644 clib/link_static.go diff --git a/README.md b/README.md index 139a638..066c2a3 100644 --- a/README.md +++ b/README.md @@ -278,9 +278,13 @@ pkg-config --list-all See the first FAQ entry. -### I can't build this library statically +### I can't statically link this module to libxml2 -See prior discussion: https://github.com/lestrrat-go/libxml2/issues/62 +Use the `static_build` tag when building this module, for example: + +```sh +go build -tags static_build +``` ## See Also diff --git a/clib/clib.go b/clib/clib.go index 687945f..de8cf3c 100644 --- a/clib/clib.go +++ b/clib/clib.go @@ -19,7 +19,6 @@ warned. package clib /* -#cgo pkg-config: libxml-2.0 #include #include #include diff --git a/clib/link_dynamic.go b/clib/link_dynamic.go new file mode 100644 index 0000000..bf647a4 --- /dev/null +++ b/clib/link_dynamic.go @@ -0,0 +1,6 @@ +// +build !static_build + +package clib + +// #cgo pkg-config: libxml-2.0 +import "C" diff --git a/clib/link_static.go b/clib/link_static.go new file mode 100644 index 0000000..f55cf82 --- /dev/null +++ b/clib/link_static.go @@ -0,0 +1,7 @@ +// +build static_build + +package clib + +// #cgo pkg-config: --static libxml-2.0 +// #cgo LDFLAGS: -static +import "C" From db21a857260221e3ae7e5d6d123a21df1f8eae60 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Wed, 27 Mar 2024 16:41:49 +0000 Subject: [PATCH 2/5] Add linking configuration CI test for Ubuntu --- .github/workflows/ci.yml | 20 ++++++++++++++++---- test/link/test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 test/link/test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a125f73..5fb2c3e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,15 @@ jobs: strategy: matrix: go: [ '1.21' ] - name: "Test [ Go ${{ matrix.go }} ]" + link: + - type: dynamic + goflags: "" + - type: static + # On Ubuntu, libxml2 is compiled with GCC and is linked to libicu, which introduces a + # stealth dependency on libstdc++ at link-time + goflags: "-ldflags '-extldflags \"-static -lstdc++\"' -tags 'osusergo netgo static_build'" + name: "Test [ Go ${{ matrix.go }}, ${{ matrix.link.type }} linking ]" + env: steps: - name: Checkout repository uses: actions/checkout@v4 @@ -30,8 +38,12 @@ jobs: with: go-version: ${{ matrix.go }} check-latest: true - - name: Test - run: go test ./... + - name: Run Go tests + run: go test ${{ matrix.link.goflags }} ./... + - name: Test linking capability + run: | + go build -o linktest ${{ matrix.link.goflags }} ./test/link + file linktest | grep '${{ matrix.link.type }}ally linked' archlinux: runs-on: ubuntu-latest strategy: @@ -53,4 +65,4 @@ jobs: with: go-version: ${{ matrix.go }} - name: Test - run: go test ./... \ No newline at end of file + run: go test ./... diff --git a/test/link/test.go b/test/link/test.go new file mode 100644 index 0000000..4310f16 --- /dev/null +++ b/test/link/test.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/lestrrat-go/libxml2" +) + +func main() { + doc, err := libxml2.ParseHTMLString(`

Hello, World!

Lorem Ipsum

`) + if err != nil { + panic(err) + } + doc.Free() +} From cd5124492c6aba13745d3f59ff3b0a83028cfd97 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Wed, 27 Mar 2024 16:52:22 +0000 Subject: [PATCH 3/5] Simplify goflags --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5fb2c3e..3123dd4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: - type: static # On Ubuntu, libxml2 is compiled with GCC and is linked to libicu, which introduces a # stealth dependency on libstdc++ at link-time - goflags: "-ldflags '-extldflags \"-static -lstdc++\"' -tags 'osusergo netgo static_build'" + goflags: "-ldflags '-extldflags -lstdc++' -tags 'osusergo netgo static_build'" name: "Test [ Go ${{ matrix.go }}, ${{ matrix.link.type }} linking ]" env: steps: From ac24a152afd5038d4473dc681bae724e78f86733 Mon Sep 17 00:00:00 2001 From: lestrrat <49281+lestrrat@users.noreply.github.com> Date: Thu, 28 Mar 2024 09:29:26 +0900 Subject: [PATCH 4/5] Update ci.yml See if removing this key works --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3123dd4..4c936fc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,6 @@ jobs: # stealth dependency on libstdc++ at link-time goflags: "-ldflags '-extldflags -lstdc++' -tags 'osusergo netgo static_build'" name: "Test [ Go ${{ matrix.go }}, ${{ matrix.link.type }} linking ]" - env: steps: - name: Checkout repository uses: actions/checkout@v4 From 286f14dc3444aef4a88c3230cb9659b1b255fedb Mon Sep 17 00:00:00 2001 From: lestrrat <49281+lestrrat@users.noreply.github.com> Date: Thu, 28 Mar 2024 09:37:30 +0900 Subject: [PATCH 5/5] Update ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4c936fc..886460d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,7 @@ jobs: pacman -Syy --noconfirm pacman -Syu --noconfirm pacman -S --noconfirm base-devel - pacman -S --noconfirm libxml2=2.12.0 + pacman -S --noconfirm libxml2=2.12.6 - name: Set up Go uses: actions/setup-go@v4 with: