Skip to content

Commit

Permalink
codegen presets: add cgo preamble
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Nov 16, 2024
1 parent af475a3 commit a838dac
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
5 changes: 4 additions & 1 deletion cmd/codegen/cimgui-go-preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@
],
"OriginReplace": {
"TextEditor_GetText": "TextEditor_GetText_alloc"
}
},
"ExtraCGOPreamble": [
"#include \"../imgui/extra_types.h\""
]
}
4 changes: 2 additions & 2 deletions cmd/codegen/gengo_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,10 @@ func (g *callbacksGenerator) saveToDisk() error {
fmt.Fprintf(result,
`// #include <stdlib.h>
// #include <memory.h>
// #include "../imgui/extra_types.h"
// #include "wrapper.h"
// #include "typedefs.h"
`)
%s
`, g.ctx.preset.MergeCGoPreamble())

fmt.Fprintf(result, g.cgoSb.String())

Expand Down
11 changes: 6 additions & 5 deletions cmd/codegen/gengo_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func GenerateGoFuncs(
enumNames: context.enumNames,
refTypedefs: context.refTypedefs,
context: context,
sb: &strings.Builder{},
}

generator.writeFuncsFileHeader()
Expand Down Expand Up @@ -105,7 +106,7 @@ type goFuncsGenerator struct {
enumNames map[CIdentifier]bool
refTypedefs map[CIdentifier]bool

sb strings.Builder
sb *strings.Builder
convertedFuncCount int
shouldGenerate bool

Expand All @@ -116,14 +117,14 @@ type goFuncsGenerator struct {
func (g *goFuncsGenerator) writeFuncsFileHeader() {
g.sb.WriteString(getGoPackageHeader(g.context))

g.sb.WriteString(
`// #include "../imgui/extra_types.h"
// #include "structs_accessor.h"
fmt.Fprintf(g.sb,
`// #include "structs_accessor.h"
// #include "wrapper.h"
%s
import "C"
import "unsafe"
`)
`, g.context.preset.MergeCGoPreamble())
}

func (g *goFuncsGenerator) GenerateFunction(f FuncDef, args []GoIdentifier, argWrappers []ArgumentWrapperData) (noErrors bool) {
Expand Down
13 changes: 13 additions & 0 deletions cmd/codegen/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ type Preset struct {
// Introduced to replace TextEditor_GetText -> TextEditor_GetText_alloc
// but could be re-used to force use of another function than json tells us to use.
OriginReplace map[CIdentifier]CIdentifier
// ExtraCGOPreamble allows to specify additional C code elements in Go files.
// For example could be used to include extra files.
// For ease of use, its in form of []string. These lines will be merged and prefixed (if appliable) with '//'
ExtraCGOPreamble []string
}

func (p *Preset) MergeCGoPreamble() string {
result := ""
for _, line := range p.ExtraCGOPreamble {
result += "// " + line + "\n"
}

return result
}

0 comments on commit a838dac

Please sign in to comment.