diff --git a/chore/_xtool/llcppsymg/_cmptest/dylib_test/dylib.go b/chore/_xtool/llcppsymg/_cmptest/dylib_test/dylib.go deleted file mode 100644 index 9999a165..00000000 --- a/chore/_xtool/llcppsymg/_cmptest/dylib_test/dylib.go +++ /dev/null @@ -1,51 +0,0 @@ -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/_cmptest/dylib_test/llgo.expect b/chore/_xtool/llcppsymg/_cmptest/dylib_test/llgo.expect deleted file mode 100644 index 3feb86f3..00000000 --- a/chore/_xtool/llcppsymg/_cmptest/dylib_test/llgo.expect +++ /dev/null @@ -1,22 +0,0 @@ -#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/_cmptest/header_test/header.go b/chore/_xtool/llcppsymg/_cmptest/header_test/header.go deleted file mode 100644 index 44611b29..00000000 --- a/chore/_xtool/llcppsymg/_cmptest/header_test/header.go +++ /dev/null @@ -1,71 +0,0 @@ -package main - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/goplus/llgo/chore/_xtool/llcppsymg/header" -) - -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 := header.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/_cmptest/header_test/llgo.expect b/chore/_xtool/llcppsymg/_cmptest/header_test/llgo.expect deleted file mode 100644 index cff9314e..00000000 --- a/chore/_xtool/llcppsymg/_cmptest/header_test/llgo.expect +++ /dev/null @@ -1,23 +0,0 @@ -#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/_cmptest/parse_test/llgo.expect b/chore/_xtool/llcppsymg/_cmptest/parse_test/llgo.expect index 819ff4b8..55bb70a2 100644 --- a/chore/_xtool/llcppsymg/_cmptest/parse_test/llgo.expect +++ b/chore/_xtool/llcppsymg/_cmptest/parse_test/llgo.expect @@ -1,4 +1,22 @@ #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 + === Test NewSymbolProcessor === Before: No prefixes After: Prefixes: [lua_ luaL_] diff --git a/chore/_xtool/llcppsymg/_cmptest/parse_test/parse.go b/chore/_xtool/llcppsymg/_cmptest/parse_test/parse.go index 4a827b45..0f738594 100644 --- a/chore/_xtool/llcppsymg/_cmptest/parse_test/parse.go +++ b/chore/_xtool/llcppsymg/_cmptest/parse_test/parse.go @@ -2,12 +2,15 @@ package main import ( "fmt" + "os" + "path/filepath" "sort" "github.com/goplus/llgo/chore/_xtool/llcppsymg/parse" ) func main() { + TestGenHeaderFilePath() TestNewSymbolProcessor() TestRemovePrefix() TestToGoName() @@ -16,6 +19,64 @@ func main() { TestParseHeaderFile() } +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 := parse.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 TestNewSymbolProcessor() { fmt.Println("=== Test NewSymbolProcessor ===") process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"}) diff --git a/chore/_xtool/llcppsymg/_cmptest/symbol_test/llgo.expect b/chore/_xtool/llcppsymg/_cmptest/symbol_test/llgo.expect index 4a329943..6ca2d4d7 100644 --- a/chore/_xtool/llcppsymg/_cmptest/symbol_test/llgo.expect +++ b/chore/_xtool/llcppsymg/_cmptest/symbol_test/llgo.expect @@ -1,4 +1,21 @@ #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 + === Test GetCommonSymbols === Test Case: Lua symbols diff --git a/chore/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go b/chore/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go index 81bbe460..272e3224 100644 --- a/chore/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go +++ b/chore/_xtool/llcppsymg/_cmptest/symbol_test/symbol.go @@ -12,10 +12,50 @@ import ( ) func main() { + TestGenDylibPaths() TestGetCommonSymbols() TestReadExistingSymbolTable() TestGenSymbolTableData() } +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 := symbol.GenDylibPaths(tc.input) + + if err != nil { + fmt.Printf("Error: %v\n", err) + } else { + fmt.Printf("Output: %v\n", result) + } + fmt.Println() + } +} func TestGetCommonSymbols() { fmt.Println("=== Test GetCommonSymbols ===") testCases := []struct { @@ -67,7 +107,6 @@ func TestGetCommonSymbols() { } fmt.Println() } - func TestReadExistingSymbolTable() { fmt.Println("=== Test ReadExistingSymbolTable ===") diff --git a/chore/_xtool/llcppsymg/clangutils/clangutils.go b/chore/_xtool/llcppsymg/clangutils/clangutils.go index 79770006..e5821b9d 100644 --- a/chore/_xtool/llcppsymg/clangutils/clangutils.go +++ b/chore/_xtool/llcppsymg/clangutils/clangutils.go @@ -75,6 +75,11 @@ func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit return index, unit, nil } +func GetLocation(loc clang.SourceLocation) (file clang.File, line c.Uint, column c.Uint, offset c.Uint) { + loc.SpellingLocation(&file, &line, &column, &offset) + return +} + // Traverse up the semantic parents func BuildScopingParts(cursor clang.Cursor) []string { var parts []string diff --git a/chore/_xtool/llcppsymg/dylib/dylib.go b/chore/_xtool/llcppsymg/dylib/dylib.go deleted file mode 100644 index 41752e4f..00000000 --- a/chore/_xtool/llcppsymg/dylib/dylib.go +++ /dev/null @@ -1,77 +0,0 @@ -package dylib - -import ( - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/goplus/llgo/xtool/nm" -) - -// ParseDylibSymbols parses symbols from dynamic libraries specified in the lib string. -// It handles multiple libraries (e.g., -L/opt/homebrew/lib -llua -lm) and returns -// symbols if at least one library is successfully parsed. Errors from inaccessible -// libraries (like standard libs) are logged as warnings. -// -// Returns symbols and nil error if any symbols are found, or nil and error if none found. -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 { - fmt.Printf("Warning: failed to generate some dylib paths: %v\n", err) - } - - var symbols []*nm.Symbol - var parseErrors []string - - for _, dylibPath := range dylibPaths { - if _, err := os.Stat(dylibPath); err != nil { - fmt.Printf("Warning: Failed to access dylib %s: %v\n", dylibPath, err) - continue - } - - files, err := nm.New("").List(dylibPath) - if err != nil { - parseErrors = append(parseErrors, fmt.Sprintf("Failed to list symbols in dylib %s: %v", dylibPath, err)) - continue - } - - for _, file := range files { - symbols = append(symbols, file.Symbols...) - } - } - - if len(symbols) > 0 { - if len(parseErrors) > 0 { - fmt.Printf("Warning: Some libraries could not be parsed: %v\n", parseErrors) - } - return symbols, nil - } - - return nil, fmt.Errorf("no symbols found in any dylib. Errors: %v", parseErrors) -} - -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/header/header.go b/chore/_xtool/llcppsymg/header/header.go deleted file mode 100644 index 9390ea2c..00000000 --- a/chore/_xtool/llcppsymg/header/header.go +++ /dev/null @@ -1,42 +0,0 @@ -package header - -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 -} diff --git a/chore/_xtool/llcppsymg/llcppsymg.go b/chore/_xtool/llcppsymg/llcppsymg.go index 272efd7a..98fb7617 100644 --- a/chore/_xtool/llcppsymg/llcppsymg.go +++ b/chore/_xtool/llcppsymg/llcppsymg.go @@ -22,8 +22,6 @@ import ( "os" "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/_xtool/llcppsymg/symbol" ) @@ -50,10 +48,10 @@ func main() { if err != nil { fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile) } - symbols, err := dylib.ParseDylibSymbols(conf.Libs) + symbols, err := symbol.ParseDylibSymbols(conf.Libs) check(err) - filepaths, err := header.GenHeaderFilePath(conf.CFlags, conf.Include) + filepaths, err := parse.GenHeaderFilePath(conf.CFlags, conf.Include) check(err) headerInfos, err := parse.ParseHeaderFile(filepaths, conf.TrimPrefixes, conf.Cplusplus, false) check(err) diff --git a/chore/_xtool/llcppsymg/parse/parse.go b/chore/_xtool/llcppsymg/parse/parse.go index 292a8ff7..d7d34a01 100644 --- a/chore/_xtool/llcppsymg/parse/parse.go +++ b/chore/_xtool/llcppsymg/parse/parse.go @@ -2,6 +2,9 @@ package parse import ( "errors" + "fmt" + "os" + "path/filepath" "strconv" "strings" @@ -145,8 +148,7 @@ func (p *SymbolProcessor) visitTop(cursor, parent clang.Cursor) clang.ChildVisit clangutils.VisitChildren(cursor, p.visitTop) case clang.CursorCXXMethod, clang.CursorFunctionDecl, clang.CursorConstructor, clang.CursorDestructor: loc := cursor.Location() - var file clang.File - loc.SpellingLocation(&file, nil, nil, nil) + file, _, _, _ := clangutils.GetLocation(loc) filename := file.FileName() defer filename.Dispose() @@ -185,3 +187,37 @@ func ParseHeaderFile(files []string, Prefixes []string, isCpp bool, isTemp bool) index.Dispose() return processer.SymbolMap, nil } + +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 +} diff --git a/chore/_xtool/llcppsymg/symbol/symbol.go b/chore/_xtool/llcppsymg/symbol/symbol.go index 60ab5c6d..c043c8d8 100644 --- a/chore/_xtool/llcppsymg/symbol/symbol.go +++ b/chore/_xtool/llcppsymg/symbol/symbol.go @@ -2,7 +2,9 @@ package symbol import ( "errors" + "fmt" "os" + "path/filepath" "strings" "unsafe" @@ -14,6 +16,73 @@ import ( "github.com/goplus/llgo/xtool/nm" ) +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 +} + +// ParseDylibSymbols parses symbols from dynamic libraries specified in the lib string. +// It handles multiple libraries (e.g., -L/opt/homebrew/lib -llua -lm) and returns +// symbols if at least one library is successfully parsed. Errors from inaccessible +// libraries (like standard libs) are logged as warnings. +// +// Returns symbols and nil error if any symbols are found, or nil and error if none found. +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 { + fmt.Printf("Warning: failed to generate some dylib paths: %v\n", err) + } + + var symbols []*nm.Symbol + var parseErrors []string + + for _, dylibPath := range dylibPaths { + if _, err := os.Stat(dylibPath); err != nil { + fmt.Printf("Warning: Failed to access dylib %s: %v\n", dylibPath, err) + continue + } + + files, err := nm.New("").List(dylibPath) + if err != nil { + parseErrors = append(parseErrors, fmt.Sprintf("Failed to list symbols in dylib %s: %v", dylibPath, err)) + continue + } + + for _, file := range files { + symbols = append(symbols, file.Symbols...) + } + } + + if len(symbols) > 0 { + if len(parseErrors) > 0 { + fmt.Printf("Warning: Some libraries could not be parsed: %v\n", parseErrors) + } + return symbols, nil + } + + return nil, fmt.Errorf("no symbols found in any dylib. Errors: %v", parseErrors) +} + // 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 {