Skip to content

Commit

Permalink
gogensig:skip sys header
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Nov 12, 2024
1 parent e3d4fc1 commit 7e04d17
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 69 deletions.
23 changes: 21 additions & 2 deletions chore/gogensig/cmptest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import (
cppgtypes "github.com/goplus/llgo/chore/llcppg/types"
)

func GetTempHeaderDir() string {
tempHeaderDir, err := filepath.Abs("./temp")
if err != nil {
panic(err)
}
return tempHeaderDir
}

// The validateFunc is used to validate the generated file,
func RunTest(t *testing.T, pkgName string, isCpp bool, symbolEntries []config.SymbolEntry, public map[string]string, cppgConf *cppgtypes.Config, originalCode, expectedOutput string, validateFunc func(t *testing.T, pkg *convert.Package)) {
RunTestWithCheckEqual(t, pkgName, isCpp, symbolEntries, public, cppgConf, originalCode, expectedOutput, validateFunc, CheckResult)
Expand All @@ -30,6 +38,18 @@ func CheckResult(t *testing.T, expected, content string) {
func RunTestWithCheckEqual(t *testing.T, pkgName string, isCpp bool, symbolEntries []config.SymbolEntry, public map[string]string, cppgConf *cppgtypes.Config, originalCode, expectedOutput string, validateFunc func(t *testing.T, pkg *convert.Package), checkEqual func(t *testing.T, expected, content string)) {
t.Helper()

tempHeaderDir := GetTempHeaderDir()
err := os.MkdirAll(tempHeaderDir, 0744)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempHeaderDir)
tempFile := filepath.Join(tempHeaderDir, "temp.h")
absTempFile, err := filepath.Abs(tempFile)
if err != nil {
t.Fatal(err)
}
os.WriteFile(absTempFile, []byte(originalCode), 0644)
tempDir, err := os.MkdirTemp("", "gogensig-test")
if err != nil {
t.Fatal("failed to create temp dir")
Expand Down Expand Up @@ -98,13 +118,12 @@ func RunTestWithCheckEqual(t *testing.T, pkgName string, isCpp bool, symbolEntri

// Fetch the signature file
// The signature file is generated by llcppsigfetch
bytes, err := config.Sigfetch(originalCode, true, isCpp)
bytes, err := config.Sigfetch(absTempFile, false, isCpp)
if err != nil {
t.Fatal(err)
}

inputdata, err := unmarshal.UnmarshalFileSet(bytes)
inputdata[0].IncPath = "temp.h"
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion chore/gogensig/convert/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ConvertProcesser(cfg *Config) (*processor.DocFileSetProcessor, *convert.Pac

return processor.NewDocFileSetProcessor(&processor.ProcesserConfig{
Exec: func(file *unmarshal.FileEntry) error {
visitorManager.Visit(file.Doc, file.IncPath)
visitorManager.Visit(file.Doc, file.Path)
return nil
},
DepIncs: astConvert.Pkg.AllDepIncs(),
Expand Down
2 changes: 1 addition & 1 deletion chore/gogensig/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (p *AstConvert) VisitTypedefDecl(typedefDecl *ast.TypedefDecl) {

func (p *AstConvert) VisitStart(incPath string) {
inPkgIncPath := false
for _, includePath := range p.Pkg.conf.CppgConf.Include {
for _, includePath := range p.Pkg.incPaths {
if includePath == incPath {
inPkgIncPath = true
break
Expand Down
3 changes: 3 additions & 0 deletions chore/gogensig/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ func CustomExecuteFoo(a c.Int, b Foo) c.Int
// Test if function names and type names can remove specified prefixes,
// generate correct linkname, and use function names defined in llcppg.symb.json
func TestCustomStruct(t *testing.T) {
// 获得当前目录的绝对路径
tempDir := cmptest.GetTempHeaderDir()
cmptest.RunTest(t, "typeref", false, []config.SymbolEntry{
{MangleName: "lua_close", CppName: "lua_close", GoName: "Close"},
{MangleName: "lua_newthread", CppName: "lua_newthread", GoName: "Newthread"},
{MangleName: "lua_closethread", CppName: "lua_closethread", GoName: "Closethread"},
{MangleName: "lua_resetthread", CppName: "lua_resetthread", GoName: "Resetthread"},
}, map[string]string{}, &cppgtypes.Config{
CFlags: "-I" + tempDir,
TrimPrefixes: []string{"lua_"},
Include: []string{"temp.h"},
// prefix only remove in the llcppg.cfg includes
Expand Down
15 changes: 11 additions & 4 deletions chore/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/goplus/gogen"
"github.com/goplus/llgo/chore/_xtool/llcppsymg/config/cfgparse"
cfg "github.com/goplus/llgo/chore/gogensig/config"
"github.com/goplus/llgo/chore/gogensig/convert/deps"
"github.com/goplus/llgo/chore/llcppg/ast"
Expand Down Expand Up @@ -43,6 +44,7 @@ type Package struct {
depIncs []string
inCurPkg bool // whether the current file is in the llcppg package not the dependent files
public map[string]string // original C name -> public Go name
incPaths []string //current pkg's include file
}

type PackageConfig struct {
Expand All @@ -68,6 +70,12 @@ func NewPackage(config *PackageConfig) *Package {
p.outputDir = config.OutputDir
p.public = config.Public

cflags := cfgparse.ParseCFlags(config.CppgConf.CFlags)
incPaths, _, err := cflags.GenHeaderFilePaths(config.CppgConf.Include)
if err != nil {
log.Println("failed to gen include paths: \n", err.Error())
}
p.incPaths = incPaths
// init deps
deps, err := deps.LoadDeps(p.outputDir, config.CppgConf.Deps)
if err != nil {
Expand All @@ -89,6 +97,7 @@ func NewPackage(config *PackageConfig) *Package {
return p
}

// absolute path
func (p *Package) SetCurFile(file string, isHeaderFile bool, inCurPkg bool) error {
var fileName string
if isHeaderFile {
Expand Down Expand Up @@ -360,11 +369,9 @@ func (p *Package) WriteToBuffer(genFName string) (*bytes.Buffer, error) {
}

// /path/to/foo.h -> foo.go
// /path/to/_intptr.h -> SYS_intptr.go
// for std include header file path
// /path/to/_intptr.h -> X_intptr.go
func HeaderFileToGo(incPath string) string {
// _, fileName := filepath.Split(headerFile)
fileName := strings.ReplaceAll(incPath, string(filepath.Separator), "_")
_, fileName := filepath.Split(incPath)
ext := filepath.Ext(fileName)
if len(ext) > 0 {
fileName = strings.TrimSuffix(fileName, ext)
Expand Down
32 changes: 17 additions & 15 deletions chore/gogensig/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ProcesserConfig struct {
}

func defaultExec(file *unmarshal.FileEntry) error {
fmt.Println(file.IncPath)
fmt.Println(file.Path)
return nil
}

Expand All @@ -64,31 +64,33 @@ func NewDocFileSetProcessor(cfg *ProcesserConfig) *DocFileSetProcessor {
return p
}

func (p *DocFileSetProcessor) visitFile(incPath string, files unmarshal.FileSet) {
if _, ok := p.visitedFile[incPath]; ok {
func (p *DocFileSetProcessor) visitFile(path string, files unmarshal.FileSet) {
if _, ok := p.visitedFile[path]; ok {
return
}
if _, ok := p.processing[incPath]; ok {
if _, ok := p.processing[path]; ok {
return
}
p.processing[incPath] = struct{}{}
idx := FindEntry(files, incPath)
p.processing[path] = struct{}{}
idx := FindEntry(files, path)
if idx < 0 {
return
}
findFile := files[idx]
for _, include := range findFile.Doc.Includes {
p.visitFile(include.Path, files)
if !findFile.IsSys {
for _, include := range findFile.Doc.Includes {
p.visitFile(include.Path, files)
}
p.exec(&findFile)
return
}

p.exec(&findFile)
p.visitedFile[findFile.IncPath] = struct{}{}
delete(p.processing, findFile.IncPath)
p.visitedFile[findFile.Path] = struct{}{}
delete(p.processing, findFile.Path)
}

func (p *DocFileSetProcessor) ProcessFileSet(files unmarshal.FileSet) error {
for _, file := range files {
p.visitFile(file.IncPath, files)
p.visitFile(file.Path, files)
}
if p.done != nil {
p.done()
Expand All @@ -112,9 +114,9 @@ func (p *DocFileSetProcessor) ProcessFileSetFromPath(filePath string) error {
return p.ProcessFileSetFromByte(data)
}

func FindEntry(files unmarshal.FileSet, incPath string) int {
func FindEntry(files unmarshal.FileSet, path string) int {
for i, e := range files {
if e.IncPath == incPath {
if e.Path == path {
return i
}
}
Expand Down
74 changes: 37 additions & 37 deletions chore/gogensig/processor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestProcessFileNotExist(t *testing.T) {
manager := processor.NewDocVisitorManager(docVisitors)
p := processor.NewDocFileSetProcessor(&processor.ProcesserConfig{
Exec: func(file *unmarshal.FileEntry) error {
manager.Visit(file.Doc, file.IncPath)
manager.Visit(file.Doc, file.Path)
return nil
},
DepIncs: []string{},
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestProcessInvalidSigfetchContent(t *testing.T) {
manager := processor.NewDocVisitorManager(docVisitors)
p := processor.NewDocFileSetProcessor(&processor.ProcesserConfig{
Exec: func(file *unmarshal.FileEntry) error {
manager.Visit(file.Doc, file.IncPath)
manager.Visit(file.Doc, file.Path)
return nil
},
DepIncs: []string{},
Expand All @@ -139,97 +139,97 @@ func TestProcessInvalidSigfetchContent(t *testing.T) {
func TestDefaultExec(t *testing.T) {
file := unmarshal.FileSet{
{
Path: "foo.h",
IncPath: "foo.h",
Doc: &ast.File{},
Path: "foo.h",
IsSys: false,
Doc: &ast.File{},
},
}
p := processor.NewDocFileSetProcessor(&processor.ProcesserConfig{})
p.ProcessFileSet(file)
}

func TestExecOrder(t *testing.T) {
depIncs := []string{"int16_t.h"}
depIncs := []string{"/path/to/int16_t.h"}
fileSet := unmarshal.FileSet{
{
Path: "/path/to/foo.h",
IncPath: "foo.h",
Path: "/path/to/foo.h",
IsSys: false,
Doc: &ast.File{
Includes: []*ast.Include{
{Path: "cdef.h"},
{Path: "stdint.h"},
{Path: "/path/to/cdef.h"},
{Path: "/path/to/stdint.h"},
},
},
},
{
Path: "/path/to/cdef.h",
IncPath: "cdef.h",
Path: "/path/to/cdef.h",
IsSys: false,
Doc: &ast.File{
Includes: []*ast.Include{
{Path: "int8_t.h"},
{Path: "int16_t.h"},
{Path: "/path/to/int8_t.h"},
{Path: "/path/to/int16_t.h"},
},
},
},
{
Path: "/path/to/stdint.h",
IncPath: "stdint.h",
Path: "/path/to/stdint.h",
IsSys: false,
Doc: &ast.File{
Includes: []*ast.Include{
{Path: "int8_t.h"},
{Path: "int16_t.h"},
{Path: "/path/to/int8_t.h"},
{Path: "/path/to/int16_t.h"},
},
},
},
{
Path: "/path/to/int8_t.h",
IncPath: "int8_t.h",
Path: "/path/to/int8_t.h",
IsSys: false,
Doc: &ast.File{
Includes: []*ast.Include{},
},
},
{
Path: "/path/to/int16_t.h",
IncPath: "int16_t.h",
Path: "/path/to/int16_t.h",
IsSys: false,
Doc: &ast.File{
Includes: []*ast.Include{},
},
},
{
Path: "/path/to/bar.h",
IncPath: "bar.h",
Path: "/path/to/bar.h",
IsSys: false,
Doc: &ast.File{
Includes: []*ast.Include{
{Path: "stdint.h"},
{Path: "a.h"},
{Path: "/path/to/stdint.h"},
{Path: "/path/to/a.h"},
},
},
},
// circular dependency
{
Path: "/path/to/a.h",
IncPath: "a.h",
Path: "/path/to/a.h",
IsSys: false,
Doc: &ast.File{
Includes: []*ast.Include{
{Path: "bar.h"},
{Path: "/path/to/bar.h"},
// will not appear in normal
{Path: "noexist.h"},
{Path: "/path/to/noexist.h"},
},
},
},
}
var processFiles []string
expectedOrder := []string{
"int8_t.h",
"cdef.h",
"stdint.h",
"foo.h",
"a.h",
"bar.h",
"/path/to/int8_t.h",
"/path/to/cdef.h",
"/path/to/stdint.h",
"/path/to/foo.h",
"/path/to/a.h",
"/path/to/bar.h",
}
p := processor.NewDocFileSetProcessor(&processor.ProcesserConfig{
Exec: func(file *unmarshal.FileEntry) error {
processFiles = append(processFiles, file.IncPath)
processFiles = append(processFiles, file.Path)
return nil
},
DepIncs: depIncs,
Expand Down
18 changes: 9 additions & 9 deletions chore/gogensig/unmarshal/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var nodeUnmarshalers map[string]NodeUnmarshaler
type FileSet []FileEntry

type FileEntry struct {
Path string
IncPath string
Doc *ast.File
Path string
IsSys bool
Doc *ast.File
}

func init() {
Expand Down Expand Up @@ -55,9 +55,9 @@ func init() {

func UnmarshalFileSet(data []byte) (FileSet, error) {
var filesWrapper []struct {
Path string `json:"path"`
IncPath string `json:"incPath"`
Doc json.RawMessage `json:"doc"`
Path string `json:"path"`
IsSys bool `json:"isSys"`
Doc json.RawMessage `json:"doc"`
}
if err := json.Unmarshal(data, &filesWrapper); err != nil {
return nil, fmt.Errorf("error unmarshalling FilesWithPath: %w", err)
Expand All @@ -77,9 +77,9 @@ func UnmarshalFileSet(data []byte) (FileSet, error) {
}

result = append(result, FileEntry{
Path: fileData.Path,
IncPath: fileData.IncPath,
Doc: file,
Path: fileData.Path,
IsSys: fileData.IsSys,
Doc: file,
})
}

Expand Down

0 comments on commit 7e04d17

Please sign in to comment.