Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hardcoded imgui refs #395

Merged
merged 6 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ImGuiColorTextEdit/callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ImGuiColorTextEdit/funcs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ImGuiColorTextEdit/typedefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion cmd/codegen/cimgui-go-preset.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"PackagePath": "github.com/AllenDang/cimgui-go",
"SkipFuncs": {
"igInputText": true,
"igInputTextWithHint": true,
Expand Down Expand Up @@ -73,5 +74,12 @@
],
"OriginReplace": {
"TextEditor_GetText": "TextEditor_GetText_alloc"
}
},
"ExtraCGOPreamble": [
"#include \"../imgui/extra_types.h\""
],
"InternalFiles": [
"imgui_internal"
],
"InternalPrefix": "Internal"
}
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
18 changes: 11 additions & 7 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 Expand Up @@ -302,8 +303,11 @@ func (g *goFuncsGenerator) generateFuncDeclarationStmt(receiver GoIdentifier, fu

// if file comes from imgui_internal.h,prefix Internal is added.
// ref: https://github.com/AllenDang/cimgui-go/pull/118
if strings.Contains(f.Location, "imgui_internal") {
goFuncName = "Internal" + goFuncName
for _, internalFile := range g.context.preset.InternalFiles {
if strings.HasPrefix(f.Location, internalFile) {
goFuncName = GoIdentifier(g.context.preset.InternalPrefix) + goFuncName
break
}
}

// Generate default param value hint
Expand Down
4 changes: 2 additions & 2 deletions cmd/codegen/gengo_typedefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ extern "C" {
fmt.Fprintf(g.GoSb,
`// #include <stdlib.h>
// #include <memory.h>
// #include "../imgui/extra_types.h"
// #include "wrapper.h"
// #include "typedefs.h"
%s
import "C"
import "unsafe"
`)
`, g.ctx.preset.MergeCGoPreamble())
}

// k is plain C name of the typedef (key in typedefs_dict.json)
Expand Down
21 changes: 16 additions & 5 deletions cmd/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
)

const (
cimguiGoPackagePath = "github.com/AllenDang/cimgui-go"
generatorInfo = "// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go.\n// DO NOT EDIT.\n\n"
goPackageHeader = generatorInfo + "package %[1]s\n\nimport (\n\"" + cimguiGoPackagePath + "/datautils\"\n\t\"" + cimguiGoPackagePath + "/%[2]s\"\n)\n\n"
cppFileHeader = generatorInfo
generatorInfo = "// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go.\n// DO NOT EDIT.\n\n"
cppFileHeader = generatorInfo
)

// this cextracts enums and structs names from json file.
Expand Down Expand Up @@ -273,7 +271,20 @@ func main() {
}

func getGoPackageHeader(ctx *Context) string {
return fmt.Sprintf(goPackageHeader, ctx.flags.packageName, ctx.flags.refPackageName)
return fmt.Sprintf(
`%[1]s
package %[2]s

import (
"%[4]s/utils"
"%[4]s/%[3]s"
)
`,
generatorInfo,
ctx.flags.packageName,
ctx.flags.refPackageName,
ctx.preset.PackagePath,
)
}

func prefixGoPackage(t, sourcePackage GoIdentifier, ctx *Context) GoIdentifier {
Expand Down
30 changes: 30 additions & 0 deletions cmd/codegen/presets.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "strings"

// Preset is a set of rules iperative to XXX_templates/ json files.
// They are used to manually set some properties of the generator.
type Preset struct {
Expand Down Expand Up @@ -30,4 +32,32 @@ 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
// InternalFiles allows to specify files that are considered Internal.
// If an identifier is found in such a file, it will be generated but its
// name will be prefixed with InternalPrefix
InternalFiles []string
// InternalPrefix is a prefix for identifiers from InternalFiles.
InternalPrefix string
// PackagePath is an import path of the package.
// This is base path. flags.packageName will be added.
// Example:
// "github.com/AllenDang/cimgui-go"
// If enerated with -pkg imgui, import path
// is supposed to be "github.com/AllenDang/cimgui-go/imgui"
PackagePath string
}

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

result = strings.TrimSuffix(result, "\n")

return result
}
2 changes: 1 addition & 1 deletion imgui/callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imgui/funcs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imgui/typedefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imguizmo/callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imguizmo/funcs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imguizmo/typedefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion immarkdown/callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion immarkdown/funcs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion immarkdown/typedefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imnodes/callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imnodes/funcs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imnodes/typedefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion implot/callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion implot/funcs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion implot/typedefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading