diff --git a/cl/internal/convert/convert.go b/cl/internal/convert/convert.go index 4e5a1930f..e8e2e043e 100644 --- a/cl/internal/convert/convert.go +++ b/cl/internal/convert/convert.go @@ -108,7 +108,16 @@ func (p *Converter) Process() error { } return fmt.Errorf("ConvMacro: %w", err) } - ctx.setGoFile(goFile) + + ctx.setGoFile(goFile.FileName, func() { + // todo(zzy):write build condition to file + // var os, arch string + // if goFile.Condition != nil { + // os = goFile.Condition.OS + // arch = goFile.Condition.Arch + // } + }) + err = ctx.NewMacro(goName, macro) if err != nil { return err @@ -124,7 +133,9 @@ func (p *Converter) Process() error { } return fmt.Errorf("ConvDecl: %w", err) } - ctx.setGoFile(goFile) + ctx.setGoFile(goFile.FileName, func() { + // todo(zzy):write build condition to file + }) switch decl := decl.(type) { case *ast.TypeDecl: err = ctx.NewTypeDecl(goName, decl, pnc) diff --git a/cl/internal/convert/package.go b/cl/internal/convert/package.go index a2b65811a..cb166ca40 100644 --- a/cl/internal/convert/package.go +++ b/cl/internal/convert/package.go @@ -126,8 +126,10 @@ func (p *Package) LookupFunc(goName string, fn *ast.FuncDecl) (*GoFuncSpec, erro return NewGoFuncSpec(goName), nil } +// If not have goFile in current package,it will create a new file,and the init function will be execute // to keep the unsafe package load to use go:linkname command -func (p *Package) setGoFile(goFile string) { +func (p *Package) setGoFile(goFile string, init func()) { + // todo(zzy):support write build condition to file p.p.SetCurFile(goFile, true) // todo(zzy):avoid remark p.p.Unsafe().MarkForceUsed(p.p) diff --git a/cl/nc/ncimpl/ncimpl.go b/cl/nc/ncimpl/ncimpl.go index 83a2512c1..14384e509 100644 --- a/cl/nc/ncimpl/ncimpl.go +++ b/cl/nc/ncimpl/ncimpl.go @@ -44,7 +44,7 @@ type Converter struct { KeepUnderScore bool } -func (p *Converter) convFile(file string, obj *ast.Object) (goFile string, ok bool) { +func (p *Converter) convFile(file string, obj *ast.Object) (goFile *nc.GoFile, ok bool) { info, exist := p.FileMap[file] if !exist { var availableFiles []string @@ -58,10 +58,13 @@ func (p *Converter) convFile(file string, obj *ast.Object) (goFile string, ok bo if obj != nil && obj.Name != nil && hf.FileType == llconfig.Third { p.locMap.Add(obj.Name, obj.Loc) } - return hf.ToGoFileName(p.PkgName), hf.InCurPkg() + return &nc.GoFile{ + FileName: hf.ToGoFileName(p.PkgName), + // todo(zzy):support condition + }, hf.InCurPkg() } -func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName, goFile string, err error) { +func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName string, goFile *nc.GoFile, err error) { obj := ast.ObjectOf(decl) goFile, ok := p.convFile(file, obj) if !ok { @@ -88,7 +91,7 @@ func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName, goFile string, return } -func (p *Converter) ConvMacro(file string, macro *ast.Macro) (goName, goFile string, err error) { +func (p *Converter) ConvMacro(file string, macro *ast.Macro) (goName string, goFile *nc.GoFile, err error) { goFile, ok := p.convFile(file, nil) if !ok { err = nc.ErrSkip diff --git a/cl/nc/ncimpl/ncimpl_test.go b/cl/nc/ncimpl/ncimpl_test.go index 7cd283304..ec89a1870 100644 --- a/cl/nc/ncimpl/ncimpl_test.go +++ b/cl/nc/ncimpl/ncimpl_test.go @@ -169,8 +169,8 @@ func TestConverterConvFile(t *testing.T) { t.Run(tc.name, func(t *testing.T) { goFile, ok := converter.convFile(tc.file, nil) - if goFile != tc.expected { - t.Errorf("Expected file %s, got %s", tc.expected, goFile) + if goFile.FileName != tc.expected { + t.Errorf("Expected file %s, got %s", tc.expected, goFile.FileName) } if ok != tc.ok { @@ -226,7 +226,7 @@ func TestConv(t *testing.T) { trimPrefixes []string expectFile string expectName string - exec func(conv *Converter, convNode ast.Node) (string, string, error) + exec func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) expectErr error }{ { @@ -235,7 +235,7 @@ func TestConv(t *testing.T) { Name: "MACRO_NAME", Loc: &ast.Location{File: interFile}, }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvMacro(interFile, convNode.(*ast.Macro)) }, expectFile: "inter.go", @@ -248,7 +248,7 @@ func TestConv(t *testing.T) { Name: "MACRO_NAME", Loc: &ast.Location{File: interFile}, }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvMacro(interFile, convNode.(*ast.Macro)) }, trimPrefixes: []string{"MACRO_"}, @@ -263,7 +263,7 @@ func TestConv(t *testing.T) { Loc: &ast.Location{File: interFile}, }, pubs: map[string]string{"MACRO_CONST": "CustomMacro"}, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvMacro(interFile, convNode.(*ast.Macro)) }, expectFile: "inter.go", @@ -276,7 +276,7 @@ func TestConv(t *testing.T) { Name: "MACRO_NAME", Loc: &ast.Location{File: thirdFile}, }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvMacro(thirdFile, convNode.(*ast.Macro)) }, expectFile: "testpkg_autogen.go", @@ -292,7 +292,7 @@ func TestConv(t *testing.T) { MangledName: "validFunc", }, convSym: mockSymConv, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvDecl(interFile, convNode.(*ast.FuncDecl)) }, expectFile: "inter.go", @@ -307,7 +307,7 @@ func TestConv(t *testing.T) { Loc: &ast.Location{File: thirdFile}, }, }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvDecl(thirdFile, convNode.(*ast.FuncDecl)) }, expectFile: "testpkg_autogen.go", @@ -322,7 +322,7 @@ func TestConv(t *testing.T) { }, MangledName: "noSymbolFunc", }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvDecl(interFile, convNode.(*ast.FuncDecl)) }, convSym: mockSymConv, @@ -337,7 +337,7 @@ func TestConv(t *testing.T) { Loc: &ast.Location{File: interFile}, }, }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvDecl(interFile, convNode.(*ast.EnumTypeDecl)) }, expectFile: "inter.go", @@ -352,7 +352,7 @@ func TestConv(t *testing.T) { Loc: &ast.Location{File: interFile}, }, }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvDecl(interFile, convNode.(*ast.EnumTypeDecl)) }, expectFile: "inter.go", @@ -366,7 +366,7 @@ func TestConv(t *testing.T) { Loc: &ast.Location{File: interFile}, }, }, - exec: func(conv *Converter, convNode ast.Node) (string, string, error) { + exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) { return conv.ConvDecl(interFile, convNode.(*ast.TypeDecl)) }, expectFile: "inter.go", @@ -388,8 +388,8 @@ func TestConv(t *testing.T) { if goName != tc.expectName { t.Errorf("Expected %s, got %s", tc.expectName, goName) } - if goFile != tc.expectFile { - t.Errorf("Expected %s, got %s", tc.expectFile, goFile) + if goFile.FileName != tc.expectFile { + t.Errorf("Expected %s, got %s", tc.expectFile, goFile.FileName) } if err != tc.expectErr { t.Errorf("Expected %v, got %v", tc.expectErr, err) diff --git a/cl/nc/nodeconv.go b/cl/nc/nodeconv.go index 3b308df27..41419b645 100644 --- a/cl/nc/nodeconv.go +++ b/cl/nc/nodeconv.go @@ -11,9 +11,19 @@ var ( ErrSkip = errors.New("skip this node") ) +type Condition struct { + OS string // OS,like darwin,linux,windows + Arch string // Architecture,like amd64,arm64 +} + +type GoFile struct { + FileName string // Go file name,like cJSON.go ini_darwin_amd64.go + Condition *Condition // Condition for the given file,if no condition,it is nil +} + type NodeConverter interface { - ConvDecl(file string, decl ast.Decl) (goName, goFile string, err error) - ConvMacro(file string, macro *ast.Macro) (goName, goFile string, err error) + ConvDecl(file string, decl ast.Decl) (goName string, goFile *GoFile, err error) + ConvMacro(file string, macro *ast.Macro) (goName string, goFile *GoFile, err error) ConvEnumItem(decl *ast.EnumTypeDecl, item *ast.EnumItem) (goName string, err error) ConvTagExpr(cname string) string Lookup(name string) (locFile string, ok bool)