Debian Codegraph @

README.md

raw symbols SCIP
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
4 A 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
12 The [Ebitengine](https://github.com/hajimehoshi/ebiten) game engine was ported to use only Go on Windows. This enabled
13 cross-compiling to Windows from any other operating system simply by setting `GOOS=windows`. The purego project was
14 born 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.
24 This also means unsupported GOARCHs (freebsd/riscv64, linux/mips, etc.) will still work
25 except for float arguments and return values.
26
27 ## Supported Platforms
28
29 ### Tier 1
30
31 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.
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, arm64
38
39 ### Tier 2
40
41 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.
42
43 - **Android**: 386<sup>1</sup>, arm<sup>1</sup>
44 - **FreeBSD**: amd64<sup>2</sup>, arm64<sup>2</sup>
45 - **Linux**: 386, arm, loong64, ppc64le, riscv64, s390x<sup>1</sup>
46 - **Windows**: 386<sup>3</sup>, arm<sup>3,4</sup>
47
48 #### Support Notes
49
50 1. These architectures require CGO_ENABLED=1 to compile
51 2. These architectures require the special flag `-gcflags="github.com/ebitengine/purego/internal/fakecgo=-std"` to compile with CGO_ENABLED=0
52 3. These architectures only support `SyscallN` and `NewCallback`
53 4. These architectures are no longer supported as of Go 1.26
54
55 ## Example
56
57 The example below only showcases purego use for macOS and Linux. The other platforms require special handling which can
58 be seen in the complete example at [examples/libc](https://github.com/ebitengine/purego/tree/main/examples/libc) which supports FreeBSD and Windows.
59
60 ```go
61 package main
62
63 import (
64 "fmt"
65 "runtime"
66
67 "github.com/ebitengine/purego"
68 )
69
70 func getSystemLibrary() string {
71 switch runtime.GOOS {
72 case "darwin":
73 return "/usr/lib/libSystem.B.dylib"
74 case "linux":
75 return "libc.so.6"
76 default:
77 panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
78 }
79 }
80
81 func main() {
82 libc, err := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL)
83 if err != nil {
84 panic(err)
85 }
86 var puts func(string)
87 purego.RegisterLibFunc(&puts, libc, "puts")
88 puts("Calling C from Go without Cgo!")
89 }
90 ```
91
92 Then to run: `CGO_ENABLED=0 go run main.go`
93
94 ## Questions
95
96 If you have questions about how to incorporate purego in your project or want to discuss
97 how it works join the [Discord](https://discord.gg/HzGZVD6BkY)!
98
99 ### External Code
100
101 Purego uses code that originates from the Go runtime. These files are under the BSD-3
102 License that can be found [in the Go Source](https://github.com/golang/go/blob/master/LICENSE).
103 This is a list of the copied files:
104
105 * `abi_*.h` from package `runtime/cgo`
106 * `wincallback.go` from package `runtime`
107 * `zcallback_darwin_*.s` from package `runtime`
108 * `internal/fakecgo/abi_*.h` from package `runtime/cgo`
109 * `internal/fakecgo/asm_GOARCH.s` from package `runtime/cgo`
110 * `internal/fakecgo/callbacks.go` from package `runtime/cgo`
111 * `internal/fakecgo/iscgo.go` from package `runtime/cgo`
112 * `internal/fakecgo/setenv.go` from package `runtime/cgo`
113 * `internal/fakecgo/freebsd.go` from package `runtime/cgo`
114 * `internal/fakecgo/netbsd.go` from package `runtime/cgo`
115
116 The `internal/fakecgo/go_GOOS.go` files were modified from `runtime/cgo/gcc_GOOS_GOARCH.go`.
117
118 The files `abi_*.h` and `internal/fakecgo/abi_*.h` are the same because Bazel does not support cross-package use of
119 `#include` so we need each one once per package. (cf. [issue](https://github.com/bazelbuild/rules_go/issues/3636))