Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/cgo/main.go

Documentation: cmd/cgo

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

View as plain text