@@ -46,10 +46,13 @@ type Package struct {
4646
4747type 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-
146143func (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
730724func (p * Package ) deferTypeBuild () error {
0 commit comments