Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Feb 18, 2025
1 parent 9b3dc41 commit 61f556e
Show file tree
Hide file tree
Showing 27 changed files with 328 additions and 278 deletions.
4 changes: 2 additions & 2 deletions _xtool/llcppsigfetch/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/goplus/llcppg/_xtool/llcppsigfetch/dbg"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c/cjson"
)

Expand All @@ -19,7 +19,7 @@ type Context struct {
}

type ContextConfig struct {
Conf *types.Config
Conf *llcppg.Config
IncFlags []string
}

Expand Down
6 changes: 3 additions & 3 deletions _xtool/llcppsymg/_cmptest/symbol_test/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/goplus/llcppg/_xtool/llcppsymg/parse"
"github.com/goplus/llcppg/_xtool/llcppsymg/symbol"
"github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/xtool/nm"
)

Expand Down Expand Up @@ -129,14 +129,14 @@ func TestReadExistingSymbolTable() {
func TestGenSymbolTableData() {
fmt.Println("=== Test GenSymbolTableData ===")

commonSymbols := []*types.SymbolInfo{
commonSymbols := []*llcppg.SymbolInfo{
{Mangle: "lua_absindex", CPP: "lua_absindex(lua_State *, int)", Go: "Absindex"},
{Mangle: "lua_arith", CPP: "lua_arith(lua_State *, int)", Go: "Arith"},
{Mangle: "lua_atpanic", CPP: "lua_atpanic(lua_State *, lua_CFunction)", Go: "Atpanic"},
{Mangle: "lua_callk", CPP: "lua_callk(lua_State *, int, int, lua_KContext, lua_KFunction)", Go: "Callk"},
}

existingSymbols := map[string]types.SymbolInfo{
existingSymbols := map[string]llcppg.SymbolInfo{
"lua_absindex": {Mangle: "lua_absindex", CPP: "lua_absindex(lua_State *, int)", Go: "Absindex"},
"lua_arith": {Mangle: "lua_arith", CPP: "lua_arith(lua_State *, int)", Go: "Arith"},
"lua_callk": {Mangle: "lua_callk", CPP: "lua_callk(lua_State *, int, int, lua_KContext, lua_KFunction)", Go: "ModifiedCallk"},
Expand Down
6 changes: 3 additions & 3 deletions _xtool/llcppsymg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"errors"
"unsafe"

"github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
)

type Conf struct {
*cjson.JSON
*types.Config
*llcppg.Config
}

func GetConf(data []byte) (Conf, error) {
Expand All @@ -20,7 +20,7 @@ func GetConf(data []byte) (Conf, error) {
return Conf{}, errors.New("failed to parse config")
}

config := &types.Config{
config := &llcppg.Config{
Name: GetStringItem(parsedConf, "name", ""),
CFlags: GetStringItem(parsedConf, "cflags", ""),
Libs: GetStringItem(parsedConf, "libs", ""),
Expand Down
16 changes: 8 additions & 8 deletions _xtool/llcppsymg/symbol/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/goplus/llcppg/_xtool/llcppsymg/dbg"
"github.com/goplus/llcppg/_xtool/llcppsymg/parse"
"github.com/goplus/llcppg/_xtool/llcppsymg/syspath"
"github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
"github.com/goplus/llgo/xtool/nm"
Expand Down Expand Up @@ -96,15 +96,15 @@ func ParseDylibSymbols(lib string) ([]*nm.Symbol, error) {

// finds the intersection of symbols from the dynamic library's symbol table and the symbols parsed from header files.
// It returns a list of symbols that can be externally linked.
func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse.SymbolInfo) []*types.SymbolInfo {
var commonSymbols []*types.SymbolInfo
func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse.SymbolInfo) []*llcppg.SymbolInfo {
var commonSymbols []*llcppg.SymbolInfo
for _, dylibSym := range dylibSymbols {
symName := dylibSym.Name
if runtime.GOOS == "darwin" {
symName = strings.TrimPrefix(symName, "_")
}
if symInfo, ok := headerSymbols[symName]; ok {
symbolInfo := &types.SymbolInfo{
symbolInfo := &llcppg.SymbolInfo{
Mangle: symName,
CPP: symInfo.ProtoName,
Go: symInfo.GoName,
Expand All @@ -115,7 +115,7 @@ func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse
return commonSymbols
}

func ReadExistingSymbolTable(fileName string) (map[string]types.SymbolInfo, bool) {
func ReadExistingSymbolTable(fileName string) (map[string]llcppg.SymbolInfo, bool) {
if _, err := os.Stat(fileName); err != nil {
return nil, false
}
Expand All @@ -130,12 +130,12 @@ func ReadExistingSymbolTable(fileName string) (map[string]types.SymbolInfo, bool
return nil, false
}

existingSymbols := make(map[string]types.SymbolInfo)
existingSymbols := make(map[string]llcppg.SymbolInfo)
arraySize := parsedJSON.GetArraySize()

for i := 0; i < int(arraySize); i++ {
item := parsedJSON.GetArrayItem(c.Int(i))
symbol := types.SymbolInfo{
symbol := llcppg.SymbolInfo{
Mangle: config.GetStringItem(item, "mangle", ""),
CPP: config.GetStringItem(item, "c++", ""),
Go: config.GetStringItem(item, "go", ""),
Expand All @@ -146,7 +146,7 @@ func ReadExistingSymbolTable(fileName string) (map[string]types.SymbolInfo, bool
return existingSymbols, true
}

func GenSymbolTableData(commonSymbols []*types.SymbolInfo, existingSymbols map[string]types.SymbolInfo) ([]byte, error) {
func GenSymbolTableData(commonSymbols []*llcppg.SymbolInfo, existingSymbols map[string]llcppg.SymbolInfo) ([]byte, error) {
if len(existingSymbols) > 0 {
if dbg.GetDebugSymbol() {
fmt.Println("GenSymbolTableData:generate symbol table with exist symbol table")
Expand Down
6 changes: 3 additions & 3 deletions cmd/gogensig/config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (

"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/cmd/gogensig/unmarshal"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
)

// llcppg.cfg
func GetCppgCfgFromPath(filePath string) (*cppgtypes.Config, error) {
func GetCppgCfgFromPath(filePath string) (*llcppg.Config, error) {
bytes, err := ReadFile(filePath)
if err != nil {
return nil, err
}
conf := &cppgtypes.Config{}
conf := &llcppg.Config{}
err = json.Unmarshal(bytes, &conf)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions cmd/gogensig/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/goplus/llcppg/cmd/gogensig/config"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
)

func TestLookupSymbolOK(t *testing.T) {
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestGetCppgCfgFromPath(t *testing.T) {
t.Fatal("Expected non-nil config")
}

expectedConfig := &cppgtypes.Config{
expectedConfig := &llcppg.Config{
Name: "lua",
CFlags: "$(pkg-config --cflags lua5.4)",
Include: []string{"litelua.h"},
Expand Down
4 changes: 2 additions & 2 deletions cmd/gogensig/convert/_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/goplus/llcppg/cmd/gogensig/cmptest"
"github.com/goplus/llcppg/cmd/gogensig/config"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
)

// TODO
Expand All @@ -18,7 +18,7 @@ func TestCommentSlashStarStar(t *testing.T) {
GoName: "CustomExecuteFoo",
},
},
&cppgtypes.Config{},
&llcppg.Config{},
`
/**
Foo comment
Expand Down
48 changes: 0 additions & 48 deletions cmd/gogensig/convert/basic/basic.go

This file was deleted.

20 changes: 11 additions & 9 deletions cmd/gogensig/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
cfg "github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
"github.com/goplus/llcppg/cmd/gogensig/visitor"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
)

type AstConvert struct {
Expand All @@ -18,15 +18,17 @@ type AstConvert struct {
visitDone func(pkg *Package, incPath string)
}

type AstConvertConfig struct {
PkgName string
SymbFile string // llcppg.symb.json
CfgFile string // llcppg.cfg
PubFile string // llcppg.pub
OutputDir string
type Config struct {
PkgName string
SigfetchFile string
SymbFile string // llcppg.symb.json
CfgFile string // llcppg.cfg
PubFile string // llcppg.pub
OutputDir string
PrepareFunc func(*Package)
}

func NewAstConvert(config *AstConvertConfig) (*AstConvert, error) {
func NewAstConvert(config *Config) (*AstConvert, error) {
if config == nil {
return nil, errors.New("config is nil")
}
Expand All @@ -45,7 +47,7 @@ func NewAstConvert(config *AstConvertConfig) (*AstConvert, error) {
if dbg.GetDebugError() {
log.Printf("Cant get llcppg.cfg from %s Use empty config\n", config.CfgFile)
}
conf = &cppgtypes.Config{}
conf = llcppg.NewDefaultConfig()
}

pubs, err := cfg.GetPubFromPath(config.PubFile)
Expand Down
32 changes: 15 additions & 17 deletions cmd/gogensig/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/convert"
"github.com/goplus/llcppg/cmd/gogensig/convert/basic"
"github.com/goplus/llcppg/cmd/gogensig/convert/filesetprocessor"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
"github.com/goplus/llcppg/cmd/gogensig/unmarshal"
"github.com/goplus/llcppg/llcppg"
ctoken "github.com/goplus/llcppg/token"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/llgo/xtool/env"
)

Expand Down Expand Up @@ -196,15 +196,13 @@ func testFrom(t *testing.T, name, dir string, gen bool, validateFunc func(t *tes
}
}

p, pkg, err := basic.ConvertProcesser(&basic.Config{
PkgPreprocessor: preprocess,
AstConvertConfig: convert.AstConvertConfig{
PkgName: name,
SymbFile: symbPath,
CfgFile: flagedCfgPath,
OutputDir: outputDir,
PubFile: pubPath,
},
p, pkg, err := filesetprocessor.New(&convert.Config{
PkgName: name,
SymbFile: symbPath,
CfgFile: flagedCfgPath,
OutputDir: outputDir,
PubFile: pubPath,
PrepareFunc: preprocess,
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -270,7 +268,7 @@ func testFrom(t *testing.T, name, dir string, gen bool, validateFunc func(t *tes

// ===========================error
func TestNewAstConvert(t *testing.T) {
_, err := convert.NewAstConvert(&convert.AstConvertConfig{
_, err := convert.NewAstConvert(&convert.Config{
PkgName: "test",
SymbFile: "",
CfgFile: "",
Expand All @@ -288,7 +286,7 @@ func TestNewAstConvertFail(t *testing.T) {
}

func TestVisitDone(t *testing.T) {
pkg, err := convert.NewAstConvert(&convert.AstConvertConfig{
pkg, err := convert.NewAstConvert(&convert.Config{
PkgName: "test",
SymbFile: "",
CfgFile: "",
Expand All @@ -305,7 +303,7 @@ func TestVisitDone(t *testing.T) {
}

func TestVisitFail(t *testing.T) {
converter, err := convert.NewAstConvert(&convert.AstConvertConfig{
converter, err := convert.NewAstConvert(&convert.Config{
PkgName: "test",
SymbFile: "",
CfgFile: "",
Expand Down Expand Up @@ -416,7 +414,7 @@ func TestWritePkgFilesFail(t *testing.T) {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
converter, err := convert.NewAstConvert(&convert.AstConvertConfig{
converter, err := convert.NewAstConvert(&convert.Config{
PkgName: "test",
SymbFile: "",
CfgFile: "",
Expand Down Expand Up @@ -444,13 +442,13 @@ func TestWritePkgFilesFail(t *testing.T) {
}

func TestGetIncPathFail(t *testing.T) {
cfg, err := config.CreateTmpJSONFile("llcppg.cfg", &cppgtypes.Config{
cfg, err := config.CreateTmpJSONFile("llcppg.cfg", &llcppg.Config{
Include: []string{"unexist.h"},
})
if err != nil {
t.Fatal(err)
}
converter, err := convert.NewAstConvert(&convert.AstConvertConfig{
converter, err := convert.NewAstConvert(&convert.Config{
PkgName: "test",
SymbFile: "",
CfgFile: cfg,
Expand Down
6 changes: 3 additions & 3 deletions cmd/gogensig/convert/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/goplus/llcppg/_xtool/llcppsymg/syspath"
cfg "github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/errs"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/xtool/env"
"github.com/goplus/mod/gopmod"
)
Expand Down Expand Up @@ -46,11 +46,11 @@ type PkgInfo struct {

type PkgBase struct {
PkgPath string // package path, e.g. github.com/goplus/llgo/cjson
CppgConf *cppgtypes.Config // llcppg.cfg
CppgConf *llcppg.Config // llcppg.cfg
Pubs map[string]string // llcppg.pub
}

func NewPkgInfo(pkgPath string, pkgDir string, conf *cppgtypes.Config, pubs map[string]string) *PkgInfo {
func NewPkgInfo(pkgPath string, pkgDir string, conf *llcppg.Config, pubs map[string]string) *PkgInfo {
return &PkgInfo{
PkgBase: PkgBase{PkgPath: pkgPath, Pubs: pubs, CppgConf: conf},
Dir: pkgDir,
Expand Down
Loading

0 comments on commit 61f556e

Please sign in to comment.