Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/asm/internal/flags/flags.go

Documentation: cmd/asm/internal/flags

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

View as plain text