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
5 changes: 5 additions & 0 deletions cl/internal/convert/_testdata/withoutlib/conf/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "withoutlib",
"include": ["temp.h"],
"cplusplus":false
}
20 changes: 20 additions & 0 deletions cl/internal/convert/_testdata/withoutlib/gogensig.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
===== temp.go =====
package withoutlib

import (
"github.com/goplus/lib/c"
_ "unsafe"
)

type Temp struct {
A c.Int
B c.Int
}

===== withoutlib_autogen_link.go =====
package withoutlib

import _ "github.com/goplus/lib/c"

===== llcppg.pub =====
temp Temp
4 changes: 4 additions & 0 deletions cl/internal/convert/_testdata/withoutlib/hfile/temp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
typedef struct temp {
int a;
int b;
} temp;
52 changes: 23 additions & 29 deletions cl/internal/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ type Package struct {

type PackageConfig struct {
PkgBase
Name string // current package name
OutputDir string
GenConf *gogen.Config
LibCommand string // use to gen link command like $(pkg-config --libs xxx)
Name string // current package name
OutputDir string
GenConf *gogen.Config

// use to gen link command like $(pkg-config --libs xxx)
// if have this field,will generate a const named LLGoPackage,and it's not necessary
LibCommand string
}

// When creating a new package for conversion, a Go file named after the package is generated by default.
Expand Down Expand Up @@ -92,10 +95,12 @@ func NewPackage(pnc nc.NodeConverter, config *PackageConfig) (*Package, error) {
if err != nil {
return nil, fmt.Errorf("failed to init deps: %w", err)
}
err = p.initLink()
if err != nil {
return nil, fmt.Errorf("failed to init link: %w", err)

// allow have not lib command
if p.conf.LibCommand != "" {
p.initLink(p.conf.LibCommand)
Comment thread
MeteorsLiu marked this conversation as resolved.
}

p.markUseDeps(pkgManager)
p.cvt = NewConv(p.p, p.p.Types, pnc, p.lookupType)
return p, nil
Expand All @@ -111,7 +116,7 @@ func (p *Package) Pkg() *gogen.Package {

func (p *Package) markUseDeps(pkgMgr *PkgDepLoader) {
defer p.p.RestoreCurFile(p.p.CurFile())
p.p.SetCurFile(p.autoLinkFile(), true)
p.setCurFile(p.autoLinkFile())
pkgs, err := pkgMgr.Imports(p.conf.Deps)
if err != nil {
log.Panicf("failed to import deps: %s", err.Error())
Expand All @@ -128,21 +133,10 @@ func (p *Package) LookupFunc(goName string, fn *ast.FuncDecl) (*GoFuncSpec, erro

// to keep the unsafe package load to use go:linkname command
func (p *Package) setGoFile(goFile string) {
p.p.SetCurFile(goFile, true)
// todo(zzy):avoid remark
p.setCurFile(goFile)
p.p.Unsafe().MarkForceUsed(p.p)
}

// 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
}

func (p *Package) newReceiver(typ *ast.FuncType) (*types.Var, error) {
recvField := typ.Params.List[0]
recvType, err := p.ToType(recvField.Type)
Expand Down Expand Up @@ -714,17 +708,17 @@ func (p *Package) autoLinkFile() string {
return p.conf.Name + "_autogen_link.go"
}

func (p *Package) initLink() error {
fileName := p.autoLinkFile()
_, err := p.p.SetCurFile(fileName, true)
if err != nil {
return fmt.Errorf("failed to set current file: %w", err)
}
err = p.linkLib(p.conf.LibCommand)
func (p *Package) initLink(lib string) {
p.setCurFile(p.autoLinkFile())
linkString := fmt.Sprintf("link: %s;", lib)
p.p.CB().NewConstStart(types.Typ[types.String], "LLGoPackage").Val(linkString).EndInit(1)
}

func (p *Package) setCurFile(goFile string) {
_, err := p.p.SetCurFile(goFile, true)
if err != nil {
return err
log.Panicf("setCurFile: %s fail: %v", goFile, err)
}
return nil
}

func (p *Package) deferTypeBuild() error {
Expand Down
6 changes: 3 additions & 3 deletions cl/internal/convert/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2035,15 +2035,15 @@ func TestUnkownHfile(t *testing.T) {
ncimpl.NewHeaderFile("/path/to/foo.h", 0).ToGoFileName("Pkg")
}

func TestNewPackageLinkFail(t *testing.T) {
func TestNewPackageLinkWithoutLibCommand(t *testing.T) {
_, err := convert.NewPackage(nil, &convert.PackageConfig{
PkgBase: convert.PkgBase{
PkgPath: ".",
},
Name: pkgname,
GenConf: &gogen.Config{},
})
if err == nil {
t.Fatal("Expect Error")
if err != nil {
t.Fatal("Unexpect Error")
}
}
Loading