]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/asm/internal/flags/flags.go
[dev.link] all: merge branch 'master' into dev.link
[gostls13.git] / src / cmd / asm / internal / flags / flags.go
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         Importpath = flag.String("p", "", "set expected package import to path")
27         Spectre    = flag.String("spectre", "", "enable spectre mitigations in `list` (all, ret)")
28
29         Go115Newobj = flag.Bool("go115newobj", true, "use new object file format")
30 )
31
32 var (
33         D MultiFlag
34         I MultiFlag
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 }
42
43 // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
44 type MultiFlag []string
45
46 func (m *MultiFlag) String() string {
47         if len(*m) == 0 {
48                 return ""
49         }
50         return fmt.Sprint(*m)
51 }
52
53 func (m *MultiFlag) Set(val string) error {
54         (*m) = append(*m, val)
55         return nil
56 }
57
58 func Usage() {
59         fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
60         fmt.Fprintf(os.Stderr, "Flags:\n")
61         flag.PrintDefaults()
62         os.Exit(2)
63 }
64
65 func Parse() {
66         flag.Usage = Usage
67         flag.Parse()
68         if flag.NArg() == 0 {
69                 flag.Usage()
70         }
71
72         // Flag refinement.
73         if *OutputFile == "" {
74                 if flag.NArg() != 1 {
75                         flag.Usage()
76                 }
77                 input := filepath.Base(flag.Arg(0))
78                 if strings.HasSuffix(input, ".s") {
79                         input = input[:len(input)-2]
80                 }
81                 *OutputFile = fmt.Sprintf("%s.o", input)
82         }
83 }