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  	PrintOut   = flag.Bool("S", false, "print assembly and machine code")
    21  	TrimPath   = flag.String("trimpath", "", "remove prefix from recorded source file paths")
    22  	Shared     = flag.Bool("shared", false, "generate code that can be linked into a shared library")
    23  	Dynlink    = flag.Bool("dynlink", false, "support references to Go symbols defined in other 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  	Newobj     = flag.Bool("newobj", false, "use new object file format")
    27  )
    28  
    29  var (
    30  	D MultiFlag
    31  	I MultiFlag
    32  )
    33  
    34  func init() {
    35  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
    36  	flag.Var(&I, "I", "include directory; can be set multiple times")
    37  	objabi.AddVersionFlag() // -V
    38  }
    39  
    40  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    41  type MultiFlag []string
    42  
    43  func (m *MultiFlag) String() string {
    44  	if len(*m) == 0 {
    45  		return ""
    46  	}
    47  	return fmt.Sprint(*m)
    48  }
    49  
    50  func (m *MultiFlag) Set(val string) error {
    51  	(*m) = append(*m, val)
    52  	return nil
    53  }
    54  
    55  func Usage() {
    56  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
    57  	fmt.Fprintf(os.Stderr, "Flags:\n")
    58  	flag.PrintDefaults()
    59  	os.Exit(2)
    60  }
    61  
    62  func Parse() {
    63  	flag.Usage = Usage
    64  	flag.Parse()
    65  	if flag.NArg() == 0 {
    66  		flag.Usage()
    67  	}
    68  
    69  	// Flag refinement.
    70  	if *OutputFile == "" {
    71  		if flag.NArg() != 1 {
    72  			flag.Usage()
    73  		}
    74  		input := filepath.Base(flag.Arg(0))
    75  		if strings.HasSuffix(input, ".s") {
    76  			input = input[:len(input)-2]
    77  		}
    78  		*OutputFile = fmt.Sprintf("%s.o", input)
    79  	}
    80  }
    81  

View as plain text