From ffa823f748b17a7b8e19772af76106d89b95b6ad Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 25 Sep 2024 11:56:43 +0800 Subject: [PATCH] llcppsymg:gendylib path test llcppsymg:headerpath test --- chore/_xtool/llcppsymg/dylib/dylib.go | 66 +++++++++++ .../llcppsymg/dylib/dylib_test/dylib.go | 51 +++++++++ .../llcppsymg/dylib/dylib_test/llgo.expect | 22 ++++ chore/_xtool/llcppsymg/header/header.go | 23 ++++ .../llcppsymg/header/header_test/header.go | 104 ++++++++++++++++++ .../llcppsymg/header/header_test/llgo.expect | 23 ++++ chore/_xtool/llcppsymg/llcppsymg.go | 58 +--------- 7 files changed, 294 insertions(+), 53 deletions(-) create mode 100644 chore/_xtool/llcppsymg/dylib/dylib.go create mode 100644 chore/_xtool/llcppsymg/dylib/dylib_test/dylib.go create mode 100644 chore/_xtool/llcppsymg/dylib/dylib_test/llgo.expect create mode 100644 chore/_xtool/llcppsymg/header/header.go create mode 100644 chore/_xtool/llcppsymg/header/header_test/header.go create mode 100644 chore/_xtool/llcppsymg/header/header_test/llgo.expect diff --git a/chore/_xtool/llcppsymg/dylib/dylib.go b/chore/_xtool/llcppsymg/dylib/dylib.go new file mode 100644 index 00000000..73df5f4e --- /dev/null +++ b/chore/_xtool/llcppsymg/dylib/dylib.go @@ -0,0 +1,66 @@ +package dylib + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/goplus/llgo/xtool/nm" +) + +func ParseDylibSymbols(lib string) ([]*nm.Symbol, error) { + fmt.Printf("parse dylib symbols from config lib:%s\n", lib) + dylibPaths, err := GenDylibPaths(lib) + if err != nil { + return nil, errors.New("failed to generate dylib path") + } + + var symbols []*nm.Symbol + for _, dylibPath := range dylibPaths { + if _, err := os.Stat(dylibPath); err != nil { + fmt.Printf("Failed to access dylib error: %s\n", err) + continue + } + + files, err := nm.New("").List(dylibPath) + if err != nil { + fmt.Printf("Failed to list symbols in dylib: %s, error: %s\n", dylibPath, err) + continue + } + + for _, file := range files { + symbols = append(symbols, file.Symbols...) + } + } + + if len(symbols) == 0 { + return nil, errors.New("no symbols found in any dylib") + } + + return symbols, nil +} + +func GenDylibPaths(lib string) ([]string, error) { + parts := strings.Fields(lib) + var libPath, libName string + var dylibPaths []string + + for _, part := range parts { + if strings.HasPrefix(part, "-L") { + libPath = part[2:] + } else if strings.HasPrefix(part, "-l") { + libName = part[2:] + if libPath != "" && libName != "" { + dylibPaths = append(dylibPaths, filepath.Join(libPath, "lib"+libName+".dylib")) + } + } + } + + if len(dylibPaths) == 0 { + return nil, fmt.Errorf("failed to parse pkg-config output: %s", lib) + } + + return dylibPaths, nil +} diff --git a/chore/_xtool/llcppsymg/dylib/dylib_test/dylib.go b/chore/_xtool/llcppsymg/dylib/dylib_test/dylib.go new file mode 100644 index 00000000..9999a165 --- /dev/null +++ b/chore/_xtool/llcppsymg/dylib/dylib_test/dylib.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + + "github.com/goplus/llgo/chore/_xtool/llcppsymg/dylib" +) + +func TestGenDylibPaths() { + fmt.Println("=== Test GenDylibPaths ===") + + testCases := []struct { + name string + input string + }{ + { + name: "Lua library", + input: "-L/opt/homebrew/lib -llua -lm", + }, + { + name: "SQLite library", + input: "-L/opt/homebrew/opt/sqlite/lib -lsqlite3", + }, + { + name: "INIReader library", + input: "-L/opt/homebrew/Cellar/inih/58/lib -lINIReader", + }, + { + name: "No valid library", + input: "-L/opt/homebrew/lib", + }, + } + + for _, tc := range testCases { + fmt.Printf("Test case: %s\n", tc.name) + fmt.Printf("Input: %s\n", tc.input) + + result, err := dylib.GenDylibPaths(tc.input) + + if err != nil { + fmt.Printf("Error: %v\n", err) + } else { + fmt.Printf("Output: %v\n", result) + } + fmt.Println() + } +} + +func main() { + TestGenDylibPaths() +} diff --git a/chore/_xtool/llcppsymg/dylib/dylib_test/llgo.expect b/chore/_xtool/llcppsymg/dylib/dylib_test/llgo.expect new file mode 100644 index 00000000..3feb86f3 --- /dev/null +++ b/chore/_xtool/llcppsymg/dylib/dylib_test/llgo.expect @@ -0,0 +1,22 @@ +#stdout +=== Test GenDylibPaths === +Test case: Lua library +Input: -L/opt/homebrew/lib -llua -lm +Output: [/opt/homebrew/lib/liblua.dylib /opt/homebrew/lib/libm.dylib] + +Test case: SQLite library +Input: -L/opt/homebrew/opt/sqlite/lib -lsqlite3 +Output: [/opt/homebrew/opt/sqlite/lib/libsqlite3.dylib] + +Test case: INIReader library +Input: -L/opt/homebrew/Cellar/inih/58/lib -lINIReader +Output: [/opt/homebrew/Cellar/inih/58/lib/libINIReader.dylib] + +Test case: No valid library +Input: -L/opt/homebrew/lib +Error: failed to parse pkg-config output: -L/opt/homebrew/lib + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsymg/header/header.go b/chore/_xtool/llcppsymg/header/header.go new file mode 100644 index 00000000..392b0628 --- /dev/null +++ b/chore/_xtool/llcppsymg/header/header.go @@ -0,0 +1,23 @@ +package header + +import ( + "fmt" + "path/filepath" + "strings" +) + +func GenHeaderFilePath(cflags string, files []string) ([]string, error) { + fmt.Printf("get filepath from config cflags%s & include:%v\n", cflags, files) + prefixPath := strings.TrimPrefix(cflags, "-I") + var includePaths []string + for _, file := range files { + if file == "" { + continue + } + includePaths = append(includePaths, filepath.Join(prefixPath, "/"+file)) + } + if len(includePaths) == 0 { + return nil, fmt.Errorf("no valid header files") + } + return includePaths, nil +} diff --git a/chore/_xtool/llcppsymg/header/header_test/header.go b/chore/_xtool/llcppsymg/header/header_test/header.go new file mode 100644 index 00000000..9378aae7 --- /dev/null +++ b/chore/_xtool/llcppsymg/header/header_test/header.go @@ -0,0 +1,104 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +func GenHeaderFilePath(cflags string, files []string) ([]string, error) { + prefixPath := strings.TrimPrefix(cflags, "-I") + + var validPaths []string + var errs []string + + for _, file := range files { + if file == "" { + continue + } + fullPath := filepath.Join(prefixPath, file) + if f, err := os.Open(fullPath); err != nil { + if os.IsNotExist(err) { + errs = append(errs, fmt.Sprintf("file not found: %s", file)) + } else { + errs = append(errs, fmt.Sprintf("error accessing file %s: %v", file, err)) + } + } else { + f.Close() + validPaths = append(validPaths, fullPath) + } + } + + if len(validPaths) == 0 && len(errs) == 0 { + return nil, fmt.Errorf("no valid header files") + } + + if len(errs) > 0 { + return validPaths, fmt.Errorf("some files not found or inaccessible: %v", errs) + } + + return validPaths, nil +} + +func TestGenHeaderFilePath() { + fmt.Println("=== Test GenHeaderFilePath ===") + + tempDir := os.TempDir() + tempFile1 := filepath.Join(tempDir, "test1.h") + tempFile2 := filepath.Join(tempDir, "test2.h") + os.Create(tempFile1) + os.Create(tempFile2) + defer os.Remove(tempFile1) + defer os.Remove(tempFile2) + + testCases := []struct { + name string + cflags string + files []string + }{ + { + name: "Valid files", + cflags: "-I" + tempDir, + files: []string{"test1.h", "test2.h"}, + }, + { + name: "Mixed existing and non-existing files", + cflags: "-I" + tempDir, + files: []string{"test1.h", "nonexistent.h"}, + }, + { + name: "No existing files", + cflags: "-I" + tempDir, + files: []string{"nonexistent1.h", "nonexistent2.h"}, + }, + { + name: "Empty file list", + cflags: "-I/usr/include", + files: []string{}, + }, + } + + for _, tc := range testCases { + fmt.Printf("Test case: %s\n", tc.name) + fmt.Printf("Input files: %v\n", tc.files) + + result, err := GenHeaderFilePath(tc.cflags, tc.files) + + if err != nil { + fmt.Printf("Error: %v\n", err) + } + if result != nil { + relativeResult := make([]string, len(result)) + for i, path := range result { + relativeResult[i] = filepath.Base(path) + } + fmt.Printf("Output: %v\n", relativeResult) + } + fmt.Println() + } +} + +func main() { + TestGenHeaderFilePath() +} diff --git a/chore/_xtool/llcppsymg/header/header_test/llgo.expect b/chore/_xtool/llcppsymg/header/header_test/llgo.expect new file mode 100644 index 00000000..cff9314e --- /dev/null +++ b/chore/_xtool/llcppsymg/header/header_test/llgo.expect @@ -0,0 +1,23 @@ +#stdout +=== Test GenHeaderFilePath === +Test case: Valid files +Input files: [test1.h test2.h] +Output: [test1.h test2.h] + +Test case: Mixed existing and non-existing files +Input files: [test1.h nonexistent.h] +Error: some files not found or inaccessible: [file not found: nonexistent.h] +Output: [test1.h] + +Test case: No existing files +Input files: [nonexistent1.h nonexistent2.h] +Error: some files not found or inaccessible: [file not found: nonexistent1.h file not found: nonexistent2.h] + +Test case: Empty file list +Input files: [] +Error: no valid header files + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsymg/llcppsymg.go b/chore/_xtool/llcppsymg/llcppsymg.go index 67cf03a1..3a212cae 100644 --- a/chore/_xtool/llcppsymg/llcppsymg.go +++ b/chore/_xtool/llcppsymg/llcppsymg.go @@ -21,13 +21,14 @@ import ( "fmt" "io" "os" - "path/filepath" "strings" "unsafe" "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/dylib" + "github.com/goplus/llgo/chore/_xtool/llcppsymg/header" "github.com/goplus/llgo/chore/_xtool/llcppsymg/parse" "github.com/goplus/llgo/chore/llcppg/types" "github.com/goplus/llgo/xtool/nm" @@ -54,11 +55,11 @@ func main() { if err != nil { fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile) } - symbols, err := parseDylibSymbols(conf.Libs) - + symbols, err := dylib.ParseDylibSymbols(conf.Libs) check(err) - filepaths := genHeaderFilePath(conf.CFlags, conf.Include) + filepaths, err := header.GenHeaderFilePath(conf.CFlags, conf.Include) + check(err) headerInfos, err := parse.ParseHeaderFile(filepaths, conf.TrimPrefixes, conf.Cplusplus, false) check(err) @@ -74,55 +75,6 @@ func check(err error) { } } -func parseDylibSymbols(lib string) ([]*nm.Symbol, error) { - dylibPath, err := genDylibPath(lib) - if err != nil { - return nil, errors.New("failed to generate dylib path") - } - - files, err := nm.New("").List(dylibPath) - if err != nil { - return nil, errors.New("failed to list symbols in dylib") - } - - var symbols []*nm.Symbol - for _, file := range files { - symbols = append(symbols, file.Symbols...) - } - - return symbols, nil -} - -func genDylibPath(lib string) (string, error) { - output := lib - libPath := "" - libName := "" - for _, part := range strings.Fields(string(output)) { - if strings.HasPrefix(part, "-L") { - libPath = part[2:] - } else if strings.HasPrefix(part, "-l") { - libName = part[2:] - } - } - - if libPath == "" || libName == "" { - return "", fmt.Errorf("failed to parse pkg-config output: %s", output) - } - - dylibPath := filepath.Join(libPath, "lib"+libName+".dylib") - return dylibPath, nil -} - -func genHeaderFilePath(cflags string, files []string) []string { - prefixPath := cflags - prefixPath = strings.TrimPrefix(prefixPath, "-I") - var includePaths []string - for _, file := range files { - includePaths = append(includePaths, filepath.Join(prefixPath, "/"+file)) - } - return includePaths -} - func getCommonSymbols(dylibSymbols []*nm.Symbol, symbolMap map[string]*parse.SymbolInfo, prefix []string) []*types.SymbolInfo { var commonSymbols []*types.SymbolInfo for _, dylibSym := range dylibSymbols {