v0.12.0-alpha.1 9f4a5c8

README.md RawHistoryFolder

purego

[Go Reference]

A library for calling C functions from Go without Cgo.

This is beta software so expect bugs and potentially API breaking changes but each release will be tagged to avoid breaking people's code. Bug reports are encouraged.

Motivation

The Ebitengine game engine was ported to use only Go on Windows. This enabled cross-compiling to Windows from any other operating system simply by setting GOOS=windows. The purego project was born to bring that same vision to the other platforms supported by Ebitengine.

Benefits

  • Simple Cross-Compilation: No C means you can build for other platforms easily without a C compiler.
  • Faster Compilation: Efficiently cache your entirely Go builds.
  • Smaller Binaries: Using Cgo generates a C wrapper function for each C function called. Purego doesn't!
  • Dynamic Linking: Load symbols at runtime and use it as a plugin system.
  • Foreign Function Interface: Call into other languages that are compiled into shared objects.
  • Cgo Fallback: Works even with CGO_ENABLED=1 so incremental porting is possible. This also means unsupported GOARCHs (freebsd/riscv64, linux/mips, etc.) will still work except for float arguments and return values.

Supported Platforms

Tier 1

Tier 1 platforms are the primary targets officially supported by PureGo. When a new version of PureGo is released, any critical bugs found on Tier 1 platforms are treated as release blockers. The release will be postponed until such issues are resolved.

  • Android: amd641, arm641
  • iOS: amd641, arm641
  • Linux: amd64, arm64
  • macOS: amd64, arm64
  • Windows: amd642, arm642

Tier 2

Tier 2 platforms are supported by PureGo on a best-effort basis. Critical bugs on Tier 2 platforms do not block new PureGo releases. However, fixes contributed by external contributors are very welcome and encouraged.

  • Android: 3861,3, arm1,3
  • FreeBSD: amd643,4, arm643,4
  • Linux: 3863, arm3, loong642, ppc64le2, riscv643, s390x3, 5
  • NetBSD: amd643,4, arm643,4
  • Windows: 3863,6, arm3,6,7

Support Notes

  1. These architectures require CGO_ENABLED=1 to compile
  2. These architectures support passing structs by value as arguments and return values when calling C functions, but not in callbacks created with NewCallback
  3. These architectures do not support passing structs by value as arguments or return values
  4. These architectures require the special flag -gcflags="github.com/ebitengine/purego/internal/fakecgo=-std" to compile with CGO_ENABLED=0
  5. These architectures require CGO_ENABLED=1 to compile in versions before Go 1.27, but will be supported without Cgo in Go 1.27 and later
  6. These architectures only support SyscallN and NewCallback
  7. These architectures are no longer supported as of Go 1.26

Example

The example below only showcases purego use for macOS and Linux. The other platforms require special handling which can be seen in the complete example at examples/libc which supports FreeBSD and Windows.

package main

import (
	"fmt"
	"runtime"

	"github.com/ebitengine/purego"
)

func getSystemLibrary() string {
	switch runtime.GOOS {
	case "darwin":
		return "/usr/lib/libSystem.B.dylib"
	case "linux":
		return "libc.so.6"
	default:
		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
	}
}

func main() {
	libc, err := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL)
	if err != nil {
		panic(err)
	}
	var puts func(string)
	purego.RegisterLibFunc(&puts, libc, "puts")
	puts("Calling C from Go without Cgo!")
}

Then to run: CGO_ENABLED=0 go run main.go

Questions

If you have questions about how to incorporate purego in your project or want to discuss how it works join the Discord!

External Code

Purego uses code that originates from the Go runtime. These files are under the BSD-3 License that can be found in the Go Source. This is a list of the copied files:

  • abi_*.h from package runtime/cgo
  • wincallback.go from package runtime
  • internal/fakecgo/abi_*.h from package runtime/cgo
  • internal/fakecgo/asm_GOARCH.s from package runtime/cgo
  • internal/fakecgo/callbacks.go from package runtime/cgo
  • internal/fakecgo/iscgo.go from package runtime/cgo
  • internal/fakecgo/setenv.go from package runtime/cgo
  • internal/fakecgo/freebsd.go from package runtime/cgo
  • internal/fakecgo/netbsd.go from package runtime/cgo
  • internal/fakecgo/linux.go from package runtime/cgo

The internal/fakecgo/go_GOOS.go files were modified from runtime/cgo/gcc_GOOS_GOARCH.go. The internal/fakecgo/linux.go file is a combination of runtime/cgo/linux.go and runtime/cgo/linux_syscall.c.

The files abi_*.h and internal/fakecgo/abi_*.h are the same because Bazel does not support cross-package use of #include so we need each one once per package. (cf. issue)

