Skip to content

Commit

Permalink
Merge pull request #83 from xushiwei/q
Browse files Browse the repository at this point in the history
go.mod: support GoCompiler
  • Loading branch information
xushiwei authored Jul 7, 2024
2 parents 98d467e + 33ef86f commit aaad4a2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ require (
github.com/qiniu/x v1.13.10
golang.org/x/mod v0.19.0
)

retract v0.13.11
7 changes: 6 additions & 1 deletion modfile/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ import (
"golang.org/x/mod/modfile"
)

type Compiler struct {
Name string
Version string
}

// A File is the parsed, interpreted form of a gop.mod file.
type File struct {
Gop *Gop
Compiler *Compiler // the underlying go compiler in go.mod (not gop.mod)
Projects []*Project
ClassMods []string // calc by require statements in go.mod (not gop.mod)
LLGoVer string // llgo version in go.mod (not gop.mod)

Syntax *FileSyntax
}
Expand Down
19 changes: 16 additions & 3 deletions modload/modtest/modtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,28 @@ require (
return mod
}

func LLGoVer(t *testing.T) modload.Module {
func LLGoCompiler(t *testing.T) modload.Module {
const gomodText = `
module github.com/goplus/llgo
go 1.18 // llgo 0.9
`
mod := Load(t, gomodText, ``, ``)
if ver := mod.Opt.LLGoVer; ver != "0.9" {
t.Fatal("mod.Opt.LLGoVer:", ver)
if cl := mod.Opt.Compiler; cl == nil || cl.Name != "llgo" || cl.Version != "0.9" {
t.Fatal("mod.Opt.Compiler:", cl)
}
return mod
}

func TinyGoCompiler(t *testing.T) modload.Module {
const gomodText = `
module github.com/tinygo-org/tinygo
go 1.18 // tinygo 0.32
`
mod := Load(t, gomodText, ``, ``)
if cl := mod.Opt.Compiler; cl == nil || cl.Name != "tinygo" || cl.Version != "0.32" {
t.Fatal("mod.Opt.Compiler:", cl)
}
return mod
}
Expand Down
40 changes: 33 additions & 7 deletions modload/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,31 @@ func LoadFromEx(gomod, gopmod string, readFile func(string) ([]byte, error)) (p
opt = newGopMod(gopmod, defaultGopVer)
}
importClassfileFromGoMod(opt, f)
if ver := getLLGoVersion(f); ver != "" {
opt.LLGoVer = ver
if cl := getGoCompiler(f); cl != nil {
opt.Compiler = cl
}
return Module{f, opt}, nil
}

func (p Module) AddCompiler(compiler, ver string) {
f := p.File
if f.Go == nil {
f.AddGoStmt(defaultGoVer)
}
addCompiler(p.Opt, f.Go, compiler, ver)
p.Opt.Compiler = &modfile.Compiler{Name: compiler, Version: ver}
}

func addCompiler(opt *modfile.File, r *gomodfile.Go, compiler, ver string) {
if line := r.Syntax; line != nil {
line.Suffix = []gomodfile.Comment{{
Token: "// " + compiler + " " + ver,
Suffix: true,
}}
opt.Compiler = &modfile.Compiler{Name: compiler, Version: ver}
}
}

func (p Module) AddRequire(path, vers string, hasProj bool) error {
f := p.File
f.AddRequire(path, vers)
Expand Down Expand Up @@ -279,22 +298,29 @@ func isClass(r *gomodfile.Require) bool {
}

/*
* go.mod:
go 1.18 // llgo 0.9
go 1.18 // tinygo 0.32
*/
func getLLGoVersion(f *gomodfile.File) string {
func getGoCompiler(f *gomodfile.File) *modfile.Compiler {
if gostmt := f.Go; gostmt != nil {
if line := gostmt.Syntax; line != nil {
for _, c := range line.Suffix {
text := strings.TrimLeft(c.Token[2:], " \t")
if strings.HasPrefix(text, "llgo ") {
return strings.TrimSpace(text[5:])
return &modfile.Compiler{
Name: "llgo",
Version: strings.TrimSpace(text[5:]),
}
} else if strings.HasPrefix(text, "tinygo ") {
return &modfile.Compiler{
Name: "tinygo",
Version: strings.TrimSpace(text[7:]),
}
}
}
}
}
return ""
return nil
}

// -----------------------------------------------------------------------------
Expand Down
27 changes: 27 additions & 0 deletions modload/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ func TestLoad(t *testing.T) {
}
}

func TestCreateWithGoCompiler(t *testing.T) {
mod, err := Create("/foo/bar", "github.com/foo/bar", "1.20", defaultGopVer)
if err != nil {
t.Fatal("Create failed:", err)
}
mod.AddCompiler("llgo", "0.9")
if b, err := mod.File.Format(); err != nil {
t.Fatal("AddCompiler & Format:", err)
} else if v := string(b); v != `module github.com/foo/bar
go 1.20 // llgo 0.9
` {
t.Fatal("AddCompiler:", v)
}

mod.File.DropGoStmt()
mod.AddCompiler("tinygo", "0.32")
if b, err := mod.File.Format(); err != nil {
t.Fatal("AddCompiler & Format:", err)
} else if v := string(b); v != `module github.com/foo/bar
go 1.18 // tinygo 0.32
` {
t.Fatal("AddCompiler:", v)
}
}

func TestCreate(t *testing.T) {
mod, err := Create("/foo/bar", "github.com/foo/bar", defaultGoVer, defaultGopVer)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions modload/regtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func TestGopClass(t *testing.T) {
modtest.GopClass(t)
}

func TestLLGoVer(t *testing.T) {
modtest.LLGoVer(t)
func TestGoCompiler(t *testing.T) {
modtest.LLGoCompiler(t)
modtest.TinyGoCompiler(t)
}

func TestImport(t *testing.T) {
Expand Down

0 comments on commit aaad4a2

Please sign in to comment.