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