v0.11.0-bnema.1 8b0a311

README.md RawHistoryFolder

purego

[Go Reference]

A library for calling C functions from Go without Cgo.

Fork notice: this repository is a maintained fork of github.com/ebitengine/purego with additional ABI, callback, and generator-facing fixes needed by the PureGoTK stack.

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: amd64, arm64

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, arm1
  • FreeBSD: amd642, arm642
  • Linux: 386, arm, loong64, ppc64le, riscv64, s390x1
  • NetBSD: amd642, arm642
  • Windows: 3863, arm3,4

Support Notes

  1. These architectures require CGO_ENABLED=1 to compile
  2. These architectures require the special flag -gcflags="github.com/bnema/purego/internal/fakecgo=-std" to compile with CGO_ENABLED=0
  3. These architectures only support SyscallN and NewCallback
  4. 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/bnema/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
  • zcallback_darwin_*.s 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/bnema/purego?GOOS=darwin.svg)](https://pkg.go.dev/github.com/bnema/purego?GOOS=darwin)
3
4A library for calling C functions from Go without Cgo.
5
6> Fork notice: this repository is a maintained fork of `github.com/ebitengine/purego` with additional ABI, callback, and generator-facing fixes needed by the PureGoTK stack.
7
8> This is beta software so expect bugs and potentially API breaking changes
9> but each release will be tagged to avoid breaking people's code.
10> Bug reports are encouraged.
11
12## Motivation
13
14The [Ebitengine](https://github.com/hajimehoshi/ebiten) game engine was ported to use only Go on Windows. This enabled
15cross-compiling to Windows from any other operating system simply by setting `GOOS=windows`. The purego project was
16born to bring that same vision to the other platforms supported by Ebitengine.
17
18## Benefits
19
20- **Simple Cross-Compilation**: No C means you can build for other platforms easily without a C compiler.
21- **Faster Compilation**: Efficiently cache your entirely Go builds.
22- **Smaller Binaries**: Using Cgo generates a C wrapper function for each C function called. Purego doesn't!
23- **Dynamic Linking**: Load symbols at runtime and use it as a plugin system.
24- **Foreign Function Interface**: Call into other languages that are compiled into shared objects.
25- **Cgo Fallback**: Works even with CGO_ENABLED=1 so incremental porting is possible. 
26This also means unsupported GOARCHs (freebsd/riscv64, linux/mips, etc.) will still work
27except for float arguments and return values.
28
29## Supported Platforms
30
31### Tier 1
32
33Tier 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.
34
35- **Android**: amd64<sup>1</sup>, arm64<sup>1</sup>
36- **iOS**: amd64<sup>1</sup>, arm64<sup>1</sup>
37- **Linux**: amd64, arm64
38- **macOS**: amd64, arm64
39- **Windows**: amd64, arm64
40
41### Tier 2
42
43Tier 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.
44
45- **Android**: 386<sup>1</sup>, arm<sup>1</sup>
46- **FreeBSD**: amd64<sup>2</sup>, arm64<sup>2</sup>
47- **Linux**: 386, arm, loong64, ppc64le, riscv64, s390x<sup>1</sup>
48- **NetBSD**: amd64<sup>2</sup>, arm64<sup>2</sup>
49- **Windows**: 386<sup>3</sup>, arm<sup>3,4</sup>
50
51#### Support Notes
52
531. These architectures require CGO_ENABLED=1 to compile
542. These architectures require the special flag `-gcflags="github.com/bnema/purego/internal/fakecgo=-std"` to compile with CGO_ENABLED=0
553. These architectures only support `SyscallN` and `NewCallback`
564. These architectures are no longer supported as of Go 1.26
57
58## Example
59
60The example below only showcases purego use for macOS and Linux. The other platforms require special handling which can
61be seen in the complete example at [examples/libc](https://github.com/bnema/purego/tree/main/examples/libc) which supports FreeBSD and Windows.
62
63```go
64package main
65
66import (
67	"fmt"
68	"runtime"
69
70	"github.com/bnema/purego"
71)
72
73func getSystemLibrary() string {
74	switch runtime.GOOS {
75	case "darwin":
76		return "/usr/lib/libSystem.B.dylib"
77	case "linux":
78		return "libc.so.6"
79	default:
80		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
81	}
82}
83
84func main() {
85	libc, err := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL)
86	if err != nil {
87		panic(err)
88	}
89	var puts func(string)
90	purego.RegisterLibFunc(&puts, libc, "puts")
91	puts("Calling C from Go without Cgo!")
92}
93```
94
95Then to run: `CGO_ENABLED=0 go run main.go`
96
97## Questions
98
99If you have questions about how to incorporate purego in your project or want to discuss
100how it works join the [Discord](https://discord.gg/HzGZVD6BkY)!
101
102### External Code
103
104Purego uses code that originates from the Go runtime. These files are under the BSD-3
105License that can be found [in the Go Source](https://github.com/golang/go/blob/master/LICENSE).
106This is a list of the copied files:
107
108* `abi_*.h` from package `runtime/cgo`
109* `wincallback.go` from package `runtime`
110* `zcallback_darwin_*.s` 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