From 2d8f5dbc5108ec168c6eeb9a6a62a8dffd515f59 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 22 Apr 2024 21:16:43 +0800 Subject: [PATCH] remove dep of goplus/gop --- build_install_run.go | 8 +++ cl/compile.go | 2 +- cmd/internal/build/build.go | 15 +++-- go.mod | 1 - go.sum | 2 - internal/projs/proj.go | 110 ++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 internal/projs/proj.go diff --git a/build_install_run.go b/build_install_run.go index 383ab785..3d9a92ab 100644 --- a/build_install_run.go +++ b/build_install_run.go @@ -18,10 +18,18 @@ package llgo import ( "github.com/goplus/llgo/x/gocmd" + "github.com/goplus/mod/gopmod" ) // ----------------------------------------------------------------------------- +// NotFound returns if cause err is ErrNotFound or not +func NotFound(err error) bool { + return gopmod.IsNotFound(err) +} + +// ----------------------------------------------------------------------------- + func BuildDir(dir string, conf *Config, build *gocmd.BuildConfig) (err error) { panic("todo") } diff --git a/cl/compile.go b/cl/compile.go index 9487fb55..38a20fcc 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -19,12 +19,12 @@ package cl import ( "fmt" "go/ast" + "go/token" "go/types" "log" "os" "sort" - "github.com/goplus/gop/token" llssa "github.com/goplus/llgo/ssa" "golang.org/x/tools/go/ssa" ) diff --git a/cmd/internal/build/build.go b/cmd/internal/build/build.go index 0492f923..910c75fe 100644 --- a/cmd/internal/build/build.go +++ b/cmd/internal/build/build.go @@ -24,10 +24,9 @@ import ( "path/filepath" "reflect" - "github.com/goplus/gop" - "github.com/goplus/gop/x/gopprojs" "github.com/goplus/llgo" "github.com/goplus/llgo/cmd/internal/base" + "github.com/goplus/llgo/internal/projs" "github.com/goplus/llgo/x/gocmd" ) @@ -58,7 +57,7 @@ func runCmd(cmd *base.Command, args []string) { args = []string{"."} } - proj, args, err := gopprojs.ParseOne(args...) + proj, args, err := projs.ParseOne(args...) if err != nil { log.Panicln(err) } @@ -78,22 +77,22 @@ func runCmd(cmd *base.Command, args []string) { build(proj, conf, confCmd) } -func build(proj gopprojs.Proj, conf *llgo.Config, build *gocmd.BuildConfig) { +func build(proj projs.Proj, conf *llgo.Config, build *gocmd.BuildConfig) { var obj string var err error switch v := proj.(type) { - case *gopprojs.DirProj: + case *projs.DirProj: obj = v.Dir err = llgo.BuildDir(obj, conf, build) - case *gopprojs.PkgPathProj: + case *projs.PkgPathProj: obj = v.Path err = llgo.BuildPkgPath("", obj, conf, build) - case *gopprojs.FilesProj: + case *projs.FilesProj: err = llgo.BuildFiles(v.Files, conf, build) default: log.Panicln("`llgo build` doesn't support", reflect.TypeOf(v)) } - if gop.NotFound(err) { + if llgo.NotFound(err) { fmt.Fprintf(os.Stderr, "llgo build %v: not found\n", obj) } else if err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/go.mod b/go.mod index b0dd4215..84f9e077 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.18 require ( github.com/aykevl/go-wasm v0.0.1 github.com/goplus/gogen v1.15.2 - github.com/goplus/gop v1.2.6 github.com/goplus/llvm v0.7.1 github.com/goplus/mod v0.13.10 github.com/qiniu/x v1.13.10 diff --git a/go.sum b/go.sum index dacfde90..27f80d06 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,6 @@ github.com/aykevl/go-wasm v0.0.1 h1:lPxy8l48P39W7I0tLrtCrLfZBOUq9IWZ7odGdyJP2AM= github.com/aykevl/go-wasm v0.0.1/go.mod h1:b4nggwg3lEkNKOU4wzhtLKz2q2sLxSHFnc98aGt6z/Y= github.com/goplus/gogen v1.15.2 h1:Q6XaSx/Zi5tWnjfAziYsQI6Jv6MgODRpFtOYqNkiiqM= github.com/goplus/gogen v1.15.2/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk= -github.com/goplus/gop v1.2.6 h1:kog3c5Js+8EopqmI4+CwueXsqibnBwYVt5q5N7juRVY= -github.com/goplus/gop v1.2.6/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E= github.com/goplus/llvm v0.7.1 h1:B12Fr/wc3pAsq5PLuac9u9IuKpLRuCufdVAeGDP/MRw= github.com/goplus/llvm v0.7.1/go.mod h1:PeVK8GgzxwAYCiMiUAJb5wJR6xbhj989tu9oulKLLT4= github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE= diff --git a/internal/projs/proj.go b/internal/projs/proj.go new file mode 100644 index 00000000..f79c3b3e --- /dev/null +++ b/internal/projs/proj.go @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package projs + +import ( + "errors" + "path/filepath" + "syscall" +) + +// ----------------------------------------------------------------------------- + +type Proj = interface { + projObj() +} + +type FilesProj struct { + Files []string +} + +type PkgPathProj struct { + Path string +} + +type DirProj struct { + Dir string +} + +func (p *FilesProj) projObj() {} +func (p *PkgPathProj) projObj() {} +func (p *DirProj) projObj() {} + +// ----------------------------------------------------------------------------- + +func ParseOne(args ...string) (proj Proj, next []string, err error) { + if len(args) == 0 { + return nil, nil, syscall.ENOENT + } + arg := args[0] + if isFile(arg) { + n := 1 + for n < len(args) && isFile(args[n]) { + n++ + } + return &FilesProj{Files: args[:n]}, args[n:], nil + } + if isLocal(arg) { + return &DirProj{Dir: arg}, args[1:], nil + } + return &PkgPathProj{Path: arg}, args[1:], nil +} + +func isFile(fname string) bool { + n := len(filepath.Ext(fname)) + return n > 1 +} + +func isLocal(ns string) bool { + if len(ns) > 0 { + switch c := ns[0]; c { + case '/', '\\', '.': + return true + default: + return len(ns) >= 2 && ns[1] == ':' && ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z') + } + } + return false +} + +// ----------------------------------------------------------------------------- + +func ParseAll(args ...string) (projs []Proj, err error) { + var hasFiles, hasNotFiles bool + for { + proj, next, e := ParseOne(args...) + if e != nil { + if hasFiles && hasNotFiles { + return nil, ErrMixedFilesProj + } + return + } + if _, ok := proj.(*FilesProj); ok { + hasFiles = true + } else { + hasNotFiles = true + } + projs = append(projs, proj) + args = next + } +} + +var ( + ErrMixedFilesProj = errors.New("mixed files project") +) + +// -----------------------------------------------------------------------------