Skip to content

Commit 443c968

Browse files
committed
cmd: gop go -multi-files
1 parent cd02745 commit 443c968

File tree

6 files changed

+67
-16
lines changed

6 files changed

+67
-16
lines changed

cl/classfile.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type gmxProject struct {
5555
gameIsPtr bool
5656
isTest bool
5757
hasMain_ bool
58+
file string
5859
}
5960

6061
func (p *gmxProject) hasMain() bool {
@@ -319,14 +320,14 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
319320
classType = base.Name()
320321
proj.gameClass = classType
321322
}
322-
323+
goFile := parent.genGoFile(proj.file, false)
323324
ld := getTypeLoader(parent, parent.syms, token.NoPos, proj.gameClass)
324325
if ld.typ == nil {
325326
ld.typ = func() {
326327
if debugLoad {
327328
log.Println("==> Load > NewType", classType)
328329
}
329-
old, _ := pkg.SetCurFile(defaultGoFile, true)
330+
old, _ := pkg.SetCurFile(goFile, true)
330331
defer pkg.RestoreCurFile(old)
331332

332333
baseType := base.Type()
@@ -342,7 +343,7 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
342343
if debugLoad {
343344
log.Println("==> Load > InitType", classType)
344345
}
345-
old, _ := pkg.SetCurFile(defaultGoFile, true)
346+
old, _ := pkg.SetCurFile(goFile, true)
346347
defer pkg.RestoreCurFile(old)
347348

348349
decl.InitType(pkg, types.NewStruct(flds, nil))
@@ -351,7 +352,7 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
351352
}
352353
}
353354
ld.methods = append(ld.methods, func() {
354-
old, _ := pkg.SetCurFile(defaultGoFile, true)
355+
old, _ := pkg.SetCurFile(goFile, true)
355356
defer pkg.RestoreCurFile(old)
356357
doInitType(ld)
357358

@@ -360,7 +361,7 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
360361
fn := pkg.NewFunc(recv, "Main", nil, nil, false)
361362

362363
parent.inits = append(parent.inits, func() {
363-
old, _ := pkg.SetCurFile(defaultGoFile, true)
364+
old, _ := pkg.SetCurFile(goFile, true)
364365
defer pkg.RestoreCurFile(old)
365366

366367
cb := fn.BodyStart(pkg).Typ(base.Type()).MemberVal("Main")
@@ -389,9 +390,11 @@ func gmxProjMain(pkg *gogen.Package, parent *pkgCtx, proj *gmxProject) {
389390
})
390391
}
391392

392-
func gmxMainFunc(pkg *gogen.Package, proj *gmxProject) func() {
393+
func gmxMainFunc(pkg *gogen.Package, ctx *pkgCtx, proj *gmxProject) func() {
393394
return func() {
394395
if o := pkg.TryRef(proj.gameClass); o != nil {
396+
old, _ := pkg.SetCurFile(ctx.genGoFile(proj.file, false), true)
397+
defer pkg.RestoreCurFile(old)
395398
// new(gameClass).Main()
396399
new := pkg.Builtin().Ref("new")
397400
pkg.NewFunc(nil, "main", nil, nil, false).

cl/compile.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ type Config struct {
203203

204204
// Outline = true means to skip compiling function bodies.
205205
Outline bool
206+
207+
// MultiFiles = true means generate multi files.
208+
MultiFiles bool
206209
}
207210

208211
type nodeInterp struct {
@@ -342,6 +345,8 @@ type pkgCtx struct {
342345
generics map[string]bool // generic type record
343346
idents []*ast.Ident // toType ident recored
344347
inInst int // toType in generic instance
348+
349+
multiFiles bool // generate multi files
345350
}
346351

347352
type pkgImp struct {
@@ -501,6 +506,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gogen.Packag
501506
overpos: make(map[string]token.Pos),
502507
syms: make(map[string]loader),
503508
generics: make(map[string]bool),
509+
multiFiles: conf.MultiFiles,
504510
}
505511
confGox := &gogen.Config{
506512
Types: conf.Types,
@@ -609,7 +615,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gogen.Packag
609615

610616
if genMain { // make classfile main func if need
611617
if proj != nil && !multi { // only one project file
612-
gen = gmxMainFunc(p, proj)
618+
gen = gmxMainFunc(p, ctx, proj)
613619
}
614620
}
615621

@@ -634,7 +640,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gogen.Packag
634640
if gen != nil { // generate classfile main func
635641
gen()
636642
} else if genMain && !conf.NoAutoGenMain { // generate empty main func
637-
old, _ := p.SetCurFile(defaultGoFile, false)
643+
old, _ := p.SetCurFile(ctx.genGoFile("", false), false)
638644
p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
639645
p.RestoreCurFile(old)
640646
}
@@ -702,10 +708,13 @@ func loadFile(ctx *pkgCtx, f *ast.File) {
702708
//
703709
// *_test.gop
704710
// *test.gox
705-
func genGoFile(file string, goxTestFile bool) string {
711+
func (ctx *pkgCtx) genGoFile(file string, goxTestFile bool) string {
706712
if goxTestFile || strings.HasSuffix(file, "_test.gop") {
707713
return testingGoFile
708714
}
715+
if ctx.multiFiles {
716+
return file
717+
}
709718
return defaultGoFile
710719
}
711720

@@ -744,14 +753,15 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
744753
if proj.gameIsPtr {
745754
baseType = types.NewPointer(baseType)
746755
}
756+
proj.file = file
747757
} else {
748758
o := proj.sprite[c.ext]
749759
ctx.baseClass = o
750760
baseTypeName, baseType, spxClass = o.Name(), o.Type(), true
751761
}
752762
}
753763
}
754-
goFile := genGoFile(file, goxTestFile)
764+
goFile := ctx.genGoFile(file, goxTestFile)
755765
if classType != "" {
756766
if debugLoad {
757767
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 = "gop_autogen_"
3031
autoGenFileSuffix = "_autogen.go"
3132
autoGenTestFile = "gop_autogen_test.go"
3233
autoGen2TestFile = "gop_autogen2_test.go"
@@ -53,7 +54,7 @@ func cleanAGFiles(dir string, execAct bool) {
5354
}
5455
continue
5556
}
56-
if strings.HasSuffix(fname, autoGenFileSuffix) {
57+
if strings.HasSuffix(fname, autoGenFileSuffix) || strings.HasPrefix(fname, autoGenFilePrefix) {
5758
file := filepath.Join(dir, fname)
5859
fmt.Printf("Cleaning %s ...\n", file)
5960
if execAct {

cmd/internal/gengo/go.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ 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+
flagMultiFiles = flag.Bool("multi-files", false, "genarate multi files for package")
4748
)
4849

4950
func init() {
@@ -87,6 +88,9 @@ func runCmd(cmd *base.Command, args []string) {
8788
if *flagSingleMode {
8889
flags |= gop.GenFlagSingleFile
8990
}
91+
if *flagMultiFiles {
92+
flags |= gop.GenFlagMultiFiles
93+
}
9094
for _, proj := range projs {
9195
switch v := proj.(type) {
9296
case *gopprojs.DirProj:

gengo.go

Lines changed: 35 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/gopmod"
2829
"github.com/goplus/mod/modcache"
2930
"github.com/goplus/mod/modfetch"
@@ -35,13 +36,15 @@ const (
3536
autoGenFile = "gop_autogen.go"
3637
autoGenTestFile = "gop_autogen_test.go"
3738
autoGen2TestFile = "gop_autogen2_test.go"
39+
autoGenPrefix = "gop_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, conf *Config, flags GenFlags) (err error) {
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 Go+ 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 `gop_autogen.go generated`
186197
*gen[0] = true
@@ -204,6 +215,25 @@ 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+
names[fname] = autoGenPrefix + name[:len(name)-len(filepath.Ext(name))] + ".go"
226+
}
227+
})
228+
for fname, file := range names {
229+
err := pkg.WriteFile(filepath.Join(dir, file), fname)
230+
if err != nil {
231+
return err
232+
}
233+
}
234+
return nil
235+
}
236+
207237
// -----------------------------------------------------------------------------
208238

209239
const (

load.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
272272
Importer: imp,
273273
LookupClass: mod.LookupClass,
274274
}
275+
if len(promptGenGo) > 1 && promptGenGo[1] {
276+
clConf.MultiFiles = true
277+
}
275278

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

0 commit comments

Comments
 (0)