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

View as plain text