detached de702ef

internal/fakecgo: clean up gen.go (#235)

commit
de702ef019637a84d1b980077f2ba4376ea1f3a4
author
TotallyGamerJet · · 2024-04-21 02:54 UTC
committer
GitHub · · 2024-04-21 02:54 UTC
parent
042b201
signature
GPG UnverifiedB5690EEEBB952194 The key is unknown
2 files · +49 -22
M internal/fakecgo/doc.go +1 -2
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd
4+//go:build darwin || freebsd || linux
55
66 // Package fakecgo implements the Cgo runtime (runtime/cgo) entirely in Go.
77 // This allows code that calls into C to function properly when CGO_ENABLED=0.
@@ -30,4 +30,3 @@
3030 package fakecgo
3131
3232 //go:generate go run gen.go
33-//go:generate gofmt -s -w symbols.go
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build darwin || freebsd4+//go:build darwin || freebsd || linux
5 5
6 // Package fakecgo implements the Cgo runtime (runtime/cgo) entirely in Go.6 // Package fakecgo implements the Cgo runtime (runtime/cgo) entirely in Go.
7 // This allows code that calls into C to function properly when CGO_ENABLED=0.7 // This allows code that calls into C to function properly when CGO_ENABLED=0.
@@ -30,4 +30,3 @@
30 package fakecgo30 package fakecgo
31 31
32 //go:generate go run gen.go32 //go:generate go run gen.go
33-//go:generate gofmt -s -w symbols.go
M internal/fakecgo/gen.go +48 -20
@@ -100,6 +100,20 @@ TEXT _{{.Name}}(SB), NOSPLIT|NOFRAME, $0-0
100100 {{ end -}}
101101 `
102102
103+const templateSymbolsGoos = `// Code generated by 'go generate' with gen.go. DO NOT EDIT.
104+
105+// SPDX-License-Identifier: Apache-2.0
106+// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
107+
108+package fakecgo
109+
110+{{- range $location := . }}
111+{{- range .Symbols }}
112+//go:cgo_import_dynamic purego_{{ .Name }} {{ .Name }} "{{ $location.SharedObject }}"
113+{{- end }}
114+{{- end }}
115+`
116+
103117 type Arg struct {
104118 Name string
105119 Type string
@@ -111,6 +125,11 @@ type Symbol struct {
111125 Return string
112126 }
113127
128+type LocatedSymbols struct {
129+ SharedObject string
130+ Symbols []Symbol
131+}
132+
114133 var (
115134 libcSymbols = []Symbol{
116135 {"malloc", [5]Arg{{"size", "uintptr"}}, "unsafe.Pointer"},
@@ -176,36 +195,45 @@ func run() error {
176195 if err := t.Execute(f, allSymbols); err != nil {
177196 return err
178197 }
198+ t, err = template.New("symbols_goos.go").Parse(templateSymbolsGoos)
199+ if err != nil {
200+ return err
201+ }
179202 for _, goos := range []string{"darwin", "linux", "freebsd"} {
180203 f, err = os.Create(fmt.Sprintf("symbols_%s.go", goos))
181204 defer f.Close()
182205 if err != nil {
183206 return err
184207 }
185- f.WriteString(`// Code generated by 'go generate' with gen.go. DO NOT EDIT.
186-
187-// SPDX-License-Identifier: Apache-2.0
188-// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
189-
190-package fakecgo
191-
192-`)
193- const importPragma = `//go:cgo_import_dynamic purego_%s %s "%s"` + "\n"
194- libcSO := "/usr/lib/libSystem.B.dylib"
195- pthreadSO := "/usr/lib/libSystem.B.dylib"
196- if goos == "linux" {
197- libcSO = "libc.so.6"
198- pthreadSO = "libpthread.so.0"
199- } else if goos == "freebsd" {
208+ b := &bytes.Buffer{}
209+ var libcSO, pthreadSO string
210+ switch goos {
211+ case "darwin":
212+ libcSO = "/usr/lib/libSystem.B.dylib"
213+ pthreadSO = "/usr/lib/libSystem.B.dylib"
214+ case "freebsd":
200215 libcSO = "libc.so.7"
201216 pthreadSO = "libpthread.so"
217+ case "linux":
218+ libcSO = "libc.so.6"
219+ pthreadSO = "libpthread.so.0"
220+ default:
221+ return fmt.Errorf("unsupported OS: %s", goos)
202222 }
203-
204- for _, sym := range libcSymbols {
205- f.WriteString(fmt.Sprintf(importPragma, sym.Name, sym.Name, libcSO))
223+ located := []LocatedSymbols{
224+ {SharedObject: libcSO, Symbols: libcSymbols},
225+ {SharedObject: pthreadSO, Symbols: pthreadSymbols},
226+ }
227+ if err = t.Execute(b, located); err != nil {
228+ return err
206229 }
207- for _, sym := range pthreadSymbols {
208- f.WriteString(fmt.Sprintf(importPragma, sym.Name, sym.Name, pthreadSO))
230+ var src []byte
231+ src, err = format.Source(b.Bytes())
232+ if err != nil {
233+ return err
234+ }
235+ if _, err = f.Write(src); err != nil {
236+ return err
209237 }
210238 }
211239 return nil
@@ -100,6 +100,20 @@ TEXT _{{.Name}}(SB), NOSPLIT|NOFRAME, $0-0
100 {{ end -}}100 {{ end -}}
101 `101 `
102 102
103+const templateSymbolsGoos = `// Code generated by 'go generate' with gen.go. DO NOT EDIT.
104+
105+// SPDX-License-Identifier: Apache-2.0
106+// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
107+
108+package fakecgo
109+
110+{{- range $location := . }}
111+{{- range .Symbols }}
112+//go:cgo_import_dynamic purego_{{ .Name }} {{ .Name }} "{{ $location.SharedObject }}"
113+{{- end }}
114+{{- end }}
115+`
116+
103 type Arg struct {117 type Arg struct {
104 Name string118 Name string
105 Type string119 Type string
@@ -111,6 +125,11 @@ type Symbol struct {
111 Return string125 Return string
112 }126 }
113 127
128+type LocatedSymbols struct {
129+ SharedObject string
130+ Symbols []Symbol
131+}
132+
114 var (133 var (
115 libcSymbols = []Symbol{134 libcSymbols = []Symbol{
116 {"malloc", [5]Arg{{"size", "uintptr"}}, "unsafe.Pointer"},135 {"malloc", [5]Arg{{"size", "uintptr"}}, "unsafe.Pointer"},
@@ -176,36 +195,45 @@ func run() error {
176 if err := t.Execute(f, allSymbols); err != nil {195 if err := t.Execute(f, allSymbols); err != nil {
177 return err196 return err
178 }197 }
198+ t, err = template.New("symbols_goos.go").Parse(templateSymbolsGoos)
199+ if err != nil {
200+ return err
201+ }
179 for _, goos := range []string{"darwin", "linux", "freebsd"} {202 for _, goos := range []string{"darwin", "linux", "freebsd"} {
180 f, err = os.Create(fmt.Sprintf("symbols_%s.go", goos))203 f, err = os.Create(fmt.Sprintf("symbols_%s.go", goos))
181 defer f.Close()204 defer f.Close()
182 if err != nil {205 if err != nil {
183 return err206 return err
184 }207 }
185- f.WriteString(`// Code generated by 'go generate' with gen.go. DO NOT EDIT.208+ b := &bytes.Buffer{}
186-209+ var libcSO, pthreadSO string
187-// SPDX-License-Identifier: Apache-2.0210+ switch goos {
188-// SPDX-FileCopyrightText: 2022 The Ebitengine Authors211+ case "darwin":
189-212+ libcSO = "/usr/lib/libSystem.B.dylib"
190-package fakecgo213+ pthreadSO = "/usr/lib/libSystem.B.dylib"
191-214+ case "freebsd":
192-`)
193- const importPragma = `//go:cgo_import_dynamic purego_%s %s "%s"` + "\n"
194- libcSO := "/usr/lib/libSystem.B.dylib"
195- pthreadSO := "/usr/lib/libSystem.B.dylib"
196- if goos == "linux" {
197- libcSO = "libc.so.6"
198- pthreadSO = "libpthread.so.0"
199- } else if goos == "freebsd" {
200 libcSO = "libc.so.7"215 libcSO = "libc.so.7"
201 pthreadSO = "libpthread.so"216 pthreadSO = "libpthread.so"
217+ case "linux":
218+ libcSO = "libc.so.6"
219+ pthreadSO = "libpthread.so.0"
220+ default:
221+ return fmt.Errorf("unsupported OS: %s", goos)
202 }222 }
203-223+ located := []LocatedSymbols{
204- for _, sym := range libcSymbols {224+ {SharedObject: libcSO, Symbols: libcSymbols},
205- f.WriteString(fmt.Sprintf(importPragma, sym.Name, sym.Name, libcSO))225+ {SharedObject: pthreadSO, Symbols: pthreadSymbols},
226+ }
227+ if err = t.Execute(b, located); err != nil {
228+ return err
206 }229 }
207- for _, sym := range pthreadSymbols {230+ var src []byte
208- f.WriteString(fmt.Sprintf(importPragma, sym.Name, sym.Name, pthreadSO))231+ src, err = format.Source(b.Bytes())
232+ if err != nil {
233+ return err
234+ }
235+ if _, err = f.Write(src); err != nil {
236+ return err
209 }237 }
210 }238 }
211 return nil239 return nil

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