Skip to content

Commit 1bd362f

Browse files
all: Add support for NetBSD (#268)
Closes #249
1 parent 3999af5 commit 1bd362f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+391
-117
lines changed

.github/ISSUE_TEMPLATE/00-bug.yml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ body:
1717
- label: macOS
1818
- label: Linux
1919
- label: FreeBSD
20+
- label: NetBSD
2021
- label: Android
2122
- label: iOS
2223
validations:

.github/scripts/bsd_tests.sh

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/sh
2+
3+
# SPDX-License-Identifier: Apache-2.0
4+
# SPDX-FileCopyrightText: 2025 The Ebitengine Authors
5+
6+
# FreeBSD & NetBSD tests run within QEMU on Ubuntu.
7+
# vmactions/freebsd-vm only supports a single "step" where it
8+
# brings down the VM at the end of the step, so all
9+
# the commands to run need to be put into this single block.
10+
11+
echo "Running tests on $(uname -a) at $PWD"
12+
13+
PATH=$PATH:/usr/local/go/bin/
14+
15+
# verify Go is available
16+
go version
17+
18+
echo "=> go build"
19+
go build -v ./...
20+
# Compile without optimization to check potential stack overflow.
21+
# The option '-gcflags=all=-N -l' is often used at Visual Studio Code.
22+
# See also https://go.googlesource.com/vscode-go/+/HEAD/docs/debugging.md#launch and the issue hajimehoshi/ebiten#2120.
23+
go build "-gcflags=all=-N -l" -v ./...
24+
25+
# Check cross-compiling Windows binaries.
26+
env GOOS=windows GOARCH=386 go build -v ./...
27+
env GOOS=windows GOARCH=amd64 go build -v ./...
28+
env GOOS=windows GOARCH=arm go build -v ./...
29+
env GOOS=windows GOARCH=arm64 go build -v ./...
30+
31+
# Check cross-compiling macOS binaries.
32+
env GOOS=darwin GOARCH=amd64 go build -v ./...
33+
env GOOS=darwin GOARCH=arm64 go build -v ./...
34+
35+
# Check cross-compiling Linux binaries.
36+
env GOOS=linux GOARCH=amd64 go build -v ./...
37+
env GOOS=linux GOARCH=arm64 go build -v ./...
38+
39+
# Check cross-compiling FreeBSD binaries.
40+
env GOOS=freebsd GOARCH=amd64 go build -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -v ./...
41+
env GOOS=freebsd GOARCH=arm64 go build -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -v ./...
42+
43+
# Check cross-compiling NetBSD binaries.
44+
env GOOS=netbsd GOARCH=amd64 go build -v ./...
45+
env GOOS=netbsd GOARCH=arm64 go build -v ./...
46+
47+
echo "=> go build (plugin)"
48+
# Make sure that plugin buildmode works since we save the R15 register (#254)
49+
go build -buildmode=plugin ./examples/libc
50+
51+
echo "=> go mod vendor"
52+
mkdir /tmp/vendoring
53+
cd /tmp/vendoring
54+
go mod init foo
55+
echo 'package main' > main.go
56+
echo 'import (' >> main.go
57+
echo ' _ "github.com/ebitengine/purego"' >> main.go
58+
echo ')' >> main.go
59+
echo 'func main() {}' >> main.go
60+
go mod edit -replace github.com/ebitengine/purego=$GITHUB_WORKSPACE
61+
go mod tidy
62+
go mod vendor
63+
go build -v .
64+
65+
cd $GITHUB_WORKSPACE
66+
echo "=> go test CGO_ENABLED=0"
67+
env CGO_ENABLED=0 go test -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -shuffle=on -v -count=10 ./...
68+
69+
echo "=> go test CGO_ENABLED=1"
70+
env CGO_ENABLED=1 go test -shuffle=on -v -count=10 ./...
71+
72+
echo "=> go test CGO_ENABLED=0 w/o optimization"
73+
env CGO_ENABLED=0 go test "-gcflags=all=-N -l" -v ./...
74+
75+
echo "=> go test CGO_ENABLED=1 w/o optimization"
76+
env CGO_ENABLED=1 go test "-gcflags=all=-N -l" -v ./...
77+
78+
if [ -z "$(go version | grep '^1.1')" ]; then
79+
echo "=> go test race"
80+
go test -race -shuffle=on -v -count=10 ./...
81+
fi

.github/workflows/test.yml

+25-74
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ jobs:
6565
# gcflags -std is necessary to make fakecgo the Cgo for
6666
# FreeBSD to add the symbols that libc.so depends on.
6767
env GOOS=freebsd GOARCH=amd64 go build -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -v ./...
68-
env GOOS=freebsd GOARCH=arm64 go build -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -v ./...
68+
env GOOS=freebsd GOARCH=arm64 go build -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -v ./...
69+
70+
# Check cross-compiling NetBSD binaries.
71+
env GOOS=netbsd GOARCH=amd64 go build -v ./...
72+
env GOOS=netbsd GOARCH=arm64 go build -v ./...
6973
7074
- name: go build (plugin)
7175
if: runner.os == 'Linux' || runner.os == 'macOS'
@@ -134,93 +138,40 @@ jobs:
134138
run: |
135139
go test -race -shuffle=on -v -count=10 ./...
136140
137-
freebsd:
141+
bsd:
138142
strategy:
139143
matrix:
144+
os: ['FreeBSD', 'NetBSD']
140145
go: ['1.18.10', '1.19.13', '1.20.14', '1.21.13', '1.22.12', '1.23.6', '1.24.0']
141-
name: Test with Go ${{ matrix.go }} on FreeBSD
146+
name: Test with Go ${{ matrix.go }} on ${{ matrix.os }}
142147
runs-on: ubuntu-22.04
143148
defaults:
144149
run:
145150
shell: bash
146151
steps:
147152
- uses: actions/checkout@v4
148153
- name: Run in freebsd
154+
if: matrix.os == 'FreeBSD'
149155
uses: vmactions/freebsd-vm@v1
150156
with:
151157
usesh: true
152158
prepare: |
153159
fetch https://go.dev/dl/go${{matrix.go}}.freebsd-amd64.tar.gz
154160
rm -fr /usr/local/go && tar -C /usr/local -xf go${{matrix.go}}.freebsd-amd64.tar.gz
155161
ln -s /usr/local/go/bin/go /usr/local/bin
156-
run: |
157-
# FreeBSD tests run within QEMU on Ubuntu.
158-
# vmactions/freebsd-vm only supports a single "step" where it
159-
# brings down the VM at the end of the step, so all
160-
# the commands to run need to be put into this single block.
161-
162-
echo "Running tests on $(uname -a) at $PWD"
163-
164-
# verify Go is available
165-
go version
166-
167-
echo "=> go build"
168-
go build -v ./...
169-
# Compile without optimization to check potential stack overflow.
170-
# The option '-gcflags=all=-N -l' is often used at Visual Studio Code.
171-
# See also https://go.googlesource.com/vscode-go/+/HEAD/docs/debugging.md#launch and the issue hajimehoshi/ebiten#2120.
172-
go build "-gcflags=all=-N -l" -v ./...
173-
174-
# Check cross-compiling Windows binaries.
175-
env GOOS=windows GOARCH=386 go build -v ./...
176-
env GOOS=windows GOARCH=amd64 go build -v ./...
177-
env GOOS=windows GOARCH=arm go build -v ./...
178-
env GOOS=windows GOARCH=arm64 go build -v ./...
179-
180-
# Check cross-compiling macOS binaries.
181-
env GOOS=darwin GOARCH=amd64 go build -v ./...
182-
env GOOS=darwin GOARCH=arm64 go build -v ./...
183-
184-
# Check cross-compiling Linux binaries.
185-
env GOOS=linux GOARCH=amd64 go build -v ./...
186-
env GOOS=linux GOARCH=arm64 go build -v ./...
187-
188-
# Check cross-compiling FreeBSD binaries.
189-
env GOOS=freebsd GOARCH=amd64 go build -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -v ./...
190-
env GOOS=freebsd GOARCH=arm64 go build -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -v ./...
191-
192-
echo "=> go build (plugin)"
193-
# Make sure that plugin buildmode works since we save the R15 register (#254)
194-
go build -buildmode=plugin ./examples/libc
195-
196-
echo "=> go mod vendor"
197-
mkdir /tmp/vendoring
198-
cd /tmp/vendoring
199-
go mod init foo
200-
echo 'package main' > main.go
201-
echo 'import (' >> main.go
202-
echo ' _ "github.com/ebitengine/purego"' >> main.go
203-
echo ')' >> main.go
204-
echo 'func main() {}' >> main.go
205-
go mod edit -replace github.com/ebitengine/purego=$GITHUB_WORKSPACE
206-
go mod tidy
207-
go mod vendor
208-
go build -v .
209-
210-
cd $GITHUB_WORKSPACE
211-
echo "=> go test CGO_ENABLED=0"
212-
env CGO_ENABLED=0 go test -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" -shuffle=on -v -count=10 ./...
213-
214-
echo "=> go test CGO_ENABLED=1"
215-
env CGO_ENABLED=1 go test -shuffle=on -v -count=10 ./...
216-
217-
echo "=> go test CGO_ENABLED=0 w/o optimization"
218-
env CGO_ENABLED=0 go test "-gcflags=all=-N -l" -v ./...
219-
220-
echo "=> go test CGO_ENABLED=1 w/o optimization"
221-
env CGO_ENABLED=1 go test "-gcflags=all=-N -l" -v ./...
222-
223-
if [ -z "$(echo ${{matrix.go}} | grep '^1.1')" ]; then
224-
echo "=> go test race"
225-
go test -race -shuffle=on -v -count=10 ./...
226-
fi
162+
chmod +x $GITHUB_WORKSPACE/.github/scripts/bsd_tests.sh
163+
run: $GITHUB_WORKSPACE/.github/scripts/bsd_tests.sh
164+
- name: Run in netbsd
165+
# there are no prebuilt download links for these versions of Go for NetBSD
166+
if: matrix.os == 'NetBSD' && !startsWith(matrix.go, '1.18.') && !startsWith(matrix.go, '1.19.') && !startsWith(matrix.go, '1.20.')
167+
uses: vmactions/netbsd-vm@v1
168+
with:
169+
usesh: true
170+
prepare: |
171+
/usr/sbin/pkg_add -v bash
172+
ftp https://go.dev/dl/go${{matrix.go}}.netbsd-amd64.tar.gz
173+
mkdir /usr/local
174+
rm -fr /usr/local/go && tar -C /usr/local -xf go${{matrix.go}}.netbsd-amd64.tar.gz
175+
ln -s /usr/local/go/bin/go /usr/local/bin
176+
chmod +x $GITHUB_WORKSPACE/.github/scripts/bsd_tests.sh
177+
run: $GITHUB_WORKSPACE/.github/scripts/bsd_tests.sh

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ This is a list of the copied files:
9292
* `internal/fakecgo/iscgo.go` from package `runtime/cgo`
9393
* `internal/fakecgo/setenv.go` from package `runtime/cgo`
9494
* `internal/fakecgo/freebsd.go` from package `runtime/cgo`
95+
* `internal/fakecgo/netbsd.go` from package `runtime/cgo`
9596

9697
The files `abi_*.h` and `internal/fakecgo/abi_*.h` are the same because Bazel does not support cross-package use of
9798
`#include` so we need each one once per package. (cf. [issue](https://github.com/bazelbuild/rules_go/issues/3636))

cgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33

4-
//go:build cgo && (darwin || freebsd || linux)
4+
//go:build cgo && (darwin || freebsd || linux || netbsd)
55

66
package purego
77

dlerror.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
33

4-
//go:build darwin || freebsd || linux
4+
//go:build darwin || freebsd || linux || netbsd
55

66
package purego
77

dlfcn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33

4-
//go:build (darwin || freebsd || linux) && !android && !faketime
4+
//go:build (darwin || freebsd || linux || netbsd) && !android && !faketime
55

66
package purego
77

dlfcn_netbsd.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
3+
4+
package purego
5+
6+
// Source for constants: https://github.com/NetBSD/src/blob/trunk/include/dlfcn.h
7+
8+
const (
9+
intSize = 32 << (^uint(0) >> 63) // 32 or 64
10+
RTLD_DEFAULT = 1<<intSize - 2 // Pseudo-handle for dlsym so search for any loaded symbol
11+
RTLD_LAZY = 0x00000001 // Relocations are performed at an implementation-dependent time.
12+
RTLD_NOW = 0x00000002 // Relocations are performed when the object is loaded.
13+
RTLD_LOCAL = 0x00000000 // All symbols are not made available for relocation processing by other modules.
14+
RTLD_GLOBAL = 0x00000100 // All symbols are available for relocation processing of other modules.
15+
)

dlfcn_nocgo_netbsd.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
3+
4+
package purego
5+
6+
//go:cgo_import_dynamic purego_dlopen dlopen "libc.so"
7+
//go:cgo_import_dynamic purego_dlsym dlsym "libc.so"
8+
//go:cgo_import_dynamic purego_dlerror dlerror "libc.so"
9+
//go:cgo_import_dynamic purego_dlclose dlclose "libc.so"

dlfcn_stubs.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33

4-
//go:build darwin || !cgo && (freebsd || linux) && !faketime
4+
//go:build darwin || !cgo && (freebsd || linux || netbsd) && !faketime
55

66
#include "textflag.h"
77

dlfcn_test.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
33

4-
//go:build darwin || freebsd || linux
4+
//go:build darwin || freebsd || linux || netbsd
55

66
package purego_test
77

@@ -53,12 +53,7 @@ func buildSharedLib(compilerEnv, libFile string, sources ...string) error {
5353
return errors.New("compiler not found")
5454
}
5555

56-
var args []string
57-
if runtime.GOOS == "freebsd" {
58-
args = []string{"-shared", "-Wall", "-Werror", "-fPIC", "-o", libFile}
59-
} else {
60-
args = []string{"-shared", "-Wall", "-Werror", "-o", libFile}
61-
}
56+
args := []string{"-shared", "-Wall", "-Werror", "-fPIC", "-o", libFile}
6257
if runtime.GOARCH == "386" {
6358
args = append(args, "-m32")
6459
}

examples/libc/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33

4-
//go:build darwin || freebsd || linux || windows
4+
//go:build darwin || freebsd || linux || netbsd || windows
55

66
package main
77

@@ -20,6 +20,8 @@ func getSystemLibrary() string {
2020
return "libc.so.6"
2121
case "freebsd":
2222
return "libc.so.7"
23+
case "netbsd":
24+
return "libc.so"
2325
case "windows":
2426
return "ucrtbase.dll"
2527
default:

examples/libc/main_unix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
33

4-
//go:build darwin || freebsd || linux
4+
//go:build darwin || freebsd || linux || netbsd
55

66
package main
77

func.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33

4-
//go:build darwin || freebsd || linux || windows
4+
//go:build darwin || freebsd || linux || netbsd || windows
55

66
package purego
77

func_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ func getSystemLibrary() (string, error) {
1717
switch runtime.GOOS {
1818
case "darwin":
1919
return "/usr/lib/libSystem.B.dylib", nil
20-
case "linux":
21-
return "libc.so.6", nil
2220
case "freebsd":
2321
return "libc.so.7", nil
22+
case "linux":
23+
return "libc.so.6", nil
24+
case "netbsd":
25+
return "libc.so", nil
2426
case "windows":
2527
return "ucrtbase.dll", nil
2628
default:

go_runtime.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33

4-
//go:build darwin || freebsd || linux || windows
4+
//go:build darwin || freebsd || linux || netbsd || windows
55

66
package purego
77

internal/cgo/dlfcn_cgo_unix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
33

4-
//go:build freebsd || linux
4+
//go:build freebsd || linux || netbsd
55

66
package cgo
77

88
/*
9-
#cgo LDFLAGS: -ldl
9+
#cgo !netbsd LDFLAGS: -ldl
1010
1111
#include <dlfcn.h>
1212
#include <stdlib.h>

0 commit comments

Comments
 (0)