diff --git a/chore/_xtool/llcppsymg/config/config.go b/chore/_xtool/llcppsymg/config/config.go new file mode 100644 index 00000000..3545e25b --- /dev/null +++ b/chore/_xtool/llcppsymg/config/config.go @@ -0,0 +1,60 @@ +package config + +import ( + "errors" + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" + "github.com/goplus/llgo/chore/llcppg/types" +) + +type Conf struct { + *cjson.JSON + *types.Config +} + +func GetConf(data []byte) (Conf, error) { + parsedConf := cjson.ParseBytes(data) + if parsedConf == nil { + return Conf{}, errors.New("failed to parse config") + } + + config := &types.Config{ + Name: GetStringItem(parsedConf, "name", ""), + CFlags: GetStringItem(parsedConf, "cflags", ""), + Libs: GetStringItem(parsedConf, "libs", ""), + Include: GetStringArrayItem(parsedConf, "include"), + TrimPrefixes: GetStringArrayItem(parsedConf, "trimPrefixes"), + } + + return Conf{ + JSON: parsedConf, + Config: config, + }, nil +} + +func GetString(obj *cjson.JSON) (value string) { + str := obj.GetStringValue() + return unsafe.String((*byte)(unsafe.Pointer(str)), c.Strlen(str)) +} + +func GetStringItem(obj *cjson.JSON, key string, defval string) (value string) { + item := obj.GetObjectItemCaseSensitive(c.AllocaCStr(key)) + if item == nil { + return defval + } + return GetString(item) +} + +func GetStringArrayItem(obj *cjson.JSON, key string) (value []string) { + item := obj.GetObjectItemCaseSensitive(c.AllocaCStr(key)) + if item == nil { + return + } + value = make([]string, item.GetArraySize()) + for i := range value { + value[i] = GetString(item.GetArrayItem(c.Int(i))) + } + return +} diff --git a/chore/_xtool/llcppsymg/llcppsymg.go b/chore/_xtool/llcppsymg/llcppsymg.go index a3d1bede..6edde02f 100644 --- a/chore/_xtool/llcppsymg/llcppsymg.go +++ b/chore/_xtool/llcppsymg/llcppsymg.go @@ -28,6 +28,7 @@ import ( "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/cjson" + "github.com/goplus/llgo/chore/_xtool/llcppsymg/config" "github.com/goplus/llgo/chore/_xtool/llcppsymg/parse" "github.com/goplus/llgo/chore/llcppg/types" "github.com/goplus/llgo/cpp/llvm" @@ -49,22 +50,22 @@ func main() { } check(err) - config, err := getConf(data) + conf, err := config.GetConf(data) check(err) - defer config.Delete() + defer conf.Delete() if err != nil { fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile) } - symbols, err := parseDylibSymbols(config.Libs) + symbols, err := parseDylibSymbols(conf.Libs) check(err) - filepaths := generateHeaderFilePath(config.CFlags, config.Include) + filepaths := generateHeaderFilePath(conf.CFlags, conf.Include) astInfos, err := parse.ParseHeaderFile(filepaths) check(err) - symbolInfo := getCommonSymbols(symbols, astInfos, config.TrimPrefixes) + symbolInfo := getCommonSymbols(symbols, astInfos, conf.TrimPrefixes) err = genSymbolTableFile(symbolInfo) check(err) @@ -76,51 +77,6 @@ func check(err error) { } } -func getConf(data []byte) (types.Conf, error) { - parsedConf := cjson.ParseBytes(data) - if parsedConf == nil { - return types.Conf{}, errors.New("failed to parse config") - } - - config := &types.Config{ - Name: getStringItem(parsedConf, "name", ""), - CFlags: getStringItem(parsedConf, "cflags", ""), - Libs: getStringItem(parsedConf, "libs", ""), - Include: getStringArrayItem(parsedConf, "include"), - TrimPrefixes: getStringArrayItem(parsedConf, "trimPrefixes"), - } - - return types.Conf{ - JSON: parsedConf, - Config: config, - }, nil -} - -func getString(obj *cjson.JSON) (value string) { - str := obj.GetStringValue() - return unsafe.String((*byte)(unsafe.Pointer(str)), c.Strlen(str)) -} - -func getStringItem(obj *cjson.JSON, key string, defval string) (value string) { - item := obj.GetObjectItemCaseSensitive(c.AllocaCStr(key)) - if item == nil { - return defval - } - return getString(item) -} - -func getStringArrayItem(obj *cjson.JSON, key string) (value []string) { - item := obj.GetObjectItemCaseSensitive(c.AllocaCStr(key)) - if item == nil { - return - } - value = make([]string, item.GetArraySize()) - for i := range value { - value[i] = getString(item.GetArrayItem(c.Int(i))) - } - return -} - func parseDylibSymbols(lib string) ([]types.CPPSymbol, error) { dylibPath, err := generateDylibPath(lib) if err != nil { diff --git a/chore/llcppg/types/types.go b/chore/llcppg/types/types.go index 6ca9d833..9d700881 100644 --- a/chore/llcppg/types/types.go +++ b/chore/llcppg/types/types.go @@ -17,7 +17,6 @@ package types import ( - "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/xtool/nm" ) @@ -30,11 +29,6 @@ type Config struct { TrimPrefixes []string `json:"trimPrefixes"` } -type Conf struct { - *cjson.JSON - *Config -} - type CPPSymbol struct { DemangleName string *nm.Symbol