Source file src/cmd/go/internal/load/flag.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package load
     6  
     7  import (
     8  	"cmd/go/internal/base"
     9  	"cmd/internal/quoted"
    10  	"fmt"
    11  	"strings"
    12  )
    13  
    14  var (
    15  	BuildAsmflags   PerPackageFlag // -asmflags
    16  	BuildGcflags    PerPackageFlag // -gcflags
    17  	BuildLdflags    PerPackageFlag // -ldflags
    18  	BuildGccgoflags PerPackageFlag // -gccgoflags
    19  )
    20  
    21  // A PerPackageFlag is a command-line flag implementation (a flag.Value)
    22  // that allows specifying different effective flags for different packages.
    23  // See 'go help build' for more details about per-package flags.
    24  type PerPackageFlag struct {
    25  	raw     string
    26  	present bool
    27  	values  []ppfValue
    28  }
    29  
    30  // A ppfValue is a single <pattern>=<flags> per-package flag value.
    31  type ppfValue struct {
    32  	match func(*Package) bool // compiled pattern
    33  	flags []string
    34  }
    35  
    36  // Set is called each time the flag is encountered on the command line.
    37  func (f *PerPackageFlag) Set(v string) error {
    38  	return f.set(v, base.Cwd())
    39  }
    40  
    41  // set is the implementation of Set, taking a cwd (current working directory) for easier testing.
    42  func (f *PerPackageFlag) set(v, cwd string) error {
    43  	f.raw = v
    44  	f.present = true
    45  	match := func(p *Package) bool { return p.Internal.CmdlinePkg || p.Internal.CmdlineFiles } // default predicate with no pattern
    46  	// For backwards compatibility with earlier flag splitting, ignore spaces around flags.
    47  	v = strings.TrimSpace(v)
    48  	if v == "" {
    49  		// Special case: -gcflags="" means no flags for command-line arguments
    50  		// (overrides previous -gcflags="-whatever").
    51  		f.values = append(f.values, ppfValue{match, []string{}})
    52  		return nil
    53  	}
    54  	if !strings.HasPrefix(v, "-") {
    55  		i := strings.Index(v, "=")
    56  		if i < 0 {
    57  			return fmt.Errorf("missing =<value> in <pattern>=<value>")
    58  		}
    59  		if i == 0 {
    60  			return fmt.Errorf("missing <pattern> in <pattern>=<value>")
    61  		}
    62  		pattern := strings.TrimSpace(v[:i])
    63  		match = MatchPackage(pattern, cwd)
    64  		v = v[i+1:]
    65  	}
    66  	flags, err := quoted.Split(v)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	if flags == nil {
    71  		flags = []string{}
    72  	}
    73  	f.values = append(f.values, ppfValue{match, flags})
    74  	return nil
    75  }
    76  
    77  func (f *PerPackageFlag) String() string { return f.raw }
    78  
    79  // Present reports whether the flag appeared on the command line.
    80  func (f *PerPackageFlag) Present() bool {
    81  	return f.present
    82  }
    83  
    84  // For returns the flags to use for the given package.
    85  func (f *PerPackageFlag) For(p *Package) []string {
    86  	flags := []string{}
    87  	for _, v := range f.values {
    88  		if v.match(p) {
    89  			flags = v.flags
    90  		}
    91  	}
    92  	return flags
    93  }
    94  

View as plain text