github.com/goplus/igop@v0.25.0/cmd/internal/base/flag.go (about) 1 /* 2 * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved. 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 base 18 19 import ( 20 "go/build" 21 "runtime" 22 "strings" 23 24 "github.com/goplus/igop/load" 25 ) 26 27 var ( 28 BuildContext = defaultContext() 29 // BuildMod string // -mod flag 30 BuildModExplicit bool // whether -mod was set explicitly 31 BuildX bool // -x flag 32 BuildV bool // -v flag 33 BuildSSA bool // -ssa flag 34 DebugSSATrace bool // -ssa-trace flag 35 ExperimentalGC bool // -exp-gc flag experimental support runtime.GC 36 ) 37 38 func defaultContext() build.Context { 39 runtime.GC() 40 return build.Default 41 } 42 43 type BuildFlagMask int 44 45 const ( 46 DefaultBuildFlags BuildFlagMask = 0 47 OmitModFlag BuildFlagMask = 1 << iota 48 OmitVFlag 49 OmitSSAFlag 50 OmitSSATraceFlag 51 OmitExperimentalGCFlag 52 ) 53 54 // AddBuildFlags adds the flags common to the build, run, and test commands. 55 func AddBuildFlags(cmd *Command, mask BuildFlagMask) { 56 cmd.Flag.BoolVar(&BuildX, "x", false, "print the commands.") 57 if mask&OmitVFlag != 0 { 58 cmd.Flag.BoolVar(&BuildV, "v", false, "print the names of packages as they are compiled.") 59 } 60 if mask&OmitModFlag != 0 { 61 cmd.Flag.Var(explicitStringFlag{value: &load.BuildMod, explicit: &BuildModExplicit}, "mod", "module download mode to use: readonly, vendor, or mod.") 62 } 63 if mask&OmitSSAFlag != 0 { 64 cmd.Flag.BoolVar(&BuildSSA, "ssa", false, "print SSA instruction code") 65 } 66 if mask&OmitSSATraceFlag != 0 { 67 cmd.Flag.BoolVar(&DebugSSATrace, "ssa-trace", false, "trace SSA interpreter code") 68 } 69 if mask&OmitExperimentalGCFlag != 0 { 70 cmd.Flag.BoolVar(&ExperimentalGC, "exp-gc", false, "experimental support runtime.GC") 71 } 72 cmd.Flag.Var((*tagsFlag)(&BuildContext.BuildTags), "tags", "a comma-separated list of build tags to consider satisfied during the build") 73 } 74 75 // tagsFlag is the implementation of the -tags flag. 76 type tagsFlag []string 77 78 func (v *tagsFlag) Set(s string) error { 79 // Split on commas, ignore empty strings. 80 *v = []string{} 81 for _, s := range strings.Split(s, ",") { 82 if s != "" { 83 *v = append(*v, s) 84 } 85 } 86 return nil 87 } 88 89 func (v *tagsFlag) String() string { 90 return "<TagsFlag>" 91 } 92 93 // explicitStringFlag is like a regular string flag, but it also tracks whether 94 // the string was set explicitly to a non-empty value. 95 type explicitStringFlag struct { 96 value *string 97 explicit *bool 98 } 99 100 func (f explicitStringFlag) String() string { 101 if f.value == nil { 102 return "" 103 } 104 return *f.value 105 } 106 107 func (f explicitStringFlag) Set(v string) error { 108 *f.value = v 109 if v != "" { 110 *f.explicit = true 111 } 112 return nil 113 }