Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Statically link to libxml2 when static_build tag is defined (#95)
Browse files Browse the repository at this point in the history
* 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.

* Add linking configuration CI test for Ubuntu

* Simplify goflags

* Update ci.yml

See if removing this key works

* Update ci.yml

---------

Co-authored-by: lestrrat <49281+lestrrat@users.noreply.github.com>
  • Loading branch information
chrisnovakovic and lestrrat authored Mar 28, 2024
1 parent 99c7102 commit c5fabe2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
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

Check failure on line 1 in clib/link_dynamic.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)

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()
}

0 comments on commit c5fabe2

Please sign in to comment.