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
|
||||||
23
chore/_xtool/llcppsymg/header/header.go
Normal file
23
chore/_xtool/llcppsymg/header/header.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
104
chore/_xtool/llcppsymg/header/header_test/header.go
Normal file
104
chore/_xtool/llcppsymg/header/header_test/header.go
Normal file
@@ -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()
|
||||||
|
}
|
||||||
23
chore/_xtool/llcppsymg/header/header_test/llgo.expect
Normal file
23
chore/_xtool/llcppsymg/header/header_test/llgo.expect
Normal file
@@ -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
|
||||||
@@ -21,13 +21,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c"
|
||||||
"github.com/goplus/llgo/c/cjson"
|
"github.com/goplus/llgo/c/cjson"
|
||||||
"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/llcppg/types"
|
"github.com/goplus/llgo/chore/llcppg/types"
|
||||||
"github.com/goplus/llgo/xtool/nm"
|
"github.com/goplus/llgo/xtool/nm"
|
||||||
@@ -54,11 +55,11 @@ 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 := parseDylibSymbols(conf.Libs)
|
symbols, err := dylib.ParseDylibSymbols(conf.Libs)
|
||||||
|
|
||||||
check(err)
|
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)
|
headerInfos, err := parse.ParseHeaderFile(filepaths, conf.TrimPrefixes, conf.Cplusplus, false)
|
||||||
check(err)
|
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 {
|
func getCommonSymbols(dylibSymbols []*nm.Symbol, symbolMap map[string]*parse.SymbolInfo, prefix []string) []*types.SymbolInfo {
|
||||||
var commonSymbols []*types.SymbolInfo
|
var commonSymbols []*types.SymbolInfo
|
||||||
for _, dylibSym := range dylibSymbols {
|
for _, dylibSym := range dylibSymbols {
|
||||||
|
|||||||
Reference in New Issue
Block a user