github.com/goplus/igop@v0.25.0/cmd/internal/build/build.go (about) 1 /* 2 Copyright 2021 The GoPlus Authors (goplus.org) 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package build implements the ``igop build'' command. 18 package build 19 20 import ( 21 "fmt" 22 "os" 23 "path/filepath" 24 25 "github.com/goplus/igop" 26 "github.com/goplus/igop/cmd/internal/base" 27 "github.com/goplus/igop/cmd/internal/load" 28 "golang.org/x/tools/go/ssa" 29 ) 30 31 // ----------------------------------------------------------------------------- 32 33 // Cmd - igop build 34 var Cmd = &base.Command{ 35 UsageLine: "igop build [build flags] [package]", 36 Short: "compile a Go/Go+ package", 37 } 38 39 var ( 40 flag = &Cmd.Flag 41 ) 42 43 func init() { 44 Cmd.Run = buildCmd 45 base.AddBuildFlags(Cmd, base.OmitModFlag|base.OmitSSAFlag|base.OmitVFlag) 46 } 47 48 func buildCmd(cmd *base.Command, args []string) { 49 err := flag.Parse(args) 50 if err != nil { 51 os.Exit(2) 52 } 53 paths := flag.Args() 54 if len(paths) == 0 { 55 paths = []string{"."} 56 } 57 path := paths[0] 58 var mode igop.Mode 59 if base.BuildSSA { 60 mode |= igop.EnableDumpInstr 61 } 62 if base.BuildX { 63 mode |= igop.EnableDumpImports 64 } 65 ctx := igop.NewContext(mode) 66 ctx.BuildContext = base.BuildContext 67 path, _ = filepath.Abs(path) 68 isDir, err := load.IsDir(path) 69 if err != nil { 70 fmt.Fprintln(os.Stderr, err) 71 os.Exit(2) 72 } 73 var pkg *ssa.Package 74 if isDir { 75 if load.SupportGop && load.IsGopProject(path) { 76 err := load.BuildGopDir(ctx, path) 77 if err != nil { 78 fmt.Fprintln(os.Stderr, err) 79 os.Exit(2) 80 } 81 } 82 pkg, err = ctx.LoadDir(path, false) 83 } else { 84 pkg, err = ctx.LoadFile(path, nil) 85 } 86 if err != nil { 87 fmt.Fprintln(os.Stderr, err) 88 os.Exit(2) 89 } 90 _, err = ctx.NewInterp(pkg) 91 if err != nil { 92 fmt.Fprintln(os.Stderr, err) 93 os.Exit(2) 94 } 95 if base.BuildV { 96 fmt.Println(pkg.Pkg.Path()) 97 } 98 } 99 100 // -----------------------------------------------------------------------------