diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a125f73..886460d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,14 @@ 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 -lstdc++' -tags 'osusergo netgo static_build'" + name: "Test [ Go ${{ matrix.go }}, ${{ matrix.link.type }} linking ]" steps: - name: Checkout repository uses: actions/checkout@v4 @@ -30,8 +37,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: @@ -47,10 +58,10 @@ 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: go-version: ${{ matrix.go }} - name: Test - run: go test ./... \ No newline at end of file + run: go test ./... 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" 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() +}