chore:remove folder
This commit is contained in:
@@ -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()
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
@@ -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()
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
@@ -1,4 +1,22 @@
|
|||||||
#stdout
|
#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 ===
|
=== Test NewSymbolProcessor ===
|
||||||
Before: No prefixes After: Prefixes: [lua_ luaL_]
|
Before: No prefixes After: Prefixes: [lua_ luaL_]
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/goplus/llgo/chore/_xtool/llcppsymg/parse"
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/parse"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
TestGenHeaderFilePath()
|
||||||
TestNewSymbolProcessor()
|
TestNewSymbolProcessor()
|
||||||
TestRemovePrefix()
|
TestRemovePrefix()
|
||||||
TestToGoName()
|
TestToGoName()
|
||||||
@@ -16,6 +19,64 @@ func main() {
|
|||||||
TestParseHeaderFile()
|
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() {
|
func TestNewSymbolProcessor() {
|
||||||
fmt.Println("=== Test NewSymbolProcessor ===")
|
fmt.Println("=== Test NewSymbolProcessor ===")
|
||||||
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
|
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
|
||||||
|
|||||||
@@ -1,4 +1,21 @@
|
|||||||
#stdout
|
#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 GetCommonSymbols ===
|
||||||
|
|
||||||
Test Case: Lua symbols
|
Test Case: Lua symbols
|
||||||
|
|||||||
@@ -12,10 +12,50 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
TestGenDylibPaths()
|
||||||
TestGetCommonSymbols()
|
TestGetCommonSymbols()
|
||||||
TestReadExistingSymbolTable()
|
TestReadExistingSymbolTable()
|
||||||
TestGenSymbolTableData()
|
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() {
|
func TestGetCommonSymbols() {
|
||||||
fmt.Println("=== Test GetCommonSymbols ===")
|
fmt.Println("=== Test GetCommonSymbols ===")
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
@@ -67,7 +107,6 @@ func TestGetCommonSymbols() {
|
|||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadExistingSymbolTable() {
|
func TestReadExistingSymbolTable() {
|
||||||
fmt.Println("=== Test ReadExistingSymbolTable ===")
|
fmt.Println("=== Test ReadExistingSymbolTable ===")
|
||||||
|
|
||||||
|
|||||||
@@ -75,6 +75,11 @@ func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit
|
|||||||
return index, unit, nil
|
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
|
// Traverse up the semantic parents
|
||||||
func BuildScopingParts(cursor clang.Cursor) []string {
|
func BuildScopingParts(cursor clang.Cursor) []string {
|
||||||
var parts []string
|
var parts []string
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -22,8 +22,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/goplus/llgo/chore/_xtool/llcppsymg/config"
|
"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/parse"
|
||||||
"github.com/goplus/llgo/chore/_xtool/llcppsymg/symbol"
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/symbol"
|
||||||
)
|
)
|
||||||
@@ -50,10 +48,10 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile)
|
fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile)
|
||||||
}
|
}
|
||||||
symbols, err := dylib.ParseDylibSymbols(conf.Libs)
|
symbols, err := symbol.ParseDylibSymbols(conf.Libs)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
filepaths, err := header.GenHeaderFilePath(conf.CFlags, conf.Include)
|
filepaths, err := parse.GenHeaderFilePath(conf.CFlags, conf.Include)
|
||||||
check(err)
|
check(err)
|
||||||
headerInfos, err := parse.ParseHeaderFile(filepaths, conf.TrimPrefixes, conf.Cplusplus, false)
|
headerInfos, err := parse.ParseHeaderFile(filepaths, conf.TrimPrefixes, conf.Cplusplus, false)
|
||||||
check(err)
|
check(err)
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package parse
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -145,8 +148,7 @@ func (p *SymbolProcessor) visitTop(cursor, parent clang.Cursor) clang.ChildVisit
|
|||||||
clangutils.VisitChildren(cursor, p.visitTop)
|
clangutils.VisitChildren(cursor, p.visitTop)
|
||||||
case clang.CursorCXXMethod, clang.CursorFunctionDecl, clang.CursorConstructor, clang.CursorDestructor:
|
case clang.CursorCXXMethod, clang.CursorFunctionDecl, clang.CursorConstructor, clang.CursorDestructor:
|
||||||
loc := cursor.Location()
|
loc := cursor.Location()
|
||||||
var file clang.File
|
file, _, _, _ := clangutils.GetLocation(loc)
|
||||||
loc.SpellingLocation(&file, nil, nil, nil)
|
|
||||||
filename := file.FileName()
|
filename := file.FileName()
|
||||||
defer filename.Dispose()
|
defer filename.Dispose()
|
||||||
|
|
||||||
@@ -185,3 +187,37 @@ func ParseHeaderFile(files []string, Prefixes []string, isCpp bool, isTemp bool)
|
|||||||
index.Dispose()
|
index.Dispose()
|
||||||
return processer.SymbolMap, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package symbol
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
@@ -14,6 +16,73 @@ import (
|
|||||||
"github.com/goplus/llgo/xtool/nm"
|
"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.
|
// 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.
|
// It returns a list of symbols that can be externally linked.
|
||||||
func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse.SymbolInfo) []*types.SymbolInfo {
|
func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse.SymbolInfo) []*types.SymbolInfo {
|
||||||
|
|||||||
Reference in New Issue
Block a user