Skip to content

Commit 0d72acd

Browse files
committed
refactor(cl):allow empty lib command
1 parent 0e3333d commit 0d72acd

4 files changed

Lines changed: 48 additions & 25 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "withoutlib",
3+
"include": ["temp.h"],
4+
"cplusplus":false
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
===== temp.go =====
2+
package withoutlib
3+
4+
import (
5+
"github.com/goplus/lib/c"
6+
_ "unsafe"
7+
)
8+
9+
type Temp struct {
10+
A c.Int
11+
B c.Int
12+
}
13+
14+
===== withoutlib_autogen_link.go =====
15+
package withoutlib
16+
17+
import _ "github.com/goplus/lib/c"
18+
19+
===== llcppg.pub =====
20+
temp Temp
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
typedef struct temp {
2+
int a;
3+
int b;
4+
} temp;

cl/internal/convert/package.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ type Package struct {
4646

4747
type PackageConfig struct {
4848
PkgBase
49-
Name string // current package name
50-
OutputDir string
51-
GenConf *gogen.Config
52-
LibCommand string // use to gen link command like $(pkg-config --libs xxx)
49+
Name string // current package name
50+
OutputDir string
51+
GenConf *gogen.Config
52+
53+
// use to gen link command like $(pkg-config --libs xxx)
54+
// if have this field,will generate a const named LLGoPackage,and it's not necessary
55+
LibCommand string
5356
}
5457

5558
// When creating a new package for conversion, a Go file named after the package is generated by default.
@@ -92,10 +95,14 @@ func NewPackage(pnc nc.NodeConverter, config *PackageConfig) (*Package, error) {
9295
if err != nil {
9396
return nil, fmt.Errorf("failed to init deps: %w", err)
9497
}
95-
err = p.initLink()
96-
if err != nil {
97-
return nil, fmt.Errorf("failed to init link: %w", err)
98+
99+
// allow have not lib command
100+
if p.conf.LibCommand != "" {
101+
if err := p.initLink(p.conf.LibCommand); err != nil {
102+
return nil, fmt.Errorf("failed to init link: %w", err)
103+
}
98104
}
105+
99106
p.markUseDeps(pkgManager)
100107
p.cvt = NewConv(p.p, p.p.Types, pnc, p.lookupType)
101108
return p, nil
@@ -133,16 +140,6 @@ func (p *Package) setGoFile(goFile string) {
133140
p.p.Unsafe().MarkForceUsed(p.p)
134141
}
135142

136-
// todo(zzy):refine logic
137-
func (p *Package) linkLib(lib string) error {
138-
if lib == "" {
139-
return fmt.Errorf("empty lib name")
140-
}
141-
linkString := fmt.Sprintf("link: %s;", lib)
142-
p.p.CB().NewConstStart(types.Typ[types.String], "LLGoPackage").Val(linkString).EndInit(1)
143-
return nil
144-
}
145-
146143
func (p *Package) newReceiver(typ *ast.FuncType) (*types.Var, error) {
147144
recvField := typ.Params.List[0]
148145
recvType, err := p.ToType(recvField.Type)
@@ -714,17 +711,14 @@ func (p *Package) autoLinkFile() string {
714711
return p.conf.Name + "_autogen_link.go"
715712
}
716713

717-
func (p *Package) initLink() error {
718-
fileName := p.autoLinkFile()
719-
_, err := p.p.SetCurFile(fileName, true)
714+
func (p *Package) initLink(lib string) (err error) {
715+
_, err = p.p.SetCurFile(p.autoLinkFile(), true)
720716
if err != nil {
721717
return fmt.Errorf("failed to set current file: %w", err)
722718
}
723-
err = p.linkLib(p.conf.LibCommand)
724-
if err != nil {
725-
return err
726-
}
727-
return nil
719+
linkString := fmt.Sprintf("link: %s;", lib)
720+
p.p.CB().NewConstStart(types.Typ[types.String], "LLGoPackage").Val(linkString).EndInit(1)
721+
return
728722
}
729723

730724
func (p *Package) deferTypeBuild() error {

0 commit comments

Comments
 (0)