github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/cmd/cgo/main.go (about)

     1  // Copyright 2009 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  // Cgo; see gmp.go for an overview.
     6  
     7  // TODO(rsc):
     8  //	Emit correct line number annotations.
     9  //	Make gc understand the annotations.
    10  
    11  package main
    12  
    13  import (
    14  	"crypto/md5"
    15  	"flag"
    16  	"fmt"
    17  	"go/ast"
    18  	"go/printer"
    19  	"go/token"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"reflect"
    24  	"runtime"
    25  	"sort"
    26  	"strings"
    27  
    28  	"cmd/internal/objabi"
    29  )
    30  
    31  // A Package collects information about the package we're going to write.
    32  type Package struct {
    33  	PackageName string // name of package
    34  	PackagePath string
    35  	PtrSize     int64
    36  	IntSize     int64
    37  	GccOptions  []string
    38  	GccIsClang  bool
    39  	CgoFlags    map[string][]string // #cgo flags (CFLAGS, LDFLAGS)
    40  	Written     map[string]bool
    41  	Name        map[string]*Name // accumulated Name from Files
    42  	ExpFunc     []*ExpFunc       // accumulated ExpFunc from Files
    43  	Decl        []ast.Decl
    44  	GoFiles     []string // list of Go files
    45  	GccFiles    []string // list of gcc output files
    46  	Preamble    string   // collected preamble for _cgo_export.h
    47  }
    48  
    49  // A File collects information about a single Go input file.
    50  type File struct {
    51  	AST      *ast.File           // parsed AST
    52  	Comments []*ast.CommentGroup // comments from file
    53  	Package  string              // Package name
    54  	Preamble string              // C preamble (doc comment on import "C")
    55  	Ref      []*Ref              // all references to C.xxx in AST
    56  	Calls    []*Call             // all calls to C.xxx in AST
    57  	ExpFunc  []*ExpFunc          // exported functions for this file
    58  	Name     map[string]*Name    // map from Go name to Name
    59  	NamePos  map[*Name]token.Pos // map from Name to position of the first reference
    60  }
    61  
    62  func nameKeys(m map[string]*Name) []string {
    63  	var ks []string
    64  	for k := range m {
    65  		ks = append(ks, k)
    66  	}
    67  	sort.Strings(ks)
    68  	return ks
    69  }
    70  
    71  // A Call refers to a call of a C.xxx function in the AST.
    72  type Call struct {
    73  	Call     *ast.CallExpr
    74  	Deferred bool
    75  }
    76  
    77  // A Ref refers to an expression of the form C.xxx in the AST.
    78  type Ref struct {
    79  	Name    *Name
    80  	Expr    *ast.Expr
    81  	Context astContext
    82  }
    83  
    84  func (r *Ref) Pos() token.Pos {
    85  	return (*r.Expr).Pos()
    86  }
    87  
    88  // A Name collects information about C.xxx.
    89  type Name struct {
    90  	Go       string // name used in Go referring to package C
    91  	Mangle   string // name used in generated Go
    92  	C        string // name used in C
    93  	Define   string // #define expansion
    94  	Kind     string // "iconst", "fconst", "sconst", "type", "var", "fpvar", "func", "macro", "not-type"
    95  	Type     *Type  // the type of xxx
    96  	FuncType *FuncType
    97  	AddError bool
    98  	Const    string // constant definition
    99  }
   100  
   101  // IsVar reports whether Kind is either "var" or "fpvar"
   102  func (n *Name) IsVar() bool {
   103  	return n.Kind == "var" || n.Kind == "fpvar"
   104  }
   105  
   106  // IsConst reports whether Kind is either "iconst", "fconst" or "sconst"
   107  func (n *Name) IsConst() bool {
   108  	return strings.HasSuffix(n.Kind, "const")
   109  }
   110  
   111  // An ExpFunc is an exported function, callable from C.
   112  // Such functions are identified in the Go input file
   113  // by doc comments containing the line //export ExpName
   114  type ExpFunc struct {
   115  	Func    *ast.FuncDecl
   116  	ExpName string // name to use from C
   117  	Doc     string
   118  }
   119  
   120  // A TypeRepr contains the string representation of a type.
   121  type TypeRepr struct {
   122  	Repr       string
   123  	FormatArgs []interface{}
   124  }
   125  
   126  // A Type collects information about a type in both the C and Go worlds.
   127  type Type struct {
   128  	Size       int64
   129  	Align      int64
   130  	C          *TypeRepr
   131  	Go         ast.Expr
   132  	EnumValues map[string]int64
   133  	Typedef    string
   134  }
   135  
   136  // A FuncType collects information about a function type in both the C and Go worlds.
   137  type FuncType struct {
   138  	Params []*Type
   139  	Result *Type
   140  	Go     *ast.FuncType
   141  }
   142  
   143  func usage() {
   144  	fmt.Fprint(os.Stderr, "usage: cgo -- [compiler options] file.go ...\n")
   145  	flag.PrintDefaults()
   146  	os.Exit(2)
   147  }
   148  
   149  var ptrSizeMap = map[string]int64{
   150  	"386":      4,
   151  	"amd64":    8,
   152  	"arm":      4,
   153  	"arm64":    8,
   154  	"mips":     4,
   155  	"mipsle":   4,
   156  	"mips64":   8,
   157  	"mips64le": 8,
   158  	"ppc64":    8,
   159  	"ppc64le":  8,
   160  	"s390":     4,
   161  	"s390x":    8,
   162  }
   163  
   164  var intSizeMap = map[string]int64{
   165  	"386":      4,
   166  	"amd64":    8,
   167  	"arm":      4,
   168  	"arm64":    8,
   169  	"mips":     4,
   170  	"mipsle":   4,
   171  	"mips64":   8,
   172  	"mips64le": 8,
   173  	"ppc64":    8,
   174  	"ppc64le":  8,
   175  	"s390":     4,
   176  	"s390x":    8,
   177  }
   178  
   179  var cPrefix string
   180  
   181  var fset = token.NewFileSet()
   182  
   183  var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import data for that file")
   184  var dynout = flag.String("dynout", "", "write -dynimport output to this file")
   185  var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
   186  var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
   187  
   188  // This flag is for bootstrapping a new Go implementation,
   189  // to generate Go types that match the data layout and
   190  // constant values used in the host's C libraries and system calls.
   191  var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
   192  
   193  var srcDir = flag.String("srcdir", "", "source directory")
   194  var objDir = flag.String("objdir", "", "object directory")
   195  var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
   196  var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
   197  
   198  var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
   199  var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
   200  var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used with gccgo")
   201  var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
   202  var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
   203  var goarch, goos string
   204  
   205  func main() {
   206  	objabi.AddVersionFlag() // -V
   207  	flag.Usage = usage
   208  	flag.Parse()
   209  
   210  	if *dynobj != "" {
   211  		// cgo -dynimport is essentially a separate helper command
   212  		// built into the cgo binary. It scans a gcc-produced executable
   213  		// and dumps information about the imported symbols and the
   214  		// imported libraries. The 'go build' rules for cgo prepare an
   215  		// appropriate executable and then use its import information
   216  		// instead of needing to make the linkers duplicate all the
   217  		// specialized knowledge gcc has about where to look for imported
   218  		// symbols and which ones to use.
   219  		dynimport(*dynobj)
   220  		return
   221  	}
   222  
   223  	if *godefs {
   224  		// Generating definitions pulled from header files,
   225  		// to be checked into Go repositories.
   226  		// Line numbers are just noise.
   227  		conf.Mode &^= printer.SourcePos
   228  	}
   229  
   230  	args := flag.Args()
   231  	if len(args) < 1 {
   232  		usage()
   233  	}
   234  
   235  	// Find first arg that looks like a go file and assume everything before
   236  	// that are options to pass to gcc.
   237  	var i int
   238  	for i = len(args); i > 0; i-- {
   239  		if !strings.HasSuffix(args[i-1], ".go") {
   240  			break
   241  		}
   242  	}
   243  	if i == len(args) {
   244  		usage()
   245  	}
   246  
   247  	goFiles := args[i:]
   248  
   249  	for _, arg := range args[:i] {
   250  		if arg == "-fsanitize=thread" {
   251  			tsanProlog = yesTsanProlog
   252  		}
   253  	}
   254  
   255  	p := newPackage(args[:i])
   256  
   257  	// Record CGO_LDFLAGS from the environment for external linking.
   258  	if ldflags := os.Getenv("CGO_LDFLAGS"); ldflags != "" {
   259  		args, err := splitQuoted(ldflags)
   260  		if err != nil {
   261  			fatalf("bad CGO_LDFLAGS: %q (%s)", ldflags, err)
   262  		}
   263  		p.addToFlag("LDFLAGS", args)
   264  	}
   265  
   266  	// Need a unique prefix for the global C symbols that
   267  	// we use to coordinate between gcc and ourselves.
   268  	// We already put _cgo_ at the beginning, so the main
   269  	// concern is other cgo wrappers for the same functions.
   270  	// Use the beginning of the md5 of the input to disambiguate.
   271  	h := md5.New()
   272  	fs := make([]*File, len(goFiles))
   273  	for i, input := range goFiles {
   274  		if *srcDir != "" {
   275  			input = filepath.Join(*srcDir, input)
   276  		}
   277  
   278  		b, err := ioutil.ReadFile(input)
   279  		if err != nil {
   280  			fatalf("%s", err)
   281  		}
   282  		if _, err = h.Write(b); err != nil {
   283  			fatalf("%s", err)
   284  		}
   285  
   286  		f := new(File)
   287  		f.ParseGo(input, b)
   288  		f.DiscardCgoDirectives()
   289  		fs[i] = f
   290  	}
   291  
   292  	cPrefix = fmt.Sprintf("_%x", h.Sum(nil)[0:6])
   293  
   294  	if *objDir == "" {
   295  		// make sure that _obj directory exists, so that we can write
   296  		// all the output files there.
   297  		os.Mkdir("_obj", 0777)
   298  		*objDir = "_obj"
   299  	}
   300  	*objDir += string(filepath.Separator)
   301  
   302  	for i, input := range goFiles {
   303  		f := fs[i]
   304  		p.Translate(f)
   305  		for _, cref := range f.Ref {
   306  			switch cref.Context {
   307  			case ctxCall, ctxCall2:
   308  				if cref.Name.Kind != "type" {
   309  					break
   310  				}
   311  				*cref.Expr = cref.Name.Type.Go
   312  			}
   313  		}
   314  		if nerrors > 0 {
   315  			os.Exit(2)
   316  		}
   317  		p.PackagePath = f.Package
   318  		p.Record(f)
   319  		if *godefs {
   320  			os.Stdout.WriteString(p.godefs(f, input))
   321  		} else {
   322  			p.writeOutput(f, input)
   323  		}
   324  	}
   325  
   326  	if !*godefs {
   327  		p.writeDefs()
   328  	}
   329  	if nerrors > 0 {
   330  		os.Exit(2)
   331  	}
   332  }
   333  
   334  // newPackage returns a new Package that will invoke
   335  // gcc with the additional arguments specified in args.
   336  func newPackage(args []string) *Package {
   337  	goarch = runtime.GOARCH
   338  	if s := os.Getenv("GOARCH"); s != "" {
   339  		goarch = s
   340  	}
   341  	goos = runtime.GOOS
   342  	if s := os.Getenv("GOOS"); s != "" {
   343  		goos = s
   344  	}
   345  	ptrSize := ptrSizeMap[goarch]
   346  	if ptrSize == 0 {
   347  		fatalf("unknown ptrSize for $GOARCH %q", goarch)
   348  	}
   349  	intSize := intSizeMap[goarch]
   350  	if intSize == 0 {
   351  		fatalf("unknown intSize for $GOARCH %q", goarch)
   352  	}
   353  
   354  	// Reset locale variables so gcc emits English errors [sic].
   355  	os.Setenv("LANG", "en_US.UTF-8")
   356  	os.Setenv("LC_ALL", "C")
   357  
   358  	p := &Package{
   359  		PtrSize:  ptrSize,
   360  		IntSize:  intSize,
   361  		CgoFlags: make(map[string][]string),
   362  		Written:  make(map[string]bool),
   363  	}
   364  	p.addToFlag("CFLAGS", args)
   365  	return p
   366  }
   367  
   368  // Record what needs to be recorded about f.
   369  func (p *Package) Record(f *File) {
   370  	if p.PackageName == "" {
   371  		p.PackageName = f.Package
   372  	} else if p.PackageName != f.Package {
   373  		error_(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
   374  	}
   375  
   376  	if p.Name == nil {
   377  		p.Name = f.Name
   378  	} else {
   379  		for k, v := range f.Name {
   380  			if p.Name[k] == nil {
   381  				p.Name[k] = v
   382  			} else if !reflect.DeepEqual(p.Name[k], v) {
   383  				error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
   384  			}
   385  		}
   386  	}
   387  
   388  	if f.ExpFunc != nil {
   389  		p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
   390  		p.Preamble += "\n" + f.Preamble
   391  	}
   392  	p.Decl = append(p.Decl, f.AST.Decls...)
   393  }