Skip to content

Commit 7aae351

Browse files
committed
cmd: gop go -multi-files
1 parent fc370da commit 7aae351

File tree

7 files changed

+74
-19
lines changed

7 files changed

+74
-19
lines changed

cl/builtin_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,19 +581,21 @@ func lookupClassErr(ext string) (c *modfile.Project, ok bool) {
581581
}
582582

583583
func TestGetGoFile(t *testing.T) {
584-
if f := genGoFile("a_test.gop", false); f != testingGoFile {
584+
ctx := &pkgCtx{}
585+
if f := ctx.genGoFile("a_test.xgo", false); f != testingGoFile {
585586
t.Fatal("TestGetGoFile:", f)
586587
}
587-
if f := genGoFile("a_test.xgo", false); f != testingGoFile {
588+
if f := ctx.genGoFile("a_test.gox", true); f != testingGoFile {
588589
t.Fatal("TestGetGoFile:", f)
589590
}
590-
if f := genGoFile("a_test.gox", true); f != testingGoFile {
591+
if f := ctx.genGoFile("a.gop", false); f != defaultGoFile {
591592
t.Fatal("TestGetGoFile:", f)
592593
}
593-
if f := genGoFile("a.gop", false); f != defaultGoFile {
594+
ctx.multiFiles = true
595+
if f := ctx.genGoFile("a.gop", false); f != "a.gop" {
594596
t.Fatal("TestGetGoFile:", f)
595597
}
596-
if f := genGoFile("a.xgo", false); f != defaultGoFile {
598+
if f := ctx.genGoFile("a.xgo", false); f != "a.xgo" {
597599
t.Fatal("TestGetGoFile:", f)
598600
}
599601
}

cl/classfile.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type gmxProject struct {
8585
gameIsPtr bool
8686
isTest bool
8787
hasMain_ bool
88+
file string
8889
}
8990

9091
func (p *gmxProject) embed(flds []*types.Var, pkg *gogen.Package) []*types.Var {
@@ -463,13 +464,14 @@ func gmxCheckProjs(pkg *gogen.Package, ctx *pkgCtx) (*gmxProject, bool) {
463464
func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
464465
base := proj.game // project base class
465466
classType := proj.getGameClass(parent) // project class
467+
goFile := parent.genGoFile(proj.file, false)
466468
ld := getTypeLoader(parent, parent.syms, token.NoPos, classType)
467469
if ld.typ == nil { // no project class, use default
468470
ld.typ = func() {
469471
if debugLoad {
470472
log.Println("==> Load > NewType", classType)
471473
}
472-
old, _ := pkg.SetCurFile(defaultGoFile, true)
474+
old, _ := pkg.SetCurFile(goFile, true)
473475
defer pkg.RestoreCurFile(old)
474476

475477
baseType := base.Type()
@@ -486,7 +488,7 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
486488
if debugLoad {
487489
log.Println("==> Load > InitType", classType)
488490
}
489-
old, _ := pkg.SetCurFile(defaultGoFile, true)
491+
old, _ := pkg.SetCurFile(goFile, true)
490492
defer pkg.RestoreCurFile(old)
491493

492494
decl.InitType(pkg, types.NewStruct(flds, nil))
@@ -495,7 +497,7 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
495497
}
496498
}
497499
ld.methods = append(ld.methods, func() {
498-
old, _ := pkg.SetCurFile(defaultGoFile, true)
500+
old, _ := pkg.SetCurFile(goFile, true)
499501
defer pkg.RestoreCurFile(old)
500502
doInitType(ld)
501503

@@ -504,7 +506,7 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
504506
fn := pkg.NewFunc(recv, "Main", nil, nil, false)
505507

506508
parent.inits = append(parent.inits, func() {
507-
old, _ := pkg.SetCurFile(defaultGoFile, true)
509+
old, _ := pkg.SetCurFile(goFile, true)
508510
defer pkg.RestoreCurFile(old)
509511

510512
cb := fn.BodyStart(pkg).Typ(base.Type()).MemberVal("Main")

cl/compile.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ type Config struct {
204204

205205
// Outline = true means to skip compiling function bodies.
206206
Outline bool
207+
208+
// MultiFiles = true means generate multi files.
209+
MultiFiles bool
207210
}
208211

209212
type nodeInterp struct {
@@ -352,6 +355,7 @@ type pkgCtx struct {
352355
goxMain int32 // normal gox files with main func
353356

354357
featTypesAlias bool // support types alias
358+
multiFiles bool // generate multi files
355359
}
356360

357361
type pkgImp struct {
@@ -519,6 +523,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gogen.Packag
519523
overpos: make(map[string]token.Pos),
520524
syms: make(map[string]loader),
521525
generics: make(map[string]bool),
526+
multiFiles: conf.MultiFiles,
522527
}
523528
confGox := &gogen.Config{
524529
Types: conf.Types,
@@ -657,7 +662,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gogen.Packag
657662
if mainClass != "" { // generate classfile main func
658663
genMainFunc(p, mainClass)
659664
} else if genMain && !conf.NoAutoGenMain { // generate empty main func
660-
old, _ := p.SetCurFile(defaultGoFile, false)
665+
old, _ := p.SetCurFile(ctx.genGoFile("", false), false)
661666
p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
662667
p.RestoreCurFile(old)
663668
}
@@ -726,10 +731,13 @@ func loadFile(ctx *pkgCtx, f *ast.File) {
726731
// *_test.xgo
727732
// *_test.gop
728733
// *test.gox
729-
func genGoFile(file string, goxTestFile bool) string {
734+
func (ctx *pkgCtx) genGoFile(file string, goxTestFile bool) string {
730735
if goxTestFile || strings.HasSuffix(file, "_test.xgo") || strings.HasSuffix(file, "_test.gop") {
731736
return testingGoFile
732737
}
738+
if ctx.multiFiles {
739+
return file
740+
}
733741
return defaultGoFile
734742
}
735743

@@ -770,6 +778,7 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
770778
if proj.gameIsPtr {
771779
baseType = types.NewPointer(baseType)
772780
}
781+
proj.file = file
773782
} else {
774783
sp = c.sp
775784
o := sp.obj
@@ -778,7 +787,7 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
778787
}
779788
}
780789
}
781-
goFile := genGoFile(file, goxTestFile)
790+
goFile := ctx.genGoFile(file, goxTestFile)
782791
if classType != "" {
783792
if debugLoad {
784793
log.Println("==> Preload type", classType)

cmd/internal/clean/clean.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
)
2828

2929
const (
30+
autoGenFilePrefix = "xgo_autogen_"
3031
autoGenFileSuffix = "_autogen.go"
3132
autoGenGopTestFile = "gop_autogen_test.go"
3233
autoGen2GopTestFile = "gop_autogen2_test.go"
@@ -55,7 +56,7 @@ func cleanAGFiles(dir string, execAct bool) {
5556
}
5657
continue
5758
}
58-
if strings.HasSuffix(fname, autoGenFileSuffix) {
59+
if strings.HasSuffix(fname, autoGenFileSuffix) || strings.HasPrefix(fname, autoGenFilePrefix) {
5960
file := filepath.Join(dir, fname)
6061
fmt.Printf("Cleaning %s ...\n", file)
6162
if execAct {

cmd/internal/gengo/go.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ var (
4444
flagSingleMode = flag.Bool("s", false, "run in single file mode for package")
4545
flagIgnoreNotatedErr = flag.Bool(
4646
"ignore-notated-error", false, "ignore notated errors, only available together with -t (check mode)")
47-
flagTags = flag.String("tags", "", "a comma-separated list of additional build tags to consider satisfied")
47+
flagTags = flag.String("tags", "", "a comma-separated list of additional build tags to consider satisfied")
48+
flagMultiFiles = flag.Bool("multi-files", false, "genarate multi files for package")
4849
)
4950

5051
func init() {
@@ -88,6 +89,9 @@ func runCmd(cmd *base.Command, args []string) {
8889
if *flagSingleMode {
8990
flags |= tool.GenFlagSingleFile
9091
}
92+
if *flagMultiFiles {
93+
flags |= tool.GenFlagMultiFiles
94+
}
9195
for _, proj := range projs {
9296
switch v := proj.(type) {
9397
case *xgoprojs.DirProj:

tool/gengo.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525
"syscall"
2626

27+
"github.com/goplus/gogen"
2728
"github.com/goplus/mod/modcache"
2829
"github.com/goplus/mod/modfetch"
2930
"github.com/goplus/mod/xgomod"
@@ -35,13 +36,15 @@ const (
3536
autoGenFile = "xgo_autogen.go"
3637
autoGenTestFile = "xgo_autogen_test.go"
3738
autoGen2TestFile = "xgo_autogen2_test.go"
39+
autoGenPrefix = "xgo_autogen_"
3840
)
3941

4042
type GenFlags int
4143

4244
const (
4345
GenFlagCheckOnly GenFlags = 1 << iota
4446
GenFlagSingleFile
47+
GenFlagMultiFiles
4548
GenFlagPrintError
4649
GenFlagPrompt
4750
)
@@ -166,7 +169,7 @@ func genGoSingleFile(file string, extn int, conf *Config, flags GenFlags) (err e
166169
}
167170

168171
func genGoIn(dir string, conf *Config, genTestPkg bool, flags GenFlags, gen ...*bool) (err error) {
169-
out, test, err := LoadDir(dir, conf, genTestPkg, (flags&GenFlagPrompt) != 0)
172+
out, test, err := LoadDir(dir, conf, genTestPkg, (flags&GenFlagPrompt) != 0, (flags&GenFlagMultiFiles) != 0)
170173
if err != nil {
171174
if NotFound(err) { // no XGo source files
172175
return nil
@@ -177,10 +180,18 @@ func genGoIn(dir string, conf *Config, genTestPkg bool, flags GenFlags, gen ...*
177180
return nil
178181
}
179182
os.MkdirAll(dir, 0755)
180-
file := filepath.Join(dir, autoGenFile)
181-
err = out.WriteFile(file)
182-
if err != nil {
183-
return errors.NewWith(err, `out.WriteFile(file)`, -2, "(*gogen.Package).WriteFile", out, file)
183+
184+
if flags&GenFlagMultiFiles != 0 {
185+
err = writeMultiFiles(out, dir)
186+
if err != nil {
187+
return errors.NewWith(err, `writeMultiFiles(out, dir)`, -2, "(*gogen.Package).WriteFile", out, dir)
188+
}
189+
} else {
190+
file := filepath.Join(dir, autoGenFile)
191+
err = out.WriteFile(file)
192+
if err != nil {
193+
return errors.NewWith(err, `out.WriteFile(file)`, -2, "(*gogen.Package).WriteFile", out, file)
194+
}
184195
}
185196
if gen != nil { // say `xgo_autogen.go generated`
186197
*gen[0] = true
@@ -204,6 +215,29 @@ func genGoIn(dir string, conf *Config, genTestPkg bool, flags GenFlags, gen ...*
204215
return
205216
}
206217

218+
func writeMultiFiles(pkg *gogen.Package, dir string) error {
219+
names := make(map[string]string)
220+
pkg.ForEachFile(func(fname string, file *gogen.File) {
221+
if fname == "" {
222+
names[fname] = autoGenFile
223+
} else {
224+
_, name := filepath.Split(fname)
225+
name = name[:len(name)-len(filepath.Ext(name))]
226+
if strings.HasSuffix(name, "_test") {
227+
return
228+
}
229+
names[fname] = autoGenPrefix + name + ".go"
230+
}
231+
})
232+
for fname, file := range names {
233+
err := pkg.WriteFile(filepath.Join(dir, file), fname)
234+
if err != nil {
235+
return err
236+
}
237+
}
238+
return nil
239+
}
240+
207241
// -----------------------------------------------------------------------------
208242

209243
const (

tool/load.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
277277
Importer: imp,
278278
LookupClass: mod.LookupClass,
279279
}
280+
if len(promptGenGo) > 1 && promptGenGo[1] {
281+
clConf.MultiFiles = true
282+
}
280283

281284
for name, pkg := range pkgs {
282285
if strings.HasSuffix(name, "_test") {

0 commit comments

Comments
 (0)