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  )
    36  
    37  func init() {
    38  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
    39  	flag.Var(&I, "I", "include directory; can be set multiple times")
    40  	objabi.AddVersionFlag() // -V
    41  	objabi.Flagcount("S", "print assembly and machine code", &PrintOut)
    42  }
    43  
    44  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    45  type MultiFlag []string
    46  
    47  func (m *MultiFlag) String() string {
    48  	if len(*m) == 0 {
    49  		return ""
    50  	}
    51  	return fmt.Sprint(*m)
    52  }
    53  
    54  func (m *MultiFlag) Set(val string) error {
    55  	(*m) = append(*m, val)
    56  	return nil
    57  }
    58  
    59  func Usage() {
    60  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
    61  	fmt.Fprintf(os.Stderr, "Flags:\n")
    62  	flag.PrintDefaults()
    63  	os.Exit(2)
    64  }
    65  
    66  func Parse() {
    67  	flag.Usage = Usage
    68  	flag.Parse()
    69  	if flag.NArg() == 0 {
    70  		flag.Usage()
    71  	}
    72  
    73  	// Flag refinement.
    74  	if *OutputFile == "" {
    75  		if flag.NArg() != 1 {
    76  			flag.Usage()
    77  		}
    78  		input := filepath.Base(flag.Arg(0))
    79  		if strings.HasSuffix(input, ".s") {
    80  			input = input[:len(input)-2]
    81  		}
    82  		*OutputFile = fmt.Sprintf("%s.o", input)
    83  	}
    84  }
    85  

View as plain text