github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/asm/flags/flags.go (about)

     1  // Copyright 2015 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 flags implements top-level flags and the usage message for the assembler.
     6  package flags
     7  
     8  import (
     9  	"flag"
    10  	"fmt"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  
    15  	"github.com/go-asm/go/cmd/obj"
    16  	"github.com/go-asm/go/cmd/objabi"
    17  )
    18  
    19  var (
    20  	Debug      = flag.Bool("debug", false, "dump instructions as they are parsed")
    21  	OutputFile = flag.String("o", "", "output file; default foo.o for /a/b/c/foo.s as first argument")
    22  	TrimPath   = flag.String("trimpath", "", "remove prefix from recorded source file paths")
    23  	Shared     = flag.Bool("shared", false, "generate code that can be linked into a shared library")
    24  	Dynlink    = flag.Bool("dynlink", false, "support references to Go symbols defined in other shared libraries")
    25  	Linkshared = flag.Bool("linkshared", false, "generate code that will be linked against Go shared libraries")
    26  	AllErrors  = flag.Bool("e", false, "no limit on number of errors reported")
    27  	SymABIs    = flag.Bool("gensymabis", false, "write symbol ABI information to output file, don't assemble")
    28  	Importpath = flag.String("p", obj.UnlinkablePkg, "set expected package import to path")
    29  	Spectre    = flag.String("spectre", "", "enable spectre mitigations in `list` (all, ret)")
    30  )
    31  
    32  var DebugFlags struct {
    33  	MayMoreStack string `help:"call named function before all stack growth checks"`
    34  	PCTab        string `help:"print named pc-value table\nOne of: pctospadj, pctofile, pctoline, pctoinline, pctopcdata"`
    35  }
    36  
    37  var (
    38  	D        MultiFlag
    39  	I        MultiFlag
    40  	PrintOut int
    41  	DebugV   bool
    42  )
    43  
    44  func init() {
    45  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
    46  	flag.Var(&I, "I", "include directory; can be set multiple times")
    47  	flag.BoolVar(&DebugV, "v", false, "print debug output")
    48  	flag.Var(objabi.NewDebugFlag(&DebugFlags, nil), "d", "enable debugging settings; try -d help")
    49  	objabi.AddVersionFlag() // -V
    50  	objabi.Flagcount("S", "print assembly and machine code", &PrintOut)
    51  }
    52  
    53  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    54  type MultiFlag []string
    55  
    56  func (m *MultiFlag) String() string {
    57  	if len(*m) == 0 {
    58  		return ""
    59  	}
    60  	return fmt.Sprint(*m)
    61  }
    62  
    63  func (m *MultiFlag) Set(val string) error {
    64  	(*m) = append(*m, val)
    65  	return nil
    66  }
    67  
    68  func Usage() {
    69  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
    70  	fmt.Fprintf(os.Stderr, "Flags:\n")
    71  	flag.PrintDefaults()
    72  	os.Exit(2)
    73  }
    74  
    75  func Parse() {
    76  	objabi.Flagparse(Usage)
    77  	if flag.NArg() == 0 {
    78  		flag.Usage()
    79  	}
    80  
    81  	// Flag refinement.
    82  	if *OutputFile == "" {
    83  		if flag.NArg() != 1 {
    84  			flag.Usage()
    85  		}
    86  		input := filepath.Base(flag.Arg(0))
    87  		input = strings.TrimSuffix(input, ".s")
    88  		*OutputFile = fmt.Sprintf("%s.o", input)
    89  	}
    90  }