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