llcppsymg:gendylib path test
llcppsymg:headerpath test
This commit is contained in:
66
chore/_xtool/llcppsymg/dylib/dylib.go
Normal file
66
chore/_xtool/llcppsymg/dylib/dylib.go
Normal file
@@ -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
|
||||
}
|
||||
51
chore/_xtool/llcppsymg/dylib/dylib_test/dylib.go
Normal file
51
chore/_xtool/llcppsymg/dylib/dylib_test/dylib.go
Normal file
@@ -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()
|
||||
}
|
||||
22
chore/_xtool/llcppsymg/dylib/dylib_test/llgo.expect
Normal file
22
chore/_xtool/llcppsymg/dylib/dylib_test/llgo.expect
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user