From 00bc10f96709929a9c32b9cc4057fe4f6d89b80b Mon Sep 17 00:00:00 2001 From: tsingbx Date: Tue, 18 Feb 2025 15:18:25 +0800 Subject: [PATCH] refactoring --- _xtool/llcppsigfetch/parse/parse.go | 4 +- .../llcppsymg/_cmptest/symbol_test/symbol.go | 6 +- _xtool/llcppsymg/config/config.go | 6 +- _xtool/llcppsymg/symbol/symbol.go | 16 +++--- cmd/gogensig/config/conf.go | 6 +- cmd/gogensig/config/config_test.go | 4 +- cmd/gogensig/convert/_comment_test.go | 4 +- cmd/gogensig/convert/convert.go | 4 +- cmd/gogensig/convert/convert_test.go | 4 +- cmd/gogensig/convert/deps.go | 6 +- cmd/gogensig/convert/package_bulitin_test.go | 4 +- cmd/gogensig/convert/package_test.go | 26 ++++----- cmd/gogensig/convert/type_builtin_test.go | 4 +- cmd/gogensig/gogensig.go | 56 +++++++++---------- cmd/llcppcfg/llcppgcfg/cfg.go | 16 +++--- cmd/llcppcfg/llcppgcfg/cfg_test.go | 16 +++--- llcppg.go | 4 +- llcppg/llcppg.go | 43 ++++++++++++++ types/types.go | 34 ----------- 19 files changed, 133 insertions(+), 130 deletions(-) create mode 100644 llcppg/llcppg.go delete mode 100644 types/types.go diff --git a/_xtool/llcppsigfetch/parse/parse.go b/_xtool/llcppsigfetch/parse/parse.go index d197f102..b89b5885 100644 --- a/_xtool/llcppsigfetch/parse/parse.go +++ b/_xtool/llcppsigfetch/parse/parse.go @@ -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" ) @@ -19,7 +19,7 @@ type Context struct { } type ContextConfig struct { - Conf *types.Config + Conf *llcppg.Config IncFlags []string } diff --git a/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go b/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go index bbcf8c90..b370ac00 100644 --- a/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go +++ b/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go @@ -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" ) @@ -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"}, diff --git a/_xtool/llcppsymg/config/config.go b/_xtool/llcppsymg/config/config.go index fe7a745e..6de683e4 100644 --- a/_xtool/llcppsymg/config/config.go +++ b/_xtool/llcppsymg/config/config.go @@ -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) { @@ -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", ""), diff --git a/_xtool/llcppsymg/symbol/symbol.go b/_xtool/llcppsymg/symbol/symbol.go index 7c3f381d..a23a67d9 100644 --- a/_xtool/llcppsymg/symbol/symbol.go +++ b/_xtool/llcppsymg/symbol/symbol.go @@ -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" @@ -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, @@ -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 } @@ -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", ""), @@ -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") diff --git a/cmd/gogensig/config/conf.go b/cmd/gogensig/config/conf.go index 6a6253ac..3dbc8631 100644 --- a/cmd/gogensig/config/conf.go +++ b/cmd/gogensig/config/conf.go @@ -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 diff --git a/cmd/gogensig/config/config_test.go b/cmd/gogensig/config/config_test.go index bea7e1e7..dad2cb5d 100644 --- a/cmd/gogensig/config/config_test.go +++ b/cmd/gogensig/config/config_test.go @@ -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) { @@ -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"}, diff --git a/cmd/gogensig/convert/_comment_test.go b/cmd/gogensig/convert/_comment_test.go index bce7b98b..77c7e106 100644 --- a/cmd/gogensig/convert/_comment_test.go +++ b/cmd/gogensig/convert/_comment_test.go @@ -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 @@ -18,7 +18,7 @@ func TestCommentSlashStarStar(t *testing.T) { GoName: "CustomExecuteFoo", }, }, - &cppgtypes.Config{}, + &llcppg.Config{}, ` /** Foo comment diff --git a/cmd/gogensig/convert/convert.go b/cmd/gogensig/convert/convert.go index 42f38952..7991ead5 100644 --- a/cmd/gogensig/convert/convert.go +++ b/cmd/gogensig/convert/convert.go @@ -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 { @@ -45,7 +45,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) diff --git a/cmd/gogensig/convert/convert_test.go b/cmd/gogensig/convert/convert_test.go index 2ec619db..1ebc7e55 100644 --- a/cmd/gogensig/convert/convert_test.go +++ b/cmd/gogensig/convert/convert_test.go @@ -16,8 +16,8 @@ import ( "github.com/goplus/llcppg/cmd/gogensig/convert/basic" "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" ) @@ -444,7 +444,7 @@ 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 { diff --git a/cmd/gogensig/convert/deps.go b/cmd/gogensig/convert/deps.go index f76da8ac..76a2ba6a 100644 --- a/cmd/gogensig/convert/deps.go +++ b/cmd/gogensig/convert/deps.go @@ -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" ) @@ -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, diff --git a/cmd/gogensig/convert/package_bulitin_test.go b/cmd/gogensig/convert/package_bulitin_test.go index 3530b9ac..1becd0cc 100644 --- a/cmd/gogensig/convert/package_bulitin_test.go +++ b/cmd/gogensig/convert/package_bulitin_test.go @@ -7,7 +7,7 @@ import ( "github.com/goplus/gogen" "github.com/goplus/llcppg/ast" cfg "github.com/goplus/llcppg/cmd/gogensig/config" - cppgtypes "github.com/goplus/llcppg/types" + "github.com/goplus/llcppg/llcppg" ) func TestTypeRefIncompleteFail(t *testing.T) { @@ -19,7 +19,7 @@ func TestTypeRefIncompleteFail(t *testing.T) { pkg := NewPackage(&PackageConfig{ PkgBase: PkgBase{ PkgPath: ".", - CppgConf: &cppgtypes.Config{}, + CppgConf: &llcppg.Config{}, Pubs: make(map[string]string), }, Name: "testpkg", diff --git a/cmd/gogensig/convert/package_test.go b/cmd/gogensig/convert/package_test.go index d68324a4..35caa117 100644 --- a/cmd/gogensig/convert/package_test.go +++ b/cmd/gogensig/convert/package_test.go @@ -14,7 +14,7 @@ import ( "github.com/goplus/llcppg/cmd/gogensig/convert" "github.com/goplus/llcppg/cmd/gogensig/convert/names" "github.com/goplus/llcppg/cmd/gogensig/dbg" - cppgtypes "github.com/goplus/llcppg/types" + "github.com/goplus/llcppg/llcppg" "github.com/goplus/mod/gopmod" ) @@ -111,7 +111,7 @@ func TestLinkFileOK(t *testing.T) { pkg := createTestPkg(t, &convert.PackageConfig{ OutputDir: tempDir, PkgBase: convert.PkgBase{ - CppgConf: &cppgtypes.Config{ + CppgConf: &llcppg.Config{ Libs: "pkg-config --libs libcjson", }, }, @@ -133,7 +133,7 @@ func TestLinkFileFail(t *testing.T) { pkg := createTestPkg(t, &convert.PackageConfig{ OutputDir: tempDir, PkgBase: convert.PkgBase{ - CppgConf: &cppgtypes.Config{}, + CppgConf: &llcppg.Config{}, }, }) @@ -151,7 +151,7 @@ func TestLinkFileFail(t *testing.T) { pkg := createTestPkg(t, &convert.PackageConfig{ OutputDir: tempDir, PkgBase: convert.PkgBase{ - CppgConf: &cppgtypes.Config{ + CppgConf: &llcppg.Config{ Libs: "${pkg-config --libs libcjson}", }, }, @@ -699,7 +699,7 @@ func Foo(a unsafe.Pointer) unsafe.Pointer GoName: "Foo", }, }, - cppgconf: &cppgtypes.Config{ + cppgconf: &llcppg.Config{ Name: "testpkg", }, expected: ` @@ -1525,7 +1525,7 @@ func TestIdentRefer(t *testing.T) { t.Run("type alias", func(t *testing.T) { pkg := createTestPkg(t, &convert.PackageConfig{ PkgBase: convert.PkgBase{ - CppgConf: &cppgtypes.Config{}, + CppgConf: &llcppg.Config{}, }, }) err := pkg.NewTypedefDecl(&ast.TypedefDecl{ @@ -1636,7 +1636,7 @@ type genDeclTestCase struct { name string decl ast.Decl symbs []cfg.SymbolEntry - cppgconf *cppgtypes.Config + cppgconf *llcppg.Config expected string expectedErr string } @@ -1690,13 +1690,13 @@ func compareError(t *testing.T, err error, expectErr string) { func createTestPkg(t *testing.T, config *convert.PackageConfig) *convert.Package { t.Helper() if config.CppgConf == nil { - config.CppgConf = &cppgtypes.Config{} + config.CppgConf = &llcppg.Config{} } if config.SymbolTable == nil { config.SymbolTable = cfg.CreateSymbolTable([]cfg.SymbolEntry{}) } if config.CppgConf == nil { - config.CppgConf = &cppgtypes.Config{} + config.CppgConf = &llcppg.Config{} } if config.SymbolTable == nil { config.SymbolTable = cfg.CreateSymbolTable([]cfg.SymbolEntry{}) @@ -2106,7 +2106,7 @@ func TestImport(t *testing.T) { if err != nil { t.Fatal(err) } - p.PkgInfo = convert.NewPkgInfo(".", ".", &cppgtypes.Config{ + p.PkgInfo = convert.NewPkgInfo(".", ".", &llcppg.Config{ Deps: []string{ "github.com/goplus/llcppg/cmd/gogensig/convert/testdata/invalidpath", "github.com/goplus/llcppg/cmd/gogensig/convert/testdata/partfinddep", @@ -2133,7 +2133,7 @@ func TestImport(t *testing.T) { createTestPkg(t, &convert.PackageConfig{ OutputDir: ".", PkgBase: convert.PkgBase{ - CppgConf: &cppgtypes.Config{ + CppgConf: &llcppg.Config{ Deps: []string{ "github.com/goplus/llcppg/cmd/gogensig/convert/testdata/invalidpub", }, @@ -2150,7 +2150,7 @@ func TestImport(t *testing.T) { createTestPkg(t, &convert.PackageConfig{ OutputDir: ".", PkgBase: convert.PkgBase{ - CppgConf: &cppgtypes.Config{ + CppgConf: &llcppg.Config{ Deps: []string{ "github.com/goplus/llcppg/cmd/gogensig/convert/testdata/invaliddep", }, @@ -2162,7 +2162,7 @@ func TestImport(t *testing.T) { createTestPkg(t, &convert.PackageConfig{ OutputDir: ".", PkgBase: convert.PkgBase{ - CppgConf: &cppgtypes.Config{ + CppgConf: &llcppg.Config{ Deps: []string{ "github.com/goplus/llcppg/cmd/gogensig/convert/testdata/cjson", "github.com/goplus/llcppg/cmd/gogensig/convert/testdata/cjsonbool", diff --git a/cmd/gogensig/convert/type_builtin_test.go b/cmd/gogensig/convert/type_builtin_test.go index 4ab6516d..780598e7 100644 --- a/cmd/gogensig/convert/type_builtin_test.go +++ b/cmd/gogensig/convert/type_builtin_test.go @@ -8,7 +8,7 @@ import ( "github.com/goplus/gogen" "github.com/goplus/llcppg/ast" "github.com/goplus/llcppg/cmd/gogensig/config" - cppgtypes "github.com/goplus/llcppg/types" + "github.com/goplus/llcppg/llcppg" ) func TestIdentRef(t *testing.T) { @@ -31,7 +31,7 @@ func TestSubstObj(t *testing.T) { pkg := NewPackage(&PackageConfig{ PkgBase: PkgBase{ PkgPath: ".", - CppgConf: &cppgtypes.Config{}, + CppgConf: &llcppg.Config{}, }, Name: "testpkg", GenConf: &gogen.Config{}, diff --git a/cmd/gogensig/gogensig.go b/cmd/gogensig/gogensig.go index 7a1999eb..c88aa7d1 100644 --- a/cmd/gogensig/gogensig.go +++ b/cmd/gogensig/gogensig.go @@ -17,55 +17,49 @@ package main import ( + "flag" "fmt" "io" "os" "path/filepath" - "strings" - "github.com/goplus/llcppg/_xtool/llcppsymg/args" "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/dbg" "github.com/goplus/llcppg/cmd/gogensig/unmarshal" + "github.com/goplus/llcppg/llcppg" ) -func main() { - ags, remainArgs := args.ParseArgs(os.Args[1:], args.LLCPPG_SIGFETCH, nil) - - if ags.Help { - printUsage() - return - } - - if ags.Verbose { - dbg.SetDebugAll() - } - +func readSigfetchFile(sigfetchFile string) ([]byte, error) { var data []byte var err error - if ags.UseStdin { + if sigfetchFile == "-" { data, err = io.ReadAll(os.Stdin) } else { - data, err = os.ReadFile(ags.CfgFile) + data, err = os.ReadFile(sigfetchFile) } - check(err) + return data, err +} - var cfg string - for i := 0; i < len(remainArgs); i++ { - arg := remainArgs[i] - if strings.HasPrefix(arg, "-cfg=") { - cfg = args.StringArg(arg, args.LLCPPG_CFG) - } +func main() { + verbose, help, cfgFile := false, false, "" + flag.BoolVar(&verbose, "v", false, "verbose") + flag.BoolVar(&help, "h", false, "print help") + flag.StringVar(&cfgFile, "cfg", llcppg.LLCPPG_CFG, "llcppg.cfg config file") + flag.Parse() + if help || len(flag.Args()) == 0 { + printUsage() + return } - if cfg == "" { - cfg = args.LLCPPG_CFG + if verbose { + dbg.SetDebugAll() } - - conf, err := config.GetCppgCfgFromPath(cfg) + sigfetchFile := flag.Arg(0) + sigfetchFileData, err := readSigfetchFile(sigfetchFile) + check(err) + conf, err := config.GetCppgCfgFromPath(cfgFile) check(err) - wd, err := os.Getwd() check(err) @@ -75,14 +69,14 @@ func main() { p, _, err := basic.ConvertProcesser(&basic.Config{ AstConvertConfig: convert.AstConvertConfig{ PkgName: conf.Name, - CfgFile: filepath.Join(wd, cfg), + CfgFile: filepath.Join(wd, cfgFile), SymbFile: filepath.Join(wd, "llcppg.symb.json"), PubFile: filepath.Join(wd, "llcppg.pub"), }, }) check(err) - inputdata, err := unmarshal.FileSet(data) + inputdata, err := unmarshal.FileSet(sigfetchFileData) check(err) err = p.ProcessFileSet(inputdata) @@ -124,5 +118,5 @@ func prepareEnv(wd, pkg string, deps []string) error { } func printUsage() { - fmt.Fprintln(os.Stderr, "Usage: gogensig [-v] [sigfetch-file]") + fmt.Fprintln(os.Stderr, "Usage: gogensig [-v|-cfg] [sigfetch-file]") } diff --git a/cmd/llcppcfg/llcppgcfg/cfg.go b/cmd/llcppcfg/llcppgcfg/cfg.go index e083b30c..1c312302 100644 --- a/cmd/llcppcfg/llcppgcfg/cfg.go +++ b/cmd/llcppcfg/llcppgcfg/cfg.go @@ -11,7 +11,7 @@ import ( "unicode" "github.com/goplus/llcppg/cmdout" - "github.com/goplus/llcppg/types" + "github.com/goplus/llcppg/llcppg" ) type llcppCfgKey string @@ -40,8 +40,6 @@ func newEmptyStringError(name string) *emptyStringError { return &emptyStringError{name: name} } -type LLCppConfig types.Config - func getDir(relPath string) string { index := strings.IndexRune(relPath, filepath.Separator) if index < 0 { @@ -171,7 +169,7 @@ func parseCFlagsEntry(cflags, cflag string, exts []string, excludeSubdirs []stri return &cflagEntry } -func sortIncludes(expandCflags string, cfg *LLCppConfig, exts []string, excludeSubdirs []string) { +func sortIncludes(expandCflags string, cfg *llcppg.Config, exts []string, excludeSubdirs []string) { list := strings.Fields(expandCflags) includeList := NewIncludeList() for i, cflag := range list { @@ -181,13 +179,13 @@ func sortIncludes(expandCflags string, cfg *LLCppConfig, exts []string, excludeS cfg.Include = includeList.include } -func NewLLCppConfig(name string, flag FlagMode) *LLCppConfig { - cfg := &LLCppConfig{ - Name: name, - } +func NewLLCppgConfig(name string, flag FlagMode) *llcppg.Config { + cfg := llcppg.NewDefaultConfig() + cfg.Name = name cfg.CFlags = fmt.Sprintf("$(pkg-config --cflags %s)", name) cfg.Libs = fmt.Sprintf("$(pkg-config --libs %s)", name) cfg.TrimPrefixes = []string{} + cfg.Deps = []string{} cfg.Cplusplus = (flag&WithCpp != 0) return cfg } @@ -208,7 +206,7 @@ func GenCfg(name string, flag FlagMode, exts []string, excludeSubdirs []string) if len(name) == 0 { return nil, newEmptyStringError("name") } - cfg := NewLLCppConfig(name, flag) + cfg := NewLLCppgConfig(name, flag) expandCFlags := ExpandName(name, "", cfgCflagsKey) sortIncludes(expandCFlags, cfg, exts, excludeSubdirs) diff --git a/cmd/llcppcfg/llcppgcfg/cfg_test.go b/cmd/llcppcfg/llcppgcfg/cfg_test.go index b8bf40bb..7185c47b 100644 --- a/cmd/llcppcfg/llcppgcfg/cfg_test.go +++ b/cmd/llcppcfg/llcppgcfg/cfg_test.go @@ -10,6 +10,8 @@ import ( "runtime" "strings" "testing" + + "github.com/goplus/llcppg/llcppg" ) func Test_emptyStringError_Error(t *testing.T) { @@ -372,19 +374,19 @@ func Test_parseCFlagsEntry(t *testing.T) { func Test_sortIncludes(t *testing.T) { cflags, _ := newCflags("cfg_test_data/cjson/include/") depsCflags, _ := newCflags("cfg_test_data/deps/") - cfg := &LLCppConfig{ + cfg := &llcppg.Config{ Name: "libcjson", CFlags: cflags, Libs: "$(pkg-config --libs libcjson)", } - depCfg := &LLCppConfig{ + depCfg := &llcppg.Config{ Name: "deps", CFlags: depsCflags, Libs: "", } type args struct { expandCflags string - cfg *LLCppConfig + cfg *llcppg.Config exts []string excludeSubdirs []string } @@ -482,7 +484,7 @@ func TestNewLLCppConfig(t *testing.T) { tests := []struct { name string args args - want *LLCppConfig + want *llcppg.Config }{ { "libcjson", @@ -490,7 +492,7 @@ func TestNewLLCppConfig(t *testing.T) { "libcjson", WithTab, }, - &LLCppConfig{ + &llcppg.Config{ Name: "libcjson", CFlags: "$(pkg-config --cflags libcjson)", Libs: "$(pkg-config --libs libcjson)", @@ -503,8 +505,8 @@ func TestNewLLCppConfig(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewLLCppConfig(tt.args.name, tt.args.flag); !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewLLCppConfig() = %v, want %v", got, tt.want) + if got := NewLLCppgConfig(tt.args.name, tt.args.flag); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewLLCppgConfig() = %v, want %v", got, tt.want) } }) } diff --git a/llcppg.go b/llcppg.go index c5254db0..9e12de26 100644 --- a/llcppg.go +++ b/llcppg.go @@ -26,7 +26,7 @@ import ( "os/exec" "github.com/goplus/llcppg/_xtool/llcppsymg/args" - "github.com/goplus/llcppg/types" + "github.com/goplus/llcppg/llcppg" "github.com/goplus/llgo/xtool/env" ) @@ -162,7 +162,7 @@ func do(cfgFile string, mode modeFlags, verbose verboseFlags) { check(err) defer f.Close() - var conf types.Config + var conf llcppg.Config json.NewDecoder(f).Decode(&conf) conf.CFlags = env.ExpandEnv(conf.CFlags) conf.Libs = env.ExpandEnv(conf.Libs) diff --git a/llcppg/llcppg.go b/llcppg/llcppg.go new file mode 100644 index 00000000..33982520 --- /dev/null +++ b/llcppg/llcppg.go @@ -0,0 +1,43 @@ +package llcppg + +const LLCPPG_CFG = "llcppg.cfg" +const LLCPPG_SYMB = "llcppg.symb.json" +const LLCPPG_SIGFETCH = "llcppg.sigfetch.json" +const LLCPPG_PUB = "llcppg.pub" + +type Condition struct { + OS []string `json:"os"` + Arch []string `json:"arch"` +} + +type ImplFiles struct { + Files []string `json:"files"` + Cond Condition `json:"cond"` +} + +func NewImplFiles() ImplFiles { + return ImplFiles{Files: []string{}, Cond: Condition{OS: []string{}, Arch: []string{}}} +} + +// Config represents a configuration for the llcppg tool. +type Config struct { + Name string `json:"name"` + CFlags string `json:"cflags"` + Libs string `json:"libs"` + Include []string `json:"include"` + TrimPrefixes []string `json:"trimPrefixes"` + Cplusplus bool `json:"cplusplus"` + Deps []string `json:"deps"` + IgnoreUnderScore bool `json:"ignoreUnderScore"` + Impl []ImplFiles `json:"impl"` +} + +func NewDefaultConfig() *Config { + return &Config{Impl: []ImplFiles{NewImplFiles()}} +} + +type SymbolInfo struct { + Mangle string `json:"mangle"` // C++ Symbol + CPP string `json:"c++"` // C++ function name + Go string `json:"go"` // Go function name +} diff --git a/types/types.go b/types/types.go deleted file mode 100644 index f0c7ec48..00000000 --- a/types/types.go +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package types - -// Config represents a configuration for the llcppg tool. -type Config struct { - Name string `json:"name"` - CFlags string `json:"cflags"` - Libs string `json:"libs"` - Include []string `json:"include"` - Deps []string `json:"deps"` - TrimPrefixes []string `json:"trimPrefixes"` - Cplusplus bool `json:"cplusplus"` -} - -type SymbolInfo struct { - Mangle string `json:"mangle"` // C++ Symbol - CPP string `json:"c++"` // C++ function name - Go string `json:"go"` // Go function name -}