Skip to content

Commit

Permalink
Merge pull request #113 from tsingbx/llgolink
Browse files Browse the repository at this point in the history
write LLGoPackage to link file
  • Loading branch information
luoliwoshang authored Oct 24, 2024
2 parents 6bb3d81 + 4ce67e8 commit a96ecd5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion chore/gogensig/cmptest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func RunTest(t *testing.T, pkgName string, isCpp bool, symbolEntries []config.Sy
t.Fatal(err)
}

p.ProcessFileSet(inputdata)
p.ProcessFileSet(inputdata, nil)

// Note: The converted file path for llcppsigfetch's temp header file is temp.h,
genFilePath := filepath.Join(rootDir, "temp.go")
Expand Down
4 changes: 4 additions & 0 deletions chore/gogensig/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (p *AstConvert) SetVisitDone(fn func(pkg *Package, docPath string)) {
p.visitDone = fn
}

func (p *AstConvert) WriteLinkFile() {
p.pkg.WriteLinkFile()
}

func (p *AstConvert) GetPackage() *Package {
return p.pkg
}
Expand Down
25 changes: 15 additions & 10 deletions chore/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,11 @@ func NewPackage(config *PackageConfig) *Package {
panic(fmt.Errorf("failed to prepare output directory: %w", err))
}
p.outputDir = dir

// default file name is the package name
err = p.SetCurFile(config.Name, false)
if err != nil {
panic(fmt.Errorf("SetDefaultFile %s for gogen Fail %w", config.Name+".go", err))
}

clib := p.p.Import("github.com/goplus/llgo/c")
typeMap := NewBuiltinTypeMapWithPkgRefS(clib, p.p.Unsafe())
p.cvt = NewConv(p.p.Types, typeMap)
p.newTypes = make(map[string]struct{}, 0)
p.SetCurFile(p.Name(), false)
return p
}

Expand Down Expand Up @@ -101,9 +95,6 @@ func (p *Package) SetCppgConf(conf *cppgtypes.Config) {
return
}
p.cvt.SetCppgConf(conf)
if conf.Libs != "" {
p.linkLib(conf.Libs)
}
}

func (p *Package) GetGenPackage() *gogen.Package {
Expand All @@ -124,6 +115,9 @@ func (p *Package) Name() string {

// todo(zzy):refine logic
func (p *Package) linkLib(lib string) error {
if lib == "" {
return fmt.Errorf("empty lib name")
}
linkString := fmt.Sprintf("link: %s;", lib)
p.p.CB().NewConstStart(types.Typ[types.String], "LLGoPackage").Val(linkString).EndInit(1)
return nil
Expand Down Expand Up @@ -262,6 +256,17 @@ func (p *Package) Write(headerFile string) error {
return nil
}

func (p *Package) WriteLinkFile() (string, error) {
fileName := p.name + "_autogen_link.go"
filePath := filepath.Join(p.outputDir, fileName)
p.p.SetCurFile(fileName, true)
p.linkLib(p.cvt.cppgConf.Libs)
if err := p.p.WriteFile(filePath, fileName); err != nil {
return "", fmt.Errorf("failed to write file: %w", err)
}
return filePath, nil
}

// WriteToBuffer writes the Go file to a buffer.
// Include the aliased file for debug
func (p *Package) WriteToBuffer(headerFile string) (*bytes.Buffer, error) {
Expand Down
31 changes: 30 additions & 1 deletion chore/gogensig/convert/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@ func init() {
convert.SetDebug(convert.DbgFlagAll)
}

func TestLinkFileOK(t *testing.T) {
tempDir, err := os.MkdirTemp("", "test_package_link")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)

pkg := createTestPkg(t, tempDir)
pkg.SetCppgConf(&cppgtypes.Config{
Libs: "pkg-config --libs libcjson",
})
filePath, _ := pkg.WriteLinkFile()
_, err = os.Stat(filePath)
if os.IsNotExist(err) {
t.FailNow()
}
}

func TestLinkFileFail(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.FailNow()
} else {
t.Log("success!")
}
}()
pkg := createTestPkg(t, "")
pkg.WriteLinkFile()
}

func TestToType(t *testing.T) {
pkg := createTestPkg(t, "")

Expand Down Expand Up @@ -75,7 +105,6 @@ func TestSetCppgConf(t *testing.T) {
`
package testpkg
import _ "unsafe"
const LLGoPackage string = "link: pkg-config --libs lua5.4;"
`)
}

Expand Down
4 changes: 3 additions & 1 deletion chore/gogensig/gogensig.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func main() {
inputdata, err := unmarshal.UnmarshalFileSet(data)
check(err)

err = p.ProcessFileSet(inputdata)
err = p.ProcessFileSet(inputdata, func() {
astConvert.WriteLinkFile()
})
check(err)
}

Expand Down
7 changes: 5 additions & 2 deletions chore/gogensig/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ func (p *DocFileSetProcessor) visitFile(docVisitor *DocVisitorManager, file unma
p.visitedFile[file.Path] = struct{}{}
}

func (p *DocFileSetProcessor) ProcessFileSet(files unmarshal.FileSet) error {
func (p *DocFileSetProcessor) ProcessFileSet(files unmarshal.FileSet, done func()) error {
docVisitor := NewDocVisitorManager(p.docVisitorList)
for _, file := range files {
p.visitFile(docVisitor, file, files)
}
if done != nil {
done()
}
return nil
}

Expand All @@ -92,7 +95,7 @@ func (p *DocFileSetProcessor) ProcessFileSetFromByte(data []byte) error {
if err != nil {
return err
}
return p.ProcessFileSet(fileSet)
return p.ProcessFileSet(fileSet, nil)
}

func (p *DocFileSetProcessor) ProcessFileSetFromPath(filePath string) error {
Expand Down
4 changes: 2 additions & 2 deletions chore/gogensig/unmarshal/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,10 @@ func UnmarshalFile(data []byte) (ast.Node, error) {
Decls: []ast.Decl{},
}

for _, declData := range file.Decls {
for i, declData := range file.Decls {
decl, err := UnmarshalNode(declData)
if err != nil {
return nil, fmt.Errorf("error unmarshalling Decl in File: %w", err)
return nil, fmt.Errorf("error unmarshalling %d Decl in File: %w", i, err)
}
result.Decls = append(result.Decls, decl.(ast.Decl))
}
Expand Down
2 changes: 1 addition & 1 deletion chore/gogensig/unmarshal/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,7 @@ func TestUnmarshalErrors(t *testing.T) {
name: "unmarshalFile - Invalid Decl",
fn: unmarshal.UnmarshalFile,
input: `{"decls": [{"_Type": "InvalidType"}], "includes": [], "macros": []}`,
expectedErr: "error unmarshalling Decl in File",
expectedErr: "error unmarshalling 0 Decl in File",
},
}

Expand Down

0 comments on commit a96ecd5

Please sign in to comment.