Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statically link to libxml2 when static_build tag is defined #95

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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 ./...
run: go test ./...
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion clib/clib.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ warned.
package clib

/*
#cgo pkg-config: libxml-2.0
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
Expand Down
6 changes: 6 additions & 0 deletions clib/link_dynamic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build !static_build

package clib

// #cgo pkg-config: libxml-2.0
import "C"
7 changes: 7 additions & 0 deletions clib/link_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build static_build

package clib

// #cgo pkg-config: --static libxml-2.0
// #cgo LDFLAGS: -static
import "C"
13 changes: 13 additions & 0 deletions test/link/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"github.com/lestrrat-go/libxml2"
)

func main() {
doc, err := libxml2.ParseHTMLString(`<html><body><h1>Hello, World!</h1><p>Lorem Ipsum</p></body></html>`)
if err != nil {
panic(err)
}
doc.Free()
}
Loading