source
1# purego
2[![Go Reference](https://pkg.go.dev/badge/github.com/ebitengine/purego?GOOS=darwin.svg)](https://pkg.go.dev/github.com/ebitengine/purego?GOOS=darwin)
3
4A library for calling C functions from Go without Cgo.
5
6> This is beta software so expect bugs and potentially API breaking changes
7> but each release will be tagged to avoid breaking people's code.
8> Bug reports are encouraged.
9
10## Motivation
11
12The [Ebitengine](https://github.com/hajimehoshi/ebiten) game engine was ported to use only Go on Windows. This enabled
13cross-compiling to Windows from any other operating system simply by setting `GOOS=windows`. The purego project was
14born to bring that same vision to the other platforms supported by Ebitengine.
15
16## Benefits
17
18- **Simple Cross-Compilation**: No C means you can build for other platforms easily without a C compiler.
19- **Faster Compilation**: Efficiently cache your entirely Go builds.
20- **Smaller Binaries**: Using Cgo generates a C wrapper function for each C function called. Purego doesn't!
21- **Dynamic Linking**: Load symbols at runtime and use it as a plugin system.
22- **Foreign Function Interface**: Call into other languages that are compiled into shared objects.
23- **Cgo Fallback**: Works even with CGO_ENABLED=1 so incremental porting is possible. 
24This also means unsupported GOARCHs (freebsd/riscv64, linux/mips, etc.) will still work
25except for float arguments and return values.
26
27## Supported Platforms
28
29### Tier 1
30
31Tier 1 platforms are the primary targets officially supported by PureGo. When a new version of PureGo is released, any critical bugs found on Tier 1 platforms are treated as release blockers. The release will be postponed until such issues are resolved.
32
33- **Android**: amd64<sup>1</sup>, arm64<sup>1</sup>
34- **iOS**: amd64<sup>1</sup>, arm64<sup>1</sup>
35- **Linux**: amd64, arm64
36- **macOS**: amd64, arm64
37- **Windows**: amd64<sup>2</sup>, arm64<sup>2</sup>
38
39### Tier 2
40
41Tier 2 platforms are supported by PureGo on a best-effort basis. Critical bugs on Tier 2 platforms do not block new PureGo releases. However, fixes contributed by external contributors are very welcome and encouraged.
42
43- **Android**: 386<sup>1,3</sup>, arm<sup>1,3</sup>
44- **FreeBSD**: amd64<sup>3,4</sup>, arm64<sup>3,4</sup>
45- **Linux**: 386<sup>3</sup>, arm<sup>3</sup>, loong64<sup>2</sup>, ppc64le<sup>2</sup>, riscv64<sup>3</sup>, s390x<sup>3, 5</sup>
46- **NetBSD**: amd64<sup>3,4</sup>, arm64<sup>3,4</sup>
47- **Windows**: 386<sup>3,6</sup>, arm<sup>3,6,7</sup>
48
49#### Support Notes
50
511. These architectures require CGO_ENABLED=1 to compile
522. These architectures support passing structs by value as arguments and return values when calling C functions, but not in callbacks created with `NewCallback`
533. These architectures do not support passing structs by value as arguments or return values
544. These architectures require the special flag `-gcflags="github.com/ebitengine/purego/internal/fakecgo=-std"` to compile with CGO_ENABLED=0
555. These architectures require CGO_ENABLED=1 to compile in versions before Go 1.27, but will be supported without Cgo in Go 1.27 and later
566. These architectures only support `SyscallN` and `NewCallback`
577. These architectures are no longer supported as of Go 1.26
58
59## Example
60
61The example below only showcases purego use for macOS and Linux. The other platforms require special handling which can
62be seen in the complete example at [examples/libc](https://github.com/ebitengine/purego/tree/main/examples/libc) which supports FreeBSD and Windows.
63
64```go
65package main
66
67import (
68	"fmt"
69	"runtime"
70
71	"github.com/ebitengine/purego"
72)
73
74func getSystemLibrary() string {
75	switch runtime.GOOS {
76	case "darwin":
77		return "/usr/lib/libSystem.B.dylib"
78	case "linux":
79		return "libc.so.6"
80	default:
81		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
82	}
83}
84
85func main() {
86	libc, err := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL)
87	if err != nil {
88		panic(err)
89	}
90	var puts func(string)
91	purego.RegisterLibFunc(&puts, libc, "puts")
92	puts("Calling C from Go without Cgo!")
93}
94```
95
96Then to run: `CGO_ENABLED=0 go run main.go`
97
98## Questions
99
100If you have questions about how to incorporate purego in your project or want to discuss
101how it works join the [Discord](https://discord.gg/HzGZVD6BkY)!
102
103### External Code
104
105Purego uses code that originates from the Go runtime. These files are under the BSD-3
106License that can be found [in the Go Source](https://github.com/golang/go/blob/master/LICENSE).
107This is a list of the copied files:
108
109* `abi_*.h` from package `runtime/cgo`
110* `wincallback.go` from package `runtime`
111* `internal/fakecgo/abi_*.h` from package `runtime/cgo`
112* `internal/fakecgo/asm_GOARCH.s` from package `runtime/cgo`
113* `internal/fakecgo/callbacks.go` from package `runtime/cgo`
114* `internal/fakecgo/iscgo.go` from package `runtime/cgo`
115* `internal/fakecgo/setenv.go` from package `runtime/cgo`
116* `internal/fakecgo/freebsd.go` from package `runtime/cgo`
117* `internal/fakecgo/netbsd.go` from package `runtime/cgo`
118* `internal/fakecgo/linux.go` from package `runtime/cgo`
119
120The `internal/fakecgo/go_GOOS.go` files were modified from `runtime/cgo/gcc_GOOS_GOARCH.go`.
121The `internal/fakecgo/linux.go` file is a combination of `runtime/cgo/linux.go` and `runtime/cgo/linux_syscall.c`.
122
123The files `abi_*.h` and `internal/fakecgo/abi_*.h` are the same because Bazel does not support cross-package use of
124`#include` so we need each one once per package. (cf. [issue](https://github.com/bazelbuild/rules_go/issues/3636))

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close