Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions _cmptest/llcppgend_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package _cmptest

import (
"encoding/json"
"fmt"
"io"
"maps"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -114,7 +116,7 @@ var mkdirTempLazily = sync.OnceValue(func() string {
return dir
})

func logFile(tc testCase) (*os.File, error) {
func logFile(tc testCase, isStatic bool) (*os.File, error) {
caseName := fmt.Sprintf("%s-%s-llcppg-%s-%s", runtime.GOOS, runtime.GOARCH, tc.pkg.Name, tc.pkg.Version)
dirPath := filepath.Join(mkdirTempLazily(), caseName)

Expand All @@ -123,6 +125,9 @@ func logFile(tc testCase) (*os.File, error) {
return nil, err
}

if isStatic {
return os.Create(filepath.Join(dirPath, fmt.Sprintf("%s-static.log", caseName)))
}
return os.Create(filepath.Join(dirPath, fmt.Sprintf("%s.log", caseName)))
}

Expand All @@ -131,13 +136,23 @@ func TestEnd2End(t *testing.T) {
tc := tc
t.Run(fmt.Sprintf("%s/%s", tc.pkg.Name, tc.pkg.Version), func(t *testing.T) {
t.Parallel()
testFrom(t, tc, false)
testFrom(t, tc, false, false)
})
t.Run(fmt.Sprintf("%s/%s-static", tc.pkg.Name, tc.pkg.Version), func(t *testing.T) {
t.Parallel()
testFrom(t, tc, true, false)
})
}
}

func testFrom(t *testing.T, tc testCase, gen bool) {
logFile, err := logFile(tc)
func testFrom(t *testing.T, tc testCase, isStatic bool, gen bool) {
null, err := os.OpenFile(os.DevNull, os.O_RDWR, 0644)
if err != nil {
t.Fatal(err)
}
defer null.Close()

logFile, err := logFile(tc, isStatic)
if err != nil {
t.Fatal(err)
}
Expand All @@ -160,10 +175,42 @@ func testFrom(t *testing.T, tc testCase, gen bool) {

cfgPath := filepath.Join(wd, tc.dir, tc.pkg.Name, config.LLCPPG_CFG)
processCfgPath := filepath.Join(resultDir, config.LLCPPG_CFG)
copyFile(cfgPath, processCfgPath)

// when isStatic is true, replace the staticLib=False to staticLib=True
if !isStatic {
copyFile(cfgPath, processCfgPath)
} else {
cfgContent, err := os.ReadFile(cfgPath)
if err != nil {
t.Fatal(err)
}
var cfg map[string]interface{}
err = json.Unmarshal(cfgContent, &cfg)
if err != nil {
t.Fatal(err)
}
cfg["staticLib"] = true
cfgContent, err = json.Marshal(cfg)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(processCfgPath, cfgContent, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}

if tc.config == nil {
tc.config = make(map[string]string)
}

conanOpt := maps.Clone(tc.config)
if isStatic {
conanOpt["options"] = "*:shared=False " + conanOpt["options"]
}

conanInstallMutex.Lock()
_, err = conan.NewConanInstaller(tc.config).Install(tc.pkg, conanDir)
_, err = conan.NewConanInstaller(conanOpt).Install(tc.pkg, conanDir)
conanInstallMutex.Unlock()
if err != nil {
t.Fatal(err)
Expand All @@ -180,7 +227,7 @@ func testFrom(t *testing.T, tc testCase, gen bool) {

// llcppg.symb.json is a middle file
os.Remove(filepath.Join(resultDir, config.LLCPPG_SYMB))
copyFile(processCfgPath, filepath.Join(resultDir, tc.pkg.Name, config.LLCPPG_CFG))
copyFile(cfgPath, filepath.Join(resultDir, tc.pkg.Name, config.LLCPPG_CFG))

if gen {
os.RemoveAll(dir)
Expand Down
4 changes: 2 additions & 2 deletions _xtool/llcppsymg/internal/symg/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func ParseLibs(libs string) *Libs {
type LibMode = symbol.Mode

// searches for each library name in the provided paths and default paths,
// appending the appropriate file extension (.dylib for macOS, .so for Linux).
// appending the appropriate file extension (.dylib for macOS, .so for Linux at dylib mode, .a for static mode).
//
// Example: For "-L/opt/homebrew/lib -llua -lm":
// Example: For "-L/opt/homebrew/lib -llua -lm" and at dylib mode:
// - It will search for liblua.dylib (on macOS) or liblua.so (on Linux)
// - System libs like -lm are ignored and included in notFound
//
Expand Down
2 changes: 1 addition & 1 deletion _xtool/llcppsymg/internal/symg/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestGenDylibPaths(t *testing.T) {
}

if err != nil {
t.Fatalf("expected no error, got %w", err)
t.Fatalf("expected no error, got %v", err)
}

if !reflect.DeepEqual(notFounds, tc.wantNotFound) {
Expand Down
11 changes: 6 additions & 5 deletions _xtool/llcppsymg/internal/symg/symg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/goplus/llcppg/_xtool/internal/clangtool"
"github.com/goplus/llcppg/_xtool/internal/header"
"github.com/goplus/llcppg/_xtool/internal/ld"
"github.com/goplus/llcppg/_xtool/internal/symbol"
llcppg "github.com/goplus/llcppg/config"
"github.com/goplus/llgo/xtool/nm"
)
Expand Down Expand Up @@ -37,11 +38,11 @@ type Config struct {
TrimPrefixes []string
SymMap map[string]string
IsCpp bool
libMode LibMode
LibMode LibMode
}

func Do(conf *Config) (symbolTable []*llcppg.SymbolInfo, err error) {
symbols, err := FetchSymbols(conf.Libs, conf.libMode)
symbols, err := FetchSymbols(conf.Libs, conf.LibMode)
if err != nil {
return
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func FetchSymbols(lib string, mode LibMode) ([]*nm.Symbol, error) {

libFiles, notFounds, err := lbs.Files(sysPaths, mode)
if err != nil {
return nil, fmt.Errorf("failed to generate some dylib paths: %v", err)
return nil, fmt.Errorf("failed to generate some lib paths: %v", err)
}

if dbgSymbol {
Expand All @@ -117,13 +118,13 @@ func FetchSymbols(lib string, mode LibMode) ([]*nm.Symbol, error) {

for _, libFile := range libFiles {
args := []string{"-g"}
if runtime.GOOS == "linux" {
if runtime.GOOS == "linux" && mode == symbol.ModeDynamic {
args = append(args, "-D")
}

files, err := nm.New("llvm-nm").List(libFile, args...)
if err != nil {
parseErrors = append(parseErrors, fmt.Sprintf("fetchSymbols:Failed to list symbols in dylib %s: %v", libFile, err))
parseErrors = append(parseErrors, fmt.Sprintf("fetchSymbols:Failed to list symbols in lib %s: %v", libFile, err))
continue
}

Expand Down
Loading
Loading